Chapter 6-1
Chapter 6-1
c d
C
A D
e
Kneiphof
f
a b C
B g -Eulerian walk
c d
e Degree of each vertex is even
(a) A D
a b
(b) f
Figure 6.1 : (a) Section of the river Pregel in Konigsberg; (b) Euler's graph
Graph Abstract Data Type (Cont.)
◼ Graph G=(V, E)
◼ V is a finite, nonempty set of vertices
◼ E is a set of edges
◼ An edge is a pair of vertices
◼ V(G) is the set of vertices of G
◼ E(G) is the set of edges of G
0 0 0
1 2 1 2 1
3 3 4 5 6 2
0 1
0
1
0 3
0
2
0 2
0
◼ Subgraph of G
◼ Graph G' such that V(G')⊆V(G) and E(G')⊆E(G)
Graph Abstract Data Type (Cont.)
0 0 0
1 2 1 2 1 2
3 3
(i) (ii) (iii) (iv)
(a) some
1 of the subgraphs of G1
0 0 0 0
1 1 1
2 2 2
(i) (ii) (iii) (iv)
(b) some
3 of the subgraphs of G3
0 0 0
1 2 1 2 1
Figure 6.4 : Some subgraphs
3 3 4 5 6 2
H1 0 4 H2
2 1 5 6
3 7
G4
Figure 6.5 : A graph with two connected components
Graph Abstract Data Type (Cont.)
◼ Tree
◼ Connected acyclic graph
◼ Degree of vertex
◼ Number of edges incident to that vertex
◼ Adjacency Matrix
◼ Adjacency Lists
Adjacency Matrix
◼ Definition
◼ G=(V,B) is a graph with n vertices, n≥1
◼ Adjacency matrix A of G
◼ two dimensional n x n array
◼ A[i][j]=1 iff edge(i, j) is in E(G)
◼ Properties
◼ Space needed is n2
◼ A is symmetric for undirected G
◼ Need only the upper or lower triangle of A
Adjacency Matrix (Cont.)
◼ Degree of vertex i for an undirected graph
◼
0 0 1 1 0 0 0 0 0
1 1 0 0 1 0 0 0 0
2 1 0 0 1 0 0 0 0
0 1 2 3
3 0 1 1 0 0 0 0 0
0 0 1 1 1 0 1 2 4 0 0 0 0 0 1 0 0
1 1 0 1 1 0 0 1 0 5 0 0 0 0 1 0 1 0
2 1 1 0 1 1 1 0 1 6 0 0 0 0 0 1 0 0
3 1 1 1 0 2 0 0 0 7 0 0 0 0 0 0 1 0
(a) G1 (b) G3 (c) G4
◼ Complexity of operations
◼ Number of nodes in adjacency list = O(n+e)
Adjacency Lists (Cont.)
HeadNodes data link
[0] 3 1 2 0
[1] 2 3 0 0
[2] 1 3 0 0
[3] 0 1 2 0
(a) G1
HeadNodes
[0] 1 0
[1] 2 0 0
[2] 0
(b) G3
HeadNodes H1 0 4 H2
[0] 2 1 0
[1] 3 0 0
0 3 0
2 1 5 6
[2]
[3] 1 2 0
5 0
3 7
[4]
6 4 0
G4
[5]
[6] 5 7 0
[7] 6 0
(c) G4
◼ Representation
◼ Adjacency matrix
◼ A[i][j] keeps weight
◼ Adjacency list
◼ Additional field in list node keeps weight
Elementary Graph Operations
◼ Graph traversal
◼ Given G=(V, E) and a vertex v in V(G)
◼ Visit all vertices reachable from v
Depth-First-Search
◼ Procedure
1. Visit start vertex v
2. An unvisited vertex w adjacent to v is selected, and initiate
DFS from w
3. When u is reached such that all its adjacent vertices have
been visited
◼ Back up to the last vertex visited that has an unvisited vertex w
adjacent to it
◼ Initiate DFS from w
4. Search terminates when no unvisited vertex can be reached
from visited vertices
Depth-First-Search (Cont.)
1. virtual void Graph::DFS() // Driver
2. {
3. visited = new bool[n];
4. // visited is declared as a bool* data member of graph
5. fill(visited, visited + n, false);
6. for(int v=0; v<n; v++)
7. if(visited[v] == false)
8. DFS(v); // start search at vertex 0
9. delete [] visited;
10. }
1 2
3 4 5 6
7
(a)
[0] 1 2 0
[1] 10 3 4 0
[2] 10 5 6 0
[3] 1 7 0
[4] 1 7 0
[5] 12 7 0
[6] 12 7 0
[7] 3 4 5 6 0
(b)
0 0
1 2 1 2
3 4 5 6 3 4 5 6
7 7
9
5 0 10 8 9
Spanning Trees (Cont.)
◼ Properties
◼ If a non tree edge is introduced into any spanning tree, then
a cycle is formed
◼ Ex) If (7,6) edge is added to Fig 6.19(a), then the resulting
cycle is 7,6,2,5,7
◼ Used to obtain an independent set of circuit equations for an
electrical network
◼ A Spanning tree is a minimal subgraph G' of G such that
◼ V(G') = V(G)
◼ G’ is connected
◼ Spanning tree has n-1 edges
Biconnected Components
◼ Articulation point
◼ A vertex v whose deletion operation leaves behind a graph
that has at least two connected components
◼ Biconnected graph
◼ A connected graph that has no articulation points
◼ Biconnected component
◼ Maximal biconnected subgraph
◼ The original graph contains no other biconnected subgraph
Biconnected Components
(Cont.)
◼ A biconnected graph has just one biconnected
component.
◼ Two biconnected components of the same graph can
have at most one vertex in common.
◼ No edge can be in two or more biconnected
components.
◼ Biconnected components of a graph G partition the
edges of G.
◼ The biconnected components of a connected
undirected graph G can be found by using any depth-
first spanning tree of G.
Biconnected Components (Cont.)
0 8 9
1 7
2 3 5
4 6
0 8 9
7 7
1
1 7
2 3 3 5 5
4 6
1 0 10 9 2
8 9 1
2 1 7 8 Nontree edge
6 3 2
5
3 2 3 5 dfn: depth-first number
4 4 5
4 6
3
4 7
Starting node 6
5
(a)
Nontree edge
7 6
8 7
9
10 8 9
(b)
9 2
0 8 9 1
1 0 10 9
8 9
Starting node
1 7 Nontree edge
2 3 2
1 7 8
5 6
2 3 5
3
dfn: depth-first number
2 3 5
4 4 5
4 6
4 6 3
6
4 7
5
Original Graph
(a) Nontree edge
7 6
9
10 8 9
low(w) ≥ dfn(u)
Vertex 0 1 2 3 4 5 6 7 8 9
dfn 1 2 3 5 4 6 7 8 10 9
low 1 2 2 2 2 6 6 6 10 9
9 2
0 8 9 1
1 0 10 9
8 9
Starting node
1 7 Nontree edge
2 3 2
1 7 8
5 6
2 3 5
3
dfn: depth-first number
2 3 5
4 4 5
4 6
4 6 3
6
4 7
5
Original Graph
(a) Nontree edge
7 6
9
10 8 9
low(w) ≥ dfn(u)
Vertex 0 1 2 3 4 5 6 7 8 9
dfn 1 2 3 5 4 6 7 8 10 9
low 1 2 2 2 2 6 6 6 10 9
9 2
0 8 9 1
1 0 10 9
8 9
Starting node
1 7 Nontree edge
2 3 2
1 7 8
5 6
2 3 5
3
dfn: depth-first number
2 3 5
4 4 5
4 6
4 6 3
6
4 7
5
Original Graph
(a) Nontree edge
7 6
9
10 8 9
low(w) ≥ dfn(u)
Vertex 0 1 2 3 4 5 6 7 8 9
dfn 1 2 3 5 4 6 7 8 10 9
low 1 2 2 2 2 6 6 6 10 9
9 2
0 8 9 1
1 0 10 9
8 9
Starting node
1 7 Nontree edge
2 3 2
1 7 8
5 6
2 3 5
3
dfn: depth-first number
2 3 5
4 4 5
4 6
4 6 3
6
4 7
5
Original Graph
(a) Nontree edge
7 6
9
10 8 9
low(w) ≥ dfn(u)
Vertex 0 1 2 3 4 5 6 7 8 9
dfn 1 2 3 5 4 6 7 8 10 9
low 1 2 2 2 2 6 6 6 10 9
4 6
4 7 5
Starting node
3
(a) 6
5
Nontree edge
7 6
8 7
9
10 8 9
(b)
A Depth-first Spanning Tree with Root 3
dfn: depth-first number
1
5 0 10 9
8 9 3
6
4 1 7 8 2 4 5
1 6
3 2 3 5 Nontree edge Nontree edge
3 2 7 6
4 6
4
2 7 1 8 7
Starting node
9
(a) 5 0 10 8 9
(b)
Figure 6.21: Depth-first spanning tree of Figure 6.20(a)
A Depth-first Spanning Tree
◼ A non-tree edge (u,v) is a back edge with respect to
a spanning tree T iff either u is an ancestor of v or v
is an ancestor of u.
◼ A nontree edge that is not a back edge is called a
cross edge.
◼ The root node of the depth-first spanning tree is an
articulation point iff it has at least two children.
◼ Any other vertex u is an articulation point iff it has at
least one child w such that it is not possible to reach
an ancestor of u using a path composed of w,
descendants of w and a single back edge.
Determining Biconnected Components
◼ Depth-first Number
◼ The sequence in which the vertices are visited during the DFS
◼ Back edge (u, v)
◼ Nontree edge
◼ Either u is an ancestor of v or v is an ancestor of u
◼ low(w) – the lowest depth-first number that can be reached from w
using a path of descendants followed by at most one back edge
◼ min{ dfn(w), min{low(x)| x is a child of w},
min{dfn(x)|(w, x) is a back edge} }
◼ Articulation point (2 cases)
◼ vertex u is an articulation point iff
1. If u is the root of the spanning tree and has two or more children
2. If u has a child w such that low(w) ≥ dfn(u)
Determining Biconnected Components
(Cont.)
1
0 8 9 3
5 0 10 9
8 9 6
2 4 5
1 7
4 1 7 8
1 6
2 3 5 3 7 6
3 2 3 5 2
4 6 4
4 6 1 8 7
2 7
Starting node
Original Graph 5 10 8 9
9
0
(a)
(b)
Vertex 0 1 2 3 4 5 6 7 8 9
dfn 5 4 3 1 2 6 7 8 10 9
low 5 1 1 1 1 6 6 6 10 9
Figure 6.22: dfn and low values for the spanning tree of Figure 6.21(b)
Determining Biconnected Components
(Cont.)
1
0 8 9 3
5 0 10 9
8 9 6
2 4 5
1 7
4 1 7 8
1 6
2 3 5 3 7 6
3 2 3 5 2
4 6 4
4 6 1 8 7
2 7
Starting node
Original Graph 5 10 8 9
9
0
(a)
(b)
Figure 6.22: dfn and low values for the spanning tree of Figure 6.21(b)
Determining Biconnected Components
(Cont.)
root of the spanning tree and has
two or more children 1
0 8 9 3
5 0 10 9
8 9 6
2 4 5
1 7
4 1 7 8
1 6
2 3 5 3 7 6
3 2 3 5 2
4 6 4
4 6 1 8 7
2 7
Starting node
Original Graph 5 10 8 9
9
0
(a)
(b)
Vertex 0 1 2 3 4 5 6 7 8 9
dfn 5 4 3 1 2 6 7 8 10 9
low 5 1 1 1 1 6 6 6 10 9
Figure 6.22: dfn and low values for the spanning tree of Figure 6.21(b)
Determining Biconnected Components
(Cont.)
1
0 8 9 3
5 0 10 9
8 9 6
2 4 5
1 7
4 1 7 8
1 6
2 3 5 3 7 6
3 2 3 5 2
4 6 4
4 6 1 8 7
2 7
Starting node
Original Graph 5 10 8 9
9
0
(a)
(b)
Figure 6.22: dfn and low values for the spanning tree of Figure 6.21(b)
Determining Biconnected Components
(Cont.)
1
0 8 9 3
5 0 10 9
8 9 6
2 4 5
1 7
4 1 7 8
1 6
2 3 5 3 7 6
3 2 3 5 2
4 6 4
4 6 1 8 7
2 7
Starting node
Original Graph 5 10 8 9
9
0
(a)
(b)
Figure 6.22: dfn and low values for the spanning tree of Figure 6.21(b)
Determining Biconnected Components
(Cont.)
1. virtual void Graph::DfnLow(const int x) // begin DFS at vertex x
2. {
3. num = 1; // num is an int data member of Graph
4. dfs = new int[n]; // dfn is declared as int* in Graph
5. low = new int[n]; // low is declared as int* in Graph
6. fill(dfn, dfn + n, 0);
7. fill(low, low + n, 0);
8. DfnLow(x, -1); // start at vertex x
9. delete [] dfn;
10. delete [] low;
11. }
as all those edges including the edge (u,v) will form one
biconnected component.
Printing Biconnected Components (Cont.)
1. virtual void Graph::Biconnected()
2. {
3. num = 1; // num is an int datamember of Graph
4. dfn = new int[n]; // dfn is declared as int* in Graph
5. low = new int[n]; // low is declared as int* in Graph
6. fill(dfn, dfn + n, 0);
7. fill(low, low + n, 0);
8. Biconnected(0,-1); // start at vertex 0
9. delete [] dfn;
10. delete [] low;
11. }
12. virtual void Graph::Biconnected(const int u, const int v)
13. { // Compute dfn and low, and output the edges of G by their biconnected components.
14. // v is the parent (if any) of u in the resulting spanning tree.
15. // s is an initially empty stack declared as a data member of Graph.
16. dfn[u] = low[u] = num++;
17. for(each vertex w adjacent from u) { // actual code uses an iterator
18. if((v != w)&&(dfn[w]<dfn[u])) add (u,w) to stack s;
19. if(dfn[w] == 0) { // w is an unvisited vertex
20. Biconnected(w, u);
21. low[u] = min(low[u], low[w]);
22. if(low[w] >= dfn[u]) {
23. cout << “New Biconnected Component: “ << end;
24. do {
25. delete an edge from the stack s;
26. let this edge be (x, y);
27. cout << x << “,” << y << endl;
28. } while( (x,y) and (u,w) are not the same edge)
29. }
30. }
31. else if (w != v) low[u] = min(low[u], dfn[w]); // back edge
32. }
Program 6.5: Outputting biconected components when n > 1
Minimum Spanning Trees
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Minimum Spanning Tree
◼ In the design of electronic circuitry, it is often necessary to make
the pins of several components electrically equivalent by wiring
together.
◼ To interconnect a set of n pins, we can use an arrangement of
n-1 pins.
◼ Of all such arrangements, the one using the least amount of
wire is the most desirable.
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Minimum Spanning Tree
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Minimum Spanning Tree
◼ We can model this wiring problem with a connected, undirected graph
G=(V,E), where
◼ V is the set of pins
◼ E is the set of possible interconnections between pair of pins
◼ A weight w(u,v) for each edge (u,v)∈E that specifying the cost to connect u
and v
◼ Find an acyclic subset T⊆E that connects all of the vertices and whose
total weight
w(T) = ∑(u,v)∈T w(u,v) is minimized.
◼ Since T is acyclic and connects all the vertices, we call a minimum
spanning tree.
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Minimum Spanning Tree
GENERIC-MST(G, w)
1. A = Ø
2. while A does not form a spanning tree
3. find an edge (u,v) that is safe for A
4. A = A U {(u,v)}
5. return A
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Minimum Spanning Tree
◼ If A U {(u,v)} is also a subset of a minimum spanning tree, we call the edge (u,v)
a safe edge.
◼ A cut (S, V-S) of an undirected graph G=(V,E) is a partition of V.
◼ An edge (u,v)∈E crosses the cut (S, V-S) if one of its endpoints is in S and the
other is in V-S.
◼ A cut respects a set A of edges if no edge in A crosses the cut.
◼ An edge is a light edge crossing a cut if its weight is the minimum of any edge
crossing the cut.
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
One Way of Viewing a Cut (S, V-S)
8 7
b c d
4 9
S 2 S
a i 14 e
11 4
7 6
V-S 8 10 V-S
h g f
1 2
▪ Black vertices are in the set S, and green vertices are in V-S.
▪ The edge (d,c) is the unique light edge crossing the cut.
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Another Way of Viewing a Cut
(S, V-S)
S V-S h
8
a 7
4 11 i 1
b 6
8 2 g
d 7
9 c 2
14
e 4
10 f
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Theorem 23.1
◼ Let G=(V,E) be a connected undirected graph with a real-valued
weight function w defined on E.
◼ Let A be a subset of E that is included in some minimum
spanning tree for G.
◼ Let (S,V-S) be any cut of G that respects A and let (u,v) be a
light edge crossing (S,V-S).
◼ Then, the edge (u,v) is safe for A
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Theorem 23.1 (Proof)
◼ Let T be a minimum spanning tree that includes A
◼ Assume that T does not contain the light edge (u,v)
◼ Construct another minimum spanning tree T’ that include A U
{(u,v)} by using a cut-and-paste technique, thereby showing
that (u,v) is a safe edge for A
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Theorem 23.1 (Proof)
A
x
S
u p
y
V-S
v
▪ The edge (x, y) is an edge on the unique simple path p
from u to v in T and the edges in A are shaded.
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Theorem 23.1 (Proof)
◼ The light edge (u,v) forms a cycle with the edge on the simple path p
from u to v in T.
◼ Since u and v are on opposite sides of the cut (S, V-S), there is at least
one edge in T on the simple path p that also crosses the cut. Let (x,y)
be any such edge.
◼ The edge (x, y) is not in A, since the cut respects A.
◼ Since (x,y) is on the unique path from u to v in T, removing (x,y)
breaks T into two components.
◼ Adding (u,v) re-connects them to form a new spanning tree T’ = T –
{(x,y)} U{(u,v)}.
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Theorem 23.1 (Proof)
◼ We next show that T’ = T – {(x,y)} U{(u,v)} is a minimum spanning
tree.
◼ Since (u, u) is a light edge crossing (S, V-S) and (x, y) also crosses this
cut, we have w(u,v)≤w(x, y) resulting that w(T’) = w(T)–w(x,y)+w(u,v)
≤ w(T).
◼ But w(T)≤w(T’), since T is a minimum spanning tree.
◼ Thus, T’ must be a minimum spanning tree.
◼ Because A ⊆ T’ and A ⋃ {(u,v)} ⊆ T’ where T’ is a minimum spanning
tree, (u,v) is safe for A.
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Understanding of GENERIC-MST
◼ As the method proceeds, the set A is always acyclic; otherwise, a minimum
spanning tree including A would contain a cycle, which is a contradiction.
◼ At any point in the execution, the graph GA=(V, A) is a forest, and each of the
connected components of GA is a tree.
◼ Some of the trees may contain just one vertex, as is the case, for example, when
the method begins: A is empty and the forest contains |V| trees, one for each
vertex.
◼ Moreover, any safe edge (u,v) for A connects distinct components of GA, since A
⋃ {(u,v)} must be acyclic.
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Understanding of GENERIC-MST
◼ The while loop in lines 2 - 4 of GENERIC-MST executes |V| - 1 times
because it finds one of the |V|-1 edges of a minimum spanning tree in
each iteration.
◼ Initially, when A = Ø , there are |V| trees in GA, and each iteration
reduces that number by 1.
◼ When the forest contains only a single tree, the method terminates.
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Corollary 23.2
◼ Let G=(V,E) be a connected, undirected graph with a real-valued
weight function w defined on E.
◼ Let A be a subset of E that is included in some minimum spanning tree
for G.
◼ Let C=(VC,EC) be a connected component (tree) in the forest GA=(V,A).
◼ If (u,v) is a light edge connecting C to some other component in GA,
then (u,v) is safe for A
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Corollary 23.2 (Proof)
◼ The cut (VC, V-VC) respects A and (u,v) is a light edge for this cut.
◼ Thus, (u,v) is safe for A.
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s and Prim’s Algorithms
◼ They each use a specific rule to determine a safe edge in line 3 of GENERIC-MST.
◼ In Kruskal’s algorithm,
◼ The set A is a forest whose vertices are all those of the given graph.
◼ The safe edge added to A is always a least-weight edge in the graph that connects two
distinct components.
◼ In Prim’s algorithm,
◼ The set A forms a single tree.
◼ The safe edge added to A is always a least-weight edge connecting the tree to a vertex
not in the tree.
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
◼ A greedy algorithm since at each step it adds to the forest an edge of least
possible weight.
◼ Find a safe edge to add to the growing forest by finding, of all the edges
that connect any two trees in the forest, an edge (u,v) of least weight.
◼ Let two trees C1 and C2 are connected by (u,v).
◼ Since (u,v) must be a light edge connecting C1 to some other tree,
Corollary 23.2 implies that (u,v) is a safe edge for C1.
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Implementation of Kruskal’s
Algorithm
◼ It uses a disjoint-set data structure to maintain several disjoint sets of elements.
◼ Each set contains the vertices in one tree of the current forest.
◼ The operation FIND-SET(u) returns a representative element from the set that
contains u.
◼ Thus, we can determine whether two vertices u and v belong to the same tree
by testing whether FIND-SET(u) equals FIND-SET(v).
◼ To combine trees, Kruskal’s algorithm calls the UNION procedure.
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
MST-KRUSKAL(G,w)
1. A=
2. for each v G.V
3. Make-Set(v)
4. sort the edges of G.E into nondecreasing order
by weight w
5. for each edge (u,v) G.E in sorted order
6. if Find-Set(u) Find-Set(v)
7. A = A U {{u,v}}
8. Union(u,v)
9. return A
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
8 7
b c d
4 9
2
a i 14 e
11 4
7 6
8 10
h g f
1 2
5. for each edge (u,v) G.E in sorted order
6. if Find-Set(u) Find-Set(v)
7. A = A U {{u,v}}
8. Union(u,v)
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
8 7
b c d
4 9
2
a i 14 e
11 4
7 6
8 10
h g f
1 2
5. for each edge (u,v) G.E in sorted order
6. if Find-Set(u) Find-Set(v)
7. A = A U {{u,v}}
8. Union(u,v)
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
8 7
b c d
4 9
2
a i 14 e
11 4
7 6
8 10
h g f
1 2
5. for each edge (u,v) G.E in sorted order
6. if Find-Set(u) Find-Set(v)
7. A = A U {{u,v}}
8. Union(u,v)
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
8 7
b c d
4 9
2
a i 14 e
11 4
7 6
8 10
h g f
1 2
5. for each edge (u,v) G.E in sorted order
6. if Find-Set(u) Find-Set(v)
7. A = A U {{u,v}}
8. Union(u,v)
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
8 7
b c d
4 9
2
a i 14 e
11 4
7 6
8 10
h g f
1 2
5. for each edge (u,v) G.E in sorted order
6. if Find-Set(u) Find-Set(v)
7. A = A U {{u,v}}
8. Union(u,v)
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
8 7
b c d
4 9
2
a i 14 e
11 4
7 6
8 10
h g f
1 2
5. for each edge (u,v) G.E in sorted order
6. if Find-Set(u) Find-Set(v)
7. A = A U {{u,v}}
8. Union(u,v)
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
8 7
b c d
4 9
2
a i 14 e
11 4
7 6
8 10
h g f
1 2
5. for each edge (u,v) G.E in sorted order
6. if Find-Set(u) Find-Set(v)
7. A = A U {{u,v}}
8. Union(u,v)
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
8 7
b c d
4 9
2
a i 14 e
11 4
7 6
8 10
h g f
1 2
5. for each edge (u,v) G.E in sorted order
6. if Find-Set(u) Find-Set(v)
7. A = A U {{u,v}}
8. Union(u,v)
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
8 7
b c d
4 9
2
a i 14 e
11 4
7 6
8 10
h g f
1 2
5. for each edge (u,v) G.E in sorted order
6. if Find-Set(u) Find-Set(v)
7. A = A U {{u,v}}
8. Union(u,v)
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
8 7
b c d
4 9
2
a i 14 e
11 4
7 6
8 10
h g f
1 2
5. for each edge (u,v) G.E in sorted order
6. if Find-Set(u) Find-Set(v)
7. A = A U {{u,v}}
8. Union(u,v)
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
8 7
b c d
4 9
2
a i 14 e
11 4
7 6
8 10
h g f
1 2
5. for each edge (u,v) G.E in sorted order
6. if Find-Set(u) Find-Set(v)
7. A = A U {{u,v}}
8. Union(u,v)
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
8 7
b c d
4 9
2
a i 14 e
11 4
7 6
8 10
h g f
1 2
5. for each edge (u,v) G.E in sorted order
6. if Find-Set(u) Find-Set(v)
7. A = A U {{u,v}}
8. Union(u,v)
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
8 7
b c d
4 9
2
a i 14 e
11 4
7 6
8 10
h g f
1 2
5. for each edge (u,v) G.E in sorted order
6. if Find-Set(u) Find-Set(v)
7. A = A U {{u,v}}
8. Union(u,v)
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
8 7
b c d
4 9
2
a i 14 e
11 4
7 6
8 10
h g f
1 2
5. for each edge (u,v) G.E in sorted order
6. if Find-Set(u) Find-Set(v)
7. A = A U {{u,v}}
8. Union(u,v)
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
8 7
b c d
4 9
2
a i 14 e
11 4
7 6
8 10
h g f
1 2
5. for each edge (u,v) G.E in sorted order
6. if Find-Set(u) Find-Set(v)
7. A = A U {{u,v}}
8. Union(u,v)
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
8 7
b c d
4 9
2
a i 14 e
11 4
7 6
8 10
h g f
1 2
5. for each edge (u,v) G.E in sorted order
6. if Find-Set(u) Find-Set(v)
7. A = A U {{u,v}}
8. Union(u,v)
9. return A
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Kruskal’s Algorithm
MST-KRUSKAL(G,w)
1. A=
2. for each v G.V
3. Make-Set(v) O(V) Make-Set() calls
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Running Time of Kruskal’s
Algorithm
◼ Use the disjoint-set-forest implementation with the union-by-rank and path-
compression heuristics (Section 21.3).
◼ Sorting the edges in line 4 is O(|E| lg |E|).
◼ The disjoint-set operations takes O((|V|+|E|) (|V|)) time, where is the very slowly
growing function (Section 21.4).
◼ The for loop (lines 2–3) performs |V| MAKE-SET operations.
◼ The for loop (lines 5–8) performs O(|E|) FIND-SET and UNION operations.
◼ Since G is connected, we have |E| |V|-1, the disjoint-set operations take
O(|E|(|V|)) time.
◼ Moreover, since (|V|) = O(lg |V|) = O(lg |E|), Kruskal’s algorithm takes O(|E| lg |E|)
time.
◼ Observing that |E| < |V|2 ⇒ lg |E| = O(lg V), the total running time of Kruskal’s
algorithm becomes O(E lg V).
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009
Running Time of Kruskal’s
Algorithm
◼ In a nut shell, O(|V|) Make-Set() calls
◼ Sort edges: O(|E| lg |E|) O(|E|) Find-Set() calls
◼ Disjoint-set operations O(|V|) Union() calls
◼ O(|V|+|E|) operations ⇒ O((|V|+|E|) (|V|)) time
◼ |E||V|-1 ⇒ O(E| (|V|)) time
◼ Since (n) can be upper bounded by the height of the tree,
(|V|)=O(lg |V|)=O(lg |E|).
◼ Thus, the total running time of Kruskal’s algorithm is is O(|E| lg |E|)
◼ By observing that |E| < |V|2 ⇒ lg |E| = O(lg |V|), it becomes O(|E| lg |V|).
Thomas T. Cormen , Charles E. Leiserson , Ronald L. Rivest, Clifford Stein, Introduction to Algorithms, MIT Press, Cambridge, MA, 2009