0% found this document useful (0 votes)
27 views19 pages

Graphs and It'S Examples

Kruskal's algorithm finds a minimum spanning tree by sorting the edges of a graph by weight and then selecting edges without creating cycles. It works by selecting the lowest weight edge, checking if it creates a cycle, and repeating until all vertices are connected. This results in a minimum spanning tree that connects all vertices with the lowest total edge weight. The example walks through applying Kruskal's algorithm to find the MST of a sample weighted graph.

Uploaded by

sumi kanna
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)
27 views19 pages

Graphs and It'S Examples

Kruskal's algorithm finds a minimum spanning tree by sorting the edges of a graph by weight and then selecting edges without creating cycles. It works by selecting the lowest weight edge, checking if it creates a cycle, and repeating until all vertices are connected. This results in a minimum spanning tree that connects all vertices with the lowest total edge weight. The example walks through applying Kruskal's algorithm to find the MST of a sample weighted graph.

Uploaded by

sumi kanna
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/ 19

GRAPHS AND IT’S EXAMPLES

MINIMUM COST SPANNING TREE

A Minimum Spanning Tree (MST) is a subgraph of an undirected


graph such that the subgraph spans (includes) all nodes, is
connected, is acyclic, and has minimum total edge weight
ALGORITHM CHARACTERISTICS
• Both Prim’s and Kruskal’s Algorithms work with
undirected graphs
• Both work with weighted and unweighted graphs
• Both are greedy algorithms that produce optimal
solutions
Kruskal’s algorithm Prim’s algorithm

1. Select the shortest edge in a 1. Select any vertex


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

4. Repeat step 3 until all


vertices have been
connected
KRUSKAL’S ALOGORITHM

Work with edges, instead of nodes of a graph


Two steps:
– Sort edges by increasing edge weight
– Select the first |V| – 1 edges that do not
generate a cycle
WALK-THROUGH
Consider an undirected, weight
3 graph
10
F C
A 4
4
3
8
6
5
4
B D
H 4
1
2
3
G 3
E
Sort the edges by increasing edge
3 weight
10
F C edge dv edge dv
A 4
4
3 (D,E) 1 (B,E) 4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
H 4
1 (C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do
not generate a cycle
3
10
F C edge dv edge dv
A 4
4
3 (D,E) 1  (B,E) 4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
H 4
1 (C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do
not generate a cycle
3
10
F C edge dv edge dv
A 4
4
3 (D,E) 1  (B,E) 4
8
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
H 4
1 (C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do
not generate a cycle
3
10
F C edge dv edge dv
A 4
4
3 (D,E) 1  (B,E) 4
8
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
H 4
1 (C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Accepting edge (E,G) would create a
cycle
Select first |V|–1 edges which do
not generate a cycle
3
10
F C edge dv edge dv
A 4
4
3 (D,E) 1  (B,E) 4
8
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
H 4
1 (C,D) 3  (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do
not generate a cycle
3
10
F C edge dv edge dv
A 4
4
3 (D,E) 1  (B,E) 4
8
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
H 4
1 (C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do
not generate a cycle
3
10
F C edge dv edge dv
A 4
4
3 (D,E) 1  (B,E) 4
8
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
H 4
1 (C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do
not generate a cycle
3
10
F C edge dv edge dv
A 4
4
3 (D,E) 1  (B,E) 4
8
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
H 4
1 (C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which do
not generate a cycle
3
10
F C edge dv edge dv
A 4
4
3 (D,E) 1  (B,E) 4 
8
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
H 4
1 (C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which do
not generate a cycle
3
10
F C edge dv edge dv
A 4
4
3 (D,E) 1  (B,E) 4 
8
6 (D,G) 2  (B,F) 4 
5
4
B D (E,G) 3  (B,H) 4
H 4
1 (C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which do
not generate a cycle
3
10
F C edge dv edge dv
A 4
4
3 (D,E) 1  (B,E) 4 
8
6 (D,G) 2  (B,F) 4 
5
4
B D (E,G) 3  (B,H) 4 
H 4
1 (C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which do
not generate a cycle
3
10
F C edge dv edge dv
A 4
4
3 (D,E) 1  (B,E) 4 
8
6 (D,G) 2  (B,F) 4 
5
4
B D (E,G) 3  (B,H) 4 
H 4
1 (C,D) 3  (A,H) 5 
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which do
not generate a cycle
3
F C edge dv edge dv
A 4
3 (D,E) 1  (B,E) 4 
(D,G) 2  (B,F) 4 
5
B D (E,G) 3  (B,H) 4 
H

}
1 (C,D) 3  (A,H) 5 
2
3 (G,H) 3  (D,F) 6
G E (C,F) 3  (A,B) 8
not
consider
ed
(B,C) 4  (A,F) 10
Done
Total Cost =  dv = 21

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy