You are on page 1of 49

Graph Data Structure

DR. SHWETA SHARMA


Graph
 A graph data structure is a collection of nodes that have data and are connected to other
nodes
 Example: On facebook, everything is a node. That includes User, Photo, Album, Event,
Group, Page, Comment, Story, Video, Link, Note...anything that has data is a node

 Every relationship is an edge from one node


to another. Whether you post a photo, join a
group, like a page, etc., a new edge is
created for that relationship

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 2


Graph Representation
 All of facebook is then a collection of these nodes and edges. This is
because facebook uses a graph data structure to store its data.

 More precisely, a graph is a data structure (V, E) that consists of:

 A collection of vertices V
 A collection of edges E

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 3


Types of Graph
 Undirected Graph: A graph in which the edges do not have any directions
associated with them, is known as an undirected graph.

 Directed Graph: A graph in which the edges have a particular direction


associated with them, is known as a directed graph.

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 4


Graph Terminology
 Adjacency: A vertex is said to be adjacent to another vertex if
there is an edge connecting them. Vertices 2 and 3 are not
adjacent because there is no edge between them
 Path: A sequence of edges that allows you to go from vertex
A to vertex B is called a path. 0-1, 1-2 and 0-2 are paths from
vertex 0 to vertex 2
 Directed Graph: A graph in which an edge (u,v) doesn't
necessarily mean that there is an edge (v, u) as well. The
edges in such a graph are represented by arrows to show the
direction of the edge
DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 5
Graph Representation
1. Adjacency Matrix

 An adjacency matrix is a 2D array of V x V vertices. Each row and column represent


a vertex
 If the value of any element a[i][j] is 1, it represents that there is an edge connecting
vertex i and vertex j
 Since it is an undirected graph, for edge (0,2), we also need to mark edge (2,0);
making the adjacency matrix symmetric about the diagonal.
 Edge lookup(checking if an edge exists between vertex A and vertex B) is extremely
fast in adjacency matrix representation but we have to reserve space for every
possible link between all vertices(V x V), so it requires more space
DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 6
Graph Representation
1. Adjacency Matrix

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 7


Graph Representation
1. Adjacency Matrix

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 8


Graph Representation
2. Adjacency List

 An adjacency list represents a graph as an array of linked lists

 The index of the array represents a vertex and each element in its linked list
represents the other vertices that form an edge with the vertex

 An adjacency list is efficient in terms of storage because we only need to


store the values for the edges. For a graph with millions of vertices, this can
mean a lot of saved space
DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 9
Graph Representation
2. Adjacency List

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 10


Graph Traversal

 Traversal means the process of visiting every node in the graph


 There are 2 standard methods of graph traversal:
 Breadth-First Search
 Depth First Search

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 11


Breadth-First Search (BFS)

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 12


1. Breadth-First Search (BFS)
 An algorithm used for traversing graphs or trees

 Expand all the nodes of one level first

 An iterative algorithm to search all the vertices of a graph


or a tree

 Breadth-First Search in a tree and graph is almost the


same. The only difference is that the graph may contain
cycles, so we may traverse to the same node again

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 13


Breadth-First Search (BFS)
 Example: Rubik’s Cube

 Rubik’s Cube is seen as searching for a path to convert it from a full mess of colors to a
single color

 Comparing the Rubik’s Cube to the graph:


the possible state of the cube is
corresponding to the nodes of the graph and
the possible actions of the cube is
corresponding to the edges of the graph

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 14


Breadth-First Search (BFS)
 BFS starts from a node, then it checks all the nodes at distance one
from the beginning node, then it checks all the nodes at distance
two, and so on

 So as to recollect the nodes to be visited, BFS uses a QUEUE

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 15


Breadth-First Search (BFS)

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 16


Breadth-First Search (BFS)

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 17


Breadth-First Search (BFS)

 As breadth-first search is the process of traversing each node of the


graph, a standard BFS algorithm traverses each vertex of the graph
into two parts: 1) Visited 2) Not Visited
 So, the purpose of the algorithm is to visit all the vertex while
avoiding cycles

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 18


Steps in BFS

1. Start by putting any one of the graph’s vertices at the back of the queue
2. Now take the front item of the queue and add it to the visited list
3. Create a list of that vertex's adjacent nodes. Add those which are not
within the visited list to the rear of the queue
4. Keep continuing steps two and three till the queue is empty

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 19


BFS pseudo code
create a queue Q

mark v as visited and put v into Q

while Q is non-empty

remove the head u of Q

mark and enqueue all (unvisited) neighbors of u

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 21


Example 1

Undirected graph with 5 vertices

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 22


Example 1 (Cont..)

 Source is chosen randomly (say, P)


 Begin from the vertex P, the BFS
algorithm starts by putting it
within the Visited list and puts all
its adjacent vertices (Q, R, and S)
in the queue

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 23


Example 1 (Cont..)
 Next, we have a tendency to visit
the part at the front of the queue
i.e. Q and visit its adjacent nodes
 Since P has already been visited,
we have a tendency to visit R
instead
 Note: R is in the front of the
QUEUE (FIFO)
DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 24
Example 1 (Cont..)

 Vertex R has an unvisited adjacent


vertex in T
 Thus we have a tendency to add
that to the rear of the queue and
visit S, which is at the front of the
queue

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 25


Example 1 (Cont..)

 Only T remains within the queue


since the only adjacent node of S
i.e. P is already visited.
 We have a tendency to visit it.

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 26


Example 1 (Cont..)

 Since the queue is empty, we've


completed the Traversal of the
graph

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 27


Question 1 on BFS Traversal

01234

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 28


Question 2 on BFS Traversal

A, B, C, D, E, F

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 29


Question 3 on BFS Traversal

2, 3, 0, 1
or
2, 0, 3, 1

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 30


Question 4 on BFS Traversal

R, S, V, W, T, X, U, Y

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 31


Depth-First Search (DFS)

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 32


BFS vs. DFS

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 33


Depth-First Search (DFS)

 Depth-first traversal or Depth-first Search is an


algorithm to look at all the vertices of a graph
or tree data structure

 Expand one of the nodes at the deepest level

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 34


Depth-First Search (DFS)
Example: Maze

We tend to take a route, keep going until we discover a


dead end. When touching the dead end, we again
come back and keep coming back till we see a path we
didn't attempt before

Take that new route. Once more keep going until we


discover a dead end. Take a come back again… This is
exactly how Depth-First Search works

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 35


Depth-First Search (DFS)
 The Depth-First Search is a recursive algorithm that uses the concept of
backtracking
 It involves thorough searches of all the nodes by going ahead if potential,
else by backtracking
 Here, the word backtrack means once you are moving forward and there
are not any more nodes along the present path, you progress backward on
an equivalent path to seek out nodes to traverse
 All the nodes are progressing to be visited on the current path until all the
unvisited nodes are traversed after which subsequent paths are going to
be selected
DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 36
DFS
 The recursive method of the Depth-First Search algorithm is
implemented using STACK

 A standard Depth-First Search implementation puts every vertex of


the graph into one in all 2 categories: 1) Visited 2) Not Visited

 The only purpose of this algorithm is to visit all the vertex of the
graph avoiding cycles

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 37


Steps in DFS
1. We will start by putting any one of the graph's vertex on top of the stack
2. After that take the top item of the stack and add it to the visited list of
the vertex
3. Next, create a list of that adjacent node of the vertex. Add the ones
which aren't in the visited list of vertexes to the top of the stack
4. Lastly, keep repeating steps 2 and 3 until the stack is empty

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 38


Example 1

Undirected graph with 5 vertices

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 39


Example 1 (Cont..)

 Source is chosen randomly (say, P)


 Begin from the vertex P, the DFS rule
starts by putting it within the Visited list
and putting all its adjacent vertices
within the stack

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 40


Example 1 (Cont..)

 Next, we tend to visit the part at the


highest of the stack i.e. Q, and head to
its adjacent nodes. Since P has already
been visited, we tend to visit R instead

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 41


Example 1 (Cont..)

 Vertex R has the unvisited adjacent


vertex in T, therefore we will be adding
that to the highest of the stack and visit
it

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 42


Example 1 (Cont..)

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 43


Example 1 (Cont..)

 At last, we will visit the last component


S, it does not have any unvisited
adjacent nodes, thus we've completed
the Depth First Traversal of the graph

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 44


Question 1 on DFS Traversal

01243

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 45


Question 2 on DFS Traversal

12453

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 46


Question 3 on DFS Traversal

01352476
or
02475136
or
02475361
Starting node: 0
DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 47
Question 4 on DFS Traversal

Ans: 0231

Starting node: 0
DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 48
Question 4 on DFS Traversal

Starting node: H
DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 49
Thank you!

DR. SHWETA SHARMA, DEPT. OF CSE, PEC CHANDIGARH 50

You might also like