SlideServe
Menu
Préférences
© SlideServe · v2026

MST Algorithms in Graph Theory

wherbert avatar
25 diapositives · 834 Views · Oct 21, 2019

Exploring Kruskal's & Prim's algorithms for minimum spanning trees. Learn about implementation, union-find data structure, and analysis. Compare with Dijkstra & Floyd-Warshall.

MST Algorithms in Graph Theory - slide 1
01 / 25MST Algorithms in Graph Theory
1 / 17

Télécharger la présentation

MST Algorithms in Graph Theory
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an acyclic subset of edges of G.

  2. Minimum spanning trees (MST) 1 Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an acyclic subset of edges of G. 15 7 8 4 3 5 7 2 1 1 6 2 Def: Given is a weighted (undirected) graph G=(V,E,w) where w:E->Reals defines a weight of every edge in E. A minimum spanning tree of G is a spanning tree with the minimum total weight of edges.

  3. Minimum spanning trees (MST) - Kruskal 1 15 7 8 4 3 5 7 2 1 1 6 2 • Kruskal ( G=(V,E,w) ) • Let T=; • Sort the edges in increasing order of weight • 3. For edge e do • 4. If T [ e does not contain a cycle then • Add e to T • Return T

  4. Minimum spanning trees (MST) - Kruskal 1 15 7 8 4 3 5 7 2 1 1 6 2 • Kruskal ( G=(V,E,w) ) • Let T=; • Sort the edges in increasing order of weight • 3. For edge e do • 4. If T [ e does not contain a cycle then • Add e to T • Return T

  5. Minimum spanning trees (MST) - Kruskal 1 Lemma: Algo is correct. 15 7 8 4 3 5 7 2 1 1 6 2 • Kruskal ( G=(V,E,w) ) • Let T=; • Sort the edges in increasing order of weight • 3. For edge e do • 4. If T [ e does not contain a cycle then • Add e to T • Return T

  6. Minimum spanning trees (MST) - Kruskal 1 Implementation? 15 7 8 4 3 5 7 2 1 1 6 2 • Kruskal ( G=(V,E,w) ) • Let T=; • Sort the edges in increasing order of weight • 3. For edge e do • 4. If T [ e does not contain a cycle then • Add e to T • Return T

  7. Minimum spanning trees (MST) - Kruskal 1 Implementation? - Union-Find datastructure 15 7 8 4 • Init (V) • for every vertex v do • boss[v]=v • size[v]=1 • set[v]={v} • Union (u,v) • if size[boss[u]]>size[boss[v]] then • set[boss[u]]=set[boss[u]] union set[boss[v]] • size[boss[u]]+=size[boss[v]] • for every z in set[boss[v]] do • boss[z]=boss[u] • else do steps 2.-5. with u,v switched 3 5 7 2 1 1 6 2

  8. Minimum spanning trees (MST) - Kruskal 1 Analysis of Union-Find 15 7 8 Lemma: n-1 Unions take O(n log n) time 4 3 5 7 2 1 1 6 2 • Union (u,v) • if size[boss[u]]>size[boss[v]] then • set[boss[u]]=set[boss[u]] union set[boss[v]] • size[boss[u]]+=size[boss[v]] • for every z in set[boss[v]] do • boss[z]=boss[u] • else do steps 2.-5. with u,v switched

  9. Minimum spanning trees (MST) - Kruskal 1 Analysis of Union-Find 15 7 8 Lemma: k Unions take O(k log k) time 4 3 5 7 2 1 1 6 2 Corollary: The running time of Kruskal is: O(|E| log |E|) + O(|V| log |V|)

  10. Minimum spanning trees (MST) - Prim 1 15 7 8 4 3 5 7 2 1 1 6 2

  11. Minimum spanning trees (MST) - Prim 1 • Prim ( G=(V,E,w) ) • Let T=;, H=; • For every vertex v do • cost[v]=1, parent[v]=null • Let u be a vertex • Update (u) • 6. For i=1 to n-1 do • 7. u=vertex from H of • smallest cost (remove) • Add (u,parent[u]) to T • Update(u) • Return T 15 7 8 4 3 5 7 2 1 1 6 2 • Update (u) • For every neighbor v of u • If cost[v]>w(u,v) then • cost[v]=w(u,v), parent[v]=u • If v not in H then • Add v to H

  12. Minimum spanning trees (MST) - Prim 1 Lemma: Prim is correct. 15 7 8 4 3 5 7 2 1 1 6 2 Running time:

  13. Single source shortest paths - Dijkstra 1 Input: Output: G=(V,E,w) and a vertex s (w non-negative) shortest paths from s to every other vertex 15 7 8 4 3 5 7 2 1 1 6 Can use similar idea to Prim? 2

  14. Single source shortest paths - Dijkstra 1 • Dijkstra ( G=(V,E,w), s ) • Let H=; • For every vertex v do • dist[v]=1 • dist[s]=0 • Update (s) • For i=1 to n-1 do • 7. u=extract vertex from H • of smallest cost • 8. Update(u) • Return dist[] 15 7 8 4 3 5 7 2 1 1 6 2 • Update (u) • For every neighbor v of u • If dist[v]>dist[u]+w(u,v) then • dist[v]=dist[u]+w(u,v) • If v not in H then • Add v to H

  15. Single source shortest paths - Dijkstra 1 Lemma: Dijkstra is correct. 15 7 8 4 3 5 7 2 1 1 6 2 Running time:

  16. All pairs shortest paths – Floyd-Warshall 1 Input: Output: G=(V,E,w), w non-negative shortest paths between all pairs of vertices 15 7 8 4 3 5 7 2 1 1 6 2

  17. All pairs shortest paths – Floyd-Warshall 1 Input: Output: G=(V,E,w), w non-negative shortest paths between all pairs of vertices 15 7 8 4 3 5 7 2 1 1 6 • Idea 1: • Use Dijkstra from every vertex 2

  18. All pairs shortest paths – Floyd-Warshall 1 Input: Output: G=(V,E,w), w non-negative shortest paths between all pairs of vertices 15 7 8 4 3 5 7 2 1 1 6 • Idea 1: • Use Dijkstra from every vertex • Idea 2: • How about dynamic programming? 2

  19. All pairs shortest paths – Floyd-Warshall 1 Heart of the algorithm: 15 7 8 the length of the shortest path from i to j using only vertices · k 4 3 S[i,j,k] = 5 7 2 1 1 6 2

  20. All pairs shortest paths – Floyd-Warshall 1 Heart of the algorithm: 15 7 8 the length of the shortest path from i to j using only vertices · k 4 3 S[i,j,k] = 5 7 2 1 1 6 2 How to compute S[i,j,k] ? S[i,j,k] =

  21. All pairs shortest paths – Floyd-Warshall 1 • Floyd-Warshall ( G=(V,E,w) ) • For i=1 to |V| do • For j=1 to |V| do • S[i,j,0] = w(i,j) • For k=1 to |V| do • For i=1 to |V| do • For j=1 to |V| do • S[i,j,k] = min { • S[i,j,k-1], • S[i,k,k-1]+S[k,j,k-1] } • Return ? 15 7 8 4 3 5 7 2 1 1 6 2 w(i,j) if k = 0 min { S[i,j,k-1], S[i,k,k-1] + S[k,j,k-1] } if k > 0 S[i,j,k] =

  22. Single source shortest paths – Bellman-Ford -1 • Input: • directed G=(V,E,w) and a vertex s • Output: • FALSE if exists reachable negative-weight cycle, • distance to every vertex, otherwise. 4 7 -7 4 3 5 -3 2 1 1 6 2

  23. Single source shortest paths – Bellman-Ford -1 • Input: • directed G=(V,E,w) and a vertex s • Output: • FALSE if exists reachable negative-weight cycle, • distance to every vertex, otherwise. 4 7 -1 4 3 5 -3 2 1 1 6 2

  24. Single source shortest paths – Bellman-Ford -1 4 7 -7 8 3 5 -3 • Bellman-Ford ( G=(V,E,w), s ) • For every vertex v • d[v] = 1 • d[s]=0 • For i=1 to |V|-1 do • For every edge (u,v) in E do • If d[v]>d[u]+w(u,v) then • d[v]=d[u]+w(u,v) • For every edge (u,v) in E do • If d[v]>d[u]+w(u,v) then • Return NEGATIVE CYCLE • Return d[] 2 1 1 6 2

  25. Single source shortest paths – Bellman-Ford -1 Lemma: Bellman-Ford is correct. Running time: 4 7 -7 8 3 5 -3 • Bellman-Ford ( G=(V,E,w), s ) • For every vertex v • d[v] = 1 • d[s]=0 • For i=1 to |V|-1 do • For every edge (u,v) in E do • If d[v]>d[u]+w(u,v) then • d[v]=d[u]+w(u,v) • For every edge (u,v) in E do • If d[v]>d[u]+w(u,v) then • Return NEGATIVE CYCLE • Return d[] 2 1 1 6 2