Dsa CH 6 Problems
Dsa CH 6 Problems
4 3 4 5
1 1 1
What is Minimum Spanning Tree?
• A spanning tree of a graph is just a subgraph that
contains all the vertices and is a tree.
5
Algorithm for finding
Minimum Spanning Tree
3.It then moves to the added vertex and repeats the process.
Total Cost: 0
Open List: d
Close List:
Total Cost: 0
Open List: a, f, e, b
Close List: d
Total Cost: 5
Open List: f, e, b
Close List: d, a
Total Cost: 11
Open List: b, e, g
Close List: d, a, f
Total Cost: 18
Open List: e, g, c
Close List: d, a, f, b
Total Cost: 25
Open List: c, g
Close List: d, a, f, b, e
Total Cost: 30
Open List: g
Close List: d, a, f, b, e, c
Total Cost: 39
Open List:
Close List: d, a, f, b, e, c
Prim’s Algorithm
PSEUDO-CODE FOR PRIM‘S
Designate one node as the start node
Add the start node to the priority queue of open nodes.
WHILE (there are still nodes to be added to the closed list)
{
Remove a node from priority queue of open nodes, designate it as current
node.
IF (the current node is not already in the closed list)
{
IF the node is not the first node removed from the priority queue, add
the minimal edge connecting it with a closed node to the minimal
spanning tree.
Add the current node to the closed list.
FOR each successor of current node
IF (the successor is not already in the closed list OR the successor
is now connected to a closed node by an edge of lesser weight than
before)
Add that successor to the priority queue of open nodes;
}
}
Implementation
• void prim(graph \&g, vert s) { • int minvertex(graph \&g, int *d) {
• int v;
• int dist[g.num_nodes];
• int vert[g.num_nodes]; • for (i = 0; i < g.num_nodes; i++)
• if (g.is_marked(i, UNVISITED)) {
• for (int i = 0; i < g.num_nodes; i++) { • v = i; break;
• dist[i] = INFINITY; • }
E
Kruskal’s Algorithm
List the edges in
order of size:
B 5 C ED 2
AB 3
3 AE 4
6 4
8 CD 4
BC 5
8 EF 5
A F D CF 6
7 AF 7
5 BF 8
4 CF 8
2
E
Kruskal’s Algorithm
Select the shortest
edge in the network
B 5 C ED 2
3
6 4
8
8
A F D
7
5
4
2
E
Kruskal’s Algorithm
Select the next shortest
edge which does not
B 5 create a cycle
C
3 ED 2
4 AB 3
8 6
8
A D
7 F
5
4
2
E
Kruskal’s Algorithm
Select the next shortest
edge which does not
B 5 create a cycle
C
3
ED 2
4 AB 3
8 6
CD 4 (or AE 4)
8
A D
7 F
5
4
2
E
Kruskal’s Algorithm
Select the next shortest
edge which does not
B 5 create a cycle
C
3
ED 2
4 AB 3
8 6
CD 4
AE 4
8
A D
7 F
5
4
2
E
Kruskal’s Algorithm
Select the next shortest
edge which does not
B 5 create a cycle
C
3
ED 2
4 AB 3
8 6
CD 4
AE 4
8
BC 5 – forms a cycle
A D
7 F EF 5
5
4
2
E
Kruskal’s Algorithm
All vertices have been
connected.
B 5
C
The solution is
3
4 ED 2
8 6
AB 3
CD 4
8
AE 4
A D
7 F EF 5
5
4
2 Total weight of
tree: 18
E
Let’s see the example
using
Prim’s Algorithm.
Prim’s Algorithm
Select any vertex
B 5 A
C
5
4
2
E
Prim’s Algorithm
Select the shortest
edge connected to
B 5 any vertex already
C
connected.
3
4 AE 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
4 ED 2
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
4 DC 4
8 6
8
A D
7 F
5
4
2
E
Prim’s Algorithm
8
A D
7 F
5
4
2
E
Prim’s Algorithm
5
4 Total weight of tree: 18
2
E
Minimum Connector Algorithms
10M
Q.4
10M
Q.5
10M
University Questions –DEC 2013
Q.1
2M
10M
Q.4