0% found this document useful (0 votes)
10 views

Algorithms Section 10 Graph Theory - Trees

The document discusses graph theory, focusing on trees, forests, spanning trees, and minimum spanning trees. It defines trees as connected acyclic graphs and forests as disconnected acyclic graphs, and explains the concepts of spanning trees and minimum spanning trees, including algorithms like Kruskal's and Prim's for finding them. Practical applications of minimum spanning trees are also highlighted, along with the concept of circuit rank in graphs.

Uploaded by

nh696831
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Algorithms Section 10 Graph Theory - Trees

The document discusses graph theory, focusing on trees, forests, spanning trees, and minimum spanning trees. It defines trees as connected acyclic graphs and forests as disconnected acyclic graphs, and explains the concepts of spanning trees and minimum spanning trees, including algorithms like Kruskal's and Prim's for finding them. Practical applications of minimum spanning trees are also highlighted, along with the concept of circuit rank in graphs.

Uploaded by

nh696831
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CS Algorithm Graph-Trees

Graph Theory - Trees


Trees are graphs that do not contain even a single cycle. They represent
hierarchical structure in a graphical form. Trees belong to the simplest class of
graphs. Despite their simplicity, they have a rich structure.
Tree A connected acyclic graph is called a tree. In other words, a connected graph
with no cycles is called a tree.
The edges of a tree are known as branches. Elements of trees are called their
nodes. The nodes without child nodes are called leaf nodes.
A tree with ‘n’ vertices has ‘n-1’ edges. If it has one more edge extra than ‘n-1’,
then the extra edge should obviously have to pair up with two vertices which leads
to form a cycle. Then, it becomes a cyclic graph which is a violation for the tree
graph.
Example 1
The graph shown here is a tree because it has no cycles and it is connected. It has
four vertices and three edges, i.e., for ‘n’ vertices ‘n-1’ edges as mentioned in the
definition.

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.

To understand Kruskal's algorithm let us consider the following example :

Step 1 - Remove all loops and Parallel Edges


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 - Arrange all edges in their increasing order of weight


The next step is to create a set of edges and weight, and arrange them in an
ascending order of weightage (cost).

Step 3 -Add the edge which has the least weightage


Now we start adding edges to the graph beginning from the one which has the
least weight. Throughout, we shall keep checking that the spanning properties
remain intact. In case, by adding one edge, the spanning tree property does not
hold then we shall consider not to include the edge in the graph.
The least cost is 2 and edges involved are B,D and D,T. We add them. Adding
them does not violate spanning tree properties, so we continue to our next edge
selection.
Next cost is 3, and associated edges are A,C and C,D. We add them again

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:

Step 1 - Remove all loops and Parallel Edges

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.

You might also like