You are on page 1of 10

ADVANCED DATA STRUCTURES UNIT - III IT

GRAPH ALGORITHMS
The trees are the special case of graphs. A tree may be defined as a connected graph
without any cycles.

A spanning tree of a graph is a sub - graph which is basically a tree and it contains all the
vertices of graph containing no cycles.

Minimum – cost spanning tree:


A network is a graph whose lines are weighted. It is also known as a weighted graph. The
weight is an attribute of an edge. In an adjacency matrix, the weight is stored as the
intersection value. In an adjacency list, it is stored as the value in the adjacency linked list.

A minimum – cost spanning tree is a spanning tree in which the total weight of lines is
guaranteed to be the minimum of all possible trees in the graph.

Example:

PVPSIT 1|Page
ADVANCED DATA STRUCTURES UNIT - III IT

Before going into the types of algorithms which will apply to get the minimum spanning tree
of a graph

Let’s manually determine the minimum – cost spanning tree

We can start with any vertex. Because the vertex list is usually key sequenced,

Let’s start with A

The above example shows a graph and one of its minimum – cost spanning trees. Since the
identification of a minimum – cost spanning tree involves the selection of a subset of edges.

Applications of spanning tree:

1. Spanning trees are very important in designing efficient routing algorithms

2. Spanning trees are have wide applications in many areas such as network design

There are two popular techniques to construct a minimum – cost spanning tree from a
weighted graph. One such method is known as Prim’s algorithm and another one is
Kruskal’s algorithm

PVPSIT 2|Page
ADVANCED DATA STRUCTURES UNIT - III IT

Prim’s Algorithm:
The prim’s algorithm is implemented using the adjacency matrix of a graph. This matrix is
denoted by adjMatrix [i, j] where i and j operate from 0 to n-1 for n – node weighted
undirected graph.

We can represent a weighted graph by an adjacency matrix to store the set of edges, an
entry (i, j) in an adjacency matrix contains information on the edge that goes from the
vertex i to the vertex j. each matrix entry contains the weight of the corresponding edge.

Let’s consider an example:

Now the sequence of below figures show the working of prim’s algorithm:

PVPSIT 3|Page
ADVANCED DATA STRUCTURES UNIT - III IT

Kruskal’s Algorithm:
Like Prim’s algorithm, Kruskal’s algorithm also constructs the minimum spanning tree of a
graph by adding edges to the spanning tree one – by – one. At all points during its
execution, the set of edges selected by prim’s algorithm forms exactly one tree, on the
other hand, the set of edges selected by Kruskal’s algorithm forms a forest of trees.

Let us consider a graph

The edges of the graph are arranged in increasing order of weights as shown below. Initially
the spanning tree, T is empty. We select the edge with smallest weight and include it in T.

PVPSIT 4|Page
ADVANCED DATA STRUCTURES UNIT - III IT

If selected edge creates a cycle, then it will be removed from T. We repeat these two steps
until the tree T contains n-1 edges (where n is the number of vertices in the graph)

If the tree contains less than n-1 edges and the edge list is empty, then no spanning tree is
possible for the graph

PVPSIT 5|Page
ADVANCED DATA STRUCTURES UNIT - III IT

Minimum – cost spanning tree using Kruskal’s algorithm

Shortest path Algorithm:


A minimum spanning tree gives no indication about the shortest path between two vertices.
Rather only the overall cost or weight is minimized. In real life, we are required to find the
shortest path between two cities.

For example: one would be interested in finding most economical route between any two
cities in a given railways network.

PVPSIT 6|Page
ADVANCED DATA STRUCTURES UNIT - III IT

We are given a directed graph G in which every edge has a weight and our problem is to find
a path from one vertex v to another vertex w such that the sum of the weights on the path
is as minimal as possible. We shall call such path a shortest path.

The shortest path from vertex A to vertex E is A→D→C→E and has a total cost of 12
compared to the cost of 20 for the edge directly from A to E and the cost of 14 for the path
A→B→C→E

It turns out that is just easy to solve the more general problem of starting at one vertex,
called the source, and finding the shortest path to every other vertex, instead of to just one
destination vertex. For simplicity, we take the source to be vertex A and our problem then
consists of finding the shortest path from vertex A to every other vertex in the graph.

Dijkstra’s Algorithm:
The solution we will show for the shortest path problem is called Dijkstra’s Algorithm, after
Edsger Dijkstra, who first described it in 1959. This algorithm is based on the adjacency
matrix representation of a graph. Somewhat surprisingly, it finds not only the shortest path
from one specified vertex to another, but the shortest paths from the specified vertex to all
the other vertices.

Dijkstra’s Algorithm is often called as greedy technique.

The algorithm works by maintaining a set S of vertices whose shortest distance from the
source is already known.

PVPSIT 7|Page
ADVANCED DATA STRUCTURES UNIT - III IT

Where d[i] contains the length of the current shortest path from source vertex to vertex i
Let’s apply Dijkstra’s algorithm to the given digraph for getting stages of algorithm

PVPSIT 8|Page
ADVANCED DATA STRUCTURES UNIT - III IT

Exercise problems:
1. Construct a minimum – cost spanning tree of a given graph by using prim’s algorithm
and kruskal’s algorithm

2. Find the minimum – cost spanning tree for the below graphs using prim’s and
kruskal’s algorithms

PVPSIT 9|Page
ADVANCED DATA STRUCTURES UNIT - III IT

3. Find the shortest path from A to all other vertices using Dijkstra’s algorithm for the
graph as shown below

4. Find the shortest path by using Dijkstra’s algorithm on the following edge weighted
directed graph with the vertex P as the source

PVPSIT 10 | P a g e

You might also like