Graph
Graph
A graph can be defined as group of vertices and edges that are used to connect these vertices. A graph can be
seen as a cyclic tree, where the vertices (Nodes) maintain any complex relationship among them instead of
having parent child relationship.
Definition
A graph G can be defined as an ordered set G(V, E) where V(G) represents the set of vertices and E(G)
represents the set of edges which are used to connect these vertices.
A Graph G(V, E) with 5 vertices (A, B, C, D, E) and six edges ((A,B), (B,C), (C,E), (E,D), (D,B), (D,A)) is
shown in the following figure.
Graph Terminology
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
(starting Vertex, ending Vertex). 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,D), (B,D), (B,C), (B,D), (E,D), (C,E)).
Path
A path can be defined as the sequence of nodes that are followed in order to reach some terminal node V from
the initial node U.
Closed Path
A path will be called as closed path if the initial node is same as terminal node. A path will be closed path if
V0=VN.
Simple Path
If all the nodes of the graph are distinct with an exception V0=VN, then such path P is called as closed simple
path.
Cycle
A cycle can be defined as the path which has no repeated edges or vertices except the first and last vertices.
Connected Graph
A connected graph is the one in which some path exists between every two vertices (u, v) in V. There are no
isolated nodes in connected graph.
Complete Graph
A complete graph is the one in which every node is connected with all other nodes. A complete graph contain
n(n-1)/2 edges where n is the number of nodes in the graph.
Weighted Graph
In a weighted graph, each edge is assigned with some data such as length or weight. The weight of an edge e
can be given as w(e) which must be a positive (+) value indicating the cost of traversing the edge.
Digraph
A digraph is a directed graph in which each edge of the graph is associated with some direction and the
traversing can be done only in the specified direction.
Loop
An edge that is associated with the similar end points can be called as Loop.
Adjacent Nodes
If two nodes u and v are connected via an edge e, then the nodes u and v are called as neighbors or adjacent
nodes.
Degree of the Node
A degree of a node is the number of edges that are connected with that node. A node with degree 0 is called as
isolated node.
In degree
Total number of incoming edges connected to a vertex is said to be indegree of that vertex.
Out degree
Total number of outgoing edges connected to a vertex is said to be outdegree of that vertex.
A vertex in an undirected connected graph is an articulation point (or cut vertex) iff removing it (and edges
through it) disconnects the graph. Articulation points represent vulnerabilities in a connected network – single
points whose failure would split the network into 2 or more disconnected components. They are useful for
designing reliable networks.
Graph Representation
There are two ways to store Graph into the computer's memory.
1) Sequential Representation
a) Adjacency Matrix
b) Incidence Matrix
2) Linked Representation
a) Adjacency List
Sequential Representation
in sequential representation, we use adjacency matrix to store the mapping represented by vertices and edges. In
adjacency matrix, the rows and columns are represented by the graph vertices. A graph having n vertices, will
have a dimension n x n.
An entry Mij in the adjacency matrix representation of an undirected graph G will be 1 if there exists an edge
between Vi and Vj.An undirected graph and its adjacency matrix representation is shown in the following figure.
in the above figure, we can see the mapping among the vertices (A, B, C, D, E) is represented by using the
adjacency matrix which is also shown in the figure.
There exists different adjacency matrices for the directed and undirected graph. In directed graph, an entry
Aij will be 1 only when there is an edge directed from Vi to Vj.
A directed graph and its adjacency matrix representation is shown in the following figure.
Representation of weighted directed graph is different. Instead of filling the entry by 1, the Non- zero entries of
the adjacency matrix are represented by the weight of respective edges.
The weighted directed graph along with the adjacency matrix representation is shown in the following figure.
Linked Representation
In the linked representation, an adjacency list is used to store the Graph into the computer's memory.
Consider the undirected graph shown in the following figure and check the adjacency list representation.
An adjacency list is maintained for each node present in the graph which stores the node value and a pointer to
the next adjacent node to the respective node. If all the adjacent nodes are traversed then store the NULL in the
pointer field of last node of the list. The sum of the lengths of adjacency lists is equal to the twice of the number
of edges present in an undirected graph.
Consider the directed graph shown in the following figure and check the adjacency list representation of the
graph.
In a directed graph, the sum of lengths of all the adjacency lists is equal to the number of edges present in the
graph.
In the case of weighted directed graph, each node contains an extra field that is called the weight of the node.
The adjacency list representation of a directed graph is shown in the following figure.
Incidence Matrix
In this representation, the graph is represented using a matrix of size total number of vertices by a total number
of edges. That means graph with 4 vertices and 6 edges is represented using a matrix of size 4X6. In this matrix,
rows represent vertices and columns represents edges. This matrix is filled with 0 or 1 or -1. Here, 0 represents
that the row edge is not connected to column vertex, 1 represents that the row edge is connected as the outgoing
edge to column vertex and -1 represents that the row edge is connected as the incoming edge to column vertex.
Example :
Consider the graph G along with its adjacency list, given in the figure below. Calculate the order to print all the
nodes of the graph starting from node H, by using depth first search (DFS) algorithm.
Solution :
Step 4:
Pop the top element of the stack i.e. D, print it ,status of D is now 3and push all the neighbours of D onto the
stack that are in ready statei.e F.status of F is now 2
Print D
Stack : B, F
Step6:
Pop the top of the stack i.e. B, print it, status of B is now 3and push all the neighbours. The neighbours of B is C
and F, the status of F is 3 so we can’t push in to stack. Where status of C is 1 so we push C in to stack and status
of C become 2
Print B
Stack : C
Step7:
Pop the top of the stack i.e. C print it, status of C is now 3and push all the neighbours.The neighbours of C is E,
G, H, the status of H is now 3 so we can’t push in to stack. Where status of E&G is 1 so we push E&G in to
stack and status of E&G become 2
Print C
Stack : E, G
Step8:
Pop the top of the stack i.e. G,print it, status of G is now 3 and push all its neighbours. The neighbours of G is
E, H, the status of H is 3 and E is 2 so we can’t push in to stack.
Print G
Stack : E
Print E
Stack :
Algorithm
Space Complexity
Since we are maintaining a priority queue (FIFO architecture) to keep track of
the visited nodes, in worst case, the queue could take upto the size of the
nodes(or vertices) in the graph. Hence, the space complexity is O(V).
Example :
Consider the graph G along with its adjacency list, given in the figure below. Calculate the order to print all the
nodes of the graph starting from node H, by using breadth first search (BFS) algorithm.
Solution:
Let us assume, starting node is H
Step1.
Insert H onto the queue and status of H become 2
QUEUE : H
Status 1: A B C D E F G Status 2 H Status 3
Step2.
Delete the element of the queue i.e. H, print it, status of H is now 3 and Insert all the neighbours of H onto the
queue that are is ready state i.e A. status of A is now 2
Print H
QUEUE: _, A
Status 1: B C D E F G Status 2 A Status 3 H
Step3.
Delete the element of the queue i.e. A, print it, status of A is now 3 and Insert all the neighbours of A onto the
queue that are in ready state i.e B,D. the status of B&D are 2
Print A
QUEUE : _, _,B, D
Status 1: C E F G Status 2 B D Status 3 H A
Step 4:
Delete the element of the queue i.e. B, print it , status of B is now 3and Insert all the neighbours of B onto the
queue that are in ready state i.e C &F. status of C&F is now 2
Print B
QUEUE : _,_,_,D, C ,F
Step6:
Delete the element of the queue i.e. C, print it, status of C is now 3and Insert all the neighbours. The neighbours
of C is E,G and H, the status of H is3 so we can’t Insert in to queue. Where status of E&G is 1 so we Insert
E&G in to queue and status of E&G become 2
Print C
QUEUE : _,_,_,_, _ ,F,E,G
Step7:
Delete the element of the queue i.e. F print it, status of F is now 3and Insert all the neighbours. The neighbours
of F is A. the status of A is 3 so we can’t Insert in to queue.
Print F
QUEUE : _,_,_,_, _ ,_,E, G
Step8:
Delete the element of the queue i.e. E, print it, status of E is now 3 and Insert all its neighbours. The neighbours
of E is B, F, the status of B&F is 3 so we can’t Insert in to queue.
Print E
QUEUE : _,_,_,_, _ ,_,_, G
Status 1: Status 2 G Status 3 H A B D C F E
Step9:
Delete the element of the queue i.e. G, print it and Insert all its neighbours. The neighbours of G is E&H, the
status of E&H is 3 so we can’t Insert in to queue.
Print G
QUEUE: _,_,_,_, _ ,_,_,_
Spanning Tree
Spanning tree can be defined as a sub-graph of connected, undirected graph G that is a tree produced by
removing the desired number of edges from a graph. In other words, Spanning tree is a non-cyclic sub-
graph of a connected and undirected graph G that connects all the vertices together. A graph G can have
multiple spanning trees.
There are two algorithms which are being used for this purpose.
o Prim's Algorithm
o Kruskal's Algorithm
Prim's Algorithm
o Prim's Algorithm 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.
o Prim's algorithm starts with the single node and explore 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.
o Prim’s Algorithm is a famous greedyalgorithm. (Greedy is an algorithmic paradigm that builds up a
solution piece by piece, always choosing the next piece that offers the most obvious and immediate
benefit. So the problems where choosing locally optimal also leads to global solution are best fit for
Greedy.)
Algorithm
Step-01:
Randomly choose any vertex.
The vertex connecting to the edge having least weight is usually selected.
Step-02:
Find all the edges that connect the tree to new vertices.
Find the least weight edge among those edges and include it in the existing tree.
If including that edge creates a cycle, then reject that edge and look for the next least weight edge.
Step-03:
Keep repeating step-02 until all the vertices are included and Minimum Spanning Tree (MST) is
obtained.
Problem-01:
Step-01: Step-02:
Step-03: Step-04:
Step-05: Step-06:
The above discussed steps are followed to find the minimum cost spanning tree using Prim’s Algorithm-
Since all the vertices have been included in the MST, so we stop.
Now, Cost of Minimum Spanning Tree
= Sum of all edge weights
= 10 + 25 + 22 + 12 + 16 + 14
= 99 units
Problem-02:
Using Prim’s Algorithm, find the cost of minimum spanning tree (MST) of the given graph-
Solution-
The minimum spanning tree obtained by the application of Prim’s Algorithm on the given graph is as
shown below-
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.
Kruskal's algorithm follows greedy approach which finds an optimum solution at every stage instead of
focusing on a global optimum.
To apply Kruskal’s algorithm, the given graph must be weighted, connected and undirected.
Kruskal’s Algorithm
Step-01:
Sort all the edges from low weight to high weight.
Step-02:
Take the edge with the lowest weight and use it to connect the vertices of graph.
If adding an edge creates a cycle, then reject that edge and go for the next least weight edge.
Step-03:
Keep adding edges until all the vertices are connected and a Minimum Spanning Tree (MST) is obtained.
Problem-01:
Construct the minimum spanning tree (MST) for the given graph using Kruskal’s Algorithm-
Solution-
To construct MST using Kruskal’s Algorithm,
Simply draw all the vertices on the paper.
Connect these vertices using edges with minimum weights such that no cycle gets formed.
Step-01:
Step-02:
Step-03:
Step-04:
Step-05:
Step-06:
Step-07:
Since all the vertices have been connected / included in the MST, so we stop.
Weight of the MST
= Sum of all edge weights
= 10 + 25 + 22 + 12 + 16 + 14
= 99 units
The tree that we are making or growing always The tree that we are making or growing usually remains
remains connected. disconnected.
Prim’s Algorithm grows a solution from a random Kruskal’s Algorithm grows a solution from the cheapest
vertex by adding the next cheapest vertex to the edge by adding the next cheapest edge to the existing
existing tree. tree / forest.
Here, both the algorithms on the above given graph produces the same MST as shown
Note 2:
If all the edge weights are not distinct, then both the algorithms may not always produce the same MST.
However, cost of both the MSTs would always be same in both the cases.
Example-
In general, Shortest path problem is a problem of finding the shortest path(s) between vertices or nodes in the
given graph.
Shortest path between two vertices is a path that has the least cost as compared to all other paths that exists in
the graph.
Dijkstra’s Algorithm is one of the very famous greedy algorithms.
Conditions for Dijkstra’s Algorithm-
1. Dijkstra’s algorithm works only for those graphs that are connected.
2. Dijkstra’s algorithm works only for those graphs that do not contain any edges with negative weights.
3. The actual Dijkstra’s algorithm does not output the shortest paths. It only provides the value or cost of the
shortest paths.
4. By making minor modifications in the actual algorithm, the shortest paths can be easily obtained.
5. Dijkstra’s algorithm works for directed as well as undirected graphs.
The Procedure is as –
1.Initially make source node permanent and make it the current working node.All other nodes are made
temporary
2.Examine all the temporary neighbours of the current working node and after checking the condition for
minimum weight , relabel the required nodes.
3.From all the temporary nodes,find out the node which has minimum value of dist , make this node permanent
and now this is our current working node.
4Repeat steps 2 and 3 until destination node is made permanent.
Relaxation of a node: the update the shortest path of a node is called Relaxation. Suppose u is the current
vertex and v is the neighbor vertex ,where c(u,v) is the cost of the path from u to v or this is weight of the path
from u to v. d(u) is distance from source to u. d(v) is the distance from source v.
Now if d(u)+c(u,v)<d(v) is true then we have to update the value of d(v) that is called relaxation
So if d(u)+c(u,v)<d(v) then new value of d(v)= d(u)+c(u,v) else the value of d(v) remain same.
u=a, v=b, d(u)=2,d(v)=8, c(u,v)=4. Now d(u)+c(u,v)<d(v) is true[2+4=6<8] so new value of d(v)=6
Working Example of Dijkstra's Algorithm
In the above section, you have gained the step by step process of Dijkstra’s algorithm, now let’s study the
algorithm with an explained example.
We will calculate the shortest path between node C and the other nodes in the graph.
During the execution of the algorithm, each node will be marked with its minimum distance to node C as we
have selected node C.
In this case, the minimum distance is 0 for node C. Also, for the rest of the nodes, as we don’t know this
distance, they will be marked as infinity (∞), except node C (currently marked as red dot).
Since, all the neighbours of node C have checked, so node C is marked as visited with a green check mark.
For node B, we add 1 with 3 (weight of the edge connecting node A to B) and obtain 4. This value, 4, will be
compared with the minimum distance of B, 7, and mark the lowest value at B as 4.
node |predecessor
a | c
d | c
b | a
Since it doesn’t have any unvisited neighbours, so there is not any requirement to check anything. Node E is
marked as a visited node with a green mark.
node |predecessor
a | c
d | c
b | a
e | b
Marked Node E as visited
So, we are done as no unvisited node is left. The minimum distance of each node is now representing the
minimum distance of that node from node C. node |predecessor
Now you want to find out the shortest path from c to e . a | c
1)now we try to find out predecessor of e i.e b so b->e d | c
2) next we try to find out predecessor of b i.e a so path is a->b->e b | a
3) next we try to find out predecessor of a i.e c so path is c->a->b->e e | b
Shortest path c->a->b->e And value is 1+3+1=5[c->a=1,a->b=3,b->e=1]
Note : we always start from destination and find out the predecessor of destination or current node.this process
will continue until you reach at source.
Applications of Dijkstra’s Algorithm
For map applications, it is hugely deployed in measuring the least possible distance and check direction
amidst two geographical regions like Google Maps, discovering map locations pointing to the vertices of a
graph, calculating traffic and delay-timing, etc.
For telephone networks, this is also extensively implemented in the conducting of data in networking and
telecommunication domains for decreasing the obstacle taken place for transmission.
Wherever addressing the need for shortest path explications either in the domain of robotics, transport,
embedded systems, laboratory or production plants, etc, this algorithm is applied.
Besides that, other applications are road conditions, road closures and construction, and IP routing to
detect Open Shortest Path First.
In any directed graph which has no cycle, topological sort gives the sequential order of all the nodes x, y є G
And x comes before y in sequential order if a path exists from x to y. So this sequential order will indicate the
dependency of one task on another.
Adjacency list of G will be –
A <-> B, F
B -> E, F
C -> B, D
D -> B, E
E ->
F ->
G -> E, F
Now we have a need to find the way to get topological sorting. We should first take those nodes which have no
predecessors. It can be chosen very easily. The nodes which have topological sorting can be define as –
1. Take all the nodes which have Zero in degree.
2. Delete those nodes and edges going from those nodes.
3. Do the same process again until all the nodes are deleted.
This operation will need a queue where all the nodes which have in degree zero will be first added then deleted.
Every time we need to get the information of in degree of every node. One array is also required where all the
nodes will be in sequence of deletion from queue. Let us take a graph and find the topological sorting-
Here we are taking a queue for maintaining the zero in degree elements and array topSort for sequence of
deleted elements from queue.
Step1-
In degree of nodes are-A=0,B=3,C=0,D=1,E=3,F=3,G=0
Step2-
i)Taking all the nodes, which have zero in degree.
A,C,G
ii)Add all zero in degree nodes to queue
Queue: A,C,G Front=0 , Rear=2 topSort:
iii) Delete the node A and edges going from A.
Queue: C,G Front=1 , Rear=2 topSort:A
iv)Now the in degree of nodes will be-
B=2,D=1,E=3,F=2
Step3-
i)Delete the node C and edges going from C
Queue: G Front=2 , Rear=2 topSort:A,C
ii)Now the in degree of nodes will be-
B=1,D=0,E=3,F=2
Step4-
i)Add D to queue
Queue: G,D Front=2 , Rear=3 topSort:A,C
ii) Delete the node G and edges going from G.
Queue: D Front=3 , Rear=3 topSort:A,C,G
iv)Now the in degree of nodes will be-
B=1,E=2,F=1
Step5-
i) Delete the node D and edges going from D.
Queue: Front=4 , Rear=3 topSort:A,C,G,D
ii)Now the in degree of nodes will be-
B=0,E=1,F=1
Step6-
i)Add B to queue
Queue: B Front=4 , Rear=4 topSort:A,C,G,D
ii) Delete the node B and edges going from B.
Queue: Front=5 , Rear=4 topSort:A,C,G,D,B
iii)Now the in degree of nodes will be-
E=0,F=1
Step7-
i)Add E to queue
Queue: E Front=5 , Rear=5 topSort:A,C,G,D,B
ii) Delete the node E and edges going from E.
Queue: Front=6 , Rear=5 topSort:A,C,G,D,B,E
iii)Now the in degree of nodes will be-
F=0
Step8-
i)Add F to queue
Queue: F Front=6 , Rear=6 topSort:A,C,G,D,B,E
ii) Delete the node F and edges going from F.
Queue: Front=7 , Rear=6 topSort:A,C,G,D,B,E,F
Now we have no more nodes in the graph. So the topological sorting of graph will be-
A,C,G,D,B,E,F
Algorithm name Time complexity Space complexity
Average Worst Worst
Dijkstra algorithm O(|E|*log |V|) O(|V|2) O(|V|+|E|)
Prims Algorithm O(|E|*log |V|) O(|V|2) O(|V|+|E|)
Krushkal algorithm O(|E|*log |V|) O(|E|*log |V|) O(|V|+|E|)
Topological Sort O(|V|+|E|) O(|V|+|E|) O(|V|+|E|)