Cit0663 Chapter 8 Graph Data Structure
Cit0663 Chapter 8 Graph Data Structure
v={ ? }
E={ ? }
Graph Data Structure
- Mathematical graphs can be represented in data structure.
- We can represent a graph using an array of vertices and a two-
dimensional array of edges.
Before we proceed further, let's familiarize ourselves with some
important terms:-
a) Adjacency
− Two node or vertices are adjacent if they are
connected
to each other through an edge.
- In the following example, B is adjacent to A, C is
b) Path
adjacent to B, and so on.
− Path represents a sequence of edges between the two
vertices.
- In the following example, ABCD represents a path from
A to D.
Graph Data Structure
(cont..)
c) Vertex
− Each node of the graph is represented as a vertex.
- In the following example, the labeled circle represents
vertices. Thus, A to G are vertices.
- We can represent them using an array as shown in the
following image. Here A can be identified by index 0. B can
be identified using index 1 and so on.
d) Edge
− Edge represents a path between two vertices or a line
between two vertices. In the following example, the lines
from
A to B, B to C, and so on represents edges.
- We can use a two-dimensional array to represent an array
- Here AB can be represented as 1 at row 0, column 1, BC as
1
at row 1, column 2 and so on, keeping other combinations
as 0. [Apply in Adjacaceny Matrix]
GRAPH DATA STRUCTURE
Most popular method
Undirected Graph
Example:
1 3 } No edge between node
12@21
j
Adjacency Matrix
i
- It is a matrix A [ n ][ n ]
where n is number of vertices
n would be for row & column
- A[i][j] = 1 if i & j are
adjacency
= 0 or otherwise
Question..
ANSWER ??
?
Adjacency List
- Going to have Linked List
- How many Linked List would be there
for each Vertex One Linked would be
maintain.
1, 2, 3, 4, 5
Question..
ANSWER ??
?
Adjacency List
Linked List
BFS DFS
BREADTH FIRST DEPTH FIRST
SEARCH SEARCH
[ QUEUE ] [ STACK ]
BREADTH FIRST SEARCH
[BFS]
Breadth First Traversal
Breadth First Search (BFS) algorithm traverses a graph in a
Breadthward motion and uses a queue to remember to get the
next vertex to start a search, when a dead end occurs in any
iteration.
As in the example given above, BFS
algorithm traverses from S to A to B to E to
F first then to C and G lastly to D. It
employs the following rules.
RESULT: 0
Breadth First Search [BFS]
5. Node 1 already visited then
remove
in graph.
QUEUE 0 1 3 2 5 6
RESULT: 0 1 3
Breadth First Search [BFS]
8. Node 3 already visited then
remove in graph.
QUEUE 0 1 3 2 5 6 4
RESULT: 0 1 3 2
Breadth First Search [BFS]
11. Node 2 already visited then
remove in graph.
QUEUE 0 1 3 2 5 6 4
RESULT: 0 1 3 2 5
Breadth First Search [BFS]
14. Node 5 already visited then
remove in graph.
QUEUE 0 1 3 2 5 6 4
RESULT: 0 1 3 2 5 6
Breadth First Search [BFS]
17. Node 6 already visited then
remove in graph.
Node 1 Visited
Node 4 In Queue
QUEUE 0 1 3 2 5 6 4
RESULT: 0 1 3 2 5 6 4
EXAMPLE:
EXAMPLE:
DEPTH FIRST SEARCH
[DFS]
Depth First Traversal
Depth First Search (DFS) algorithm traverses a graph in a
Depthward motion and uses a STACK (LIFO) to remember to get
the next vertex to start a search, when a dead end occurs in any
iteration.
As in the example given above, DFS
algorithm traverses from S to A to D to G
to E to B first, then to F and lastly to C. It
employs the following rules.
Step of Depth First
Traversal
1 3
Initialize
the stack.
4
Step of Depth First
Traversal
5 6
7
Depth First Search [DFS]
{1,3}
- Taken any traversing any
node
- Choose 0 as a ROOT NODE
1. Insert 0 into stack [PUSH]
2. Result: 0 will printed
3. Then, find Adjacent Vertices
with 0
1, 3 Choose 1
RESULT: 0
Depth First Search [DFS]
{0,2,3,5,6
1. 1 , 3 } Choose 1 1 }
{1,
then PUSH 1 into stack
3}
{0,1,2,4}
{0,1,2,4} 4 {1,3,4,
5}
5
{2,3,6}
{2,3,6} {1,4}
RESULT: 0 1 3 2 4
3. Result : Will print 6
6
Depth First Search [DFS]
- No unvisited adjacent for 6 1 {0,2,3,5,6
- Then, do the Backtracking
}
process
2
Backtracking Process
3
Do the POP to enter the unvisited
Node 5 POP [6]
{0,1,2,4} 4 {1,3,4,
5}
5
{2,3,6} {1,4}
RESULT: 0 1 3 2 4
6
Depth First Search [DFS]
Pop [6] check element in stack before 6 is
4
POP [ 4 ]
Then check it is have the unvisited
Adjacency Vertices NO
RESULT: 0 1 3 2 4
6
Depth First Search [DFS]
Pop [4] check element in stack before 4 is
2
POP [ 2 ]
Then check the unvisited Adjacency Vertices
of 2 is {1,3,4,5} { 5 } Unvisited
PUSH [ 5 ]
RESULT: 0 1 3 2 4 6
5
Depth First Search [DFS]
Then check the unvisited Adjacency Vertices of
5
is {1,2 } Already visited
POP [ 5 ]
RESULT: 0 1 3 2 4 6
5
Depth First Search [DFS]
Then check the unvisited Adjacency Vertices of
5
is {1,2 } Already visited
POP [ 5 ]
RESULT: 0 1 3 2 4 6
5
Depth First Search [DFS]
Then check the unvisited Adjacency Vertices of
2
is {1,3,4,5 } Already visited
POP [ 2]
RESULT: 0 1 3 2 4 6
5
Depth First Search [DFS]
Then check the unvisited Adjacency Vertices of
3
is {0,1,2,4 } Already visited
POP [ 3]
RESULT: 0 1 3 2 4 6
5
Depth First Search [DFS]
Then check the unvisited Adjacency Vertices of
3
is {0,1,2,4 } Already visited
POP [ 3]
RESULT: 0 1 3 2 4 6
5
Depth First Search [DFS]
Then check the unvisited Adjacency Vertices of
1
is {0,2,3,5,6 } Already visited
POP [ 1]
RESULT: 0 1 3 2 4 6
5
Depth First Search [DFS]
Then check the unvisited Adjacency Vertices of
0
is { 1,3 } Already visited
POP [ 0]
RESULT: 0 1 3 2 4 6
5
Depth First Search [DFS]
RESULT: 0 1 3 2 4 6 5
EXAMPLE:
EXAMPLE: