GRAPH
Introduction
Graph
Group of vertices and edges.
It’s a cyclic tree, where
the vertices (Nodes) maintain any complex
relationship among them instead of having
parent child relationship.
Graph
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.
Graph
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
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 V 0=VN.
Graph Terminology
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.
Graph Terminology
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.
Graph Terminology
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.
Graph Terminology
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.
Graph Terminology
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.
Types of Graph
Directed Graph:
In a directed graph, edges form an ordered
pair. Edges represent a specific path from
some vertex A to another vertex B. Node A is
called initial node while node B is called
terminal node.
Types of Graph
Directed Graph:
A directed graph is shown in the following
figure.
Types of Graph
Undirected Graph:
In an undirected graph, edges are not associated with
the directions with them. An undirected graph is
shown in the above figure since its edges are not
attached with any of the directions. If an edge exists
between vertex A and B then the vertices can be
traversed from B to A as well as A to B.
Types of Graph
Undirected Graph:
A undirected graph is shown in the following
figure.
Graph Representation
A graph is a data structure that consist a sets of
vertices (called nodes) and edges. There are two
ways to store Graphs into the computer's memory:
Sequential representation (or, Adjacency matrix
representation)
Linked list representation (or, Adjacency list
representation)
Graph Representation
In sequential representation, an adjacency matrix is
used to represent the mapping between vertices
and edges of the graph.
An adjacency matrix can be used to represent the
Undirected graph, directed graph, weighted directed
graph, and weighted undirected graph also.
Graph Representation
If an Undirected/Directed Graph G consists of
n vertices, then the adjacency matrix for that
graph is n x n, and the matrix A = [aij] can be
defined as -
aij = 1 {if there is a path exists from V i to Vj}
aij = 0 {Otherwise}
Graph Representation
Adjacency matrix representation of an
undirected graph.
Graph Representation
Adjacency matrix representation of an directed
graph.
Graph Representation
Adjacency matrix representation for weighted
directed graph. If adj[i][j] = w, it means that
there is an edge exists from vertex i to vertex
j with weight w.
Adjacency matrix (Pros & Cons)
Adjacency matrix is easier to implement and follow.
An adjacency matrix can be used when the graph is
dense and a number of edges are large.
Consumes more space.
Even if the graph is sparse, the matrix still
consumes the same space.
Linked list representation
An adjacency list is used in the linked
representation to store the Graph in the
computer's memory. It is efficient in terms of
storage as we only have to store the values
for edges.
Linked list representation
Let's see the adjacency list representation of
an undirected graph.
Linked list representation
In the above figure, there is a linked list or adjacency list for every node
of the graph.
From vertex A, there are paths to vertex B and vertex D. These nodes
are linked to nodes A in the given adjacency list.
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 the last node of the list.
The sum of the lengths of adjacency lists is equal to twice the number of
edges present in an undirected graph.
Linked list representation
Let's see the adjacency list representation of an
directed graph.
For a directed graph, the sum of the lengths of
adjacency lists is equal to the number of edges
present in the graph.
Linked list representation
Let's see the adjacency list representation of an weighted
directed graph.
In the case of a weighted directed graph, each node
contains an extra field that is called the weight of the node.
In an adjacency list, it is easy to add a vertex. Because of
using the linked list, it also saves space.
Graph Traversals
Traversal means visiting all the nodes of a
graph. Common approaches to traverse the
graph are:
BFS (Breadth-first search )
DFS (Depth-first search )
BFS (Breadth-first search )
Breadth First Traversal or Breadth First
Search is a recursive algorithm for searching
all the vertices of a graph or tree data
structure.
BFS algorithm
A standard BFS implementation puts each vertex of
the graph into one of two categories:
Visited
Not Visited
The purpose of the algorithm is to mark each vertex
as visited while avoiding cycles.
BFS algorithm
The algorithm works as follows:
Start by putting any one of the graph's
vertices at the back of a queue.
Take the front item of the queue and add it to
the visited list.
BFS algorithm
Create a list of that vertex's adjacent nodes. Add the ones
which aren't in the visited list to the back of the queue.
Keep repeating steps 2 and 3 until the queue is empty.
The graph might have two different disconnected parts so
to make sure that we cover every vertex, we can also run
the BFS algorithm on every node
Example of BFS algorithm
In the example given below, there is a
directed graph having 7 vertices.
Example of BFS algorithm
In the above graph, minimum path 'P' can be
found by using the BFS that will start from Node
A and end at Node E.
The algorithm uses two queues, namely QUEUE1
and QUEUE2. QUEUE1 holds all the nodes that
are to be processed, while QUEUE2 holds all the
nodes that are processed and deleted from
QUEUE1.
Example of BFS algorithm
Now, let's start examining the graph starting from Node
A
Step 1 - First, add A to queue1 and NULL to queue2.
QUEUE1 = {A}
QUEUE2 = {NULL}
Step 2 - Now, delete node A from queue1 and add it
into queue2. Insert all neighbors of node A to queue1.
QUEUE1 = {B, D}
QUEUE2 = {A}
Example of BFS algorithm
Step 3 - Now, delete node B from queue1 and add
it into queue2. Insert all neighbors of node B to
queue1.
QUEUE1 = {D, C, F}
QUEUE2 = {A, B}
Step 4 - Now, delete node D from queue1 and add
it into queue2. Insert all neighbors of node D to
queue1. The only neighbor of Node D is F since it
is already inserted, so it will not be inserted again.
QUEUE1 = {C, F}
QUEUE2 = {A, B, D}
Example of BFS algorithm
Step 5 - Delete node C from queue1 and add it into
queue2. Insert all neighbors of node C to queue1.
QUEUE1 = {F, E, G}
QUEUE2 = {A, B, D, C}
Step 6 - Delete node F from queue1 and add it into
queue2. Insert all neighbors of node F to queue1. Since all
the neighbors of node F are already present, we will not
insert them again.
QUEUE1 = {E, G}
QUEUE2 = {A, B, D, C, F}
Example of BFS algorithm
Step 7 - Delete node E from queue1. Since
all of its neighbors have already been added,
so we will not insert them again. Now, all the
nodes are visited, and the target node E is
encountered into queue2.
QUEUE1 = {G}
QUEUE2 = {A, B, D, C, F, E}
BFS Algorithm Applications
To build index by search index
For GPS navigation
Path finding algorithms
In Ford-Fulkerson algorithm to find maximum
flow in a network
Cycle detection in an undirected graph
In minimum spanning tree
DFS (Depth-first search )
Depth first Search or Depth first traversal is
a recursive algorithm for searching all the
vertices of a graph or tree data structure.
DFS (Depth-first search )
A standard DFS implementation puts each vertex
of the graph into one of two categories:
Visited
Not Visited
The purpose of the algorithm is to mark each
vertex as visited while avoiding cycles.
DFS (Depth-first search )
The DFS algorithm works as follows:
Start by putting any one of the graph's vertices
on top of a stack.
Take the top item of the stack and add it to the
visited list.
Create a list of that vertex's adjacent nodes. Add
the ones which aren't in the visited list to the top
of the stack.
Example of DFS algorithm
Now, let's understand the working of the DFS
algorithm by using an example. In the
example given below, there is a directed
graph having 7 vertices.
Example of DFS algorithm
Now, let's start examining the graph starting from Node H.
Step 1 - First, push H onto the stack.
STACK: H
Step 2 - POP the top element from the stack, i.e., H, and print
it. Now, PUSH all the neighbors of H onto the stack that are in
ready state.
Print: H
STACK: A
Example of DFS algorithm
Step 3 - POP the top element from the stack, i.e., A, and print it. Now,
PUSH all the neighbors of A onto the stack that are in ready state.
Print: A
STACK: B, D
Step 4 - POP the top element from the stack, i.e., D, and print it. Now,
PUSH all the neighbors of D onto the stack that are in ready state.
Print: D
STACK: B, F
Example of DFS algorithm
Step 5 - POP the top element from the stack, i.e., F, and
print it. Now, PUSH all the neighbors of F onto the stack
that are in ready state.
Print: F
STACK: B
Step 6 - POP the top element from the stack, i.e., B, and
print it. Now, PUSH all the neighbors of B onto the stack
that are in ready state.
Print: B
STACK: C
Example of DFS algorithm
Step 7 - POP the top element from the stack, i.e., C, and
print it. Now, PUSH all the neighbors of C onto the stack that
are in ready state.
Print: C
STACK: E, G
Step 8 - POP the top element from the stack, i.e., G and
PUSH all the neighbors of G onto the stack that are in ready
state.
Print: G
STACK: E
Example of DFS algorithm
Step 9 - POP the top element from the stack, i.e., E and
PUSH all the neighbors of E onto the stack that are in
ready state.
Print: E
STACK:
Now, all the graph nodes have been traversed, and the stack
is empty.
Application of DFS Algorithm
For finding the path
To test if the graph is bipartite
For finding the strongly connected
components of a graph
For detecting cycles in a graph
Implementation of Graph
Implementation of adjacency matrix representati
on of
Graph
Implementation of BFS of Graph
Implementation of DFS of Graph