Greedy Graphs Prim Kruskal Dijstkra
Greedy Graphs Prim Kruskal Dijstkra
Greedy Graphs Prim Kruskal Dijstkra
O
(a)
O
O
O
O
(b)
O
O
(c)
O
(d)
1
Prims algorithm
If A is the set of edges selected so far,
then A forms a tree.
The next edges (u,v) to be included in A
is a minimum cost edge not in A with the
property that A U {u, v} is also a tree.
Cost
10
(2,6)
25
1 2
6
25
(3,6)
15
1 2
25 6 15
Spanning tree
10
10
10
3
Cost
20
(1,4)
(3,5)
Reject
35
Spanning tree
10
1
2
25
4 20
6
15 3
10
1
4 20
The cost of the spanning tree is 105
2
25
6
15 3
5
35
9
15
16
2
25
30
4
45
50
40 35
5
55
20
15
6
19
Cost
10
(3,6)
15
(4,6)
20
Spanning forest
2
3
4
6
3
20
Cost
25 1
Spanning forst
2
5
21
24
29
30
COMPLEXITY OF KRUSKALS
ALGORITHM (Contd..)
Removal of any edge on the cycle e, e1,e2,.ek will leave
behind a tree T11. In particular, if we remove the edge ej
then the resulting tree T11 will have a cost no more than
the cost of T1.
33
10
50
20
Path
Length
V1
10
V4
35
2) V0V2V3
1) V0V2 10
25
20
30
V2
V3
15
V5
3) V0V2V3V1
4) V0V4
45
45
3
There is no path from V0 to V5
34
GREEDY ALGORITHM TO
GENERATE SHORTEST PATH
The paths are generated in non decreasing order of
path length. First a shortest path to the nearest
vertex is generated.
Then a shortest path to the second nearest vertex is
generated and so on.
We need to determine
(i) the next vertex to which a shortest path must
be generated and
(ii) a shortest path to this vertex.
35
39
50
V1
10
V4
15
35
20
10
20
V2
V3
15
30
V5
3
As vertex 2 is included,
= 10
U 2 S(2) 1
42
Now choose u such that dist (u)= min{Dist (3), Dist (4), Dist (5)))}
i.e min { 25, 45, } = 25
U 3 S(3) 1
The process is continued.
The time taken by the algorithm is 0(n2).
43
1
25
4
45
4
1
25
30
3
8
40 20
50
5
15 7
35
10
6
Graph G
30
3
4
40
2
5 5
20
7
15
10
6
55
45
25
2
3 4 8
5
30
5 7
15
10
6
45