Discussion #35 Chapter 7, Section 5.5 1/15 Discussion #35 Dijkstra’s Algorithm.

Slides:



Advertisements
Similar presentations
Great Theoretical Ideas in Computer Science
Advertisements

Single Source Shortest Paths
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
Discussion #33 Adjacency Matrices. Topics Adjacency matrix for a directed graph Reachability Algorithmic Complexity and Correctness –Big Oh –Proofs of.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Discussion #34 1/17 Discussion #34 Warshall’s and Floyd’s Algorithms.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Chapter 23 Minimum Spanning Trees
Chapter 7: Greedy Algorithms 7.4 Finding the Shortest Path Dijkstra’s Algorithm pp
Discussion #36 Spanning Trees
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Chapter 9 Greedy Technique Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Minimal Spanning Trees. Spanning Tree Assume you have an undirected graph G = (V,E) Spanning tree of graph G is tree T = (V,E T E, R) –Tree has same set.
Dijkstra’s Shortest Paths CS 312 Lecture 4. Announcements Project 1 comes out Friday –Min spanning trees and scheduling –due 2 weeks from Friday (1 week.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
The Travelling Salesman Algorithm A Salesman has to visit lots of different stores and return to the starting base On a graph this means visiting every.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
CS 146: Data Structures and Algorithms July 21 Class Meeting
Copyright © Cengage Learning. All rights reserved.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
COSC 2007 Data Structures II Chapter 14 Graphs III.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Introduction to Algorithms Jiafen Liu Sept
Shortest Path in Weighted Graph : Dijkstra’s Algorithm.
Spanning Trees. A spanning tree for a connected, undirected graph G is a graph S consisting of the nodes of G together with enough edges of G such that:
Lectures on Greedy Algorithms and Dynamic Programming
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
CIRCUITS, PATHS, AND SCHEDULES Euler and Königsberg.
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 10.
Dijkstra animation. Dijksta’s Algorithm (Shortest Path Between 2 Nodes) 2 Phases:initialization;iteration Initialization: 1. Included:(Boolean) 2. Distance:(Weight)
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
Shortest Path -Prim’s -Djikstra’s. PRIM’s - Minimum Spanning Tree -A spanning tree of a graph is a tree that has all the vertices of the graph connected.
Graph Algorithms Mathematical Structures for Computer Science Chapter 6 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraph Algorithms.
Inductive Proof (the process of deriving generalities from particulars) Mathematical Induction (reasoning over the natural numbers)
COMP108 Algorithmic Foundations Greedy methods
Spanning Trees.
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Shortest Path Graph represents highway system Edges have weights
Graph Definitions and Applications
Minimal Spanning Trees
Graphs & Graph Algorithms 2
Graphs Chapter 13.
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
Shortest Path.
Basic Graph Algorithms
Shortest Path Algorithms
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Lecture 12 Shortest Path.
Data Structures and Algorithm Analysis Lecture 8
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Discussion #35 Chapter 7, Section 5.5 1/15 Discussion #35 Dijkstra’s Algorithm

Discussion #35 Chapter 7, Section 5.5 2/15 Topics Shortest path Greedy algorithms Dijkstra’s algorithm

Discussion #35 Chapter 7, Section 5.5 3/15 Shortest Path Minimum length path from one node to another (e.g. from a to e, the shortest path is 2) Algorithms –Breadth-First-Search, O(n 2 ) in the worst case –Floyd’s, O(n 3 ); but finds all –BFS, starting at each node, O(nm), which beats Floyd’s for sparse graphs b ca ed

Discussion #35 Chapter 7, Section 5.5 4/15 Shortest Path in Weighted Graphs Weights on edges: positive numbers Algorithms –Simple BFS no longer works (e.g. the shortest path from a to b is not the edge that connects them) –Floyd’s, O(n 3 ); finds all Is there an algorithm that beats Floyd’s? –for the single best path? –for all paths? b ca ed

Discussion #35 Chapter 7, Section 5.5 5/15 Dijkstra’s Algorithm Find the shortest weighted path between two given nodes. (Easier to find the minimum from one to all.) Intuitively, special BFS: choose smallest accumulated path length Algorithm b ca ed Initialize D (distance) table. 2.Until all nodes are settled, 2a.find smallest distance & settle node. 2b.adjust distances in D for unsettled nodes.

Discussion #35 Chapter 7, Section 5.5 6/15 Important Aside: Greedy Algorithms A greedy algorithm always takes the best immediate or local solution while finding an answer. Greedy algorithms find optimal solutions for some optimization problems, but may find (far) less- than-optimal solutions for other optimization problems. –Dijkstra’s algorithm is greedy. –There is no greedy algorithm for the traveling salesman problem (find the shortest path to visit all nodes). When greedy algorithms work, they are usually best.

Discussion #35 Chapter 7, Section 5.5 7/15 Trace of Dijkstra’s Algorithm all nodes settled a,d,c,e,b 44 a,d,c,e 243 adjust w/e a,d,c 2252 adjust w/c a  edcba settled distanceiteration b ca ed a,d  251 adjust w/d 2 1. Initialize D (start=a) 2. Until all nodes are settled: -find smallest distance & settle node. -adjust distances in D for unsettled nodes. Circled numbers: shortest path lengths from a.

Discussion #35 Chapter 7, Section 5.5 8/15 Trace with Different Weights all nodes settled a,d,e,c,b 54 a,d,e,c 363 adjust w/c a,d,e 2462 adjust w/e a  edcba settled distanceiteration b ca ed a,d  461 adjust w/d 2 1. Initialize D (start=a) 2. Until all nodes are settled: -find smallest distance & settle node. -adjust distances in D for unsettled nodes. 5 Circled numbers: shortest path lengths from a.

Discussion #35 Chapter 7, Section 5.5 9/15 Proof that Dijkstra’s Algorithm Works Loop Invariant: For each settled node x, the minimum distance from the start node s to x is D(x). Additional observations (which help us in the proof) are also loop invariants. –(a) The shortest path from s to any settled node v consists only of settled nodes. –(b) The shortest path from s to any unsettled node y is at least the largest settled difference so far.

Discussion #35 Chapter 7, Section /15 Proof by Induction (on the number of iterations) Basis: 0 iterations: x = s, D(s) = 0 (which is the minimum since all weights are positive) –For (a): initially s = v, the only settled node –For (b): initially all nodes except s are unsettled, and the shortest path to each is at least 0, indeed > 0. Induction: Assume that the main loop invariant and the loop invariants (a) and (b) hold for k iterations and show that they hold for k+1 iterations.

Discussion #35 Chapter 7, Section /15 Proof of Induction Part (Sketch) Assume that the main loop invariant does not hold for the k+1 st iteration, then there is a shorter path from s to x. k settled s... v  x k+1 settled s... v  x By the induction hypotheses, since Dijkstra’s algorithm chooses x, the path from s to x is the shortest using settled nodes, and the path from s to y is no shorter than s to x. Since all weights are positive, the path from y to x is at least 1, so that the assumed shorter path cannot exist. Further (a) and (b) continue to hold: (a) After settling x, the path from s to x consists of only settled nodes. (b) The adjustments are only for paths from x to any unsettled nodes. Since the distance from s to any unsettled node y is at least D(x), and D(x) is the largest settled distance so far. s... v  x... w  y Assume Dijkstra’s algorithm selects x to be settled.

D(x) 0a (b,5) (c,2) (d,1) 5b (a,5) (c,2) 2c (a,2) (b,2) (d,10) (e,1) 1d (a,1) (c,10) (e,1)  e (d,1) (c,1) Discussion #35 Chapter 7, Section /15 Big-Oh for Dijkstra’s Algorithm 1.O(n)  visit at most all edges for a node  can’t be more than n 2.O(n)  each node needs to be settled 2a.O(n)  look through D(x) list to find smallest 2b.O(k)  for chosen node, go through list of length k b ca ed Adjacency List: 1. Initialize D (start=a) 2. Until all nodes are settled: 2a. find smallest distance & settle node. 2b. adjust distances in D for unsettled nodes. { O(n 2 ) Over all iterations, the k’s add up to 2(m  # of edges in the initialization). dominates  Unroll the loop: n + k 1 + n + k 2 + … + n + k (n-1) = (n  1)n = O(n 2 ) = 2(m  # of edges in the initialization) = O(m)

Discussion #35 Chapter 7, Section /15 Big-Oh for Dijkstra’s Algorithm Clever using POTs: 1.O(nlogn)  POT (Partially Ordered Tree) construction dominates 2.O(n)  each node needs to be settled 2a.O(logn)  swap and bubble down 2b.O(klogn)  for chosen node, go through list of length k b ca ed Initialize D (start=a) 2. Until all nodes are settled: 2a. find smallest distance & settle node. 2b. adjust distances in D for unsettled nodes. { O(mlogn) dominates D(x) 0a (b,5) (c,2) (d,1) 5b (a,5) (c,2) 2c (a,2) (b,2) (d,10) (e,1) 1d (a,1) (c,10) (e,1)  e (d,1) (c,1) Adjacency List:  Over all iterations, the k’s add up to 2(m  # of edges in the initialization).

Discussion #35 Chapter 7, Section /15 POT Construction/Manipulation b ca ed (b,5) (c,2) (b,5) (c,2) (b,5)(d,1) (b,5)(c,2) (d,1) (b,5)(c,2) (e,  ) POT construction (start = a)    Find smallest  always on top; then swap and bubble down (d,1) (b,5)(c,2) (e,  )  (b,5)(c,2) (d,1)  (c,2) (b,5) (e,  ) (c,2) (b,5)(e,2) Adjust distances  POT doubly linked to adjacency list nodes  bubble up and down, as needed, when going through list of chosen node to adjust values swapbubble downadjust no bubbling needed (c,2) (b,5)(e,2)  …

Discussion #35 Chapter 7, Section /15 Dijkstra’s Algorithm vs. Floyd’s Dijkstra’s algorithm is O(mlogn) = O(n 2 logn) in the worst case. Floyd’s algorithm is O(n 3 ), but computes all shortest paths. Dijkstra’s algorithm can compute all shortest paths by starting it n times, once for each node. Thus, to compute all paths, Dijkstra’s algorithm is O(nmlogn) = O(n 3 logn) in the worst case. Which is better depends on the application. –Dijkstra’s algorithm is better if only a path from a single node to another or to all others is needed. –For all paths, Dijkstra’s algorithm is better for sparse graphs, and Floyd’s algorithm is better for dense graphs.