Lec 10 - Graph Algorithms (2)
Lec 10 - Graph Algorithms (2)
1 1 1
a b a b a b
5 5 5
2 4 2 4 2 4
c 3
d c 3
d c 3
d
c 3
d c 3
d c 3
d
▪ At each step, we add one safe edge (the cheapest one) so that
tree1
it joins two trees together.
▪ Two steps: u
– Sort edges by increasing edge weight
– Select the first |V| – 1 edges that do not generate a cycle v
tree2
A 4
4
3 (D,E) 1 (B,E) 4
8 (B,F) 4
6 (D,G) 2
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Example: Select first |V|–1 edges which do not
generate a cycle
3
F C edge dv edge dv
A 3 (D,E) 1 (B,E) 4
4
(D,G) 2 (B,F) 4
5
B D (E,G) 3 (B,H) 4
H 1
(C,D) 3 (A,H) 5
2
3
G E
(G,H)
(C,F)
(B,C)
3
3
4
(D,F)
(A,B)
(A,F)
6
8
10
}
not
considered
Done
Total Cost = dv = 21
Kruskal’s Algorithm Implementation
O(lg E) O(E lg E)
O(lg E)
The total running time of Kruskal’s algorithm is O(E log E) = O(E log V)
ALGORITHM DESIGN AND ANALYSIS 24
Prim’s Algorithm
▪ Prim’s algorithm has the property that the edges in the set A always form a single tree.
▪ The tree starts from an arbitrary root vertex r and grows until it spans all the vertices in V
▪ Each step adds to the tree A a light edge (a safe edge).
▪ When the algorithm terminates, the edges in A form a minimum spanning tree.
▪ Prim’s algorithm is a “greedy” algorithm
B 5 C
3
4
8 6
8
A F D
7
5
4 2
E
Prim’s Algorithm
Select any vertex
B 5 A
C
Select the shortest
3 edge connected to
4
8 6 that vertex
8 AB 3
A D
7 F
5
4
2
E
Prim’s Algorithm
Select the shortest
edge connected to
B 5 any vertex already
C connected.
3 AE 4
4
8 6
8
A D
7 F
5
4
2
E
Prim’s Algorithm
Select the shortest
edge connected to
B 5 any vertex already
C connected.
3 ED 2
4
8 6
8
A D
7 F
5
4
2
E
Prim’s Algorithm
Select the shortest
edge connected to
B 5 any vertex already
C connected.
3 DC 4
4
8 6
8
A D
7 F
5
4
2
E
Prim’s Algorithm
Select the shortest
edge connected to
B 5 any vertex already
C connected.
3 EF 5
4
8 6
8
A D
7 F
5
4
2
E
Prim’s Algorithm
All vertices have been
connected.
B 5
C The solution is
3 AB 3
4
8 6 AE 4
ED 2
8 DC 4
A D EF 5
7 F
5
4 Total weight of tree: 18
2
E
Prim’s Algorithm Complexity
Total time: O(V lg V + E lg V) = O(E lg V)
O(V)
O(V)
O(V lg V)
O(E)
O(E lg V)
ALGORITHM DESIGN AND ANALYSIS 34
Kruskal’s Versus Prim’s Algorithms
Kruskal’s algorithm Prim’s algorithm
▪ Occasionally they will use different edges – this may happen when you have to choose
between edges with the same length.
▪ In this case there is more than one minimum connector for the network.