d1,l5 kruskal's and prim's algorithms.ppt
TRANSCRIPT
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
1/18
Minimum spanning trees
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
2/18
Minimum Connector Algorithms
Kruskal’s algorithm
1. Select the shortest edge in anetwork
2. Select the next shortest edgewhich does not create a cycle
3. Repeat step 2 until all verticeshave been connected
Prim’s algorithm
1. Select any vertex
2. Select the shortest edgeconnected to that vertex
3. Select the shortest edgeconnected to any vertexalready connected
4. Repeat step 3 until allvertices have beenconnected
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
3/18
A cable company want to connect ive villages to their networkwhich currently extends to the market town o Avon ord. !hat is theminimum length o cable needed"
Avon ord #ingley
$rinleigh %ornwell
&onster
'dan
2
7
45
8 6 4
5
3
8
Example
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
4/18
We model the situation as a network, then the problemis to find the minimum connector for the network
A #
$ %
&
'
2
7
45
8 6 4
5
3
8
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
5/18
AF
BC
D
E
2
7
45
8 6
4
5
3
8
(ist the edges inorder o si)e*
'& 2 A$ 3 A' 4
%& 4$% +'# +%# ,
A# -
$# %#
Kruskal’s Algorithm
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
6/18
Select the shortestedge in the network
ED 2
Kruskal’s Algorithm
AF
BC
D
E
2
7
45
8 6
4
5
3
8
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
7/18
Select the next shortestedge which does notcreate a cycle
ED 2AB 3
Kruskal’s Algorithm
AF
BC
D
E
2
7
45
8 6
4
5
3
8
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
8/18
Select the next shortestedge which does notcreate a cycle
ED 2AB 3
CD 4 (or AE 4)
Kruskal’s Algorithm
AF
BC
D
E
2
7
45
8 6
4
5
3
8
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
9/18
Select the next shortestedge which does notcreate a cycle
ED 2AB 3
CD 4AE 4
Kruskal’s Algorithm
AF
BC
D
E
2
7
45
8 6
4
5
3
8
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
10/18
Select the next shortestedge which does notcreate a cycle
ED 2AB 3
CD 4AE 4BC 5 – orms a !"!leE# 5
Kruskal’s Algorithm
AF
BC
D
E
2
7
4 5
8 6
4
5
3
8
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
11/18
All vertices have beenconnected.
/he solution is
ED 2
AB 3CD 4AE 4E# 5
/otal weight o tree* 1
Kruskal’s Algorithm
AF
BC
D
E
2
7
4 5
8 6
4
5
3
8
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
12/18
AF
BC
D
E
2
7
4 5
8 6
4
5
3
8
Select any vertex
A
Select the shortestedge connected to
that vertex
A$ 3
Prim’s Algorithm
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
13/18
AF
BC
D
E
2
7
4 5
8 6
4
5
3
8
Select the shortest
edge connected toany vertex alreadyconnected.
A' 4
Prim’s Algorithm
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
14/18
Select the shortest
edge connected toany vertex alreadyconnected.
'& 2
Prim’s Algorithm
AF
BC
D
E
2
7
4 5
8 6
4
5
3
8
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
15/18
Select the shortest
edge connected toany vertex alreadyconnected.
&% 4
Prim’s Algorithm
AF
BC
D
E
2
7
4 5
8 6
4
5
3
8
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
16/18
Select the shortest
edge connected toany vertex alreadyconnected.
%$ + 0 orms a cycle
'# +
Prim’s Algorithm
AF
BC
D
E
2
7
4 5
8 6
4
5
3
8
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
17/18
Prim’s Algorithm
AF
BC
D
E
2
7
4 5
8 6
4
5
3
8
All vertices have been
connected.
/he solution is
ED 2
AB 3CD 4AE 4E# 5
/otal weight o tree* 1
-
8/9/2019 D1,L5 Kruskal's and Prim's algorithms.ppt
18/18
• Both algorithms will always give solutions with thesame length.
• They will usually select edges in a different order
– you must show this in your workings.• Occasionally they will use different edges – thismay happen when you have to choose betweenedges with the same length. n this case there ismore than one minimum connector for thenetwork.
!ome points to note