Minimum Spanning Trees MST G V E w

  • Slides: 98
Download presentation
Minimum Spanning Trees

Minimum Spanning Trees

MST G = (V, E) w : E R , that assigns a weight

MST G = (V, E) w : E R , that assigns a weight to each edge

MST G = (V, E) w : E R , that assigns a weight

MST G = (V, E) w : E R , that assigns a weight to each edge Istanbul 453 324 Eskisehir 424 233 Antalya 544 Ankara 322 258 Konya

MST • given a connected undirected graph G = (V, E) with realvalued edge

MST • given a connected undirected graph G = (V, E) with realvalued edge weight w(e), an minimum spanning tree (MST) is a subset of the edges T⊆E such that T is a tree that connects all vertices of the graphs, and T has minimum total weight.

MST • given a connected undirected graph G = (V, E) with realvalued edge

MST • given a connected undirected graph G = (V, E) with realvalued edge weight w(e), an minimum spanning tree (MST) is a subset of the edges T⊆E such that T is a tree that connects all vertices of the graphs, and T has minimum total weight. w(T) = Σe in T w(e)

MST • given a connected undirected graph G = (V, E) with realvalued edge

MST • given a connected undirected graph G = (V, E) with realvalued edge weight w(e), an minimum spanning tree (MST) is a subset of the edges T⊆E such that T is a tree that connects all vertices of the graphs, and T has minimum total weight. w(T) = Σe in T w(e) • connecting all computers in an office buildings using least amount of cables

MST 32 A B 21 23 24 C 30 D 15 25 33 F

MST 32 A B 21 23 24 C 30 D 15 25 33 F E 28 G

MST 32 A B 21 23 24 C 30 D 15 25 33 F

MST 32 A B 21 23 24 C 30 D 15 25 33 F E 28 • start with an empty set of edges A G

MST 32 A B 21 23 24 C 30 D 15 25 33 F

MST 32 A B 21 23 24 C 30 D 15 25 33 F E 28 G • start with an empty set of edges A • repeat the following procedure l. Vl – 1 times : add the lightest edge that does not create a cycle to A

MST 32 A B 21 23 24 C 30 D 15 25 33 F

MST 32 A B 21 23 24 C 30 D 15 25 33 F E 28 G n-1 edges enough to connect n nodes, one more creates a cycle • start with an empty set of edges A • repeat the following procedure l. Vl – 1 times : add the lightest edge that does not create a cycle to A

MST 32 A B 21 23 24 C 30 D 15 25 33 F

MST 32 A B 21 23 24 C 30 D 15 25 33 F E 28 G • start with an empty set of edges A • repeat the following procedure l. Vl – 1 times : add the lightest edge that does not create a cycle to A

MST 32 A B 21 23 24 C 30 D 15 25 33 F

MST 32 A B 21 23 24 C 30 D 15 25 33 F E 28 G • start with an empty set of edges A • repeat the following procedure l. Vl – 1 times : add the lightest edge that does not create a cycle to A

MST 32 A B 21 23 24 C 30 D 15 25 33 F

MST 32 A B 21 23 24 C 30 D 15 25 33 F E 28 G • start with an empty set of edges A • repeat the following procedure l. Vl – 1 times : add the lightest edge that does not create a cycle to A

MST 32 A B 21 23 24 C 30 D 15 25 33 F

MST 32 A B 21 23 24 C 30 D 15 25 33 F E 28 G • start with an empty set of edges A • repeat the following procedure l. Vl – 1 times : add the lightest edge that does not create a cycle to A

MST 32 A B 21 23 24 C 30 D 15 25 33 F

MST 32 A B 21 23 24 C 30 D 15 25 33 F E 28 G • start with an empty set of edges A • repeat the following procedure l. Vl – 1 times : add the lightest edge that does not create a cycle to A

MST 32 A B 21 23 24 C 30 D 15 25 33 F

MST 32 A B 21 23 24 C 30 D 15 25 33 F E 28 G • start with an empty set of edges A • repeat the following procedure l. Vl – 1 times : add the lightest edge that does not create a cycle to A

MST 32 A B 21 23 24 C 30 D 15 25 33 F

MST 32 A B 21 23 24 C 30 D 15 25 33 F E 28 G • start with an empty set of edges A • repeat the following procedure l. Vl – 1 times : add the lightest edge that does not create a cycle to A

MST B A 21 23 24 C 30 F E D 15 28 G

MST B A 21 23 24 C 30 F E D 15 28 G • start with an empty set of edges A • repeat the following procedure l. Vl – 1 times : add the lightest edge that does not create a cycle to A

MST B A E D C G F Why does this algorithm work?

MST B A E D C G F Why does this algorithm work?

MST B A E D C G F Why does this algorithm work? (Greedy

MST B A E D C G F Why does this algorithm work? (Greedy Choice Property)

MST B A E D C (Greedy Choice Property) cut : a cut of

MST B A E D C (Greedy Choice Property) cut : a cut of G=(V, E) is a partition of V into two sets (S, V-S) G F Why does this algorithm work?

S MST B A E D C G F V-S Why does this algorithm

S MST B A E D C G F V-S Why does this algorithm work? (Greedy Choice Property) cut : a cut of G=(V, E) is a partition of V into two sets (S, V-S) {A, B, C, D} and {E, F, G}

S MST B A E D C G F V-S Why does this algorithm

S MST B A E D C G F V-S Why does this algorithm work? (Greedy Choice Property) cut : a cut of G=(V, E) is a partition of V into two sets (S, V-S) {A, B, C, D} and {E, F, G} crossing a cut : an edge (u, v) crosses a cut (S, V-S) if u in S and v in V-S

S MST B A E D C G F V-S Why does this algorithm

S MST B A E D C G F V-S Why does this algorithm work? (Greedy Choice Property) cut : a cut of G=(V, E) is a partition of V into two sets (S, V-S) {A, B, C, D} and {E, F, G} crossing a cut : an edge (u, v) crosses a cut (S, V-S) if u in S and v in V-S (B, E), (D, G), (D, F) crossing a cut

S MST B A E D C G F V-S Why does this algorithm

S MST B A E D C G F V-S Why does this algorithm work? (Greedy Choice Property) cut : a cut of G=(V, E) is a partition of V into two sets (S, V-S) {A, B, C, D} and {E, F, G} crossing a cut : an edge (u, v) crosses a cut (S, V-S) if u in S and v in V-S (B, E), (D, G), (D, F) crossing a cut respect : a set A respects a cut (S, V-S) if no edge in A crosses the cut

S MST B A E D C G F V-S Why does this algorithm

S MST B A E D C G F V-S Why does this algorithm work? (Greedy Choice Property) cut : a cut of G=(V, E) is a partition of V into two sets (S, V-S) {A, B, C, D} and {E, F, G} crossing a cut : an edge (u, v) crosses a cut (S, V-S) if u in S and v in V-S (B, E), (D, G), (D, F) crossing a cut respect : a set A respects a cut (S, V-S) if no edge in A crosses the cut A={(E, G), (F, G)} respects (S, V-S)

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree.

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. B 32 A 21 23 24 C D 30 G 28 A={} • start with any node 15 25 33 F E

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. V-S B 32 A 21 23 24 C D 30 15 25 33 F E G 28 S A={} • start with any node • A respects V-S (or S)

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. V-S B 32 A 21 23 24 C D 30 15 25 33 F E G 28 S A={} • start with any node • A respects V-S (or S) • (E, G) is the lightest edge that crosses (S, V-S)

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. V-S B 32 A 21 23 24 C D 30 15 25 33 F E G 28 S • • A={} start with any node A respects V-S (or S) (E, G) is the lightest edge that crosses (S, V-S) A = A ∪ {(E, G)}

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. B 32 A 21 23 24 C D 30 G 28 A = {(E, G)} 15 25 33 F E

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. V-S B 32 A 21 23 24 C D 30 E G 28 A = {(E, G)} • A respects V-S (or S) 15 25 33 F S

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. V-S B 32 A 21 23 24 C D 30 E 15 25 33 F S G 28 A = {(E, G)} • A respects V-S (or S) • (B, E) is the lightest edge that crosses (S, V-S)

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. V-S B 32 A 21 23 24 C D 30 E 15 25 33 F S G 28 A = {(E, G)} • A respects V-S (or S) • (B, E) is the lightest edge that crosses (S, V-S) • A = A ∪ {(B, E)}

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. B 32 A 21 23 24 C D 30 G 28 A = {(E, G), (B, E)} 15 25 33 F E

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. V-S B 32 A S 21 23 24 C D 30 15 25 33 F E G 28 A = {(E, G), (B, E)} • A respects V-S (or S) • (D, E) is the lightest edge that crosses (S, V-S)

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. V-S B 32 A S 21 23 24 C D 30 15 25 33 F E G 28 A = {(E, G), (B, E)} • A respects V-S (or S) • (D, E) is the lightest edge that crosses (S, V-S) • A = A ∪ {(D, E)}

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. Proof : If A∪{e} is already subset of T, then proof is done.

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. Proof : If A∪{e} is already subset of T, then proof is done. If not, we’ll construct a minimum spanning tree T ’ for which A∪{e} is a subset of T ‘.

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. Proof : If A∪{e} is already subset of T, then proof is done. If not, we’ll construct a minimum spanning tree T ’ for which A∪{e} is a subset of T ‘. Let e=(u, v). Since T is an MST, T must contain a path from v to u.

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. Proof : If A∪{e} is already subset of T, then proof is done. If not, we’ll construct a minimum spanning tree T ’ for which A∪{e} is a subset of T ‘. Let e=(u, v). Since T is an MST, T must contain a path from v to u. S V-S u v

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. Proof : If A∪{e} is already subset of T, then proof is done. If not, we’ll construct a minimum spanning tree T ’ for which A∪{e} is a subset of T ‘. Let e=(u, v). Since T is an MST, T must contain a path from v to u. S V-S u v f • Let f be the edge on this path that crosses (S, V-S).

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. Proof : If A∪{e} is already subset of T, then proof is done. If not, we’ll construct a minimum spanning tree T ’ for which A∪{e} is a subset of T ‘. Let e=(u, v). Since T is an MST, T must contain a path from v to u. S V-S u v f • Let f be the edge on this path that crosses (S, V-S). Such edge must exist since there should be an edge that connects this cut

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. Proof : If A∪{e} is already subset of T, then proof is done. If not, we’ll construct a minimum spanning tree T ’ for which A∪{e} is a subset of T ‘. Let e=(u, v). Since T is an MST, T must contain a path from v to u. S V-S u e f v Let f be the edge on this path that crosses (S, V-S). Such edge must exist since there should be an edge that connects this cut • define a new MST T ‘ as T ‘ = T ∪{e} – {f} •

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. Proof : If A∪{e} is already subset of T, then proof is done. If not, we’ll construct a minimum spanning tree T ’ for which A∪{e} is a subset of T ‘. Let e=(u, v). Since T is an MST, T must contain a path from v to u. S V-S u e f v Let f be the edge on this path that crosses (S, V-S). Such edge must exist since there should be an edge that connects this cut • define a new MST T ‘ as T ‘ = T ∪{e} – {f} • since e is the lightest edge that crosses (S, V-S) (from assumption), w(e) ≤ w(f) •

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. Proof : If A∪{e} is already subset of T, then proof is done. If not, we’ll construct a minimum spanning tree T ’ for which A∪{e} is a subset of T ‘. Let e=(u, v). Since T is an MST, T must contain a path from v to u. S V-S u e f v Let f be the edge on this path that crosses (S, V-S). Such edge must exist since there should be an edge that connects this cut • define a new MST T ‘ as T ‘ = T ∪{e} – {f} • since e is the lightest edge that crosses (S, V-S) (from assumption), w(e) ≤ w(f) • Thus, w(T ‘) = w(T) – w(f) + w(e) ≤ w(T) •

Greedy Choice Property Theorem : Let A be a subset of edges of some

Greedy Choice Property Theorem : Let A be a subset of edges of some minimum spanning tree T. Let (S, V-S) be a cut that A respects and e be the lightest edge that crosses (S, V-S). Then A∪{e} is a subset of some minimum spanning tree. Proof : If A∪{e} is already subset of T, then proof is done. If not, we’ll construct a minimum spanning tree T ’ for which A∪{e} is a subset of T ‘. Let e=(u, v). Since T is an MST, T must contain a path from v to u. S V-S u e v f this is a contradiction ! Let f be the edge on this path that crosses (S, V-S). Such edge must exist since there should be an edge that connects this cut • define a new MST T ‘ as T ‘ = T ∪{e} – {f} • since e is the lightest edge that crosses (S, V-S) (from assumption), w(e) ≤ w(f) • Thus, w(T ‘) = w(T) – w(f) + w(e) ≤ w(T) •

Kruskal’s Algorithm • • start with an empty tree T consider edges in increasing

Kruskal’s Algorithm • • start with an empty tree T consider edges in increasing order of weights add the lightest edge to T if it does not create a cycle If It creates a cycle, discard it

Kruskal’s Algorithm • • start with an empty tree T consider edges in increasing

Kruskal’s Algorithm • • start with an empty tree T consider edges in increasing order of weights add the lightest edge to T if it does not create a cycle If It creates a cycle, discard it How to check whether adding an edge to T will create a cycle?

Kruskal’s Algorithm • • start with an empty tree T consider edges in increasing

Kruskal’s Algorithm • • start with an empty tree T consider edges in increasing order of weights add the lightest edge to T if it does not create a cycle If It creates a cycle, discard it How to check whether adding an edge to T will create a cycle? • DFS can be used, but it takes O(l. El + l. Vl) time for each step, O(l. El. l. Vl) at total

Kruskal’s Algorithm • • start with an empty tree T consider edges in increasing

Kruskal’s Algorithm • • start with an empty tree T consider edges in increasing order of weights add the lightest edge to T if it does not create a cycle If It creates a cycle, discard it How to check whether adding an edge to T will create a cycle? • DFS can be used, but it takes O(l. El + l. Vl) time for each step, O(l. El. l. Vl) at total • use union-find data structure

Union-Find • • represent each set as a tree each node has a pointer

Union-Find • • represent each set as a tree each node has a pointer to its parent the root has a parent pointer to itself each tree has a height

Union-Find • • • represent each set as a tree each node has a

Union-Find • • • represent each set as a tree each node has a pointer to its parent the root has a parent pointer to itself each tree has a height data structure supports three operations

Union-Find • • • represent each set as a tree each node has a

Union-Find • • • represent each set as a tree each node has a pointer to its parent the root has a parent pointer to itself each tree has a height data structure supports three operations Make-Set(x) x. parent = x x. height = 0

Union-Find • • • represent each set as a tree each node has a

Union-Find • • • represent each set as a tree each node has a pointer to its parent the root has a parent pointer to itself each tree has a height data structure supports three operations Make-Set(x) x. parent = x x. height = 0 Find-Set(x) while x ≠ x. parent x = x. parent return x

Union-Find • • • represent each set as a tree each node has a

Union-Find • • • represent each set as a tree each node has a pointer to its parent the root has a parent pointer to itself each tree has a height data structure supports three operations Make-Set(x) x. parent = x x. height = 0 Find-Set(x) while x ≠ x. parent x = x. parent return x return the root of the set

Union-Find • • • represent each set as a tree each node has a

Union-Find • • • represent each set as a tree each node has a pointer to its parent the root has a parent pointer to itself each tree has a height data structure supports three operations Make-Set(x) x. parent = x x. height = 0 Find-Set(x) while x ≠ x. parent x = x. parent return x return the root of the set Union(x, y) a = Find-Set(x) B = Find-Set(y) if (a. height ≤ b. height) if (a. height = b. height) b. height = b. height + 1 a. parent = b else b. parent = a

Union-Find • • • represent each set as a tree each node has a

Union-Find • • • represent each set as a tree each node has a pointer to its parent the root has a parent pointer to itself each tree has a height data structure supports three operations Make-Set(x) x. parent = x x. height = 0 Find-Set(x) while x ≠ x. parent x = x. parent return x return the root of the set Union(x, y) a = Find-Set(x) B = Find-Set(y) if (a. height ≤ b. height) if (a. height = b. height) b. height = b. height + 1 a. parent = b else b. parent = a Always make the root of the taller one the parent of the shorter

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in increasing order by weight for each edge (u, v) if Find-Set(u) ≠ Find-Set(v) A = A ∪ {u, v} Union(u, v) return A

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V O(l. Vl) Make-Set(v) sort all

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V O(l. Vl) Make-Set(v) sort all edges in increasing order by weight for each edge (u, v) if Find-Set(u) ≠ Find-Set(v) A = A ∪ {u, v} Union(u, v) return A

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V O(l. Vl) Make-Set(v) sort all

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V O(l. Vl) Make-Set(v) sort all edges in increasing O(l. El. logl. El) order by weight for each edge (u, v) if Find-Set(u) ≠ Find-Set(v) A = A ∪ {u, v} Union(u, v) return A

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V O(l. Vl) Make-Set(v) sort all

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V O(l. Vl) Make-Set(v) sort all edges in increasing O(l. El. logl. El) order by weight for each edge (u, v) if Find-Set(u) ≠ Find-Set(v) O(l. El. logl. Vl) A = A ∪ {u, v} Union(u, v) return A

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in increasing order by weight for each edge (u, v) if Find-Set(u) ≠ Find-Set(v) A = A ∪ {u, v} Union(u, v) C 4 return A 3 D 3 4 E 2 F A 6 5 B

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in increasing order by weight for each edge (u, v) if Find-Set(u) ≠ Find-Set(v) A = A ∪ {u, v} Union(u, v) C 4 return A 3 2 F A D 3 4 E D 6 5 B F B C E A

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in increasing order by weight for each edge (u, v) if Find-Set(u) ≠ Find-Set(v) A = A ∪ {u, v} Union(u, v) C 4 return A 3 2 F A D 3 4 E D 6 5 B F B (F, B) (A, B) (D, E) (A, C) (C, E) (D, B) (D, F) C E A

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in increasing order by weight for each edge (u, v) if Find-Set(u) ≠ Find-Set(v) A = A ∪ {u, v} Union(u, v) C 4 return A 3 2 F F A D 3 4 E D 6 5 B B (F, B) (A, B) (D, E) (A, C) (C, E) (D, B) (D, F) C E A

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in increasing order by weight for each edge (u, v) if Find-Set(u) ≠ Find-Set(v) A = A ∪ {u, v} Union(u, v) C 4 return A 3 2 F F A D 3 4 E D 6 5 B B (F, B) (A, B) (D, E) (A, C) (C, E) (D, B) (D, F) C E A

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in increasing order by weight for each edge (u, v) if Find-Set(u) ≠ Find-Set(v) A = A ∪ {u, v} Union(u, v) C 4 return A 3 E 2 F F A D 3 4 C D 6 5 B B (F, B) (A, B) (D, E) (A, C) (C, E) (D, B) (D, F) E A

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in increasing order by weight for each edge (u, v) if Find-Set(u) ≠ Find-Set(v) A = A ∪ {u, v} Union(u, v) C 4 return A 3 E 2 F F A D 3 4 C D 6 5 B B (F, B) (A, B) (D, E) (A, C) (C, E) (D, B) (D, F) E A

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in increasing order by weight for each edge (u, v) if Find-Set(u) ≠ Find-Set(v) A = A ∪ {u, v} Union(u, v) C 4 return A 3 2 F F A D 3 4 E D 6 5 B C B (F, B) (A, B) (D, E) (A, C) (C, E) (D, B) (D, F) E A

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in increasing order by weight for each edge (u, v) if Find-Set(u) ≠ Find-Set(v) A = A ∪ {u, v} Union(u, v) C 4 return A 3 2 F F A D 3 4 E D 6 5 B C B (F, B) (A, B) (D, E) (A, C) (C, E) (D, B) (D, F) E A

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in

Kruskal’s Algorithm MST-Kruskal(G) A={} for each v of V Make-Set(v) sort all edges in increasing order by weight for each edge (u, v) if Find-Set(u) ≠ Find-Set(v) A = A ∪ {u, v} Union(u, v) C 4 return A 3 2 F F A D 3 4 E D 6 5 B C B (F, B) (A, B) (D, E) (A, C) (C, E) (D, B) (D, F) E A

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v)

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ O(l.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ O(l. Vl) u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v)

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ O(l.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ O(l. Vl) u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) O(l. Vl)

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ O(l.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ O(l. Vl) u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) O(l. Vl. logl. Vl)

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ O(l.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ O(l. Vl) u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) O(l. Vl. logl. Vl) O(l. El. logl. Vl)

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) B 5 A 8 7 3 C 4 E 3 D 6 F G 6 9 4 H

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) ∞ ∞ ∞ 7 ∞ B 5 A 3 8 C 4 E 3 D 6 G ∞ ∞ F 9 4 6 ∞ H 0 HFGEDCBA

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) ∞ ∞ ∞ 7 ∞ B 5 A 3 8 C 4 E 3 D 6 G ∞ ∞ F 9 4 6 ∞ H 0 FGEDCBA

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) ∞ ∞ ∞ 7 ∞ B 5 A 3 8 C 4 E 3 D 6 G 6 4 F 9 4 6 9 H 0 FGEDCBA

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) ∞ ∞ ∞ 7 ∞ B 5 A 3 8 C 4 E 3 D 6 G 6 4 F 9 4 6 9 H 0 GEDCBA

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) ∞ ∞ ∞ 7 ∞ B 5 A 3 8 C 4 E 3 D 6 G 6 4 F 9 4 6 9 H 0 GEDCBA

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) ∞ ∞ ∞ 7 ∞ B 5 A 3 8 C 4 E 3 D 6 G 6 4 F 9 4 6 3 H 0 GEDCBA

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) ∞ ∞ ∞ 7 ∞ B 5 A 3 8 C 4 E 3 D 6 4 F 9 4 G 6 H 0 6 G 3 DCBA

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) ∞ ∞ ∞ 7 ∞ B 5 A 3 8 C 4 E 3 D 6 4 F 9 4 G 6 H 0 6 G 3 DCBA

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) 8 ∞ 4 7 ∞ B 5 A 3 8 C 4 E 3 D 6 4 F 9 4 G 6 H 0 6 G 3 DCBA

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) 8 ∞ 4 7 ∞ B 5 A 3 8 C 4 E 3 D 6 4 F 9 4 G 6 H 0 6 G D 3 BA

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) 8 ∞ 4 7 ∞ B 5 A 3 8 C 4 E 3 D 6 4 F 9 4 G 6 H 0 6 G D 3 BA

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) 8 ∞ 4 7 3 B 5 A 3 8 C 4 E 3 D 6 4 F 9 4 G 6 H 0 6 G D 3 BA

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) 8 ∞ 4 7 3 B 5 A 3 8 C 4 E 3 D 6 4 F 9 4 G 6 H 0 6 G 3 BA

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) 8 ∞ 4 7 3 B 5 A 3 8 C 4 E 3 D 6 4 F 9 4 G 6 H 0 6 G 3 BA

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) 7 8 4 7 3 B 5 A 3 8 C 4 E 3 D 6 4 F 9 4 G 6 H 0 6 G 3 BA

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) 7 8 4 7 3 B 5 A 3 8 C 4 E 3 D 6 G 4 F 3 9 4 6 H 0 6 B

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) 7 5 4 7 3 B 5 A 3 8 C 4 E 3 D 6 G 4 F 3 9 4 6 H 0 6 B

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) 7 5 4 7 3 B 5 A 3 8 C 4 E 3 D 6 G 6 4 F 9 4 6 3 H 0

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u.

Prim’s Algorithm MST-Prim(G, s) for each u of V u. key = ∞ u. par = nil s. key = 0 create a minimum priority Q on V while Q ≠ { } u = Extract. Min(Q) for each v of Adj(u) if v in Q and w(u, v) < v. key v. par = u v. key = w(u, v) 7 5 4 7 3 B 5 A 3 C 4 E 3 D 4 F 4 G 6 6 H 0 3