Beruflich Dokumente
Kultur Dokumente
Structure
Tutorial 5
Sionggo Japit
sjapit@uow.edu.au
4
3
4
5
4
6
3
Kruskals Algorithm
Kruskals algorithms for finding a minimum
spanning tree:
Kruskal's algorithm starts with an empty set S and
selects at each stage the shortest edge that has
been neither selected nor rejected.
Kruskals Algorithm
Kruskals algorithm
Create a forest F (a set of trees), where each vertex in the
graph is a separate tree
Create a set S containing all the edges in the graph, and
arrange them in ascending order.
While S is nonempty
Remove an edge with minimum weight from S
If that edge connects two different trees, then add it to
the forest, combining two trees into a single tree
otherwise discard that edge
Stop when the graph is connected.
Kruskals Algorithm
Kruskals Algorithm: An Example
Step 0
1
3
5
4
S= { 1:(1
2:(2
3:(4
3:(6
4:(1
4:(2
4:(4
5:(3
6:(2
6:(3
8:(5
2),
3),
5),
7),
4),
5),
7),
5),
4),
6),
6) }
Kruskals Algorithm
Kruskals Algorithm: An Example
Step 1
5
4
{1, 2}
S= { 1:(1
2:(2
3:(4
3:(6
4:(1
4:(2
4:(4
5:(3
6:(2
6:(3
8:(5
2),
3),
5),
7),
4),
5),
7),
5),
4),
6),
6) }
Kruskals Algorithm
Kruskals Algorithm: An Example
Step 2
5
4
{2, 3}
S= { 1:(1
2:(2
3:(4
3:(6
4:(1
4:(2
4:(4
5:(3
6:(2
6:(3
8:(5
2),
3),
5),
7),
4),
5),
7),
5),
4),
6),
6) }
Kruskals Algorithm
Kruskals Algorithm: An Example
Step 3
{4, 5}
5
4
3
6
8
4
S= { 1:(1
2:(2
3:(4
3:(6
4:(1
4:(2
4:(4
5:(3
6:(2
6:(3
8:(5
2),
3),
5),
7),
4),
5),
7),
5),
4),
6),
6) }
Kruskals Algorithm
Kruskals Algorithm: An Example
Step 4
{6, 7}
5
4
3
6
6
3
8
4
S= { 1:(1
2:(2
3:(4
3:(6
4:(1
4:(2
4:(4
5:(3
6:(2
6:(3
8:(5
2),
3),
5),
7),
4),
5),
7),
5),
4),
6),
6) }
Kruskals Algorithm
Kruskals Algorithm: An Example
Step 5
{1, 4}
{1, 2, 3, 4, 5} {6, 7}
5
4
3
6
6
3
8
4
S= { 1:(1
2:(2
3:(4
3:(6
4:(1
4:(2
4:(4
5:(3
6:(2
6:(3
8:(5
2),
3),
5),
7),
4),
5),
7),
5),
4),
6),
6) }
Kruskals Algorithm
Kruskals Algorithm: An Example
Step 5
{2, 5}
8
4
5
4
4
3
6
3
S= { 1:(1
2:(2
3:(4
3:(6
4:(1
4:(2
4:(4
5:(3
6:(2
6:(3
8:(5
2),
3),
5),
7),
4),
5),
7),
5),
4),
6),
6) }
Kruskals Algorithm
Kruskals Algorithm: An Example
Step 5
{4, 7}
{1, 2, 3, 4, 5, 6, 7} - done
8
4
5
4
6
3
S= { 1:(1
2:(2
3:(4
3:(6
4:(1
4:(2
4:(4
5:(3
6:(2
6:(3
8:(5
2),
3),
5),
7),
4),
5),
7),
5),
4),
6),
6) }
Kruskals Algorithm
Kruskals Algorithm
type node = record
node_number: integer
type edge = record
start: ^node
end: ^node
length: integer
Kruskals Algorithm
Kruskals Algorithm
Function Kruskal(N[1..n]: ^node, E[1..e]: ^edge)
sort E by increasing length
S = {}
for i =1 to n do
set[i] = {N[i]}
i=0
repeat
i = i+1
u = E[i]^.start
v = E[i]^.end
uset = find u in set[]
vset = find v in set[]
if uset vset then
merge(uset, vset)
add E[i] to S
until S contains n 1 edges
return S
Prims Algorithm
Prims algorithms for finding a minimum
spanning tree:
Prims Algorithm
Prims algorithm
Let O be a set of nodes and S a set of edges
Initially O contains the first node of N and S is
empty
At each step look for the shortest edge {u, v}
in E such that u O and v O
Add {u, v} to S
Add v to O
Repeat until O = N
Note that, at each step, S is a minimum
spanning tree on the nodes in O
Prims Algorithm
Prims Algorithm: An Example
Step 0
1
{1}
2
5
4
6
3
Prims Algorithm
Prims Algorithm: An Example
Step 1
{1, 2}
3
5
4
{1, 2}
6
3
Prims Algorithm
Prims Algorithm: An Example
Step 2
{2, 3}
{1, 2, 3}
5
4
6
3
Prims Algorithm
Prims Algorithm: An Example
Step 3
{1, 4}
3
5
4
{1, 2, 3, 4}
Prims Algorithm
Prims Algorithm: An Example
Step 4
{4, 5}
{1, 2, 3, 4, 5}
5
4
3
6
8
4
Prims Algorithm
Prims Algorithm: An Example
Step 5
{4, 7}
{1, 2, 3, 4, 5, 7}
5
7
3
6
8
4
5
4
Prims Algorithm
Prims Algorithm: An Example
Step 5
{7, 6}
{1, 2, 3, 4, 5, 6, 7} done
5
4
8
4
6
3
Prims Algorithm
Prims Algorithm
Prims Algorithm
k
nearest =
mindist =
S = {}
Prims Algorithm
Prims Algorithm: after iteration 1
L=
nearest =
mindist =
-1 2
S = {{1, 2}}
Prims Algorithm
Prims Algorithm: after iteration 2
L=
nearest =
mindist =
-1
-1
Prims Algorithm
Prims Algorithm: after iteration 3
L=
nearest =
mindist =
-1
-1
-1
Prims Algorithm
Prims Algorithm: after iteration 4
L=
nearest =
mindist =
1
-1 2
-1 3
-1 4
-1 5
Prims Algorithm
Prims Algorithm: after iteration 5
L=
nearest =
mindist =
-1
-1
-1
-1
-1
S = {{1, 2}, {2, 3}, {1, 4}, {4, 5}, {4, 7}}
Prims Algorithm
Prims Algorithm: after iteration 6
L=
nearest =
mindist =
-1
-1
-1
-1
-1
-1
S = {{1, 2}, {2, 3}, {1, 4}, {4, 5}, {4, 7}, {7, 6}}
Kruskals algorithm
1
0
Prims algorithm
3
3
1
3
1
2
3
2
7
3
5
1
0
3
3
1
2
3
2
7
S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4),
2: (2 7),
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }
1
0
3
3
1
2
3
2
7
S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4),
2: (2 7),
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }
1
0
3
3
1
2
3
2
7
S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4),
2: (2 7),
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }
1
0
3
3
1
2
3
2
7
S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4),
2: (2 7),
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }
1
0
3
3
1
2
3
2
7
S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4),
2: (2 7),
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }
1
0
3
3
1
2
3
2
7
S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7),
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }
1
0
3
3
1
2
3
2
7
S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }
1
0
3
3
1
2
3
2
7
S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }
1
0
3
3
1
2
3
2
7
S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }
F = {0,1, 2, 3, 4, 5, 6, 7}
1
0
3
3
1
2
3
2
7
S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5), Rejected
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }
F = {0,1, 2, 3, 4, 5, 6, 7}
1
0
3
3
1
2
3
2
7
S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5), Rejected
3: (1 2), Rejected
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }
F = {0,1, 2, 3, 4, 5, 6, 7}
1
0
3
3
1
2
3
2
7
S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5), Rejected
3: (1 2), Rejected
3: (1 4), Rejected
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7) }
F = {0,1, 2, 3, 4, 5, 6, 7}
1
0
3
3
1
2
3
2
7
S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5), Rejected
3: (1 2), Rejected
3: (1 4), Rejected
3: (2 3), Rejected
3: (6 7),
3: (0 5),
3: (5 7) }
F = {0,1, 2, 3, 4, 5, 6, 7}
1
0
3
3
1
2
3
2
7
S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5), Rejected
3: (1 2), Rejected
3: (1 4), Rejected
3: (2 3), Rejected
3: (6 7), Rejected
3: (0 5),
3: (5 7) }
F = {0,1, 2, 3, 4, 5, 6, 7}
1
0
3
3
1
2
3
2
7
S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4), Rejected
2: (2 7), Rejected
2: (4 6),
2: (5 6),
2: (2 5), Rejected
3: (1 2), Rejected
3: (1 4), Rejected
3: (2 3), Rejected
3: (6 7), Rejected
3: (0 5), Rejected
3: (5 7) }
F = {0,1, 2, 3, 4, 5, 6, 7}
1
0
3
3
1
2
3
2
7
S = { 1: (0 - 2),
1: (1 3),
1: (3 7),
1: (4 7),
2: (0 1),
2: (3 4),
2: (2 7),
2: (4 6),
2: (5 6),
2: (2 5),
3: (1 2),
3: (1 4),
3: (2 3),
3: (6 7),
3: (0 5),
3: (5 7)
Rejected
Rejected
Rejected
Rejected
Rejected
Rejected
Rejected
Rejected
Rejected }
O = {0}
1
0
S={ }
1
2
3
2
7
O = {0}
1
0
1
2
3
2
7
O = {0}
1
0
3
3
1
2
3
2
7
O = {0,2}
1
0
1
2
3
2
7
O = {0,1, 2}
1
0
S = {0, 5}, {2, 1}, {2, 5}, {2, 3}, {2, 7}, {1,
4}, {1, 3}
1
2
3
2
7
O = {0,1, 2, 3}
1
0
S = {0, 5}, {2, 1}, {2, 5}, {2, 3}, {2, 7}, {1,
4}, {3, 4}, {3, 7}
1
2
3
2
7
O = {0,1, 2, 3, 7}
1
0
S = {0, 5}, {2, 1}, {2, 5}, {2, 3}, {2, 7}, {1,
4}, {3, 4}, {7, 4}, {7, 5}, {7,6}
1
2
3
2
7
O = {0,1, 2, 3, 4, 7}
1
0
S = {0, 5}, {2, 1}, {2, 5}, {2, 3}, {2, 7}, {1,
4}, {3, 4}, {7, 5}, {7,6}, {4, 6}
1
2
3
2
7
O = {0,1, 2, 3, 4, 6, 7}
1
0
S = {0, 5}, {2, 1}, {2, 5}, {2, 3}, {2, 7}, {1,
4}, {3, 4},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