lecture 12 graphs iii (trees, msts) (chp 11.5, 11.6)
TRANSCRIPT
![Page 1: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/1.jpg)
Lecture 12
Graphs III (Trees, MSTs) (Chp 11.5, 11.6)
![Page 2: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/2.jpg)
Outline
1. Trees1.1 Definition of a tree.1.2 Examples of trees.1.3 Theorem #1: Tree Characterization
2. Rooted Trees2.1 Definitions2.2 Definition: n-ary trees and binary trees.2.3 Definition: Full n-ary trees2.4 Theorem #2: Full Tree2.5 Theorem #3: Leaves-Height2.6 Examples
3. Spanning Trees3.1 Motivation3.2 Definition3.3 Theorem #4: Spanning Tree3.4 Minimum Spanning Tree (MST)3.5 Kruskal’s Algorithm3.6 Prim’s Algorithm
![Page 3: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/3.jpg)
1. Trees
1.1 Definition: Let G=(V,E).
G is a tree IFF
(a) G is connected; and
(b) G does not have any circuits (acyclic).
Comment: – The textbook distinguishes between trivial and
non-trivial circuits. They define a trivial circuit as a circuit of length 0.
– As far as we are concerned, unless otherwise stated, when we say ‘circuit’ we mean ‘NON-TRIVIAL circuit’. i.e. the default meaning of ‘circuit’ is a non-trivial circuit.
![Page 4: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/4.jpg)
1. Trees
1.1 Definition: Let G=(V,E).
G is a tree IFF
(a) G is connected; and
(b) G does not have any circuits (acyclic).
1.2.1 Example of a tree
![Page 5: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/5.jpg)
1. Trees
1.2.2 Examples of trees in real life usage– Family Tree– Tournaments– Directory Tree– Syntax Tree – Execution Tree– Decision Tree– Search Tree– B-Tree (Databases)
![Page 6: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/6.jpg)
1. Trees
1.3 Theorem (Tree Characterization Theorem): Let G=(V,E). G is a tree IFF G is connected and |E|=|V|-1
Proof Strategy:– () Assume G is a tree
• Prove that G is connected (Trivial)
• Prove that |E| = |V| - 1 (Prove by induction on |V|)– Lemma 1: A tree with more than 1 vertex has at least 1 vertex of
degree 1
– () Assume G is connected and |E| = |V| -1(Prove that G is a tree. How? Show that G fits the definition of a
tree.)
• Prove that G is connected (Trivial)
• Prove that G has no circuits (Prove by contradiction)– Lemma 2: Deletion of edge from a circuit of a connected graph
does not violate connectedness.
![Page 7: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/7.jpg)
1.3 Tree Characterization Theorem
(Proof: ) Assume G is a tree
(a) Prove that G is connected
(b) Prove that |E| = |V| - 1
Proof of (a): Trivial
Since G is a tree, and by definition, a tree is connected.
![Page 8: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/8.jpg)
1.3 Tree Characterization Theorem
(Proof: ) Assume G is a tree
(a) Prove that G is connected
(b) Prove that |E| = |V| - 1
Lemma 1: A tree with more than one vertex has at least one vertex of degree 1.
Before we prove (b), we need to prove a lemma.
![Page 9: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/9.jpg)
1.3 Tree Characterization Theorem
Lemma 1: A tree with more than one vertex has at least one () vertex of degree 1.
Proof:
Let T = (V,E) be a tree, such that |V| 1. Prove vV, deg(v)=1 .
() Constructive Proof: Find that vertex! We will give an algorithm to find the vertex:
1. Pick any uV. Let e be some edge incident on u, say e={u,v}.
2. while (deg(u) 1) do {
2.1 Choose another edge e’ e; say e’={u,v’} and v v’.
2.2 Let e e’, u v’. /* Move to another new vertex*/
}
3. Output u.
![Page 10: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/10.jpg)
1.3 Tree Characterization Theorem
Lemma 1: A tree with more than one vertex has at least one () vertex of degree 1.
1. Pick any uV. Let e be some edge incident on u, say e={u,v}.
2. while (deg(u) 1) do {
2.1 Choose another edge e’ e; say e’={u,v’} and v v’.
2.2 Let e e’, u v’. /* Move to another new vertex*/
}
3. Output u.
u
ev
e’v’
![Page 11: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/11.jpg)
1.3 Tree Characterization Theorem
Lemma 1: A tree with more than one vertex has at least one () vertex of degree 1.
1. Pick any uV. Let e be some edge incident on u, say e={u,v}.
2. while (deg(u) 1) do {
2.1 Choose another edge e’ e; say e’={u,v’} and v v’.
2.2 Let e e’, u v’. /* Move to another new vertex*/
}
3. Output u.
v
eu
e’v’
![Page 12: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/12.jpg)
1.3 Tree Characterization Theorem
Lemma 1: A tree with more than one vertex has at least one () vertex of degree 1.
1. Pick any uV. Let e be some edge incident on u, say e={u,v}.
2. while (deg(u) 1) do {
2.1 Choose another edge e’ e; say e’={u,v’} and v v’.
2.2 Let e e’, u v’. /* Move to another new vertex*/
}
3. Output u.
v
eu
![Page 13: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/13.jpg)
1.3 Tree Characterization Theorem
Lemma 1: A tree with more than one vertex has at least one () vertex of degree 1.
1. Pick any uV. Let e be some edge incident on u, say e={u,v}.
2. while (deg(u) 1) do {
2.1 Choose another edge e’ e; say e’={u,v’} and v v’.
2.2 Let e e’, u v’. /* Move to another new vertex*/
}
3. Output u.
Claim I: Algorithm will always terminate.
The loop will not carry on infinitely because (1) the tree does not contain any cycles; (2) The algorithm always chooses a new edge; (3) the set of vertices in the tree is finite.
![Page 14: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/14.jpg)
1.3 Tree Characterization Theorem
Lemma 1: A tree with more than one vertex has at least one () vertex of degree 1.
1. Pick any uV. Let e be some edge incident on u, say e={u,v}.
2. while (deg(u) 1) do {
2.1 Choose another edge e’ e; say e’={u,v’} and v v’.
2.2 Let e e’, u v’. /* Move to another new vertex*/
}
3. Output u.
Claim II: If the algo terminates, then it will find a degree 1 vertex.
Why? The exit condition of the loop is when deg(u) 1.
Since T is connected, deg(u) 1. Therefore deg(u) = 1.
Lemma Proven: Since we can always find that vertex of degree 1.
![Page 15: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/15.jpg)
1.3 Tree Characterization Theorem
(Proof: ) Assume G is a tree
(a) Prove that G is connected
(b) Prove that |E| = |V| - 1
Proof of (b): by induction on |V|.
Base Case: |V| = 1
Inductive Case: |V| = k |V| = k+1
![Page 16: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/16.jpg)
1.3 Tree Characterization Theorem
(Proof: ) Assume G is a tree
(a) Prove that G is connected
(b) Prove that |E| = |V| - 1
Base Case: |V| = 1
G is a tree of 1 vertex.
G has no edges.
Therefore |E| = 0 = |V| - 1
Base case is true.
![Page 17: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/17.jpg)
1.3 Tree Characterization Theorem
(Proof: ) Assume G is a tree
(a) Prove that G is connected
(b) Prove that |E| = |V| - 1
Inductive Case: |V| = k |V| = k+1
Assume true for all graphs with k vertices.
Let G = (V,E) be a tree with |V| = k+1 vertices.
Then (by lemma 1) there is a vertex v of degree 1.
Remove v with its edge from G to form a new graph G’ = (V’,E’).
where V’ = V – {v}, E’ = { {x,y}E | xv and yv }
(a) G’ is connected since all walks involving v must end with v. And all walks NOT involving v will not use the edge connected to v.
G
G’ = (V’,E’)
v
![Page 18: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/18.jpg)
1.3 Tree Characterization Theorem
(Proof: ) Assume G is a tree
(a) Prove that G is connected
(b) Prove that |E| = |V| - 1
Inductive Case: |V| = k |V| = k+1
Assume true for all graphs with k vertices.
Let G = (V,E) be a tree with |V| = k+1 vertices.
Then (by lemma 1) there is a vertex v of degree 1.
Remove v with its edge from G to form a new graph G’ = (V’,E’).
where V’ = V – {v}, E’ = { {x,y}E | xv and yv }
(b) G’ is circuit free. Why? Because G is circuit free and removing an edge cannot create a circuit.
G
G’ = (V’,E’)
v
![Page 19: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/19.jpg)
1.3 Tree Characterization Theorem
(Proof: ) Assume G is a tree
(a) Prove that G is connected
(b) Prove that |E| = |V| - 1
Inductive Case: |V| = k |V| = k+1
Assume true for all graphs with k vertices.
Let G = (V,E) be a tree with |V| = k+1 vertices.
Then (by lemma 1) there is a vertex v of degree 1.
Remove v with its edge from G to form a new graph G’ = (V’,E’).
where V’ = V – {v}, E’ = { {x,y}E | xv and yv }
G’ is a tree. |V’| = k. Inductive Hypothesis: |E’| = |V’| - 1
Now add back v with its edge to form back G=(V,E). Now…
|E| = |E’| + 1 = |V’| - 1 + 1 = |V’| = |V| - 1 (Proven)
G
G’ = (V’,E’)
v
![Page 20: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/20.jpg)
1.3 Tree Characterization Theorem
1.3 Theorem (Tree Characterization Theorem): Let G=(V,E). G is a tree IFF G is connected and |E|=|V|-1
Proof Strategy:– () Assume G is a tree
• Prove that G is connected (Trivial)
• Prove that |E| = |V| - 1 (Prove by induction on |V|)– Lemma 1: A tree with more than 1 vertex has at least 1 vertex of
degree 1
– () Assume G is connected and |E| = |V| -1(Prove that G is a tree. How? Show that G fits the definition of a
tree.)
• Prove that G is connected (Trivial)
• Prove that G has no circuits (Prove by contradiction)– Lemma 2: Deletion of edge from a circuit of a connected graph
does not violate connectedness.
![Page 21: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/21.jpg)
1.3 Tree Characterization Theorem
(Proof: ) Assume G is connected and |E| = |V| - 1
Prove that G is a tree… (by definition of a tree)– G is connected (trivial)– G does not have any circuits.
Lemma 2: Let G=(V,E) be a connected graph. Let C be a circuit in G. If an edge in C is removed from G to form G’, then G’ is still connected.
Proof of (a): Trivial, since it’s in the assumption.
Before we prove (b), we need to prove another lemma.
![Page 22: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/22.jpg)
1.3 Tree Characterization Theorem Lemma 2: Let G=(V,E) be a connected graph. Let C be a
circuit in G. If an edge in C is removed from G to form G’, then G’ is still connected.
Proof:
Let C = v0 e1 v1 e2 v2 e3 … ei-1 vi-1 ei vi ei+1 … en v0 be the circuit in G.
Let ei be the edge that is deleted from the circuit creating the new graph G’=(V,E’). Need to show that G’ is connected. (Definition of connected: x,y V, there is a walk from x to y in G’.)
v0
v1
v2
vi-1
vi ei
G
v0
v1
v2
vi-1
vi
G’
Walk b/w any 2 vertices?
![Page 23: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/23.jpg)
1.3 Tree Characterization Theorem Lemma 2: Let G=(V,E) be a connected graph. Let C be a
circuit in G. If an edge in C is removed from G to form G’, then G’ is still connected.
Proof:
Let C = v0 e1 v1 e2 v2 e3 … ei-1 vi-1 ei vi ei+1 … en v0 be the circuit in G.
Pick any 2 vertices x V and y V.
v0
v1
v2
vi-1
vi ei
G
v0
v1
v2
vi-1
vi
G’
Walk b/w any 2 vertices?
Since G is connected, then there is a walk W from x to y in G. Either (1) ei is in the walk W in G or (2) ei is NOT in the walk W in G.
![Page 24: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/24.jpg)
1.3 Tree Characterization Theorem
Proof:
Let C = v0 e1 v1 e2 v2 … ei-1 vi-1 ei vi ei+1 … en v0 be the circuit in G.
Case (1): ei is in the walk W from x to y in G. Let W be…
W = x … vi-1 ei vi … y
We construct another walk
W’ = x … vi-1 ei-1 … v2 e2 v1 e1 v0 en … ei+1 vi … y
W’ is a walk from x to y without going through ei.
W’ is a walk from x to y in G’ (Since G and G’ differ only on the edge ei). So there is a walk from x to y in G’.
Case (1) Proven.
Lemma 2: Let G=(V,E) be a connected graph. Let C be a circuit in G. If an edge in C is removed from G to form G’, then G’ is still connected.
![Page 25: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/25.jpg)
1.3 Tree Characterization Theorem
Case (2): ei is NOT in the walk W from x to y in G.
Since W does not involve ei, then W is also a walk from x to y in G’ (G and G’ differ only on the edge ei). So there is a walk from x to y in G’.
Case (2) Proven
Proof:
Let C = v0 e1 v1 e2 v2 … ei-1 vi-1 ei vi ei+1 … en v0 be the circuit in G.
Lemma 2: Let G=(V,E) be a connected graph. Let C be a circuit in G. If an edge in C is removed from G to form G’, then G’ is still connected.
![Page 26: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/26.jpg)
1.3 Tree Characterization Theorem
Proof:
Let C = v0 e1 v1 e2 v2 … vi-1 ei vi … en v0 be the circuit in G.
Let ei be the edge that is deleted from the circuit creating the new graph G’=(V,E’). Need to show that G’ is connected. (Definition of connected: x,y V, there is a walk from x to y in G’.)
Pick any 2 vertices x V and y V.
Since G is connected, then there is a walk W from x to y in G.
Either (1) ei is in the walk W in G or (2) ei is not in the walk W in G. From previous 2 slides (considering each case), there is a walk from x to y in G’. Therefore G’ is connected.
Lemma 2: Let G=(V,E) be a connected graph. Let C be a circuit in G. If an edge in C is removed from G to form G’, then G’ is still connected.
![Page 27: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/27.jpg)
1.3 Tree Characterization Theorem
(Proof: ) Assume G is connected and |E| = |V| - 1
Prove that G is a tree… (by definition of a tree)
(b) …G does not have any circuits.
Proof of (b): (by contradiction)
Assume G has circuits. Let v0 e0 v1 e1 … vn en v0 be a circuit.
Delete any edge from the circuit to form G’ = (V,E’), |E’| = |E| - 1. By lemma 2, G’ is still connected.
If G’ still has another circuit, then delete an edge from that circuit to form G’’ = (V,E’’), |E’’| = |E’| - 1. Again G’’ is still connected.
Keep repeating this process until we have a graph G* without any circuits, where G* = (V,E*), E* = |E| - n (n 1). G* is still connected.
G* is a tree. Therefore |E*| = |V| - 1 (Just proven the direction).|E| - n = |V| - 1 (n 1) |E| = |V| - 1 + n (n 1) (Contradiction)
![Page 28: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/28.jpg)
1.3 Tree Characterization Theorem Theorem (Tree Characterization Theorem):
Let G=(V,E). G is a tree IFFG is connected and |E| = |V| - 1
Proof Strategy:– () Assume G is a tree
• Prove that G is connected (Trivial)• Prove that |E| = |V| - 1 (Prove by induction on |V|)
– Lemma 1: A tree with more than 1 vertex has at least 1 vertex of degree 1
– () Assume G is connected and |E| = |V| -1(Prove that G is a tree. How? Show that G fits the definition of a
tree.• G is connected (Trivial)• G has no circuits (Prove by contradiction)
– Lemma 2: Deletion of edge from a circuit of a connected graph does not violate connectedness.
![Page 29: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/29.jpg)
Outline
1. Trees1.1 Definition of a tree.1.2 Examples of trees.1.3 Theorem #1: Tree Characterization
2. Rooted Trees2.1 Definitions2.2 Definition: n-ary trees and binary trees.2.3 Definition: Full n-ary trees2.4 Theorem #2: Full Tree2.5 Theorem #3: Leaves-Height2.6 Examples
3. Spanning Trees3.1 Motivation3.2 Definition3.3 Theorem #4: Spanning Tree3.4 Minimum Spanning Tree (MST)3.5 Kruskal’s Algorithm3.6 Prim’s Algorithm
![Page 30: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/30.jpg)
2.1 Rooted Trees (Definition)
A rooted tree is a tree in which one vertex is distinguished from the others and is called the root.
The level of a vertex v is the path length from the root to v. The height of the tree is the maximum level to any vertex of the tree.
rootLevel 0
Level 1
Level 2
Level 3
Level 4
Note: Definition of ‘Level’ and ‘Height’ different from cs1102
![Page 31: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/31.jpg)
2.1 Rooted Trees (Definition)
Given any vertex v in a rooted tree:– The children of v are the vertices adjacent to v, 1 level
further away from the root.– The parent of v is the vertex adjacent to v, 1 level
nearer to the root.– The siblings of v are the vertices which have the same
parent as v.
vParent of v
Children of v
Siblings of v
![Page 32: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/32.jpg)
2.1 Rooted Trees (Definition)
Given any vertex v in a rooted tree:– The ancestor of v are the vertices which lie in the path
from v to the root.– If u is the ancestor of v, then v is the descendant of u.
v
Ancestors of v
Descendants of v
![Page 33: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/33.jpg)
2.1 Rooted Trees (Definition)
Given any rooted tree:– The internal vertices of the tree are the vertices which have at
least 1 child.– The external vertices of the tree are the vertices which have
no children. External vertices are also known as the leaves of the tree, or terminal vertices.
External Vertices (Leaves)
The rest are Internal Vertices
![Page 34: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/34.jpg)
2.2 m-ary trees and binary trees (Defn)
A m-ary tree is a rooted tree in which every vertex has at most m children.
Example of a 4-ary Tree
Example of a 3-ary Tree
![Page 35: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/35.jpg)
2.2 m-ary trees and binary trees (Defn)
A m-ary tree is a rooted tree in which every vertex has at most m children.– A binary tree is a m-ary tree with n=2.
• Each child of the binary tree is designated either the left child or the right child.
• Given a vertex v of a binary tree, the left subtree of v (right subtree of v) is the binary tree whose root is the left child of v (right child of v).
v
Left subtree of v
Right subtree of v
Left child of v
Right child of v
Example of a Binary Tree
![Page 36: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/36.jpg)
2.3 Full m-ary Trees (Definition)
A m-ary tree is FULL iff every vertex has either 0 or m children. (OR every internal vertex has m children).
Examples of full binary trees.
Full Tree? Yes No Yes No No
Yes Yes
Note: Definition of ‘Full’ different from cs1102
![Page 37: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/37.jpg)
Proof:
2.4 Full Tree Theorem
Full Tree theorem: A full m-ary tree with k internal vertices has mk + 1 vertices.
Let T=(V,E) be a full m-ary tree, with k internal vertices.
Total number of vertices in T
Number of vertices that HAVE a parent
Number of vertices that DO NOT HAVE a parent
= +
Q: How many vertices HAVE a parent?
1. Observe for a 2-ary tree with 7 internal vertices
2. Each internal vertex has 2 children
![Page 38: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/38.jpg)
Proof:
2.4 Full Tree Theorem
Full Tree theorem: A full m-ary tree with k internal vertices has mk + 1 vertices.
Let T=(V,E) be a full m-ary tree, with k internal vertices.
Total number of vertices in T
Number of vertices that HAVE a parent
Number of vertices that DO NOT HAVE a parent
= +
Q: How many vertices HAVE a parent?
1. Observe for a 2-ary tree with 7 internal vertices
2. Each internal vertex has 2 childrenAnother way of looking at it is that for each internal vertex, there are 2 vertices which have a parent.
![Page 39: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/39.jpg)
Proof:
2.4 Full Tree Theorem
Full Tree theorem: A full m-ary tree with k internal vertices has mk + 1 vertices.
Let T=(V,E) be a full m-ary tree, with k internal vertices.
Total number of vertices in T
Number of vertices that HAVE a parent
Number of vertices that DO NOT HAVE a parent
= +
Q: How many vertices HAVE a parent?
1. Observe for a 2-ary tree with 7 internal vertices
2. Each internal vertex has 2 childrenAnother way of looking at it is that for each internal vertex, there are 2 vertices which have a parent.
![Page 40: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/40.jpg)
Proof:
2.4 Full Tree Theorem
Full Tree theorem: A full m-ary tree with k internal vertices has mk + 1 vertices.
Let T=(V,E) be a full m-ary tree, with k internal vertices.
Total number of vertices in T
Number of vertices that HAVE a parent
Number of vertices that DO NOT HAVE a parent
= +
Q: How many vertices HAVE a parent?
1. Observe for a 2-ary tree with 7 internal vertices
2. Each internal vertex has 2 childrenAnother way of looking at it is that for each internal vertex, there are 2 vertices which have a parent.
![Page 41: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/41.jpg)
Proof:
2.4 Full Tree Theorem
Full Tree theorem: A full m-ary tree with k internal vertices has mk + 1 vertices.
Let T=(V,E) be a full m-ary tree, with k internal vertices.
Total number of vertices in T
Number of vertices that HAVE a parent
Number of vertices that DO NOT HAVE a parent
= +
Q: How many vertices HAVE a parent?
1. Observe for a 2-ary tree with 7 internal vertices
2. Each internal vertex has 2 childrenAnother way of looking at it is that for each internal vertex, there are 2 vertices which have a parent.
![Page 42: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/42.jpg)
Proof:
2.4 Full Tree Theorem
Full Tree theorem: A full m-ary tree with k internal vertices has mk + 1 vertices.
Let T=(V,E) be a full m-ary tree, with k internal vertices.
Total number of vertices in T
Number of vertices that HAVE a parent
Number of vertices that DO NOT HAVE a parent
= +
Q: How many vertices HAVE a parent?
1. Observe for a 2-ary tree with 7 internal vertices
2. Each internal vertex has 2 childrenAnother way of looking at it is that for each internal vertex, there are 2 vertices which have a parent.
![Page 43: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/43.jpg)
Proof:
2.4 Full Tree Theorem
Full Tree theorem: A full m-ary tree with k internal vertices has mk + 1 vertices.
Let T=(V,E) be a full m-ary tree, with k internal vertices.
Total number of vertices in T
Number of vertices that HAVE a parent
Number of vertices that DO NOT HAVE a parent
= +
Q: How many vertices HAVE a parent?
1. Observe for a 2-ary tree with 7 internal vertices
2. Each internal vertex has 2 childrenAnother way of looking at it is that for each internal vertex, there are 2 vertices which have a parent.
![Page 44: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/44.jpg)
Proof:
2.4 Full Tree Theorem
Full Tree theorem: A full m-ary tree with k internal vertices has mk + 1 vertices.
Let T=(V,E) be a full m-ary tree, with k internal vertices.
Total number of vertices in T
Number of vertices that HAVE a parent
Number of vertices that DO NOT HAVE a parent
= +
Q: How many vertices HAVE a parent?
1. Observe for a 2-ary tree with 7 internal vertices
2. Each internal vertex has 2 childrenAnother way of looking at it is that for each internal vertex, there are 2 vertices which have a parent.
![Page 45: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/45.jpg)
Proof:
2.4 Full Tree Theorem
Full Tree theorem: A full m-ary tree with k internal vertices has mk + 1 vertices.
Let T=(V,E) be a full m-ary tree, with k internal vertices.
Total number of vertices in T
Number of vertices that HAVE a parent
Number of vertices that DO NOT HAVE a parent
= +
Q: How many vertices HAVE a parent?
A: 2k vertices for a binary tree.
mk vertices for a m-ary tree.
(Each internal vertex is the parent of m vertices)
(mk)
![Page 46: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/46.jpg)
Proof:
2.4 Full Tree Theorem
Full Tree theorem: A full m-ary tree with k internal vertices has mk + 1 vertices.
Let T=(V,E) be a full m-ary tree, with k internal vertices.
Total number of vertices in T
Number of vertices that HAVE a parent
Number of vertices that DO NOT HAVE a parent
= +
Q: How many vertices DO NOT HAVE a parent?
A: 1. The root does not have a parent
(mk) (1)
![Page 47: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/47.jpg)
Proof:
2.4 Full Tree Theorem
Full Tree theorem: A full m-ary tree with k internal vertices has mk + 1 vertices.
Let T=(V,E) be a full n-ary tree, with k internal vertices.
Total number of vertices in T
Number of vertices that HAVE a parent
Number of vertices that DO NOT HAVE a parent
= +
= mk + 1
(mk) (1)
![Page 48: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/48.jpg)
2.5 Leaves-Height Theorem. Leaves-Height Theorem for Binary Trees: Let T=(V,E) be a
binary tree that has t leaves, and height h. Thent 2h.
Proof: (by using induction on the height of the tree)
Base Case: h = 0
T has 1 vertex, which is a leaf.
t = 1 1 = 20 = 2h
Base case is true.
![Page 49: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/49.jpg)
2.5 Leaves-Height Theorem. Leaves-Height Theorem for Binary Trees: Let T=(V,E) be a
binary tree that has t leaves, and height h. Thent 2h.
Proof: (by using induction on the height of the tree)
Inductive Case: Assume that t 2h for h = 0,1,2,…,k (STRONG!)
Let T be any binary tree of height k+1. (Need to show t 2k+1)
k
1
TL TR
With respect to the root vertex, let the left and right subtrees be TL and TR respectively.
Let the number of leaves in TL and TR be tL and tR respectively. (t = tL+ tR)
Height of TL and TR are both < k+1.By inductive hypothesis, tL 2(TL Height) 2k and tR 2(TR Height) 2k.
t = tL+ tR 2k + 2k = 2k+1.
![Page 50: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/50.jpg)
2.5 Leaves-Height Theorem. Corollary to the Leaves-Height Theorem: Let T=(V,E) be a
binary tree that has t leaves, and height h. Then
log2 t h.
Proof:
Using leaves-height theorem, we have t 2h.
Taking logarithms on both sides will yield
log2t log22h
log2t h
![Page 51: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/51.jpg)
2.5 Leaves-Height Theorem.
IN GENERAL:
Leaves-Height Theorem for m-ary Trees: Let T=(V,E) be a m-ary tree that has t leaves, and height h. Then
t mh
Corollary to the Leaves-Height Theorem: Let T=(V,E) be a m-ary tree that has t leaves, and height h. Then
logmt h
Proof left as exercise (follows very closely to the proofs shown before)
![Page 52: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/52.jpg)
2.6 Examples
Q: Is there a binary tree that has height 5 and 38 external vertices?
A: No, since 38 > 25 which violates the leaves-height theorem.
![Page 53: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/53.jpg)
2.6 Examples
Q: Is there a full binary tree with 10 internal vertices and 13 external vertices?
A: No. Using the full-tree theorem, a binary tree with 10 internal vertices has 21 vertices in total. Therefore there should be 21-10 = 11 external vertices.
![Page 54: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/54.jpg)
Outline
1. Trees1.1 Definition of a tree.1.2 Examples of trees.1.3 Theorem #1: Tree Characterization
2. Rooted Trees2.1 Definitions2.2 Definition: n-ary trees and binary trees.2.3 Definition: Full n-ary trees2.4 Theorem #2: Full Tree2.5 Theorem #3: Leaves-Height2.6 Examples
3. Spanning Trees3.1 Motivation3.2 Definition3.3 Theorem #4: Spanning Tree3.4 Minimum Spanning Tree (MST)3.5 Kruskal’s Algorithm3.6 Prim’s Algorithm
![Page 55: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/55.jpg)
3.1 Spanning Trees.
Layer 2 routing of packets through network switches. Multiple connections from one switch to the rest of
network to increase fault tolerance.
When all links are operational, redundacy in connection occurs.
Network forms a spanning tree so that packets will not be redundantly routed. R
Network will elect a root. Root will broadcast packets to
all other switches. Each switch will select the
‘best’ link to use.
![Page 56: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/56.jpg)
3.1 Spanning Trees.
Layer 2 routing of packets through network switches. Multiple connections from one switch to the rest of
network to increase fault tolerance.
When all links are operational, redundacy in connection occurs.
Network forms a spanning tree so that packets will not be redundantly routed. R
Network will elect a root. Root will broadcast packets to
all other switches. Each switch will select the
‘best’ link to use.
![Page 57: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/57.jpg)
3.1 Spanning Trees.
Layer 2 routing of packets through network switches. Multiple connections from one switch to the rest of
network to increase fault tolerance.
When all links are operational, redundacy in connection occurs.
Network forms a spanning tree so that packets will not be redundantly routed. R
Network will elect a root. Root will broadcast packets to
all other switches. Each switch will select the
‘best’ link to use.
![Page 58: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/58.jpg)
3.1 Spanning Trees.
Layer 2 routing of packets through network switches. Multiple connections from one switch to the rest of
network to increase fault tolerance.
When all links are operational, redundacy in connection occurs.
Network forms a spanning tree so that packets will not be redundantly routed. R
Network will elect a root. Root will broadcast packets to
all other switches. Each switch will select the
‘best’ link to use. When a link goes down, the network reconfigures again.
![Page 59: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/59.jpg)
3.2 Spanning Trees (Definition).
Definition: Let G=(V,E). A spanning tree for G is a subgraph T=(V,E’) of G, such that T is a tree and T contains every vertex of G.
R
![Page 60: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/60.jpg)
3.3 Spanning Tree theorem. Spanning Tree theorem:
A graph is connected IFF it has a spanning tree.
Proof: ()
Assume that G=(V,E) is connected. We will show that G has a spanning tree.
Step 1: Let H = G
Step 2: while (H has a circuit C) {
Step 2a: Remove an edge from C to form new graph H’.
Step 2b: Let H = H’ }
Step 3: Output H.
1. Algorithm will terminate because G is finite and there is a finite number of edges to delete
![Page 61: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/61.jpg)
3.3 Spanning Tree theorem. Spanning Tree theorem:
A graph is connected IFF it has a spanning tree.
Proof: ()
Assume that G=(V,E) is connected. We will show that G has a spanning tree.
Step 1: Let H = G
Step 2: while (H has a circuit C) {
Step 2a: Remove an edge from C to form new graph H’.
Step 2b: Let H = H’ }
Step 3: Output H.
2. When algorithm terminates, H will be a spanning tree.
a. H is connected. Step 2a will still create a connected graph (lemma 2: “Deletion of edge from a circuit of a connected graph does not violate connectedness”)
![Page 62: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/62.jpg)
3.3 Spanning Tree theorem. Spanning Tree theorem:
A graph is connected IFF it has a spanning tree.
Proof: ()
Assume that G=(V,E) is connected. We will show that G has a spanning tree.
Step 1: Let H = G
Step 2: while (H has a circuit C) {
Step 2a: Remove an edge from C to form new graph H’.
Step 2b: Let H = H’ }
Step 3: Output H.
2. When algorithm terminates, H will be a spanning tree.
b. H has no circuits. Condition for termination in Step 2 is when there are no more circuits in H.
![Page 63: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/63.jpg)
3.3 Spanning Tree theorem. Spanning Tree theorem:
A graph is connected IFF it has a spanning tree.
Proof: ()
Assume that G=(V,E) is connected. We will show that G has a spanning tree.
Step 1: Let H = G
Step 2: while (H has a circuit C) {
Step 2a: Remove an edge from C to form new graph H’.
Step 2b: Let H = H’ }
Step 3: Output H.
2. When algorithm terminates, H will be a spanning tree.
c. H contains every vertex in V. True since we only removed edges from H and not vertices.
![Page 64: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/64.jpg)
3.3 Spanning Tree theorem. Spanning Tree theorem:
A graph is connected IFF it has a spanning tree.
Proof: ()
Assume that G=(V,E) is connected. We will show that G has a spanning tree.
Step 1: Let H = G
Step 2: while (H has a circuit C) {
Step 2a: Remove an edge from C to form new graph H’.
Step 2b: Let H = H’ }
Step 3: Output H.
Therefore the algorithm will terminate, and will give a spanning tree.
Therefore, If G is connected, then G has a spanning tree.
![Page 65: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/65.jpg)
3.3 Spanning Tree theorem. Spanning Tree theorem:
A graph is connected IFF it has a spanning tree.
Proof: ()
Assume that G=(V,E) has a spanning tree. Show that G is connected.
Let T be a spanning tree of G.
Because T is a SPANNING tree, therefore (by defn) T contains every vertex in G. There let T=(V,E’) be the spanning tree, E’ E.
T, being a tree (by defn of a tree) is connected.
(Defn: T is connected iff x,y V, walk from x to y).
Prove that G is connected: Let x and y be 2 vertices in G. x and y will also be in T. Let the walk from x to y in T be
W: x e1 v1 e2 v2 … en y
Now, x, v1, v2, … y are all in V, and e1, e2, …, en are all in E’ E.
Therefore W is also a walk from x to y in G. Therefore G is connected.
![Page 66: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/66.jpg)
3.4 Minimum Spanning Tree
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66
3
8
10
612
Let the following graph depict the scenario where the vertices are cities and the weighted edges are distances (km) between the cities.
Let’s say that the country wants to connect up the cities by building roads between them. The longer the road, the more money it has to spend.
How do we connect up the cities and spend the LEAST AMOUNT OF MONEY?
Ans: Find the MINIMUM spanning tree.
![Page 67: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/67.jpg)
3.4 Minimum Spanning Tree.
Definition: A weighted graph is a graph where each edge has a number associated with it.
G = (V,E), E Z x { {x,y} | x,y V}
The total weight of the graph is the sum of all the weights of the edges in the graph.
A minimum spanning tree (MST) for a weighted graph is a spanning tree that has the least possible total weight compared to all other spanning trees for the graph
![Page 68: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/68.jpg)
3.4 Minimum Spanning Tree.
How to find the minimum spanning tree?– Kruskal’s Algorithm– Prim’s Algorithm
![Page 69: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/69.jpg)
KRUSKAL’S ALGORITHM
![Page 70: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/70.jpg)
3.5 MST: Kruskal’s Algorithm
1. Input: G=(V,E) 2. Let T=(V,E’), E’={}, m=0 3. while (m < |V| - 1) {
– a. Find edge e in E of least weight.– b. E = E - {e}– c. If E’ {e} does not produce circuit
• E’ = E’ {e}• m = m + 1
} 4. Output T.
Idea: To add edges of the smallest weights which do not cause a circuit.
![Page 71: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/71.jpg)
3.5 MST: Kruskal’s Algorithm
1. Input: G=(V,E) 2. Let T=(V,E’), E’={}, m=0 3. while (m < |V| - 1) {
– a. Find edge e in E of least weight.– b. E = E - {e}– c. If E’ {e} does not produce circuit
• E’ = E’ {e}
• m = m + 1
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66
3
8
10
612
![Page 72: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/72.jpg)
3.5 MST: Kruskal’s Algorithm
1. Input: G=(V,E) 2. Let T=(V,E’), E’={}, m=0 3. while (m < |V| - 1) {
– a. Find edge e in E of least weight.– b. E = E - {e}– c. If E’ {e} does not produce circuit
• E’ = E’ {e}
• m = m + 1
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66
3
8
10
612
m=0
|V|-1 = 12
![Page 73: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/73.jpg)
3.5 MST: Kruskal’s Algorithm
1. Input: G=(V,E) 2. Let T=(V,E’), E’={}, m=0 3. while (m < |V| - 1) {
– a. Find edge e in E of least weight.– b. E = E - {e}– c. If E’ {e} does not produce circuit
• E’ = E’ {e}
• m = m + 1
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66
3
8
10
612
m=1
|V|-1 = 12
![Page 74: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/74.jpg)
3.5 MST: Kruskal’s Algorithm
1. Input: G=(V,E) 2. Let T=(V,E’), E’={}, m=0 3. while (m < |V| - 1) {
– a. Find edge e in E of least weight.– b. E = E - {e}– c. If E’ {e} does not produce circuit
• E’ = E’ {e}
• m = m + 1
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66
3
8
10
612
m=2
|V|-1 = 12
![Page 75: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/75.jpg)
3.5 MST: Kruskal’s Algorithm
1. Input: G=(V,E) 2. Let T=(V,E’), E’={}, m=0 3. while (m < |V| - 1) {
– a. Find edge e in E of least weight.– b. E = E - {e}– c. If E’ {e} does not produce circuit
• E’ = E’ {e}
• m = m + 1
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66
3
8
10
612
m=3
|V|-1 = 12
![Page 76: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/76.jpg)
3.5 MST: Kruskal’s Algorithm
1. Input: G=(V,E) 2. Let T=(V,E’), E’={}, m=0 3. while (m < |V| - 1) {
– a. Find edge e in E of least weight.– b. E = E - {e}– c. If E’ {e} does not produce circuit
• E’ = E’ {e}
• m = m + 1
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66
3
8
10
612
m=4
|V|-1 = 12
![Page 77: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/77.jpg)
3
3.5 MST: Kruskal’s Algorithm
1. Input: G=(V,E) 2. Let T=(V,E’), E’={}, m=0 3. while (m < |V| - 1) {
– a. Find edge e in E of least weight.– b. E = E - {e}– c. If E’ {e} does not produce circuit
• E’ = E’ {e}
• m = m + 1
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66 8
10
612
m=7
|V|-1 = 12
![Page 78: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/78.jpg)
3
3.5 MST: Kruskal’s Algorithm
1. Input: G=(V,E) 2. Let T=(V,E’), E’={}, m=0 3. while (m < |V| - 1) {
– a. Find edge e in E of least weight.– b. E = E - {e}– c. If E’ {e} does not produce circuit
• E’ = E’ {e}
• m = m + 1
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66 8
10
612
m=8
|V|-1 = 12
![Page 79: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/79.jpg)
3
3.5 MST: Kruskal’s Algorithm
1. Input: G=(V,E) 2. Let T=(V,E’), E’={}, m=0 3. while (m < |V| - 1) {
– a. Find edge e in E of least weight.– b. E = E - {e}– c. If E’ {e} does not produce circuit
• E’ = E’ {e}
• m = m + 1
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66 8
10
612
m=9
|V|-1 = 12
![Page 80: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/80.jpg)
3
3.5 MST: Kruskal’s Algorithm
1. Input: G=(V,E) 2. Let T=(V,E’), E’={}, m=0 3. while (m < |V| - 1) {
– a. Find edge e in E of least weight.– b. E = E - {e}– c. If E’ {e} does not produce circuit
• E’ = E’ {e}
• m = m + 1
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66 8
10
612
m=10
|V|-1 = 12
CIRCUIT!!!
![Page 81: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/81.jpg)
3
3.5 MST: Kruskal’s Algorithm
1. Input: G=(V,E) 2. Let T=(V,E’), E’={}, m=0 3. while (m < |V| - 1) {
– a. Find edge e in E of least weight.– b. E = E - {e}– c. If E’ {e} does not produce circuit
• E’ = E’ {e}
• m = m + 1
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
2
66 8
10
612
m=10
|V|-1 = 12
![Page 82: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/82.jpg)
3
3.5 MST: Kruskal’s Algorithm
1. Input: G=(V,E) 2. Let T=(V,E’), E’={}, m=0 3. while (m < |V| - 1) {
– a. Find edge e in E of least weight.– b. E = E - {e}– c. If E’ {e} does not produce circuit
• E’ = E’ {e}
• m = m + 1
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
2
66 8
10
612
m=11
|V|-1 = 12
![Page 83: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/83.jpg)
3
3.5 MST: Kruskal’s Algorithm
1. Input: G=(V,E) 2. Let T=(V,E’), E’={}, m=0 3. while (m < |V| - 1) {
– a. Find edge e in E of least weight.– b. E = E - {e}– c. If E’ {e} does not produce circuit
• E’ = E’ {e}
• m = m + 1
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
2
66 8
10
612
m=12
|V|-1 = 12
![Page 84: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/84.jpg)
3
3.5 MST: Kruskal’s Algorithm
1. Input: G=(V,E) 2. Let T=(V,E’), E’={}, m=0 3. while (m < |V| - 1) {
– a. Find edge e in E of least weight.– b. E = E - {e}– c. If E’ {e} does not produce circuit
• E’ = E’ {e}
• m = m + 1
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
2
2
2 33
4
4
4
2
m=12
|V|-1 = 12
Algorithm Halts. Cost = 39
![Page 85: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/85.jpg)
PRIM’S ALGORITHM
![Page 86: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/86.jpg)
3.6 MST: Prim’s Algorithm
1. Input: G=(V,E) 2. Let T=(V’,E’), V’={v}, E’={}, m=0, V=V-{v} 3. while (|V| > 0) {
– a. Find an edge e such that• e = {x,y}, x in V’, y in V. e connects T to some vertex in
V.• e has least weight of all edges connecting T to a vertex
in V.
– b. V’=V’ {y}, E’ = E’ {e}, V = V - {y}
} 4. Output T.
Idea: To ‘grow’ a spanning tree.
![Page 87: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/87.jpg)
3.6 MST: Prim’s Algorithm
1. Input: G=(V,E) 2. Let T=(V’,E’), V’={v}, E’={}, m=0, V=V-{v} 3. while (|V| > 0) {
– a. Find an edge e such that• e = {x,y}, x in V’, y in V. e connects T to some vertex in V.
• e has least weight of all edges connecting T to a vertex in V.
– b. V’=V’ {y}, E’ = E’ {e}, V = V - {y}
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66
3
8
10
612
|V| = 12 > 0
![Page 88: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/88.jpg)
3.6 MST: Prim’s Algorithm
1. Input: G=(V,E) 2. Let T=(V’,E’), V’={v}, E’={}, m=0, V=V-{v} 3. while (|V| > 0) {
– a. Find an edge e such that• e = {x,y}, x in V’, y in V. e connects T to some vertex in V.
• e has least weight of all edges connecting T to a vertex in V.
– b. V’=V’ {y}, E’ = E’ {e}, V = V - {y}
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66
3
8
10
612
|V| = 12 > 0
![Page 89: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/89.jpg)
3.6 MST: Prim’s Algorithm
1. Input: G=(V,E) 2. Let T=(V’,E’), V’={v}, E’={}, m=0, V=V-{v} 3. while (|V| > 0) {
– a. Find an edge e such that• e = {x,y}, x in V’, y in V. e connects T to some vertex in V.
• e has least weight of all edges connecting T to a vertex in V.
– b. V’=V’ {y}, E’ = E’ {e}, V = V - {y}
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66
3
8
10
612
|V| = 11 > 0
![Page 90: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/90.jpg)
3.6 MST: Prim’s Algorithm
1. Input: G=(V,E) 2. Let T=(V’,E’), V’={v}, E’={}, m=0, V=V-{v} 3. while (|V| > 0) {
– a. Find an edge e such that• e = {x,y}, x in V’, y in V. e connects T to some vertex in V.
• e has least weight of all edges connecting T to a vertex in V.
– b. V’=V’ {y}, E’ = E’ {e}, V = V - {y}
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66
3
8
10
612
|V| = 11 > 0
![Page 91: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/91.jpg)
3.6 MST: Prim’s Algorithm
1. Input: G=(V,E) 2. Let T=(V’,E’), V’={v}, E’={}, m=0, V=V-{v} 3. while (|V| > 0) {
– a. Find an edge e such that• e = {x,y}, x in V’, y in V. e connects T to some vertex in V.
• e has least weight of all edges connecting T to a vertex in V.
– b. V’=V’ {y}, E’ = E’ {e}, V = V - {y}
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66
3
8
10
612
|V| = 10 > 0
![Page 92: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/92.jpg)
3.6 MST: Prim’s Algorithm
1. Input: G=(V,E) 2. Let T=(V’,E’), V’={v}, E’={}, m=0, V=V-{v} 3. while (|V| > 0) {
– a. Find an edge e such that• e = {x,y}, x in V’, y in V. e connects T to some vertex in V.
• e has least weight of all edges connecting T to a vertex in V.
– b. V’=V’ {y}, E’ = E’ {e}, V = V - {y}
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66
3
8
10
6|V| = 9 > 0
![Page 93: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/93.jpg)
3.6 MST: Prim’s Algorithm
1. Input: G=(V,E) 2. Let T=(V’,E’), V’={v}, E’={}, m=0, V=V-{v} 3. while (|V| > 0) {
– a. Find an edge e such that• e = {x,y}, x in V’, y in V. e connects T to some vertex in V.
• e has least weight of all edges connecting T to a vertex in V.
– b. V’=V’ {y}, E’ = E’ {e}, V = V - {y}
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66
3
8
10
6|V| = 8 > 0
![Page 94: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/94.jpg)
3.6 MST: Prim’s Algorithm
1. Input: G=(V,E) 2. Let T=(V’,E’), V’={v}, E’={}, m=0, V=V-{v} 3. while (|V| > 0) {
– a. Find an edge e such that• e = {x,y}, x in V’, y in V. e connects T to some vertex in V.
• e has least weight of all edges connecting T to a vertex in V.
– b. V’=V’ {y}, E’ = E’ {e}, V = V - {y}
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
5
5
2
2
2 33
4
4
4
4 2
66
3
8
10
6|V| = 7 > 0
![Page 95: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/95.jpg)
3.6 MST: Prim’s Algorithm
1. Input: G=(V,E) 2. Let T=(V’,E’), V’={v}, E’={}, m=0, V=V-{v} 3. while (|V| > 0) {
– a. Find an edge e such that• e = {x,y}, x in V’, y in V. e connects T to some vertex in V.
• e has least weight of all edges connecting T to a vertex in V.
– b. V’=V’ {y}, E’ = E’ {e}, V = V - {y}
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
52
2
2 33
4
4
4
4 2
66
3
8
10
6|V| = 6 > 0
![Page 96: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/96.jpg)
3.6 MST: Prim’s Algorithm
1. Input: G=(V,E) 2. Let T=(V’,E’), V’={v}, E’={}, m=0, V=V-{v} 3. while (|V| > 0) {
– a. Find an edge e such that• e = {x,y}, x in V’, y in V. e connects T to some vertex in V.
• e has least weight of all edges connecting T to a vertex in V.
– b. V’=V’ {y}, E’ = E’ {e}, V = V - {y}
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
52
2
2 33
4
4
4 2
66
3
8
10
6|V| = 5 > 0
![Page 97: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/97.jpg)
3.6 MST: Prim’s Algorithm
1. Input: G=(V,E) 2. Let T=(V’,E’), V’={v}, E’={}, m=0, V=V-{v} 3. while (|V| > 0) {
– a. Find an edge e such that• e = {x,y}, x in V’, y in V. e connects T to some vertex in V.
• e has least weight of all edges connecting T to a vertex in V.
– b. V’=V’ {y}, E’ = E’ {e}, V = V - {y}
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
52
2
2 33
4
4
4 2
66
3
8
10
|V| = 4 > 0
![Page 98: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/98.jpg)
3.6 MST: Prim’s Algorithm
1. Input: G=(V,E) 2. Let T=(V’,E’), V’={v}, E’={}, m=0, V=V-{v} 3. while (|V| > 0) {
– a. Find an edge e such that• e = {x,y}, x in V’, y in V. e connects T to some vertex in V.
• e has least weight of all edges connecting T to a vertex in V.
– b. V’=V’ {y}, E’ = E’ {e}, V = V - {y}
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
52
2
2 33
4
4
4 2
66
3
8
10
|V| = 3 > 0
![Page 99: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/99.jpg)
3.6 MST: Prim’s Algorithm
1. Input: G=(V,E) 2. Let T=(V’,E’), V’={v}, E’={}, m=0, V=V-{v} 3. while (|V| > 0) {
– a. Find an edge e such that• e = {x,y}, x in V’, y in V. e connects T to some vertex in V.
• e has least weight of all edges connecting T to a vertex in V.
– b. V’=V’ {y}, E’ = E’ {e}, V = V - {y}
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
52
2
2 33
4
4
4 2
66
3
10
|V| = 2 > 0
![Page 100: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/100.jpg)
3.6 MST: Prim’s Algorithm
1. Input: G=(V,E) 2. Let T=(V’,E’), V’={v}, E’={}, m=0, V=V-{v} 3. while (|V| > 0) {
– a. Find an edge e such that• e = {x,y}, x in V’, y in V. e connects T to some vertex in V.
• e has least weight of all edges connecting T to a vertex in V.
– b. V’=V’ {y}, E’ = E’ {e}, V = V - {y}
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
52
2
2 33
4
4
4 2
66
3
|V| = 1 > 0
![Page 101: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/101.jpg)
3.6 MST: Prim’s Algorithm
1. Input: G=(V,E) 2. Let T=(V’,E’), V’={v}, E’={}, m=0, V=V-{v} 3. while (|V| > 0) {
– a. Find an edge e such that• e = {x,y}, x in V’, y in V. e connects T to some vertex in V.
• e has least weight of all edges connecting T to a vertex in V.
– b. V’=V’ {y}, E’ = E’ {e}, V = V - {y}
} 4. Output T.
A
GH
B C D
I
JK
F
L
EM5
52
2
2 33
4
4
4 2
3
|V| = 0
Algorithm Halts. Cost = 39
![Page 102: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/102.jpg)
Different output possible
3A
GH
B C D
I
JK
F
L
EM5
5
2
2
2 33
4
4
4
2
A
GH
B C D
I
JK
F
L
EM5
52
2
2 33
4
4
4 2
3
Kruskal’s Algorithm
Prim’s Algorithm
Cost of Tree = 39
![Page 103: Lecture 12 Graphs III (Trees, MSTs) (Chp 11.5, 11.6)](https://reader031.vdocuments.site/reader031/viewer/2022032702/56649caf5503460f94972aa2/html5/thumbnails/103.jpg)
End of Lecture