Path Finding We can specialize the DFS Algorithm pathDFS ( G, v, z ) algorithm to find a path setLabel ( v, VISITED ) between two given S.push ( v ) vertices u and z if v = z We call DFS ( G, u ) with u return S.elements () as the start vertex for all e ∈ G.incidentEdges ( v ) We use a stack S to keep if getLabel ( e ) = UNEXPLORED track of the path between w ← opposite ( v,e ) the start vertex and the if getLabel ( w ) = UNEXPLORED current vertex setLabel ( e, DISCOVERY ) As soon as destination S.push ( e ) vertex z is encountered, pathDFS ( G, w, z ) we return the path as the S.pop ( e ) contents of the stack else setLabel ( e, BACK ) S.pop ( v ) Graphs 28
Cycle Finding Algorithm cycleDFS ( G, v, z ) We can specialize the setLabel ( v, VISITED ) DFS algorithm to find a S.push ( v ) for all e ∈ G.incidentEdges ( v ) simple cycle if getLabel ( e ) = UNEXPLORED We use a stack S to w ← opposite ( v,e ) keep track of the path S.push ( e ) if getLabel ( w ) = UNEXPLORED between the start vertex setLabel ( e, DISCOVERY ) and the current vertex pathDFS ( G, w, z ) As soon as a back edge S.pop ( e ) else ( v, w ) is encountered, T ← new empty stack we return the cycle as repeat the portion of the stack o ← S.pop () T.push ( o ) from the top to vertex w until o = w return T.elements () S.pop ( v ) Graphs 29
30 D F C Breadth-First Search A E B L 0 L 2 Graphs L 1
Outline and Reading Breadth-first search (§12.3.2) � Algorithm � Example � Properties � Analysis � Applications DFS vs. BFS � Comparison of applications � Comparison of edge labels Graphs 31
Breadth-First Search Breadth-first search BFS on a graph with n (BFS) is a general vertices and m edges technique for traversing takes O ( n + m ) time a graph BFS can be further A BFS traversal of a extended to solve other graph G graph problems � Visits all the vertices and � Find and report a path edges of G with the minimum � Determines whether G is number of edges connected between two given � Computes the connected vertices components of G � Computes a spanning � Find a simple cycle, if forest of G there is one Graphs 32
BFS Algorithm Algorithm BFS ( G, s ) The algorithm uses a L 0 ← new empty sequence mechanism for setting and L 0 .insertLast ( s ) getting “labels” of vertices setLabel ( s, VISITED ) and edges i ← 0 Algorithm BFS ( G ) while ¬ L i .isEmpty () L i + 1 ← new empty sequence Input graph G for all v ∈ L i .elements () Output labeling of the edges for all e ∈ G.incidentEdges ( v ) and partition of the if getLabel ( e ) = UNEXPLORED vertices of G w ← opposite ( v,e ) for all u ∈ G.vertices () if getLabel ( w ) = UNEXPLORED setLabel ( u, UNEXPLORED ) setLabel ( e, DISCOVERY ) for all e ∈ G.edges () setLabel ( w, VISITED ) setLabel ( e, UNEXPLORED ) L i + 1 .insertLast ( w ) for all v ∈ G.vertices () else if getLabel ( v ) = UNEXPLORED setLabel ( e, CROSS ) i ← i + 1 BFS ( G, v ) Graphs 33
Example L 0 A unexplored vertex A L 1 visited vertex A B C D unexplored edge discovery edge E F cross edge L 0 L 0 A A L 1 L 1 B C D B C D E F E F Graphs 34
Example (cont.) L 0 L 0 A A L 1 L 1 B C D B C D L 2 E F E F L 0 L 0 A A L 1 L 1 B C D B C D L 2 L 2 E F E F Graphs 35
Example (cont.) L 0 L 0 A A L 1 L 1 B C D B C D L 2 L 2 E F E F L 0 A L 1 B C D L 2 E F Graphs 36
Properties Notation A G s : connected component of s Property 1 B C D BFS ( G, s ) visits all the vertices and edges of G s E F Property 2 The discovery edges labeled by BFS ( G, s ) form a spanning tree T s L 0 of G s A Property 3 L 1 For each vertex v in L i B C D The path of T s from s to v has i � edges L 2 Every path from s to v in G s has at E F � least i edges Graphs 37
Analysis Setting/getting a vertex/edge label takes O (1) time Each vertex is labeled twice � once as UNEXPLORED � once as VISITED Each edge is labeled twice � once as UNEXPLORED � once as DISCOVERY or CROSS Each vertex is inserted once into a sequence L i Method incidentEdges() is called once for each vertex BFS runs in O ( n + m ) time provided the graph is represented by the adjacency list structure � Recall that Σ v deg( v ) = 2 m Graphs 38
Applications Using the template method pattern, we can specialize the BFS traversal of a graph G to solve the following problems in O ( n + m ) time � Compute the connected components of G � Compute a spanning forest of G � Find a simple cycle in G , or report that G is a forest � Given two vertices of G , find a path in G between them with the minimum number of edges, or report that no such path exists Graphs 39
DFS vs. BFS Applications DFS BFS Spanning forest, connected √ √ components, paths, cycles √ Shortest paths √ Biconnected components L 0 A A L 1 B C D B C D L 2 E F E F DFS BFS Graphs 40
DFS vs. BFS (cont.) Back edge ( v,w ) Cross edge ( v,w ) � w is an ancestor of v in � w is in the same level as v or in the next level in the tree of discovery edges the tree of discovery edges L 0 A A L 1 B C D B C D L 2 E F E F DFS BFS Graphs 41
42 BOS JFK MIA ORD DFW Directed Graphs LAX Graphs SFO
Outline and Reading (§12.4) Reachability (§12.4.1) � Directed DFS � Strong connectivity Transitive closure (§12.4.2) � The Floyd-Warshall Algorithm Directed Acyclic Graphs (DAG’s) (§12.4.3) � Topological Sorting Graphs 43
Digraphs A digraph is a graph E whose edges are all directed D � Short for “directed graph” C Applications � one-way streets B � flights A � task scheduling Graphs 44
E Digraph Properties D C B A graph G= (V,E) such that � Each edge goes in one direction: A � Edge (a,b) goes from a to b, but not b to a. If G is simple, m < n* (n-1). If we keep in-edges and out-edges in separate adjacency lists, we can perform listing of in- edges and out-edges in time proportional to their size. Graphs 45
Digraph Application Scheduling: edge (a,b) means task a must be completed before b can be started ics21 ics22 ics23 ics51 ics53 ics52 ics161 ics131 ics141 ics121 ics171 The good life ics151 Graphs 46
Directed DFS We can specialize the traversal E algorithms (DFS and BFS) to digraphs by D traversing edges only along their C direction A directed DFS B starting a a vertex s determines the A vertices reachable from s Graphs 47
Reachability DFS tree rooted at v: vertices reachable from v via directed paths E D E D C A C F E D A B C F A B Graphs 48
Strong Connectivity Each vertex can reach all other vertices a g c d e b f Graphs 49
Strong Connectivity Algorithm Pick a vertex v in G. a Perform a DFS from v in G. G: g c � If there’s a w not visited, print “no”. d Let G’ be G with edges reversed. e Perform a DFS from v in G’. b f � If there’s a w not visited, print “no”. � Else, print “yes”. a g G’: c d Running time: O(n+ m). e b f Graphs 50
Strongly Connected Components Maximal subgraphs such that each vertex can reach all other vertices in the subgraph Can also be done in O(n+ m) time using DFS a g { a , c , g } c d e { f , d , e , b } b f Graphs 51
Transitive Closure D E Given a digraph G , the transitive closure of G is the B digraph G* such that G � G* has the same vertices C as G A � if G has a directed path from u to v ( u ≠ v ), G* has a directed edge from D E u to v B The transitive closure provides reachability C information about a digraph A G* Graphs 52
Computing the Transitive Closure If there's a way to get We can perform from A to B and from DFS starting at B to C, then there's a way to get from A to C. each vertex � O(n(n+ m)) Alternatively ... Use dynamic programming: The Floyd-Warshall Algorithm Graphs 53
Floyd-Warshall Transitive Closure Idea # 1: Number the vertices 1, 2, …, n. Idea # 2: Consider paths that use only vertices numbered 1, 2, …, k, as intermediate vertices: Uses only vertices numbered 1,…,k (add this edge if it’s not already in) i j Uses only vertices numbered 1,…,k-1 Uses only vertices k numbered 1,…,k-1 Graphs 54
Floyd-Warshall’s Algorithm Algorithm FloydWarshall ( G ) Floyd-Warshall’s algorithm Input digraph G numbers the vertices of G as Output transitive closure G* of G v 1 , …, v n and computes a i ← 1 series of digraphs G 0 , …, G n for all v ∈ G.vertices () � G 0 = G denote v as v i � G k has a directed edge ( v i , v j ) i ← i + 1 if G has a directed path from G 0 ← G v i to v j with intermediate for k ← 1 to n do vertices in the set { v 1 , …, v k } G k ← G k − 1 We have that G n = G* for i ← 1 to n ( i ≠ k ) do for j ← 1 to n ( j ≠ i, k ) do In phase k , digraph G k is if G k − 1 .areAdjacent ( v i , v k ) ∧ computed from G k − 1 G k − 1 .areAdjacent ( v k , v j ) Running time: O(n 3 ), if ¬ G k .areAdjacent ( v i , v j ) assuming areAdjacent is O(1) G k .insertDirectedEdge ( v i , v j , k ) (e.g., adjacency matrix) return G n Graphs 55
56 v 7 BOS v 6 JFK MIA v 5 Floyd-Warshall Example v 4 ORD Graphs DFW v 3 LAX SFO v 1 v 2
57 v 7 BOS v 6 JFK Floyd-Warshall, Iteration 1 MIA v 5 v 4 ORD Graphs DFW v 3 LAX SFO v 1 v 2
58 v 7 BOS v 6 JFK Floyd-Warshall, Iteration 2 MIA v 5 v 4 ORD Graphs DFW v 3 LAX SFO v 1 v 2
59 v 7 BOS v 6 JFK Floyd-Warshall, Iteration 3 MIA v 5 v 4 ORD Graphs DFW v 3 LAX SFO v 1 v 2
60 v 7 BOS v 6 JFK Floyd-Warshall, Iteration 4 MIA v 5 v 4 ORD Graphs DFW v 3 LAX SFO v 1 v 2
61 v 7 BOS v 6 JFK Floyd-Warshall, Iteration 5 MIA v 5 v 4 ORD Graphs DFW v 3 LAX SFO v 1 v 2
62 v 7 BOS v 6 JFK Floyd-Warshall, Iteration 6 MIA v 5 v 4 ORD Graphs DFW v 3 LAX SFO v 1 v 2
63 v 7 BOS v 6 Floyd-Warshall, Conclusion JFK MIA v 5 v 4 ORD Graphs DFW v 3 LAX SFO v 1 v 2
DAGs and Topological Ordering D E A directed acyclic graph (DAG) is a digraph that has no directed cycles B A topological ordering of a digraph is a numbering C v 1 , …, v n A DAG G of the vertices such that for every edge ( v i , v j ) , we have i < j v 4 v 5 Example: in a task scheduling digraph, a topological ordering a D E v 2 task sequence that satisfies the precedence constraints B v 3 Theorem C A digraph admits a topological v 1 Topological ordering if and only if it is a DAG A ordering of G Graphs 64
Topological Sorting Number vertices so that (u,v) in E implies u < v 1 A typical day wake up 3 2 eat study computer sci. 5 4 nap more c.s. 7 play 8 write c.s. program 6 9 work out Go out w/ friends 10 11 sleep dream about graphs Graphs 65
Algorithm for Topological Sorting Method TopologicalSort( G ) H ← G // Temporary copy of G n ← G.numVertices () while H is not empty do Let v be a vertex with no outgoing edges Label v ← n n ← n - 1 Remove v from H Running time: O(n + m). Why? Graphs 66
Topological Sorting Algorithm using DFS Algorithm topologicalDFS ( G, v ) Input graph G and a start vertex v of G Output labeling of the vertices of G Algorithm topologicalDFS ( G ) in the connected component of v Input dag G setLabel ( v, VISITED ) Output topological ordering of G for all e ∈ G.incidentEdges ( v ) n ← G.numVertices () if getLabel ( e ) = UNEXPLORED for all u ∈ G.vertices () w ← opposite ( v,e ) setLabel ( u, UNEXPLORED ) if getLabel ( w ) = UNEXPLORED for all e ∈ G.edges () setLabel ( e, DISCOVERY ) setLabel ( e, UNEXPLORED ) topologicalDFS ( G, w ) for all v ∈ G.vertices () else if getLabel ( v ) = UNEXPLORED { e is a forward or cross edge } topologicalDFS ( G, v ) Label v with topological number n n ← n - 1 O(n+ m) time Graphs 67
68 Topological Sorting Example Graphs
69 Topological Sorting Example 9 Graphs
70 Topological Sorting Example 9 Graphs 8
71 Topological Sorting Example 9 Graphs 7 8
72 Topological Sorting Example 9 Graphs 7 6 8
73 Topological Sorting Example 5 9 Graphs 7 6 8
74 Topological Sorting Example 5 9 4 Graphs 7 6 8
75 Topological Sorting Example 5 9 4 Graphs 7 6 8 3
76 Topological Sorting Example 5 9 4 Graphs 7 2 6 8 3
77 Topological Sorting Example 1 5 9 4 Graphs 7 2 6 8 3
78 3 D 5 4 8 F 1 9 2 0 C A 2 3 7 E 5 8 8 B 2 Graphs Shortest Paths
Outline and Reading Weighted graphs (§12.1) � Shortest path problem � Shortest path properties Dijkstra’s algorithm (§12.6.1) � Algorithm � Edge relaxation The Bellman-Ford algorithm Shortest paths in DAGs All-pairs shortest paths Graphs 79
Weighted Graphs In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent distances, costs, etc. Example: In a flight route graph, the weight of an edge represents the � distance in miles between the endpoint airports 9 4 8 PVD 1843 ORD 142 SFO 802 1205 3 LGA 4 337 7 1 7 8 3 1099 1 2555 HNL 1233 LAX 1120 DFW MIA Graphs 80
Shortest Path Problem Given a weighted graph and two vertices u and v , we want to find a path of minimum total weight between u and v. Length of a path is the sum of the weights of its edges. � Example: Shortest path between Providence and Honolulu � Applications Internet packet routing � Flight reservations � Driving directions � 9 4 8 PVD 1843 ORD 142 SFO 802 1205 3 LGA 4 337 7 1 7 8 3 1099 1 2555 HNL 1233 LAX 1120 DFW MIA Graphs 81
Shortest Path Properties Property 1: A subpath of a shortest path is itself a shortest path Property 2: There is a tree of shortest paths from a start vertex to all the other vertices Example: Tree of shortest paths from Providence 9 4 8 PVD 1843 ORD 142 SFO 802 1205 3 LGA 4 337 7 1 7 8 3 1099 1 2555 HNL 1233 LAX 1120 DFW MIA Graphs 82
Dijkstra’s Algorithm We grow a “ cloud ” of vertices, The distance of a vertex v from a vertex s is the beginning with s and eventually length of a shortest path covering all the vertices between s and v We store with each vertex v a Dijkstra’s algorithm label d ( v ) representing the computes the distances distance of v from s in the of all the vertices from a subgraph consisting of the cloud given start vertex s and its adjacent vertices Assumptions: At each step the graph is connected We add to the cloud the vertex � � u outside the cloud with the the edges are � smallest distance label, d ( u ) undirected We update the labels of the � the edge weights are � vertices adjacent to u (edge nonnegative relaxation) Graphs 83
Edge Relaxation Consider an edge e = ( u,z ) d ( u ) = 50 such that d ( z ) = 75 10 � u is the vertex most recently e u added to the cloud z s � z is not in the cloud The relaxation of edge e updates distance d ( z ) as follows: d ( u ) = 50 d ( z ) ← min{ d ( z ) ,d ( u ) + weight ( e )} d ( z ) = 60 10 e u z s Graphs 84
Example 0 0 A A 4 4 8 8 2 2 8 2 4 8 2 3 7 1 7 1 B C D B C D 3 9 3 9 ∞ ∞ 5 8 2 5 2 5 E F E F 0 0 A 4 A 4 8 8 2 2 8 2 3 7 2 3 7 1 7 1 B C D B C D 3 9 3 9 5 11 5 8 2 5 2 5 E F E F Graphs 85
3 86 D 5 4 8 F 1 9 2 0 C A 2 3 7 E 5 8 7 B 2 Graphs Example (cont.) 3 D 5 4 8 F 1 9 2 0 C A 2 3 7 E 5 8 7 B 2
Dijkstra’s Algorithm Algorithm DijkstraDistances ( G, s ) A priority queue stores Q ← new heap-based priority queue the vertices outside the for all v ∈ G.vertices () cloud if v = s Key: distance � setDistance ( v, 0) Element: vertex � else setDistance ( v, ∞ ) Locator-based methods l ← Q.insert ( getDistance ( v ) , v ) � insert ( k,e ) returns a setLocator ( v,l ) locator while ¬ Q.isEmpty () � replaceKey ( l,k ) changes u ← Q.removeMin () the key of an item for all e ∈ G.incidentEdges ( u ) We store two labels { relax edge e } with each vertex: z ← G.opposite ( u,e ) r ← getDistance ( u ) + weight ( e ) distance (d(v) label) � if r < getDistance ( z ) locator in priority � queue setDistance ( z,r ) Q.replaceKey ( getLocator ( z ) ,r ) Graphs 87
Analysis Graph operations Method incidentEdges is called once for each vertex � Label operations We set/get the distance and locator labels of vertex z O (deg( z )) times � Setting/getting a label takes O (1) time � Priority queue operations Each vertex is inserted once into and removed once from the priority � queue, where each insertion or removal takes O (log n ) time The key of a vertex in the priority queue is modified at most deg( w ) � times, where each key change takes O (log n ) time Dijkstra’s algorithm runs in O (( n + m ) log n ) time provided the graph is represented by the adjacency list structure Recall that Σ v deg( v ) = 2 m � The running time can also be expressed as O ( m log n ) since the graph is connected Graphs 88
Extension Algorithm DijkstraShortestPathsTree ( G, s ) We can extend Dijkstra’s algorithm to … return a tree of shortest paths from for all v ∈ G.vertices () the start vertex to all … other vertices setParent ( v, ∅ ) We store with each … vertex a third label: for all e ∈ G.incidentEdges ( u ) parent edge in the � shortest path tree { relax edge e } z ← G.opposite ( u,e ) In the edge relaxation r ← getDistance ( u ) + weight ( e ) step, we update the if r < getDistance ( z ) parent label setDistance ( z,r ) setParent ( z,e ) Q.replaceKey ( getLocator ( z ) ,r ) Graphs 89
Why Dijkstra’s Algorithm Works Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance. � Suppose it didn’t find all shortest distances. Let F be the first wrong 0 A 4 vertex the algorithm processed. 8 2 � When the previous node, D, on the 7 2 3 7 1 true shortest path was considered, B C D its distance was correct. 3 9 5 8 � But the edge (D,F) was relaxed at 2 5 E F that time! � Thus, so long as d(F)> d(D), F’s distance cannot be wrong. That is, there is no wrong vertex. Graphs 90
Why It Doesn’t Work for Negative-Weight Edges Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance. 0 A 4 8 � If a node with a negative 6 incident edge were to be added 7 5 4 7 1 B C D late to the cloud, it could mess up distances for vertices already 0 -8 5 9 in the cloud. 2 5 E F C’s true distance is 1, but it is already in the cloud with d(C)= 5! Graphs 91
Bellman-Ford Algorithm Works even with negative- Algorithm BellmanFord ( G, s ) for all v ∈ G.vertices () weight edges if v = s Must assume directed setDistance ( v, 0) edges (for otherwise we else would have negative- setDistance ( v, ∞ ) weight cycles) for i ← 1 to n-1 do Iteration i finds all shortest for each e ∈ G.edges () paths that use i edges. { relax edge e } u ← G.origin ( e ) Running time: O(nm). z ← G.opposite ( u,e ) Can be extended to detect r ← getDistance ( u ) + weight ( e ) a negative-weight cycle if it if r < getDistance ( z ) exists setDistance ( z,r ) How? � Graphs 92
Bellman-Ford Example Nodes are labeled with their d(v) values 0 4 0 4 8 8 -2 -2 -2 4 8 7 1 7 1 ∞ ∞ ∞ ∞ ∞ ∞ 3 9 3 9 -2 5 -2 5 ∞ ∞ ∞ ∞ 0 4 0 4 8 8 -2 -2 7 1 -1 7 1 5 8 -2 4 5 -2 -1 1 3 9 3 9 6 9 4 -2 5 -2 5 ∞ ∞ 1 9 Graphs 93
DAG-based Algorithm Algorithm DagDistances ( G, s ) for all v ∈ G.vertices () Works even with if v = s negative-weight edges setDistance ( v, 0) Uses topological order else setDistance ( v, ∞ ) Uses simple data Perform a topological sort of the vertices structures for u ← 1 to n do {in topological order} Is much faster than for each e ∈ G.outEdges ( u ) Dijkstra’s algorithm { relax edge e } z ← G.opposite ( u,e ) Running time: O(n+ m). r ← getDistance ( u ) + weight ( e ) if r < getDistance ( z ) setDistance ( z,r ) Graphs 94
1 DAG Example Nodes are labeled with their d(v) values 1 1 0 4 0 4 8 8 -2 -2 -2 4 4 8 4 3 2 3 2 7 1 7 1 ∞ ∞ ∞ ∞ ∞ ∞ 3 9 3 9 -5 5 -5 5 ∞ ∞ ∞ ∞ 6 5 6 5 1 1 0 4 0 4 8 8 -2 -2 5 4 4 3 2 3 2 7 1 -1 7 1 8 -2 4 5 -2 -1 9 3 3 9 0 4 1 7 -5 5 -5 5 ∞ ∞ 1 7 6 5 6 5 (two steps) Graphs 95
All-Pairs Shortest Paths Find the distance Algorithm AllPair ( G ) {assumes vertices 1,…, n } for all vertex pairs (i,j) between every pair of if i = j vertices in a weighted D 0 [ i,i ] ← 0 directed graph G. else if ( i,j ) is an edge in G We can make n calls to D 0 [ i,j ] ← weight of edge ( i,j ) Dijkstra’s algorithm (if no else negative edges), which D 0 [ i,j ] ← + ∞ takes O(nmlog n) time. for k ← 1 to n do for i ← 1 to n do Likewise, n calls to for j ← 1 to n do Bellman-Ford would take D k [ i,j ] ← min{ D k-1 [ i,j ], D k-1 [ i,k ]+ D k-1 [ k,j ]} O(n 2 m) time. return D n We can achieve O(n 3 ) time using dynamic Uses only vertices numbered 1,…,k i programming (similar to (compute weight of this edge) j the Floyd-Warshall Uses only vertices algorithm). Uses only vertices numbered 1,…,k-1 k numbered 1,…,k-1 Graphs 96
Minimum Spanning Trees 2704 BOS 867 849 PVD ORD 187 740 144 JFK 1846 621 1258 184 802 SFO BWI 1391 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342 Graphs 97
Outline and Reading Minimum Spanning Trees (§12.7) � Definitions � A crucial fact The Prim-Jarnik Algorithm (§12.7.2) Kruskal's Algorithm (§12.7.1) Baruvka's Algorithm Graphs 98
Minimum Spanning Tree Spanning subgraph ORD Subgraph of a graph G � 10 containing all the vertices of G 1 PIT Spanning tree DEN Spanning subgraph that is � 6 7 itself a (free) tree 9 Minimum spanning tree (MST) 3 DCA STL Spanning tree of a weighted � 4 graph with minimum total 8 5 edge weight 2 Applications Communications networks DFW � ATL Transportation networks � Graphs 99
Cycle Property 8 f Cycle Property: 4 C Let T be a minimum � 9 6 spanning tree of a 2 3 weighted graph G e 7 Let e be an edge of G 8 � that is not in T and let C 7 be the cycle formed by e with T Replacing f with e yields For every edge f of C, � a better spanning tree weight ( f ) ≤ weight ( e ) 8 Proof: f 4 By contradiction � C If weight ( f ) > weight ( e ) we 9 � 6 2 can get a spanning tree 3 e of smaller weight by 7 8 replacing e with f 7 Graphs 100
Recommend
More recommend