Professional Documents
Culture Documents
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 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
edges.
Example
The following is a graph with 5 vertices and 6 edges.
Vertex
Individual data element of a graph is called as Vertex. Vertex is also known as node. In
Edge
An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is
between vertices A and B is represented as (A,B). In above example graph, there are 7 edges
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.
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
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.
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:
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 directed graph representation implemented using
linked list...
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)
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.
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.
Some of the possible spanning trees that will be created from the above graph are given as
follows -
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
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.
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 -
○ 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.
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.
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 -
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
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
#include <stdio.h>
#include <limits.h>
/* 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.
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 -
○ 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.
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.
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
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 -
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
○ 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.
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)
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 —> E 3 A —> E
https://www.freecodecamp.org/news/dijkstras-shortest-path-algorithm-visual-introductio
n/