1 / 26

Minimum Spanning Trees

Minimum Spanning Trees. CSCI 2720 Spring 2005. Extended Dijkstra. // extending Dijkstra’s algorithm to compute the edges of the shortest paths Algorithm Dijkstra ( G, s ) // same implementation choices (heap or not) as for Dijkstra

igor-hester
Télécharger la présentation

Minimum Spanning Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Minimum Spanning Trees CSCI 2720 Spring 2005 MSTs

  2. Extended Dijkstra // extending Dijkstra’s algorithm to compute the edges of the shortest paths AlgorithmDijkstra(G, s) // same implementation choices (heap or not) as for Dijkstra // initialize // same performance as for Dijkstra for all v  G.vertices() // same code except for the two red lines if (v= s) setDistance(v, 0) else setDistance(v, ) v is not in cloud setParent(v, NULL) while there are nodes not in cloud u node not in cloud with min. distance add u to cloud // update neighbors of u for all e  G.adjacentEdges(u) z  G.getOpposite(e, u) d_new  getDistance(u) + weight(e) ifd_new< getDistance(z) updateDistance(z, d_new) updateParent(z, u) MSTs

  3. 0 A 4 8 2 7 1 B C D 3 2 7 3 9 2 5 E F 5 8 Example (same graph as before) A.parent = NULLB.parent = E C.parent = A D.parent = C E.parent = C F.parent = D MSTs

  4. All-Pairs Shortest Paths • Find the distance between every pair of vertices in a weighted directed graph G. • Create a matrix, i’th row = distances for vertex vi(and another matrix if we want to store parent values) • We can make n calls to Dijkstra’s algorithm (if no negative edges), one for each vertex as source. Each call fills in one row of the matrix. • This takes O(nm log n) time with heap implementation (of sparse graph with adjacency lists), O(n3) otherwise. MSTs

  5. D E B C A DAG G DAGs and Topological Ordering • A directed acyclic graph (DAG) is a digraph that has no directed cycles • Halfway between trees and digraphs • Used instead of expression trees, to “fold together” common subexpressions • Often used to represent dependencies: • tasks/jobs -- (x,y) means x must be done before y • course requirements – (x,y) means x must be taken before y • preferences – (x,y) means that x is preferable to y • In each case, acyclicity is important MSTs

  6. 4 5 D E 2 B 3 C 1 A Topological Sorting • Number vertices, so that (u,v) in E implies u < v • Often needed in DAG applications • tasks/jobs – the order in which to do jobs • course requirements – the order in which to take courses • preferences – the overall “rating • TheoremA digraph admits a topological ordering if and only if it is a DAG (why?) MSTs

  7. Algorithm for Topological Sorting AlgorithmTopologicalSort(G) H G // Temporary copy of G n G.numVertices() whileH is not emptydo vfind vertex with no outgoing edges Label v  n n  n - 1 Remove v from H • Whenever there is a choice between multiple vertices with no outgoing edges, the output is not unique. • Naïve implementation is O(n2). Why? MSTs

  8. Topological Sorting using DFS • Topological sort can be implemented in O(n + m).(Is that always faster?) • This is done as a variant of depth-first search • We label every node at the end of visiting it (before returning). • The node labels are in descending order (from highest down). • Result is the same as with naïve version before. MSTs

  9. Topological Sorting using DFS AlgorithmtopologicalDFS(G, v) Inputgraph G and a start vertex v of G Outputlabeling of the vertices of G in the connected component of v setLabel(v, VISITED) for all e  G.incidentEdges(v) ifgetLabel(e) = UNEXPLORED w opposite(v,e) if getLabel(w) = UNVISITED setLabel(e, DISCOVERY) topologicalDFS(G, w) else {e is a forward or cross edge} Set the topological order of v to n n n – 1 // end of forall loop Except for blue lines, this is the same DFS code as before. AlgorithmtopologicalDFS(G) Inputdag G Outputtopological ordering of Gn G.numVertices() for all u  G.vertices() setLabel(u, UNVISITED) for all e  G.edges() setLabel(e, UNEXPLORED) for all v  G.vertices() ifgetLabel(v) = UNEXPLORED topologicalDFS(G, v) MSTs

  10. Topological Sorting Example MSTs

  11. Topological Sorting Example 9 MSTs

  12. Topological Sorting Example 8 9 MSTs

  13. Topological Sorting Example 7 8 9 MSTs

  14. Topological Sorting Example 6 7 8 9 MSTs

  15. Topological Sorting Example 6 5 7 8 9 MSTs

  16. Topological Sorting Example 4 6 5 7 8 9 MSTs

  17. Topological Sorting Example 3 4 6 5 7 8 9 MSTs

  18. Topological Sorting Example 2 3 4 6 5 7 8 9 MSTs

  19. Topological Sorting Example 2 1 3 4 6 5 7 8 9 MSTs

  20. Spanning subgraph Subgraph of a graph G containing all the vertices of G Spanning tree Spanning subgraph that is itself a (free) tree Minimum spanning tree (MST) Spanning tree of a weighted graph with minimum total edge weight Applications Communications networks:Connect all servers, at least cost Transportation networks:Connect all cities, at least cost Minimum Spanning Tree ORD 10 1 PIT DEN 6 7 9 3 DCA STL 4 5 8 2 DFW ATL MSTs

  21. Partition Property U V 7 f 4 Partition Property: • Consider a partition of the vertices of G into subsets U and V • Let e be an edge of minimum weight across the partition • There is a minimum spanning tree of G containing edge e Proof (by contradiction): • Suppose no MSTs of G contain e. • Let T be an MST of G; T does not contain e • Consider the cycle C formed by e with T and let f be an edge of C across the partition • By the cycle property,weight(f) weight(e) • By replacing f with e, we obtaina better spanning tree! 9 5 2 8 3 8 e 7 Replacing f with e yieldsanother MST U V 7 f 4 9 5 2 8 3 e 7 MSTs

  22. MST algorithms • 2 algorithms to compute MST of a given weighted graph • Prim-Jarnik (similar to Dijkstra’s) • Kruskal (uses a new ADT, “union-find”) • Both are greedy • greedy: An algorithm that always takes the best immediate, or local, solution while finding an answer. Greedy algorithms find the overall, or globally, optimal solution for some optimization problems, but may find less-than-optimal solutions for some instances of other problems • Prim-Jarnik greedily chooses nodes for MST • Kruskal’s algorithm greedily chooses edges for MST. MSTs

  23. 7 7 D 2 B 4 4 9 5 5 F 2 C 8 3 8 E A 7 7 0 Prim-Jarnik’s Algorithm • Similar to Dijkstra’s algorithm; the only difference is what distance means (now it’s just the edge weight). • We pick an arbitrary vertex s and we grow the MST as a cloud of vertices, starting from s • We store with each vertex v a label d(v) = the smallest weight of an edge connecting v to a vertex in the cloud • At each step: • We add to the cloud the vertex u outside the cloud with the smallest distance label • We update the labels of the vertices adjacent to u MSTs

  24. 7 7 D 2 B 4 4 9 5 5 F 2 C 8 3 8 E A 7 7 0 Example 7  7 D 7 D 2 2 B B 4 4 9  9  8 5 5 5 F F 2 2 C C 8 8 3 3 8 8 E E A A 7 7 7 7 0 0 7 7 D 2 B 4 9  5 5 F 2 C 8 3 8 E A 7 7 0 MSTs

  25. Example (contd.) 7 7 D 2 B 4 4 9 5 5 F 2 C 8 3 8 E A 3 7 7 0 7 D 2 B 4 4 9 5 5 F 2 C 8 3 8 E A 3 7 0 MSTs

  26. Pseudocode AlgorithmPrim-Jarnik(G) // same implementation choices (heap or not) as for Dijkstra // initialize // same performance as for Dijkstra s  a vertex of G // same code except for the two red lines for all v  G.vertices() if (v= s) setDistance(v, 0) else setDistance(v, ) v is not in cloud setParent(v, NULL) while there are nodes not in cloud u node not in cloud with min. distance add u to cloud // update neighbors of u for all e  G.adjacentEdges(u) z  G.getOpposite(e, u) d_new  weight(e) ifd_new< getDistance(z) updateDistance(z, d_new) updateParent(z, u) MSTs

More Related