Design & Analysis of Algorithms: DR Anwar Ghani
Design & Analysis of Algorithms: DR Anwar Ghani
Dr Anwar Ghani
Example: There can be several spanning trees for a graph. Figure (i) shows a sample graph.
Figure (ii) depicts some of the trees for the graph
v1 v2 v1 v2 v1 v2 v1 v2 v1 v2
v4 v3 v4 v3 v4 v3 v4 v3 v4 v3
T1 T2 T3 T4
¾ Each spanning tree includes all the four vertices (v1,v2,v3,v4) of the parent graph
Example :Figure shows a sample weighted graph, and some of the spanning trees, with
total weight of edges for each tree. The tree T1, with smallest total weight is the minimum
spanning tree. It is shown with vertices colored red
20 20 20 20
20 v1 v2 v1 v2 v1 v2 v1 v2
v1 v2
10 30 10 10 40
10 30 50 40 50
v4 v3 v4 60 v3 v4 60 v3 v4 v3
v4 60 v3
Weight:10+20+30=50 10+20+60=90 20+50+60=130 10+20+40=70
T1 T2 T3 T4
(i) Weighted graph (ii) Spanning Trees T1,T2,T3,T4
Minimum Spanning Trees
Applications
Minimum spanning trees have many practical applications. Some typical examples are:
:
• A telephone network can be configured, using minimum spanning tree, so that
minimum cable length is used.
• The air travel routes can be selected so that the travel time or travel cost is least.
• Linking a group of islands with bridges so that total bridge span length is minimum
¾ Two important algorithms for creating a minimum spanning tree for a weighted graph,
are Kruskal’s algorithm and Prim’s algorithm, named after their inventors.
Kruskal’s Algorithm
Minimum Spanning Tree
Kruskal’s Algorithm
The Kruskal ‘s algorithm works as follows:
Step #4: Attach the edge to the corresponding vertices, if it does not form a cycle;
otherwise, drop the edge
Step #5: Repeat steps 3 to 4 until all the edges have been processed (added or
dropped)
¾ Kruskal’s algorithm is categorized as greedy, because at each step it picks an edge
with smallest weight.
45 18
15 c d e
b 40
57 8
14
10 17
12
a j 32 k 19 f
28
29
30 47 39 55
50
14 45
i h g
¾ The steps for growing the minimum spanning tree are elaborated in the next set of figures
Kruskal’s Algorithm Example
(1)In order to extract edges
with increasing weights, the
vertices forming edges of the
graph and the corresponding
weights are stored in a priority
queue. In the figure, the edges
are shaded green, and the
corresponding weights are
shaded red. The tree set T is
empty.
(26)The edge(i,j,47) is
extracted from the queue. It
forms a cycle with tree
edges (j,e,12), (e,k,14),
(k,h,29), (h,i,14). The cycle
is marked by the arrows.
The extracted edge is shown
with bold green line in the
figure
Kruskal’s Algorithm Example
45 18
15 c d e
57
b 40 8
14
10 17
12
a j 32 k 19 f
28
29
47 39 55
30
50
14 45
i h g
¾ In actual implementation, the edges can be sorted by using an efficient algorithm such as
Quick sort. Alternatively, a Priority Queue can be used. The edges are retrieved in non-
decreasing order by dequeue operation, as shown in the preceding example.
Example : Consider the set of vertices V={a ,b, c, d ,e} in a sample graph. The Union Set
UV is defined as the set consisting of members {a}, {b}, {c}, {d}. In set notation UV can be
denoted as set of sets, as under
¾ The rule for accepting or discarding an extracted edge is as follows. Suppose that an
edge (u, v) is extracted from a priority queue. Let UV be the Union Set of vertices
(i) If the vertices u, v belong to the same member S of the Union Set UV
then the edge (u, v) forms cycle, and discarded
(i) If the vertices u, v belong to two different members, S1 and S2, of the
Union Set UV then the edge (u, v) is accepted. Further, the sets S1 and
S2 are merged together to form a single set S which is placed in the
Union Set UV
Detecting Cycles
Example
Suppose at any stage of the execution of Krukal’s algorithm , the UV Union Set of vertices
consists of members {a}, {b}, { c, e}, {d} ,i.e UV= { {a}, {b}, {c, e}, {d} } . Consider the
following possibilities:
(1) Suppose an edge (e, d ) is extracted from the priority queue. Since vertices e, d
belong to two different members { c, e} and { d} of UV, the edge (e, d) does not form a cycle.
The sets {e, d} and { d } will be merged into the set { c, d, e} which will replace the sets
{ c,e} { d} in the original set UV. Thus, the updated UV would be UV= { {a}, {c, d ,e} }
(2) Assume again that an edge (c, e) is extracted. Since the vertices e and d belong to
the same set {c, d, e} of the UV, the edge (c, e ) will be discarded
(3) Again, assume that the edge (a, b) is extracted. Since the vertices a, b belong to
different sets, the corresponding disjoints set, namely, {a}, {b} will be merged. Thus, the
updated UV would be UV={{a, b}, {c, d, e} }.
(4) Next , assume that edge ( b ,c) is extracted, Since b belongs to {a,b} and c belongs to
{c, d, e} the sets {a, b} and {c, d, e} would be merged i.e UV={a, b, c, d, e}
Kruskal’s Algorithm
Using Set for MST
90 90
v1 v1
v2 v2
30 30 40 10
20 40 10 20
70 55 70
v6 55 v7 v6 v7 v3
v3
80 50 80
35 50 35 65
65
v5 v5
v4 v4
95
Edge Weight Edge Action UV={ {v1}, {v2}, {v3}, {v4}, {v5}, {v6}, {v7} }
(v2,v3) 10 (v2,v3) added { {v1}, {v2, v3}, {v4}, {v5}, {v6}, {v7} }
MST-KRUSKAL(G,W)
1 T←φ ►Initialize Minimum Spanning Tree
2 UV←φ ► Initialize Union set UV
∈
3 for each vertex v V
4 do UV ← UV U {v} ► Add Vertex sets to union set UV
5 for each (u, v) ∈ E ►Create a priority queue of edges and weights
6 do ENQUEUE(Q,u,v,w) ► weights are keys (priorities)
7 while |UV| >1 do ► Continue until all vertices form a single set of cardinality 1
8 (u, v) ←DEQUEUE(Q) ►Remove an edge (u,v) from the queue
9 if u, v belong to different sets US1,US2 ∈ UV
10 then UV ←UV – US1 ► remove set US1 from UV
11 UV←UV - US2 ►remove set US2 from UV
12 UV ←UV U ( US1 U US2) ►Join sets US1 and US2 , add the union to UV
13 T←T U (u, v) ►Add the selected edge to the spanning tree
14 return T
Kruskal Visualization
Kruskal’s Algorithm Visualization
Analysis of Kruskal’s Algorithm
Running Time
Assume that edges are processed by using priority queue, graph is represented by linked
lists, and Union Set is used to detect cycles. The main contributions to the running time are
(1) Tsort , time to sort edges
(2) Tini , time to initialize the sets
(3) Tscan , time to scan linked lists
It was shown earlier that the time to sort n data items, using quick sort is n lg n. Since edge
set contains |E| edges, time to sort edges will be
Tsort= O(|E|. lg| E|).
The time for initialization of sets will be
Tini= O(|V|)., where |V| is number of vertices in the graph
The total time to scan the linked lists representing the graph is equal to number of edges in
the graph. This time is at most
Tscan=O(|E|)
Thus, total running time is O(|E|. lg| E|) + O(|E|)+O(|V|). Ignoring |E| compared to |E|.|lg
|E|, the running time for Kruskal’s algorithm is
T kruskal= O(|E|.lg |E|) +O(|V|)
Prim’s Algorithm
Prim’s Algorithm
Strategy
The Prim’s algorithm is an alternative method for creating minimum spanning tree for
a weighted undirected graph. Unlike the Kruskal’s algorithm, it makes a systematic
selection of vertices. It proceeds by choosing some arbitrary initial vertex, and then
examines all neighbors. It selects the neighbor which has the shortest distance. Next, the
neighbors of selected vertices are examined for shortest link. This process is continued till
all the vertices have been explored. The selected shortest links form minimum spanning
tree.
Prim’s Algorithm
Procedure
Let V be the vertex set for a graph G. Let T be the minimum spanning tree for G. The Prim’s
algorithm proceeds as follows:
Step # 3: Repeat Step #:4 through Step #:6 until the set V is empty.
Step #5 :Choose the vertex u in V which has the minimum distance from vertex v in S.
Step #6: Remove vertex u from V and add it to S. Move edge (v ,u) to T.
Prim’s Algorithm
Example
In order to study the working of Prim’s algorithm, consider the weighted undirected graph
shown in the figure below
45 18
c d e
15
57 8
b 40 14
12
10
17
a j k f
28 32 19
39 29
30 47 55
50
14 45
i h g
The working of algorithm is illustrated by the next set of figures with exploratory notes
Prim’s Algorithm
Example
(1) Figure shows a sample weighted
undirected graph. The vertex set for the
graph is V={a, b, c, d, e, f, g, h, i, j, k}. A set
S is used to store vertices, which are
selected to form minimum spanning tree.
Another set T is defined to contain the edges
of minimum spanning tree. Initially, S and T
are empty: S={}, T={}