





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






















Overview
Prim’s algorithm Kruskal’s algorithm Union-Find
Spanning Tree Construction
Known = { start } explore ( start ); void explore (Node X) { for each successor Y of X if (Y is not in Known) Parent[Y] = X Add Y to Known explore(Y) }
Spanning Tree Construction
Known = { start } Discovered = { start } while ( Discovered ≠ ∅ ) { take node X out of Discovered for each successor Y of X if (Y is not in Known) Parent[Y] = X Add Y to Discovered Add Y to Known }
Depth-First Spanning Tree Example
Breadth-First Spanning Tree Example
Minimum Spanning Tree (MST)
Minimum Spanning Tree (MST)
Different spanning trees with same weight
Minimize length of telephone lines for neighborhood Minimize distance of airplane routes serving cities
Algorithms for Finding MST
Add vertices to MST in parallel
Add vertices to MST One at a time Closest vertex first
Add edges to MST One at a time Lightest edge first
Shortest Path – Dijkstra’s Algorithm S = ∅ P[ ] = none for all nodes C[start] = 0, C[ ] = ∞ for all other nodes
find node K not in S with smallest C[K] add K to S for each node J not in S adjacent to K if ( C[K] + cost of (K,J) < C[J] ) C[J] = C[K] + cost of (K,J) P[J] = K Optimal solution computed with greedy algorithm
MST – Kruskal’s Algorithm sort edges by weight (from least to most) tree = ∅ for each edge (X,Y) in order if it does not create a cycle add (X,Y) to tree stop when tree has N–1 edges Keeps track of lightest edge remaining whether adding edge to MST creates cycle Optimal solution computed with greedy algorithm
MST – Kruskal’s Algorithm Example
MST – Kruskal’s Algorithm
Traverse tree starting at X If we can reach Y, adding (X,Y) would create cycle
Question Add (X,Y) to MST? Answer No, since can already reach Y from X by traversing MST x y z w ?
MST – Kruskal’s Algorithm
Maintain set of nodes for each connected subgraph Initialize one connected subgraph for each node If X, Y in same set, adding (X,Y) would create cycle Otherwise
Use Union-Find algorithm