Algorithms Section 10 Graph Theory - Trees
Algorithms Section 10 Graph Theory - Trees
Example 2
In the above example, the vertices ‘a’ and ‘d’ has degree one. And the other two
vertices ‘b’ and ‘c’ has degree two. This is possible because for not forming a
cycle, there should be at least two single edges anywhere in the graph. It is
nothing but two edges with a degree of one.
Forest
A disconnected acyclic graph is called a forest. In other words, a disjoint collection
of trees is called a forest.
Example
The following graph looks like two sub-graphs; but it is a single disconnected
graph. There are no cycles in this graph. Hence, clearly it is a forest.
Spanning Trees
Let G be a connected graph, then the sub-graph H of G is called a spanning tree of
G if
➢ H is a tree
➢ H contains all vertices of G.
A spanning tree T of an undirected graph G is a subgraph that includes all of the
vertices of G.
Example
In the above example, G is a connected graph and H is a sub-graph of G. The
graph H has no cycles, it is a tree with six edges which is one less than the total
number of vertices. Hence H is the Spanning tree of G.
Minimum Spanning Tree
The cost of the spanning tree is the sum of the weights of all the edges in the tree.
There can be many spanning trees. Minimum spanning tree is the spanning tree
where the cost is minimum among all the spanning trees. There also can be many
minimum spanning trees.
Minimum spanning tree has direct application in the design of networks. It is used
in algorithms approximating the travelling salesman problem, multi-terminal
minimum cut problem and minimum-cost weighted perfect matching. Other
practical applications are:
1. Cluster Analysis
2. Handwriting recognition
3. Image segmentation
Circuit Rank
Let ‘G’ be a connected graph with ‘n’ vertices and ‘m’ edges. A spanning tree ‘T’ of
G contains (n-1) edges.
Therefore, the number of edges you need to delete from ‘G’ in order to get a
spanning tree = m-(n-1), which is called the circuit rank of G.
This formula is true, because in a spanning tree you need to have ‘n-1’ edges. Out
of ‘m’ edges, you need to keep ‘n–1’ edges in the graph.
Hence, deleting ‘n–1’ edges from ‘m’ gives the edges to be removed from the
graph in order to get a spanning tree, which should not form a cycle.
Example
Take a look at the following graph −
For the graph given in the above example, you have m=7 edges and n=5 vertices.
Then the circuit rank is
G = m – (n – 1)
= 7 – (5 – 1)
=3
Kruskal's Spanning Tree Algorithm
Kruskal's algorithm to find the minimum cost spanning tree uses the greedy
approach. This algorithm treats the graph as a forest and every node it has
as an individual tree. A tree connects to another only and only if, it has the
least cost among all available options and does not violate MST properties.
Next cost in the table is 4, and we observe that adding it will create a circuit in
the graph.
We ignore it. In the process we shall ignore/avoid all edges that create a circuit.
We observe that edges with cost 5 and 6 also create circuits. We ignore them
and move on.
Now we are left with only one node to be added. Between the two least cost
edges available 7 and 8, we shall add the edge with cost 7.
By adding edge S,A we have included all the nodes of the graph and we now
have minimum cost spanning tree.
Prim's Spanning Tree Algorithm
Prim's algorithm to find minimum cost spanning tree (as Kruskal's algorithm) uses
the greedy approach. Prim's algorithm shares a similarity with the shortest path
first algorithms.
Prim's algorithm, in contrast with Kruskal's algorithm, treats the nodes as a single
tree and keeps on adding new nodes to the spanning tree from the given graph.
To contrast with Kruskal's algorithm and to understand Prim's algorithm better,
we shall use the same example:
Remove all loops and parallel edges from the given graph. In case of parallel
edges, keep the one which has the least cost associated and remove all
others.
Step 2 - Choose any arbitrary node as root node
In this case, we choose S node as the root node of Prim's spanning tree. This node
is arbitrarily chosen, so any node can be the root node. One may wonder why
any video can be a root node. So the answer is, in the spanning tree all the nodes
of a graph are included and because it is connected then there must be at least
one edge, which will join it to the rest of the tree.
Step 3 - Check outgoing edges and select the one with less cost
After choosing the root node S, we see that S,A and S,C are two edges with
weight 7 and 8, respectively. We choose the edge S,A as it is lesser than
the other.
Now, the tree S-7-A is treated as one node and we check for all edges
going out from it. We select the one which has the lowest cost and include
it in the tree.
After this step, S-7-A-3-C tree is formed. Now we'll again treat it as a node
and will check all the edges again. However, we will choose only the least
cost edge. In this case, C-3-D is the new edge, which is less than other
edges' cost 8, 6, 4, etc
After adding node D to the spanning tree, we now have two edges going
out of it having the same cost, i.e. D-2-T and D-2-B. Thus, we can add
either one. But the next step will again yield edge 2 as the least cost.
Hence, we are showing a spanning tree with both edges included.
We may find that the output spanning tree of the same graph using two
different algorithms is same.