Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Graphs. Graph… ADT? Not quite an ADT… operations not clear A formalism for representing relationships between objects Graph G = (V,E)

Similar presentations


Presentation on theme: "Data Structures Graphs. Graph… ADT? Not quite an ADT… operations not clear A formalism for representing relationships between objects Graph G = (V,E)"— Presentation transcript:

1 Data Structures Graphs

2 Graph… ADT? Not quite an ADT… operations not clear A formalism for representing relationships between objects Graph G = (V,E) –Set of vertices: V = {v 1,v 2,…,v n } –Set of edges: E = {e 1,e 2,…,e m } where each e i connects two vertices (v i1,v i2 ) 2 Han Leia Luke V = {Han, Leia, Luke} E = {(Luke, Leia), (Han, Leia), (Leia, Han)}

3 Examples of Graphs The web –Vertices are webpages –Each edge is a link from one page to another Call graph of a program –Vertices are subroutines –Edges are calls and returns Social networks –Vertices are people –Edges connect friends 3

4 Graph Definitions In directed graphs, edges have a direction: In undirected graphs, they don’t (are two-way): v is adjacent to u if (u,v)  E 4 Han Leia Luke Han Leia Luke

5 Weighted Graphs 5 20 30 35 60 Mukilteo Edmonds Seattle Bremerton Bainbridge Kingston Clinton Each edge has an associated weight or cost.

6 Paths and Cycles A path is a list of vertices {v 1, v 2, …, v n } such that (v i, v i+1 )  E for all 0  i < n. A cycle is a path that begins and ends at the same node. 6 Seattle San Francisco Dallas Chicago Salt Lake City p = {Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle}

7 Path Length and Cost Path length: the number of edges in the path Path cost: the sum of the costs of each edge 7 Seattle San Francisco Dallas Chicago Salt Lake City 3.5 2 2 2.5 3 2 length(p) = 5cost(p) = 11.5

8 More Definitions: Simple Paths and Cycles A simple path repeats no vertices (except that the first can also be the last): p = {Seattle, Salt Lake City, San Francisco, Dallas} p = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle} A cycle is a path that starts and ends at the same node: p = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle} p = {Seattle, Salt Lake City, Seattle, San Francisco, Seattle} A simple cycle is a cycle that is also a simple path (in undirected graphs, no edge can be repeated) 8

9 Trees as Graphs Every tree is a graph with some restrictions: –the tree is directed –there are no cycles (directed or undirected) –there is a directed path from the root to every node 9 A B DE C F HG

10 Directed Acyclic Graphs (DAGs) DAGs are directed graphs with no (directed) cycles. 10 main() add() access() mult() read() Aside: If program call- graph is a DAG, then all procedure calls can be in- lined

11 Rep 1: Adjacency Matrix A |V| x |V| array in which an element (u,v) is true if and only if there is an edge from u to v 11 Han Leia Luke HanLukeLeia Han Luke Leia Space requirements? Runtimes: Iterate over vertices? Iterate over edges? Iterate edges adj. to vertex? Existence of edge?

12 Rep 2: Adjacency List A |V| -ary list (array) in which each entry stores a list (linked list) of all adjacent vertices 12 Han Leia Luke Han Luke Leia Space requirements? Runtimes: Iterate over vertices? Iterate over edges? Iterate edges adj. to vertex? Existence of edge?

13 Some Applications: Moving Around Washington 13 What’s the shortest way to get from Seattle to Pullman? Edge labels:

14 Some Applications: Moving Around Washington 14 What’s the fastest way to get from Seattle to Pullman? Edge labels:

15 Some Applications: Reliability of Communication 15 If Wenatchee’s phone exchange goes down, can Seattle still talk to Pullman?

16 Some Applications: Bus Routes in Downtown Seattle 16 If we’re at 3 rd and Pine, how can we get to 1 st and University using Metro? How about 4 th and Seneca?

17 Application: Topological Sort Given a directed graph, G = (V,E), output all the vertices in V such that no vertex is output before any other vertex with an edge to it. 17 CSE 142CSE 143 CSE 321 CSE 341 CSE 378 CSE 326 CSE 370 CSE 403 CSE 421 CSE 467 CSE 451 CSE 322 Is the output unique?

18 Topological Sort: Take One 1.Label each vertex with its in-degree (# of inbound edges) 2.While there are vertices remaining: a.Choose a vertex v of in-degree zero ; output v b.Reduce the in-degree of all vertices adjacent to v c.Remove v from the list of vertices 18 Runtime:

19 void Graph::topsort(){ Vertex v, w; labelEachVertexWithItsIn-degree(); for (int counter=0; counter < NUM_VERTICES; counter++){ v = findNewVertexOfDegreeZero(); v.topologicalNum = counter; for each w adjacent to v w.indegree--; } 19

20 Topological Sort: Take Two 1.Label each vertex with its in-degree 2.Initialize a queue Q to contain all in-degree zero vertices 3.While Q not empty a.v = Q.dequeue; output v b.Reduce the in-degree of all vertices adjacent to v c.If new in-degree of any such vertex u is zero Q.enqueue(u) 20 Runtime: Note: could use a stack, list, set, box, … instead of a queue

21 void Graph::topsort(){ Queue q(NUM_VERTICES); int counter = 0; Vertex v, w; labelEachVertexWithItsIn-degree(); q.makeEmpty(); for each vertex v if (v.indegree == 0) q.enqueue(v); while (!q.isEmpty()){ v = q.dequeue(); v.topologicalNum = ++counter; for each w adjacent to v if (--w.indegree == 0) q.enqueue(w); } 21 intialize the queue get a vertex with indegree 0 insert new eligible vertices Runtime:

22 Graph Connectivity Undirected graphs are connected if there is a path between any two vertices Directed graphs are strongly connected if there is a path from any one vertex to any other Directed graphs are weakly connected if there is a path between any two vertices, ignoring direction A complete graph has an edge between every pair of vertices 22

23 Graph Traversals Breadth-first search (and depth-first search) work for arbitrary (directed or undirected) graphs - not just mazes! –Must mark visited vertices so you do not go into an infinite loop! Either can be used to determine connectivity: –Is there a path between two given vertices? –Is the graph (weakly) connected? Which one: –Uses a queue? –Uses a stack? –Always finds the shortest path (for unweighted graphs)? 23

24 CSE 326: Data Structures Graph Traversals James Fogarty Autumn 2007

25 Graph Connectivity Undirected graphs are connected if there is a path between any two vertices Directed graphs are strongly connected if there is a path from any one vertex to any other Directed graphs are weakly connected if there is a path between any two vertices, ignoring direction A complete graph has an edge between every pair of vertices 25

26 Graph Traversals Breadth-first search (and depth-first search) work for arbitrary (directed or undirected) graphs - not just mazes! –Must mark visited vertices. Why? –So you do not go into an infinite loop! It’s not a tree. Either can be used to determine connectivity: –Is there a path between two given vertices? –Is the graph (weakly/strongly) connected? Which one: –Uses a queue? –Uses a stack? –Always finds the shortest path (for unweighted graphs)? 26

27 The Shortest Path Problem Given a graph G, edge costs c i,j, and vertices s and t in G, find the shortest path from s to t. For a path p = v 0 v 1 v 2 … v k –unweighted length of path p = k (a.k.a. length) –weighted length of path p =  i=0..k-1 c i,i+1 (a.k.a cost) –Path length equals path cost when ? 27

28 Single Source Shortest Paths (SSSP) Given a graph G, edge costs c i,j, and vertex s, find the shortest paths from s to all vertices in G. –Is this harder or easier than the previous problem? 28

29 All Pairs Shortest Paths (APSP) Given a graph G and edge costs c i,j, find the shortest paths between all pairs of vertices in G. –Is this harder or easier than SSSP? –Could we use SSSP as a subroutine to solve this? 29

30 Depth-First Graph Search DFS( Start, Goal_test) push(Start, Open); repeat if (empty(Open)) then return fail; Node := pop(Open); if (Goal_test(Node)) then return Node; for each Child of node do if (Child not already visited) then push(Child, Open); Mark Node as visited; end 30 Open – Stack Criteria – Pop

31 Breadth-First Graph Search BFS( Start, Goal_test) enqueue(Start, Open); repeat if (empty(Open)) then return fail; Node := dequeue(Open); if (Goal_test(Node)) then return Node; for each Child of node do if (Child not already visited) then enqueue(Child, Open); Mark Node as visited; end 31 Open – Queue Criteria – Dequeue (FIFO)

32 Comparison: DFS versus BFS Depth-first search –Does not always find shortest paths –Must be careful to mark visited vertices, or you could go into an infinite loop if there is a cycle Breadth-first search –Always finds shortest paths – optimal solutions –Marking visited nodes can improve efficiency, but even without doing so search is guaranteed to terminate –Is BFS always preferable? 32

33 DFS Space Requirements Assume: –Longest path in graph is length d –Highest number of out-edges is k DFS stack grows at most to size dk –For k=10, d=15, size is 150 33

34 BFS Space Requirements Assume –Distance from start to a goal is d –Highest number of out edges is k BFS Queue could grow to size k d –For k=10, d=15, size is 1,000,000,000,000,000 34

35 Conclusion For large graphs, DFS is hugely more memory efficient, if we can limit the maximum path length to some fixed d. –If we knew the distance from the start to the goal in advance, we can just not add any children to stack after level d –But what if we don’t know d in advance? 35

36 Iterative-Deepening DFS (I) Bounded_DFS(Start, Goal_test, Limit) Start.dist = 0; push(Start, Open); repeat if (empty(Open)) then return fail; Node := pop(Open); if (Goal_test(Node)) then return Node; if (Node.dist  Limit) then return fail; for each Child of node do if (Child not already i-visited) then Child.dist := Node.dist + 1; push(Child, Open); Mark Node as i-visited; end 36

37 Iterative-Deepening DFS (II) IDFS_Search(Start, Goal_test) i := 1; repeat answer := Bounded_DFS(Start, Goal_test, i); if (answer != fail) then return answer; i := i+1; end 37

38 Analysis of IDFS Work performed with limit < actual distance to G is wasted – but the wasted work is usually small compared to amount of work done during the last iteration 38 Ignore low order terms! Same time complexity as BFS Same space complexity as (bounded) DFS

39 Saving the Path Our pseudocode returns the goal node found, but not the path to it How can we remember the path? –Add a field to each node, that points to the previous node along the path –Follow pointers from goal back to start to recover path 39

40 Example 40 Seattle San Francisco Dallas Salt Lake City

41 Example (Unweighted Graph) 41 Seattle San Francisco Dallas Salt Lake City

42 Example (Unweighted Graph) 42 Seattle San Francisco Dallas Salt Lake City

43 Graph Search, Saving Path Search( Start, Goal_test, Criteria) insert(Start, Open); repeat if (empty(Open)) then return fail; select Node from Open using Criteria; if (Goal_test(Node)) then return Node; for each Child of node do if (Child not already visited) then Child.previous := Node; Insert( Child, Open ); Mark Node as visited; end 43

44 Weighted SSSP: The Quest For Food 44 Vending Machine in EE1 ALLEN HUB Delfino’s Ben & Jerry’s Neelam’s Cedars Coke Closet Home Schultzy’s Parent’s Home Café Allegro 10 The Ave U Village 350 375 40 25 35 15 25 15,356 35 285 75 70 365 350 Can we calculate shortest distance to all nodes from Allen Center?

45 Weighted SSSP: The Quest For Food 45 Vending Machine in EE1 ALLEN HUB Delfino’s Ben & Jerry’s Neelam’s Cedars Coke Closet Home Schultzy’s Parent’s Home Café Allegro 10 The Ave U Village 5 375 40 25 35 15 25 15,356 35 285 75 70 365 350 Can we calculate shortest distance to all nodes from Allen Center?

46 Edsger Wybe Dijkstra (1930-2002) Invented concepts of structured programming, synchronization, weakest precondition, and "semaphores" for controlling computer processes. The Oxford English Dictionary cites his use of the words "vector" and "stack" in a computing context. Believed programming should be taught without computers 1972 Turing Award “In their capacity as a tool, computers will be but a ripple on the surface of our culture. In their capacity as intellectual challenge, they are without precedent in the cultural history of mankind.” 46

47 General Graph Search Algorithm Search( Start, Goal_test, Criteria) insert(Start, Open); repeat if (empty(Open)) then return fail; select Node from Open using Criteria; if (Goal_test(Node)) then return Node; for each Child of node do if (Child not already visited) then Insert( Child, Open ); Mark Node as visited; end 47 Open – some data structure (e.g., stack, queue, heap) Criteria – some method for removing an element from Open

48 Shortest Path for Weighted Graphs Given a graph G = (V, E) with edge costs c(e), and a vertex s  V, find the shortest (lowest cost) path from s to every vertex in V Assume: only positive edge costs 48

49 Dijkstra’s Algorithm for Single Source Shortest Path Similar to breadth-first search, but uses a heap instead of a queue: –Always select (expand) the vertex that has a lowest-cost path to the start vertex Correctly handles the case where the lowest-cost (shortest) path to a vertex is not the one with fewest edges 49

50 CSE 326: Data Structures Dijkstra’s Algorithm

51 Dijkstra, Edsger Wybe Legendary figure in computer science; was a professor at University of Texas. Supported teaching introductory computer courses without computers (pencil and paper programming) Supposedly wouldn’t (until very late in life) read his e-mail; so, his staff had to print out messages and put them in his box. 51 E.W. Dijkstra (1930-2002) 1972 Turning Award Winner, Programming Languages, semaphores, and …

52 Dijkstra’s Algorithm: Idea Adapt BFS to handle weighted graphs Two kinds of vertices: –Finished or known vertices Shortest distance has been computed –Unknown vertices Have tentative distance 52

53 Dijkstra’s Algorithm: Idea At each step: 1)Pick closest unknown vertex 2)Add it to known vertices 3)Update distances 53

54 Dijkstra’s Algorithm: Pseudocode Initialize the cost of each node to  Initialize the cost of the source to 0 While there are unknown nodes left in the graph Select an unknown node b with the lowest cost Mark b as known For each node a adjacent to b a’s cost = min(a’s old cost, b’s cost + cost of (b, a)) a’s prev path node = b 54

55 Important Features Once a vertex is made known, the cost of the shortest path to that node is known While a vertex is still not known, another shorter path to it might still be found The shortest path itself can found by following the backward pointers stored in node.path 55

56 Dijkstra’s Algorithm in action 56 A B D C FH E G 0       2 2 3 1 10 2 3 1 11 7 1 9 2 4 VertexVisited?CostFound by A0 B?? C D E F G H

57 Dijkstra’s Algorithm in action 57 A B D C FH E G 0 2  4 1   2 2 3 1 10 2 3 1 11 7 1 9 2 4 VertexVisited?CostFound by AY0 B<=2A C<=1A D<=4A E?? F G H

58 Dijkstra’s Algorithm in action 58 A B D C FH E G 0 2  4 1 12  2 2 3 1 10 2 3 1 11 7 1 9 2 4 VertexVisited?CostFound by AY0 B<=2A CY1A D<=4A E<=12C F?? G H

59 Dijkstra’s Algorithm in action 59 A B D C FH E G 0 2 4  4 1 12  2 2 3 1 10 2 3 1 11 7 1 9 2 4 VertexVisited?CostFound by AY0 BY2A CY1A D<=4A E<=12C F<=4B G?? H

60 Dijkstra’s Algorithm in action 60 A B D C FH E G 0 2 4  4 1 12  2 2 3 1 10 2 3 1 11 7 1 9 2 4 VertexVisited?CostFound by AY0 BY2A CY1A DY4A E<=12C F<=4B G?? H

61 Dijkstra’s Algorithm in action 61 A B D C FH E G 0 2 47 4 1 12  2 2 3 1 10 2 3 1 11 7 1 9 2 4 VertexVisited?CostFound by AY0 BY2A CY1A DY4A E<=12C FY4B G?? H<=7F

62 Dijkstra’s Algorithm in action 62 A B D C FH E G 0 2 47 4 1 12 8 2 2 3 1 10 2 3 1 11 7 1 9 2 4 VertexVisited?CostFound by AY0 BY2A CY1A DY4A E<=12C FY4B G<=8H HY7F

63 Dijkstra’s Algorithm in action 63 A B D C FH E G 0 2 47 4 1 11 8 2 2 3 1 10 2 3 1 11 7 1 9 2 4 VertexVisited?CostFound by AY0 BY2A CY1A DY4A E<=11G FY4B GY8H HY7F

64 Dijkstra’s Algorithm in action 64 A B D C FH E G 0 2 47 4 1 11 8 2 2 3 1 10 2 3 1 11 7 1 9 2 4 VertexVisited?CostFound by AY0 BY2A CY1A DY4A EY11G FY4B GY8H HY7F

65 Your turn VVisited?CostFound by v0 v1 v2 v3 v4 v5 v6 65 v3v3 v6v6 v1v1 v2v2 v4v4 v5v5 v0v0 s 1 2 2 2 1 11 53 5 6 10

66 Answer VVisited?CostFound by v0Y0 V1Y6V3 V2Y2V0 V3Y1V0 V4Y2V3 V5Y4V2 V6Y6V3 66 v3v3 v6v6 v1v1 v2v2 v4v4 v5v5 v0v0 s 1 2 2 2 1 11 53 5 6 10

67 Dijkstra’s Alg: Implementation Initialize the cost of each node to  Initialize the cost of the source to 0 While there are unknown nodes left in the graph Select the unknown node b with the lowest cost Mark b as known For each node a adjacent to b a’s cost = min(a’s old cost, b’s cost + cost of (b, a)) a’s prev path node = b (if we updated a’s cost) 67 What data structures should we use? Running time?

68 void Graph::dijkstra(Vertex s){ Vertex v,w; Initialize s.dist = 0 and set dist of all other vertices to infinity while (there exist unknown vertices, find the one b with the smallest distance) b.known = true; for each a adjacent to b if (!a.known) if (b.dist + weight(b,a) < a.dist){ a.dist = (b.dist + weight(b,a)); a.path = b; } 68 Sounds like deleteMin on a heap… Sounds like adjacency lists Sounds like decreaseKey Running time: O(|E| log |V|) – there are |E| edges to examine, and each one causes a heap operation of time O(log |V|)

69 Dijkstra’s Algorithm: Summary Classic algorithm for solving SSSP in weighted graphs without negative weights A greedy algorithm (irrevocably makes decisions without considering future consequences) Intuition for correctness: –shortest path from source vertex to itself is 0 –cost of going to adjacent nodes is at most edge weights –cheapest of these must be shortest path to that node –update paths for new node and continue picking cheapest path 69

70 Correctness: The Cloud Proof How does Dijkstra’s decide which vertex to add to the Known set next? If path to V is shortest, path to W must be at least as long (or else we would have picked W as the next vertex) So the path through W to V cannot be any shorter! 70 The Known Cloud V Next shortest path from inside the known cloud W Better path to V? No! Source

71 Correctness: Inside the Cloud Prove by induction on # of nodes in the cloud: Initial cloud is just the source with shortest path 0 Assume: Everything inside the cloud has the correct shortest path Inductive step: Only when we prove the shortest path to some node v (which is not in the cloud) is correct, we add it to the cloud 71 When does Dijkstra’s algorithm not work?

72 The Trouble with Negative Weight Cycles 72 AB CD E 2 10 1 -5 2 What’s the shortest path from A to E? Problem?

73 Dijkstra’s vs BFS At each step: 1)Pick closest unknown vertex 2)Add it to finished vertices 3)Update distances Dijkstra’s Algorithm At each step: 1)Pick vertex from queue 2)Add it to visited vertices 3)Update queue with neighbors Breadth-first Search 73 Some Similarities :

74 Single-Source Shortest Path Given a graph G = (V, E) and a single distinguished vertex s, find the shortest weighted path from s to every other vertex in G. All-Pairs Shortest Path: Find the shortest paths between all pairs of vertices in the graph. How? 74

75 Analysis Total running time for Dijkstra’s: O(|V| log |V| + |E| log |V|) (heaps) What if we want to find the shortest path from each point to ALL other points? 75

76 Dynamic Programming Algorithmic technique that systematically records the answers to sub-problems in a table and re-uses those recorded results (rather than re-computing them). Simple Example: Calculating the Nth Fibonacci number. Fib(N) = Fib(N-1) + Fib(N-2) 76

77 Floyd-Warshall for (int k = 1; k =< V; k++) for (int i = 1; i =< V; i++) for (int j = 1; j =< V; j++) if ( ( M[i][k]+ M[k][j] ) < M[i][j] ) M[i][j] = M[i][k]+ M[k][j] 77 Invariant: After the kth iteration, the matrix includes the shortest paths for all pairs of vertices (i,j) containing only vertices 1..k as intermediate vertices

78 abcde a02--4- b-0-213 c--0-1 d---04 e----0 78 b c d e a -4 2 -2 1 3 1 4 Initial state of the matrix: M[i][j] = min(M[i][j], M[i][k]+ M[k][j])

79 abcde a020-40 b-0-21 c--0-1 d---04 e----0 79 b c d e a -4 2 -2 1 3 1 4 Floyd-Warshall - for All-pairs shortest path Final Matrix Contents

80 CSE 326: Data Structures Spanning Trees

81 A Hidden Tree 81 Start End

82 Spanning Tree in a Graph 82 Vertex = router Edge = link between routers Spanning tree - Connects all the vertices - No cycles

83 Undirected Graph G = (V,E) –V is a set of vertices (or nodes) –E is a set of unordered pairs of vertices 83 1 2 3 4 5 6 7 V = {1,2,3,4,5,6,7} E = {{1,2},{1,6},{1,5},{2,7},{2,3}, {3,4},{4,7},{4,5},{5,6}} 2 and 3 are adjacent 2 is incident to edge {2,3}

84 Spanning Tree Problem Input: An undirected graph G = (V,E). G is connected. Output: T contained in E such that –(V,T) is a connected graph –(V,T) has no cycles 84

85 Spanning Tree Algorithm 85 ST(i: vertex) mark i; for each j adjacent to i do if j is unmarked then Add {i,j} to T; ST(j); end{ST} Main T := empty set; ST(1); end{Main}

86 Example of Depth First Search 86 1 2 3 4 5 6 7 ST(1)

87 Example Step 16 87 1 2 3 4 5 6 7 ST(1) {1,2} {2,7} {7,5} {5,4} {4,3} {5,6}

88 Minimum Spanning Trees Given an undirected graph G=(V,E), find a graph G’=(V, E’) such that: –E’ is a subset of E –|E’| = |V| - 1 –G’ is connected – is minimal Applications: wiring a house, power grids, Internet connections 88 G’ is a minimum spanning tree.

89 Minimum Spanning Tree Problem Input: Undirected Graph G = (V,E) and a cost function C from E to the reals. C(e) is the cost of edge e. Output: A spanning tree T with minimum total cost. That is: T that minimizes 89

90 Best Spanning Tree Each edge has the probability that it won’t fail Find the spanning tree that is least likely to fail 90 1 2 3 4 5 6 7.80.75.95.50.95 1.0.85.84.80.89

91 Example of a Spanning Tree 91 1 2 3 4 5 6 7.80.75.95.50.95 1.0.85.84.80.89 Probability of success =.85 x.95 x.89 x.95 x 1.0 x.84 =.5735

92 Minimum Spanning Tree Problem Input: Undirected Graph G = (V,E) and a cost function C from E to the reals. C(e) is the cost of edge e. Output: A spanning tree T with minimum total cost. That is: T that minimizes 92

93 Reducing Best to Minimum 93 Let P(e) be the probability that an edge doesn’t fail. Define: Minimizing is equivalent to maximizing because

94 Example of Reduction 94 1 2 3 4 5 6 7.80.75.95.50.95 1.0.85.84.80.89 1 2 3 4 5 6 7.097.125.022.301.022.000.071.076.097.051 Best Spanning Tree ProblemMinimum Spanning Tree Problem

95 Find the MST 95 4 7 1 5 9 2

96 Find the MST 96 A C B D F H G E 1 7 6 5 11 4 12 13 2 3 9 10 4

97 Two Different Approaches 97 Prim’s Algorithm Looks familiar! Kruskals’s Algorithm Completely different!

98 Prim’s algorithm Idea: Grow a tree by adding an edge from the “known” vertices to the “unknown” vertices. Pick the edge with the smallest weight. 98 G v known

99 Prim’s Algorithm for MST A node-based greedy algorithm Builds MST by greedily adding nodes 1.Select a node to be the “root” mark it as known Update cost of all its neighbors 2.While there are unknown nodes left in the graph a.Select an unknown node b with the smallest cost from some known node a b.Mark b as known c.Add (a, b) to MST d.Update cost of all nodes adjacent to b 99

100 Find MST using Prim’s VKwnDistancepath v1 v2 v3 v4 v5 v6 v7 100 v4v4 v7v7 v2v2 v3v3 v5v5 v6v6 v1v1 Start with V 1 2 2 5 4 7 110 46 3 8 1 Your Turn Order Declared Known: V1V1

101 Prim’s Algorithm Analysis Running time: Same as Dijkstra’s:O(|E| log |V|) Correctness: Proof is similar to Dijkstra’s 101

102 Kruskal’s MST Algorithm Idea: Grow a forest out of edges that do not create a cycle. Pick an edge with the smallest weight. 102 G=(V,E) v

103 Kruskal’s Algorithm for MST An edge-based greedy algorithm Builds MST by greedily adding edges 1.Initialize with empty MST all vertices marked unconnected all edges unmarked 2.While there are still unmarked edges a.Pick the lowest cost edge (u,v) and mark it b.If u and v are not already connected, add (u,v) to the MST and mark u and v as connected to each other 103 Doesn’t it sound familiar?

104 Example of Kruskal 1 104 1 6 5 4 7 2 3 3 3 4 0 22 1 3 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} 0 1 1 2 2 3 3 3 3 4 1 3

105 Data Structures for Kruskal Sorted edge list Disjoint Union / Find –Union(a,b) - union the disjoint sets named by a and b –Find(a) returns the name of the set containing a 105 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} 0 1 1 2 2 3 3 3 3 4

106 Example of DU/F 1 106 1 6 5 4 7 2 3 3 3 4 0 22 1 3 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} 0 1 1 2 2 3 3 3 3 4 1 3 7 1 3 Find(5) = 7 Find(4) = 7

107 Example of DU/F 2 107 1 6 5 4 7 2 3 3 3 4 0 22 1 3 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} 0 1 1 2 2 3 3 3 3 4 1 3 7 1 3 Find(1) = 1 Find(6) = 7

108 Example of DU/F 3 108 1 6 5 4 7 2 3 3 3 4 0 22 1 3 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5} 0 1 1 2 2 3 3 3 3 4 1 3 7 3 Union(1,7)

109 Kruskal’s Algorithm with DU / F 109 Sort the edges by increasing cost; Initialize A to be empty; for each edge {i,j} chosen in increasing order do u := Find(i); v := Find(j); if not(u = v) then add {i,j} to A; Union(u,v);

110 Kruskal code void Graph::kruskal(){ int edgesAccepted = 0; DisjSet s(NUM_VERTICES); while (edgesAccepted < NUM_VERTICES – 1){ e = smallest weight edge not deleted yet; // edge e = (u, v) uset = s.find(u); vset = s.find(v); if (uset != vset){ edgesAccepted++; s.unionSets(uset, vset); } 110 2|E| finds |V| unions |E| heap ops

111 Find MST using Kruskal’s 111 A C B D FH G E 2 2 3 2 1 4 10 8 1 9 4 2 7 Total Cost: Now find the MST using Prim’s method. Under what conditions will these methods give the same result?


Download ppt "Data Structures Graphs. Graph… ADT? Not quite an ADT… operations not clear A formalism for representing relationships between objects Graph G = (V,E)"

Similar presentations


Ads by Google