0% found this document useful (0 votes)
22 views45 pages

Dsa CH 6 Problems

Uploaded by

Ayush Sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views45 pages

Dsa CH 6 Problems

Uploaded by

Ayush Sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

MINIMUM SPANNING TREE

What is Minimum Spanning Tree?


• Connected: every node is reachable from
every other node
• ...then a spanning tree of the graph is a
connected subgraph in which there are no
cycles
graph G

Spanning Tree from Graph G


2 2

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.

• A single graph may have many spanning trees.

• A minimum spanning tree (MST) or minimum weight


spanning tree is then a spanning tree with weight less
than or equal to the weight of every other spanning
tree.
Minimum Spanning Tree (MST)
A minimum spanning tree is a subgraph of an
undirected weighted graph G, such that

• 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

5
Algorithm for finding
Minimum Spanning Tree

• The Prim's Algorithm


• Kruskal's Algorithm
• Baruvka's Algorithm
About Prim’s Algorithm

• The algorithm was discovered in 1930 by


mathematician Vojtech Jarnik and later independently
by computer scientist Robert C. Prim in 1957.
• The algorithm continuously increases the size of a
tree starting with a single vertex until it spans all the
vertices.
• Prim's algorithm runs in O(n*n)
Let’s see an example to
understand
Prim’s Algorithm.
Prim’s Algorithm
Prim’s algorithm finds a minimum cost spanning tree by
selecting edges from the graph one-by-one as follows:

1.It starts with a tree, T, consisting of the starting vertex, x.

2.Then, it adds the shortest edge emanating from x that


connects T to the rest of the graph.

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; • }

• dist[s.number()] = 0; • for (i = 0; i < g.num_nodes; i++)


• if ((g.is_marked(i, UNVISITED)) && (dist[i] <
• for (i = 0; i < g.num_nodes; i++) { dist[v])) v = i;
• vert v = minvertex(g, dist);
• return (v);
• g.mark(v, VISITED); • }
• if (v != s) add_edge_to_MST(vert[v], v);
• if (dist[v] == INFINITY) return;

• for (edge w = g.first_edge; g.is_edge(w), w = g.next_edge(w)) {


• if (dist[g.first_vert(w)] = g.weight(w)) {
• dist[g.second_vert(w)] = g.weight(w);
• vert[g.second_vert(w)] = v;
• }
• }
• }
• }
Complexity Analysis
Minimum edge weight data Time complexity (total)
structure

adjacency matrix, searching O(V*V)

binary heap and adjacency O((V + E) log(V)) = O(E


list log(V))

Fibonacci heap and O(E + V log(V))


adjacency list
Prim’s algorithm Application
❖ One practical application of a MST would be in
the design of a network.
For instance, a group of individuals, who are
separated by varying distances, wish to be
connected together in a telephone network.
Because the cost between two terminal is
different, if we want to reduce our expenses,
Prim's Algorithm is a way to solve it
Prim’s algorithm Application
❖ Connect all computers in a computer science
building using least amount of cable.

❖ A less obvious application is that the minimum


spanning tree can be used to approximately
solve the traveling salesman problem.
A convenient formal way of defining this
problem is to find the shortest path that visits
each point at least once.
Prim’s algorithm Application
❖ Another useful application of MST would
be finding airline routes.
The vertices of the graph would represent
cities, and the edges would represent
routes between the cities.
Obviously, the further one has to travel, the
more it will cost, so MST can be applied
to optimize airline routes by finding the
least costly paths with no cycles.
Prim’s algorithm Application

❖ Cancer imaging: The BC Cancer Research


Ctr. uses minimum spanning trees to
describe the arrangements of nuclei in skin
cells
Kruskal’s Algorithm
• How is it different from Prim’s algorithm?

– Prim’s algorithm grows one tree all the time

– Kruskal’s algorithm grows multiple trees (i.e.,


a forest) at the same time.

– Trees are merged together using safe edges


B 5
C
3
4
8 6
8
A 7 F D
5
4
2

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

3 Select the shortest


4 edge connected to
8 6
that vertex
8
AB 3
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 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

Select the shortest


edge connected to
B 5 any vertex already
C
connected.
3
4 EF 5
8 6

8
A D
7 F

5
4
2

E
Prim’s Algorithm

All vertices have been


connected.
B 5
C
The solution is
3
4 AB 3
8 6
AE 4
ED 2
8
DC 4
A D
7 F EF 5

5
4 Total weight of tree: 18
2

E
Minimum Connector Algorithms

Kruskal’s algorithm Prim’s algorithm


1. Select the shortest edge 1. Select any vertex
in a network
2. Select the shortest edge
2. Select the next shortest connected to that vertex
edge which does not
create a cycle 3. Select the shortest edge
connected to any vertex
3. Repeat step 2 until all already connected
vertices have been
connected 4. Repeat step 3 until all
vertices have been
connected
University Questions
Q.1
2M

10M
Q.4

10M

Q.5

10M
University Questions –DEC 2013
Q.1
2M

10M
Q.4

DEC 2013, 2021,2019,2023

You might also like