0% found this document useful (0 votes)
53 views15 pages

Min Spanning Trees

This document provides an overview of elementary graph algorithms, specifically discussing trees and minimum spanning trees (MSTs). It defines what a tree is, provides examples of tree and non-tree graphs, and introduces the concept of spanning trees. It then discusses minimum spanning trees and how they can be used to model real-world problems with minimal costs. Finally, it describes Kruskal's and Prim's algorithms for finding the minimum spanning tree of a weighted graph in 3 steps or less.

Uploaded by

Muneeb Ali
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)
53 views15 pages

Min Spanning Trees

This document provides an overview of elementary graph algorithms, specifically discussing trees and minimum spanning trees (MSTs). It defines what a tree is, provides examples of tree and non-tree graphs, and introduces the concept of spanning trees. It then discusses minimum spanning trees and how they can be used to model real-world problems with minimal costs. Finally, it describes Kruskal's and Prim's algorithms for finding the minimum spanning tree of a weighted graph in 3 steps or less.

Uploaded by

Muneeb Ali
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/ 15

ELEMENTARY GRAPH ALGORITHMS

CS3303: Design and Analysis of Algorithm

Amanullah Yasin (PhD)

Center For Advanced Studies In Engineering


Islamabad, Pakistan
Credits
• Adapted from slides by Dr A. Sattar
What is Tree ?
• Definition: A tree is a connected undirected graph
with no simple circuits.

• Since a tree cannot have a simple circuit, a tree cannot


contain multiple edges or loops.

• Therefore, any tree must be a simple graph.

• Theorem: An undirected graph is a tree if and only if


there is a unique simple path between any of its
vertices.
Trees
• Example: Are the following graphs trees?
Spanning Trees
• A spanning tree for an undirected graph is a sub-graph which includes all
vertices but has no cycles.

• There can be several spanning trees for a graph. Figure shows some of the
trees for the graph with vertices v1,v2,v3,v4

• Each tree has same number of edges

5 Lecture #12
Adapted from slides by Dr A. Sattar
Minimum Spanning Trees
• A weighted undirected graph can have several spanning trees

• One of the spanning trees has smallest sum of all the weights
associated with the edges. This tree is called minimum spanning tree
(MST).

• Figure shows a sample weighted graph, some of the spanning trees,


and the minimum spanning tree

6 Lecture #12
Adapted from slides by Dr A. Sattar
Why do we need MST?
• Minimum spanning trees have many practical applications. Some typical
applications are:
• A telephone network can be configured, using minimum spanning tree, to have
minimum cable length.

• The air travel routes can be selected so that the travel time or travel cost is least.

• A computer network can be set up with minimum routing distance

• Linking a group of island with bridges so that total bridge span length is minimum

• However, MST is not necessary the shortest path and it does not apply to cycle
MST don’t solve TSP
• Travel salesman problem (TSP) can not be solved by
MST :
• salesman needs to go home (what’s the cost going home?)
• TSP is a cycle
• use MST to approximate
• solve TSP by exhaustive approach try every permutation on cyclic
graph

Two important algorithms for creating a minimum spanning tree for a graph,
named after their inventors, are Kruskal’s algorithm and Prim’s algorithm.
Kruskal’s Algorithm
• The Kruskal’s algorithm works as follows:

• Step #: 1 Remove all edges of the graph


• Step #:2 Arrange edges according to their weights
• Step # 3: Select a edge with least weight
• Step # 4: Attach the edge to the corresponding vertices if it does
not form cycle; otherwise, drop the edge
• Step # 5: Repeat steps 3 to 4 until all the edges are processed
(added or dropped)

• Kruskal’s algorithm is categorized as greedy, because at


each step it picks an edge with least weight.
10
Examples for Kruskal’s Algorithm
0 5

2 12 3

1 14 6 0 0 0
28
1 16 2 10 1 1 10 1
14 16
3 18 6 5 6 2 5 6 2 5 6 2
24
25
3 22 4 18 12
4 4
4
4 24 6 22
3 3 3

4 25 5
28
0 10 5

2 12 3

1 14 6
0 0 0
1 16 2
10 1 10 1 10 1
14 14 16
3 18 6
5 6 2 5 6 2 5 6 2
3 22 4
12 12 12
4 4 4
4 24 6
3 3 3
4 25 5
+ 3 6
0 28 1
cycle
0 10 5

12
2 3

1 14 6 0 0
1 16
2 10 1 10 1
14 16 14 16

3 18 6 5 6 2 5 6 2
25
3 22 4 12 12
4 4
22 22
4 24 6 3 + 4 6 3
cycle
cost = 10 +25+22+12+16+14
4 25 5
28
0 1
Kruskal’s Algorithm
MST-KRUSKAL(G,W)
1.A ← Ø
2.for each vertex v V[G}
3. Do MAKE-SET(v)
4.Sort the edges of E into non-decreasing order by weight w
5.for each edge (u, v) E, taken in non-decreasing order by weight
6. do if FIND-SET(u) ≠ FIND-SET(v)
7. then A ← A U {(u, v)}
8. UNION(u, v)
9.Return A

13 Lecture #12
Adapted from slides by Dr A. Sattar
Kruskal’s Algorithm
• MAKE-SET(x) creates a new set whose only member
(and thus representative) is x. Since sets are disjoint, x
should not be in some other set

• FIND-SET(x) returns a pointer to the representative of the


(unique set) containing x

• UNION(x,y) unites the sets into a new set. The original


sets are removed from the collection
Comparison Prim and Kruskal
• Both have the same output MST
• Kruskal’s begins with forest and merge into a tree
• Prim’s always stays as a tree
• If you don’t know all the weight on edges use Prim’s
algorithm
• If you only need partial solution on the graph use
Prim’s algorithm

You might also like