You are on page 1of 35

UNIT 5

SYLLABYS:
Graphs:
Graph Abstract Data Type, Definitions, Graph Representations, Elementary Graph
Operations, Depth First Search, Breadth First Search,Spanning Trees, Minimum Cost
Spanning Trees, Prim’s and Kruskal’s Algorithms,Shortest Paths and Transitive Closure,
Single Source All Destination - Dijkstra’s Algorithm

Introduction to Graphs
Graph is a non-linear data structure. It contains a set of points known as nodes (or vertices)

and a set of links known as edges (or Arcs). Here edges are used to connect the vertices. A

graph is defined as follows...

Graph is a collection of vertices and arcs in which vertices are connected with arcs

Graph is a collection of nodes and edges in which nodes are connected with edges

Generally, a graph G is represented as G = ( V , E ), where V is set of vertices and E is set of

edges.

Example
The following is a graph with 5 vertices and 6 edges.

This graph G can be defined as G = ( V , E )

Where V = {A,B,C,D,E} and E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.


Graph Terminology
We use the following terms in graph data structure...

Vertex
Individual data element of a graph is called as Vertex. Vertex is also known as node. In

above example graph, A, B, C, D & E are known as vertices.

Edge
An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is

represented as (startingVertex, endingVertex). For example, in above graph the link

between vertices A and B is represented as (A,B). In above example graph, there are 7 edges

(i.e., (A,B), (A,C), (A,D), (B,D), (B,E), (C,D), (D,E)).

Edges are three types.

1. Undirected Edge - An undirected egde is a bidirectional edge. If there is undirected


edge between vertices A and B then edge (A , B) is equal to edge (B , A).
2. Directed Edge - A directed egde is a unidirectional edge. If there is directed edge
between vertices A and B then edge (A , B) is not equal to edge (B , A).
3. Weighted Edge - A weighted egde is a edge with value (cost) on it.

Undirected Graph
A graph with only undirected edges is said to be undirected graph.

Directed Graph
A graph with only directed edges is said to be directed graph.

Mixed Graph
A graph with both undirected and directed edges is said to be mixed graph.

End vertices or Endpoints


The two vertices joined by edge are called end vertices (or endpoints) of that edge.

Origin
If a edge is directed, its first endpoint is said to be the origin of it.

Destination
If a edge is directed, its first endpoint is said to be the origin of it and the other endpoint is

said to be the destination of that edge.

Adjacent
If there is an edge between vertices A and B then both A and B are said to be adjacent. In

other words, vertices A and B are said to be adjacent if there is an edge between them.

Incident
Edge is said to be incident on a vertex if the vertex is one of the endpoints of that edge.

Outgoing Edge
A directed edge is said to be outgoing edge on its origin vertex.
Incoming Edge
A directed edge is said to be incoming edge on its destination vertex.

Degree
Total number of edges connected to a vertex is said to be degree of that vertex.

Indegree
Total number of incoming edges connected to a vertex is said to be indegree of that vertex.

Outdegree
Total number of outgoing edges connected to a vertex is said to be outdegree of that vertex.

Parallel edges or Multiple edges


If there are two undirected edges with same end vertices and two directed edges with same

origin and destination, such edges are called parallel edges or multiple edges.

Self-loop
Edge (undirected or directed) is a self-loop if its two endpoints coincide with each other.

Simple Graph
A graph is said to be simple if there are no parallel and self-loop edges.

Path
A path is a sequence of alternate vertices and edges that starts at a vertex and ends at other

vertex such that each edge is incident to its predecessor and successor vertex.
The Graph Abstract Data Type
The graph abstract data type (ADT) is defined as follows:

Graph() creates a new, empty graph.


addVertex(vert) adds an instance of Vertex to the graph.
addEdge(fromVert, toVert) Adds a new, directed edge to the graph that connects two
vertices.

addEdge(fromVert, toVert, weight) Adds a new, weighted, directed edge to the


graph that connects two vertices.

getVertex(vertKey) finds the vertex in the graph named vertKey.


getVertices() returns the list of all vertices in the graph.
in returns True for a statement of the form vertex in graph, if the given vertex is in the
graph, False otherwise.

Beginning with the formal definition for a graph there are several ways we can implement
the graph ADT in Python. We will see that there are trade-offs in using different
representations to implement the ADT described above. There are two well-known
implementations of a graph, the adjacency matrix and the adjacency list. We will explain
both of these options, and then implement one as a Python class.

Graph Representations
Graph data structure is represented using following representations...

1. Adjacency Matrix
2. Adjacency List

Adjacency Matrix
In this representation, the graph is represented using a matrix of size total number of

vertices by a total number of vertices. That means a graph with 4 vertices is represented
using a matrix of size 4X4. In this matrix, both rows and columns represent vertices. This

matrix is filled with either 1 or 0. Here, 1 represents that there is an edge from row vertex

to column vertex and 0 represents that there is no edge from row vertex to column vertex.

For example, consider the following undirected graph representation...

Directed graph representation...


Adjacency List
In this representation, every vertex of a graph contains list of its adjacent vertices.

For example, consider the following directed graph representation implemented using
linked list...

This representation can also be implemented using an array as follows..

ELEMENTARY GRAPH OPERATIONS

Given a graph G = (V E) and a vertex v in V(G) we wish to visit all vertices in G that are
reachable from v (i.e., all vertices that are connected to v). We shall look at two ways of
doing this: depth-first search and breadth-first search. Although these methods work on
both directed and undirected graphs the following discussion assumes that the graphs are
undirected.
It is also called as Graph Traversal

Graph Traversal
Graph traversal is a technique used for a searching vertex in a graph. The graph traversal is
also used to decide the order of vertices is visited in the search process. A graph traversal
finds the edges to be used in the search process without creating loops. That means using
graph traversal we visit all the vertices of the graph without getting into looping path.

There are two graph traversal techniques and they are as follows...
1. DFS (Depth First Search)
2. BFS (Breadth First Search)

DFS (Depth First Search)


DFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph
without loops. We use Stack data structure with maximum size of total number of vertices
in the graph to implement DFS traversal.

We use the following steps to implement DFS traversal...

​ Step 1 - Define a Stack of size total number of vertices in the graph.


​ Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push it
on to the Stack.
​ Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the
top of stack and push it on to the stack.
​ Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex
which is at the top of the stack.
​ Step 5 - When there is no new vertex to visit then use back tracking and pop one
vertex from the stack.
​ Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
​ Step 7 - When stack becomes Empty, then produce final spanning tree by removing
unused edges from the graph

Back tracking is coming back to the vertex from which we reached the current vertex.
BFS (Breadth First Search)
BFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph
without loops. We use Queue data structure with maximum size of total number of
vertices in the graph to implement BFS traversal.

We use the following steps to implement BFS traversal...

​ Step 1 - Define a Queue of size total number of vertices in the graph.


​ Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert
it into the Queue.
​ Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of
the Queue and insert them into the Queue.
​ Step 4 - When there is no new vertex to be visited from the vertex which is at front of
the Queue then delete that vertex.
​ Step 5 - Repeat steps 3 and 4 until queue becomes empty.
​ Step 6 - When queue becomes empty, then produce final spanning tree by removing
unused edges from the graph

What is a spanning tree?
A spanning tree can be defined as the subgraph of an undirected connected graph. It
includes all the vertices along with the least possible number of edges. If any vertex is
missed, it is not a spanning tree. A spanning tree is a subset of the graph that does not have
cycles, and it also cannot be disconnected.

A spanning tree consists of (n-1) edges, where 'n' is the number of vertices (or nodes).
Edges of the spanning tree may or may not have weights assigned to them. All the possible
spanning trees created from the given graph G would have the same number of vertices, but
the number of edges in the spanning tree would be equal to the number of vertices in the
given graph minus 1.

A complete undirected graph can have nn-2 number of spanning trees where n is the
number of vertices in the graph. Suppose, if n = 5, the number of maximum possible
spanning trees would be 55-2 = 125.

Example of Spanning tree

Suppose the graph be -


As discussed above, a spanning tree contains the same number of vertices as the graph, the
number of vertices in the above graph is 5; therefore, the spanning tree will contain 5
vertices. The edges in the spanning tree will be equal to the number of vertices in the graph
minus 1. So, there will be 4 edges in the spanning tree.

Some of the possible spanning trees that will be created from the above graph are given as
follows -

Minimum Spanning tree


A minimum spanning tree can be defined as the spanning tree in which the sum of the
weights of the edge is minimum. The weight of the spanning tree is the sum of the weights
given to the edges of the spanning tree. In the real world, this weight can be considered as
the distance, traffic load, congestion, or any random value.

Example of minimum spanning tree

Let's understand the minimum spanning tree with the help of an example.
The sum of the edges of the above graph is 16. Now, some of the possible spanning trees
created from the above graph are -

So, the minimum spanning tree that is selected from the above spanning trees for the given
weighted graph is -
Algorithms for Minimum spanning tree

A minimum spanning tree can be found from a weighted graph by using the algorithms
given below -

○ Prim's Algorithm

○ Kruskal's Algorithm

Let's see a brief description of both of the algorithms listed above.

Prim's Algorithm

Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning tree
from a graph. Prim's algorithm finds the subset of edges that includes every vertex of the
graph such that the sum of the weights of the edges can be minimized.

Prim's algorithm starts with the single node and explores all the adjacent nodes with all the
connecting edges at every step. The edges with the minimal weights causing no cycles in
the graph got selected.

How does the prim's algorithm work?

Prim's algorithm is a greedy algorithm that starts from one vertex and continue to add the
edges with the smallest weight until the goal is reached. The steps to implement the prim's
algorithm are given as follows -

○ First, we have to initialize an MST with the randomly chosen vertex.

○ Now, we have to find all the edges that connect the tree in the above step with the
new vertices. From the edges found, select the minimum edge and add it to the tree.

○ Repeat step 2 until the minimum spanning tree is formed.

The applications of prim's algorithm are -

○ Prim's algorithm can be used in network designing.

○ It can be used to make network cycles.

○ It can also be used to lay down electrical wiring cables.


Example of prim's algorithm

Now, let's see the working of prim's algorithm using an example. It will be easier to
understand the prim's algorithm using an example.

Suppose, a weighted graph is -

Step 1 - First, we have to choose a vertex from the above graph. Let's choose B.

Step 2 - Now, we have to choose and add the shortest edge from vertex B. There are two
edges from vertex B that are B to C with weight 10 and edge B to D with weight 4. Among
the edges, the edge BD has the minimum weight. So, add it to the MST.
Step 3 - Now, again, choose the edge with the minimum weight among all the other edges.
In this case, the edges DE and CD are such edges. Add them to MST and explore the adjacent
of C, i.e., E and A. So, select the edge DE and add it to the MST.

Step 4 - Now, select the edge CD, and add it to the MST.
Step 5 - Now, choose the edge CA. Here, we cannot select the edge CE as it would create a
cycle to the graph. So, choose the edge CA and add it to the MST.

So, the graph produced in step 5 is the minimum spanning tree of the given graph. The cost
of the MST is given below -

Cost of MST = 4 + 2 + 1 + 3 = 10 units.


Algorithm

1. Step 1: Select a starting vertex


2. Step 2: Repeat Steps 3 and 4 until there are fringe vertices
3. Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has
minimum weight
4. Step 4: Add the selected edge and the vertex to the minimum spanning tree T
5. [END OF LOOP]
6. Step 5: EXIT

Complexity of Prim's algorithm

Now, let's see the time complexity of Prim's algorithm. The running time of the prim's
algorithm depends upon using the data structure for the graph and the ordering of edges.
Below table shows some choices -

○ Time Complexity

Data structure used for the minimum edge weight Time Complexity

Adjacency matrix, linear searching O(|V|2)

Adjacency list and binary heap O(|E| log |V|)

Adjacency list and Fibonacci heap O(|E|+ |V| log |V|)

Prim's algorithm can be simply implemented by using the adjacency matrix or adjacency
list graph representation, and to add the edge with the minimum weight requires the
linearly searching of an array of weights. It requires O(|V|2) running time. It can be
improved further by using the implementation of heap to find the minimum weight edges
in the inner loop of the algorithm.

The time complexity of the prim's algorithm is O(E logV) or O(V logV), where E is the no. of
edges, and V is the no. of vertices.
Implementation of Prim's algorithm

Now, let's see the implementation of prim's algorithm.

Program: Write a program to implement prim's algorithm in C language.

#include <stdio.h>

#include <limits.h>

#define vertices 5 /*Define the number of vertices in the graph*/

/* create minimum_key() method for finding the vertex that has minimum key-value and
that is not added in MST yet */
int minimum_key(int k[], int mst[])
{
int minimum = INT_MAX, min,i;

/*iterate over all vertices to find the vertex with minimum key-value*/
for (i = 0; i < vertices; i++)
if (mst[i] == 0 && k[i] < minimum )
minimum = k[i], min = i;
return min;
}
/* create prim() method for constructing and printing the MST.
The g[vertices][vertices] is an adjacency matrix that defines the graph for MST.*/
void prim(int g[vertices][vertices])
{
/* create array of size equal to total number of vertices for storing the MST*/
int parent[vertices];
/* create k[vertices] array for selecting an edge having minimum weight*/
int k[vertices];
int mst[vertices];
int i, count,edge,v; /*Here 'v' is the vertex*/
for (i = 0; i < vertices; i++)
{
k[i] = INT_MAX;
mst[i] = 0;
}
k[0] = 0; /*It select as first vertex*/
parent[0] = -1; /* set first value of parent[] array to -1 to make it root of MST*/
for (count = 0; count < vertices-1; count++)
{
/*select the vertex having minimum key and that is not added in the MST yet from the
set of vertices*/
edge = minimum_key(k, mst);
mst[edge] = 1;
for (v = 0; v < vertices; v++)
{
if (g[edge][v] && mst[v] == 0 && g[edge][v] < k[v])
{
parent[v] = edge, k[v] = g[edge][v];
}
}
}
/*Print the constructed Minimum spanning tree*/
printf("\n Edge \t Weight\n");
for (i = 1; i < vertices; i++)
printf(" %d <-> %d %d \n", parent[i], i, g[i][parent[i]]);

}
int main()
{
int g[vertices][vertices] = {{0, 0, 3, 0, 0},
{0, 0, 10, 4, 0},
{3, 10, 0, 2, 6},
{0, 4, 2, 0, 1},
{0, 0, 6, 1, 0},
};
prim(g);
return 0;
}

Output

So, that's all about the article. Hope, the article will be helpful and informative to you.
Kruskal's Algorithm

Kruskal's Algorithm is used to find the minimum spanning tree for a connected weighted
graph. The main target of the algorithm is to find the subset of edges by using which we can
traverse every vertex of the graph. It follows the greedy approach that finds an optimum
solution at every stage instead of focusing on a global optimum.

How does Kruskal's algorithm work?

In Kruskal's algorithm, we start from edges with the lowest weight and keep adding the
edges until the goal is reached. The steps to implement Kruskal's algorithm are listed as
follows -

○ First, sort all the edges from low weight to high.

○ Now, take the edge with the lowest weight and add it to the spanning tree. If the
edge to be added creates a cycle, then reject the edge.

○ Continue to add the edges until we reach all vertices, and a minimum spanning tree
is created.

The applications of Kruskal's algorithm are -

○ Kruskal's algorithm can be used to layout electrical wiring among cities.

○ It can be used to lay down LAN connections.


Example of Kruskal's algorithm

Now, let's see the working of Kruskal's algorithm using an example. It will be easier to
understand Kruskal's algorithm using an example.

Suppose a weighted graph is -

The weight of the edges of the above graph is given in the below table -

Edge AB AC AD AE BC CD DE

Weight 1 7 10 5 3 4 2

Now, sort the edges given above in the ascending order of their weights.

Edge AB DE BC CD AE AC AD

Weight 1 2 3 4 5 7 10

Now, let's start constructing the minimum spanning tree.

Step 1 - First, add the edge AB with weight 1 to the MST.


Step 2 - Add the edge DE with weight 2 to the MST as it is not creating the cycle.

Step 3 - Add the edge BC with weight 3 to the MST, as it is not creating any cycle or loop.
Step 4 - Now, pick the edge CD with weight 4 to the MST, as it is not forming the cycle.

Step 5 - After that, pick the edge AE with weight 5. Including this edge will create the cycle,
so discard it.

Step 6 - Pick the edge AC with weight 7. Including this edge will create the cycle, so discard
it.

Step 7 - Pick the edge AD with weight 10. Including this edge will also create the cycle, so
discard it.

So, the final minimum spanning tree obtained from the given weighted graph by using
Kruskal's algorithm is -

The cost of the MST is = AB + DE + BC + CD = 1 + 2 + 3 + 4 = 10.

Now, the number of edges in the above tree equals the number of vertices minus 1. So, the
algorithm stops here.
Algorithm

1. Step 1: Create a forest F in such a way that every vertex of the graph is a separate
tree.
2. Step 2: Create a set E that contains all the edges of the graph.
3. Step 3: Repeat Steps 4 and 5 while E is NOT EMPTY and F is not spanning
4. Step 4: Remove an edge from E with minimum weight
5. Step 5: IF the edge obtained in Step 4 connects two different trees, then add it to the
forest F
6. (for combining two trees into one tree).
7. ELSE
8. Discard the edge
9. Step 6: END

Complexity of Kruskal's algorithm

Now, let's see the time complexity of Kruskal's algorithm.

○ Time Complexity
The time complexity of Kruskal's algorithm is O(E logE) or O(V logV), where E is the
no. of edges, and V is the no. of vertices.
Shortest Paths and Transitive Closure:
Given a directed graph, find out if a vertex j is reachable from another vertex i for all vertex
pairs (i, j) in the given graph. Here reachable mean that there is a path from vertex i to j. The
reach-ability matrix is called the transitive closure of a graph.

Transitive closure of above graphs is

1111

1111

1111

0001

The graph is given in the form of adjacency matrix say ‘graph[V][V]’ where graph[i][j] is 1 if
there is an edge from vertex i to vertex j or i is equal to j, otherwise graph[i][j] is 0.

(or)

Shortest Path Transitive Closure Youtube link Check below video

Transitive Closure(explained simply)

Single Source All Destination - Dijkstra’s Algorithm is one example of a


single-source shortest or SSSP algorithm, i.e., given a source vertex it finds shortest path
from source to all other vertices.

Dijkstra’s algorithm is very similar to Prim’s algorithm for


minimum spanning tree. Like Prim’s MST, we generate a SPT (shortest path tree)
with a given source as a root.
The set sptSet is initially empty and distances assigned to vertices are {0, INF, INF, INF,
INF, INF, INF, INF} where INF indicates infinite. Now pick the vertex with a minimum
distance value. The vertex 0 is picked, include it in sptSet. So sptSet becomes {0}. After
including 0 to sptSet, update distance values of its adjacent vertices. Adjacent vertices
of 0 are 1 and 7. The distance values of 1 and 7 are updated as 4 and 8. The following
subgraph shows vertices and their distance values, only the vertices with finite distance
values are shown. The vertices included in SPT are shown in green colour.

Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET). The vertex 1 is picked and added to sptSet. So sptSet now becomes {0, 1}.
Update the distance values of adjacent vertices of 1. The distance value of vertex 2
becomes 12.
Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET). Vertex 7 is picked. So sptSet now becomes {0, 1, 7}. Update the distance
values of adjacent vertices of 7. The distance value of vertex 6 and 8 becomes finite (15
and 9 respectively).

Pick the vertex with minimum distance value and not already included in SPT (not in
sptSET). Vertex 6 is picked. So sptSet now becomes {0, 1, 7, 6}. Update the distance
values of adjacent vertices of 6. The distance value of vertex 5 and 8 are updated.

We repeat the above steps until sptSet includes all vertices of the given graph. Finally,
we get the following Shortest Path Tree (SPT).
Example 2:

For example,

Minimum Route
Vertex Cost

A —> B 4 A —> E —> B

A —> C 6 A —> E —> B —> C

A —> D 5 A —> E —> D

A —> E 3 A —> E

Dijkastra’s algorithm link: click below link

https://www.freecodecamp.org/news/dijkstras-shortest-path-algorithm-visual-introductio
n/

You might also like