Post on 14-Jun-2015
transcript
Application of greedy method: Prim’s Algorithm
Greedy Method ???
A greedy method is an method that follows the problem solving technique of making the locally optimal choice at each stage with the hope of finding a global optimum.
Prim’s Algorithm
Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a connected weighted undirected graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized.
Spanning Tree ???
A spanning tree of a connected graph G can be defined as a maximal set of edges of G that contains no cycle, or as a minimal set of edges that connect all vertices.
Minimum Spanning Tree ???
• it is a tree (i.e., it is acyclic)• it covers all the vertices V
– contains |V| - 1 edges
• the total cost associated with tree edges is the minimum among all possible spanning trees
• not necessarily unique
A minimum spanning tree is a subgraph of an undirected weighted graph G, such that
Algorithm
Example 1
C
FE
A B
D
5
64
3
4
2
1 2
3
2
C
FE
A B
D
5
64
3
4
2
1 2
3
2
C
FE
A B
D
5
64
3
4
2
1 2
3
2
C
FE
A B
D
5
64
3
4
2
1 2
3
2
C
FE
A B
D
3
4
2
1 2
3
2
C
FE
A B
D
3
2
1 2
3
2
C
FE
A B
D
3
2
1 2
2
3
C
FE
A B
D
3
2
1 2
2
C
FE
A B
D
3
2
1 2
2
C
FE
A B
D
3
2
1 2
2
minimum- spanning tree
Example 2
Prim’s Algorithm
a
ce
d
b2
45
9
6
4
5
5
Prim’s algorithm
a
ce
d
b2
45
9
6
4
5
5
The MST initially consists of the vertex e
Prim’s algorithm
a
ce
d
b2
45
9
6
4
5
5
Prim’s algorithm
a
ce
d
b2
45
9
6
4
5
5
Prim’s algorithm
a
ce
d
b2
45
9
6
4
5
5
Prim’s algorithm
a
ce
d
b2
45
9
6
4
5
5
The final minimum spanning tree
Example 3
Weight (T) = 23 + 29 + 31 + 32 + 47 + 54 + 66 = 282
Problem: Laying Telephone Wire
Central office
Wiring: Normal Approach
Central office
Expensive!
Wiring: Better Approach
Central office
Minimize the total length of wire connecting the customers
Thank You