social pharmacy d-pharm 1st year by Pragati K. Mahajan
Daa chapter13
1. Introduction
A graph problem is given a set of vertices and (weighted) edges, find a
subset of edges that connects all the vertices and has minimum total weight giving a
minimum spanning tree (MST). An example of such a problem is determining the
minimum amount of copper needed to produce a common ground in an electronic
circuit. There are two well known algorithms for solving MST problems - Kruskal's
algorithm and Prim's algorithm.
4.2.1. Minimum Spanning Trees
Problem
Given a connected, undirected graph G(V,E) with weighted edges w(u,v),
find an acyclic subset of edges T ⊆ E that connect (or span) all the vertices and has
minumum total weight, i.e.
We call T a minimum spanning tree of G. While the value w(T) is unique, the tree
itself may not be.
Generic Algorithm
If A is a subset of an MST, then a safe edge is one such that
A' = A ∪ (u,v)
is also a subset of an MST.
We can then find an MST by beginning with any vertex and adding safe
edges until A' forms a spanning tree. This tree will contain |V| - 1 edges and be a
mininum spanning tree. Hence we simply need a way to determine safe edges.
A cut is a partition of V into two subsets {S} and {V-S}. An edge crosses the
cut if one vertex is in {S} and the other is in {V-S}. A set of edges A, respects a cut
if none of the edges in A crosses the cut. A light edge is an edge that crosses a cut
with minimum weight.
Using these definitions, we can prove the following theorem:
Given a connected, undirected graph G(V,E) with edge weights w(u,v)
2. If A is a subset of an MST, for any cut that respects A ⇒ a light edge is a safe edge
for A. (Note the converse is not true.)
An immediate corrolary is that if we have a forest of connected components (trees)
each with a set of minimum spanning edges, then any light edge between the trees is
a safe edge. Hence we can grow the MST by simply adding |V| - 1 light edges
between subsets of the MST.
4.2.2. Kruskal's Algorithm
A simple way to implement the generic algorithm is to grow the MST by
adding edges between trees in the forest in order of increasing weights until all trees
are connected. This approach is known as Kruskal's algorithm.
Algorithm
MST-KRUSKAL(G,w)
1. A = ∅
2. for each vertex v ∈ G.V
3. MAKE-SET(v)
4. sort the edges of G.E into nondecreasing order by weight w
5. for each edge (u,v) ∈ G.E, taken in nondecreasing order by weight
6. if FIND-SET(u) ≠ FIND-SET(v)
7. A = A ∪ {(u,v)}
8. UNION(u,v)
9. return A
Basically the algorithm works as follows:
1. Make each vertex a separate tree
2. Sort the edges in nondecreasing order
3. Add an edge if it connects different trees and merge the trees together
The run time of Kruskal's algorithm depends on the implementation of the set
operations, but can be made to run in O(E log V).
Example
Given the following undirected graph
3. We first make each vertex a separate tree and sort the edges in no decreasing order
Step 1: Add edge (u1,u3) and merge u1 and u3
Step 2: Add edge (u2,u5) and merge u2 and u5
Step 3: Add edge (u1,u2) and merge the tree from step 1 with the tree from step 2
4. Step 4: Add edge (u3,u4) (note edge (u2,u3) does not connect separate trees) and
merge u4
The final edge (u1,u4) does not connect separate trees. Thus the final MST is
With total weight 11.
An alternative algorithm for finding MST's is Prim's algorithm which runs
asymptotically faster than Kruskal's algorithm, but at the price of requiring a queue
data structure.
5. 4.2.3. Prim's Algorithm
Prim's algorithm finds MST's by growing the MST by adding light edges
between the current tree and other vertices. The vertices are stored in a minimum
priority queue based on the smallest weigh edge connecting vertices currently not
in the tree with a vertex in the tree.
Algorithm
MST-PRIM(G,w,r)
1. for each u ∈ G.V
2. u.key = ∞
3. u.pi = NIL
4. r.key = 0
5. Q = G.V
6. while Q ≠ ∅
7. u = EXTRACT-MIN(Q)
8. for each v ∈ G.Adj[u]
9. if v ∈ Q and w(u,v) < v.key
10. v.pi = u
11. v.key = w(u,v)
Basically the algorithm works as follows:
1. Initialize Q and set the source (root) key to 0
2. While Q is not empty, dequeue the vertex with minimum weight edge and
add it to the tree by adding edge (u.π,u) to T
3. For each vertex v in Adj[u] that is still in Q, check if w(u,v) (the edge
weights from u for all vertices not in T) are less than the current v.key (the
current smallest edge weight) and if so update the predecessor and key
fields
The run time of Prim's algorithm depends on the implementation of the priority
queue, but can be made to run in O(E + V lg V) using a Fibonacci heap for the
priority queue.
Example
Using the same undirected graph from here
6. We will (arbitrarily) initialize Q with vertex 1 giving
Step 1: Dequeue vertex 1 and update Q (and reprioritizing) by changing u3.key = 2
(edge (u1,u3)), u2.key = 3 (edge (u1,u2)), u4.key = 6 (edge (u1,u4))
Step 2: Dequeue vertex 3 (adding edge (u1,u3) to T) and update Q (and
reprioritizing) by changing u4.key = 4 (edge (u3,u4))
7. Step 3: Dequeue vertex 2 (adding edge (u1,u2) to T) and update Q (and
reprioritizing) by changing u5.key = 2 (edge (u2,u5))
Step 4: Dequeue vertex 5 (adding edge (u2,u5) to T) with no updates to Q
Step 5: Dequeue vertex 4 (adding edge (u3,u4) to T) with no updates to Q
8. At this point Q = ∅ giving the final MST
With total weight 11.