23
inimum 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.

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

Embed Size (px)

Citation preview

Slide 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.1Minimum 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.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.151763228451172Kruskal ( G=(V,E,w) ) Let T=; Sort the edges in increasing order of weight3. For edge e do4. If T [ e does not contain a cycle then Add e to T Return TMinimum spanning trees (MST) - Kruskal151763228451173Kruskal ( G=(V,E,w) ) Let T=; Sort the edges in increasing order of weight3. For edge e do4. If T [ e does not contain a cycle then Add e to T Return TMinimum spanning trees (MST) - Kruskal15176322845117Lemma: Algo is correct.4Kruskal ( G=(V,E,w) ) Let T=; Sort the edges in increasing order of weight3. For edge e do4. If T [ e does not contain a cycle then Add e to T Return TMinimum spanning trees (MST) - Kruskal15176322845117Implementation?5Kruskal ( G=(V,E,w) ) Let T=; Sort the edges in increasing order of weight3. For edge e do4. If T [ e does not contain a cycle then Add e to T Return TMinimum spanning trees (MST) - Kruskal15176322845117Implementation?6Init (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 switchedMinimum spanning trees (MST) - Kruskal15176322845117Implementation?- Union-Find datastructure7Union (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 switchedMinimum spanning trees (MST) - Kruskal15176322845117Analysis of Union-FindLemma:k Unions take O(k log k) time8Minimum spanning trees (MST) - Kruskal15176322845117Analysis of Union-FindLemma:k Unions take O(k log k) timeCorollary:The running time of Kruskal is: O(|E| log |E|) + O(|V| log |V|)9Prim ( 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 do7. u=vertex from H of smallest cost (remove) Add (u,parent[u]) to T Update(u)Return TMinimum spanning trees (MST) - Prim15176322845117Update (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 10Minimum spanning trees (MST) - Prim15176322845117Lemma: Prim is correct.Running time:11Single source shortest paths - Dijkstra15176322845117G=(V,E,w) and a vertex s (w non-negative)shortest paths from s to every other vertexInput:

Output:Can use similar idea to Prim?12Dijkstra ( 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 do7. u=extract vertex from H of smallest cost8. Update(u)Return dist[]15176322845117Update (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 Single source shortest paths - Dijkstra1315176322845117Single source shortest paths - DijkstraLemma: Dijkstra is correct.Running time:1415176322845117All pairs shortest paths Floyd-WarshallG=(V,E,w), w non-negativeshortest paths between all pairs of verticesInput:Output:1515176322845117All pairs shortest paths Floyd-WarshallIdea 1: Use Dijkstra from every vertex

G=(V,E,w), w non-negativeshortest paths between all pairs of verticesInput:Output:1615176322845117All pairs shortest paths Floyd-WarshallIdea 1: Use Dijkstra from every vertex

Idea 2: How about dynamic programming?

G=(V,E,w), w non-negativeshortest paths between all pairs of verticesInput:Output:1715176322845117All pairs shortest paths Floyd-Warshallthe length of the shortest path from i to j using only vertices kHeart of the algorithm:S[i,j,k] =1815176322845117All pairs shortest paths Floyd-WarshallHeart of the algorithm:S[i,j,k] =S[i,j,k] = How to compute S[i,j,k] ?the length of the shortest path from i to j using only vertices k19Floyd-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 ? 15176322845117All pairs shortest paths Floyd-WarshallS[i,j,k] =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 20Input:directed G=(V,E,w) and a vertex s

Output: FALSE if exists reachable negative-weight cycle, distance to every vertex, otherwise.-176322-74511-3Single source shortest paths Bellman-Ford421Bellman-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 CYCLEReturn d[]-176322-78511-3Single source shortest paths Bellman-Ford422Bellman-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 CYCLEReturn d[]-176322-78511-3Single source shortest paths Bellman-FordLemma: Bellman-Ford is correct.

Running time:423