In this chapter we have to Learn Graph Terminologies, Types of Graph, Representation of Graph, Traversal of Graph BFS and DFS. Its Application Use Advantages and Disadvantages
2. Graph Terminologies
By Prof. Raj Sarode 2
Graph is a non Linear Data structure which is a collection of two finite sets of V & E
Where V is vertices(node) and E is Edges (arcs)
i.e. G = (V, E)
Where V = {A, B, C, D, E, F} &
E = { (A, B}, (A,C), (B,C), (B,D), (C,D), (C,E), (D,E), (D,F), (E,F)}
The pair (A,B) defines an edge between A and B.
AE.g. B D
C E F
The number of vertices of graph constitute the order of graph. Hence In given
Example the order of graph is Six
3. By Prof. Raj Sarode 3
Graph
1. Adjacent vertices: Two vertices are said to be adjacent if they are connected by an
edge.
e.g. B & C
2. Adjacent Edges: Two edges are said to be adjacent if they are incident on the common
vertex.
e.g. CD & CE.
3. Path: A Path is a sequence of distinct vertices in which each vertex is adjacent to next
one.
e.g. ABCDEF
4. Cycle: A cycle is a path consisting of at least three vertices where last vertex is adjacent
to the first vertex.
e.g. C D F E C
5. Loop: It is special cycle that start and end with same vertex without visiting any other
vertex.
6. Degree of Vertex: The degree of a vertex is the number of lines incident on it
e.g. degree of D is 4
7. Out-Degree: The out Degree of directed graph is the number of arcs leaving the vertex.
8. In-Degree: In degree of directed graph is number of arcs entering the vertex.
9. Multiple Edges: The distinct parallel edges that connect the same end point.
4. Types of Graph
By Prof. Raj Sarode 4
1. Simple Graph: A graph that does not contain any loop or multiple edges
B
C E
A D
2. Multi graph: A graph that contain more then one edges between two vertices
B
C E
A D
5. Types of Graph
By Prof. Raj Sarode 5
3. Directed Graph: A graph with directed edges is called directed graph
B
C E
A D
4. Undirected Graph: A graph with undirected edges is called undirected graph
B
C E
A D
6. Types of Graph
By Prof. Raj Sarode 6
5. Weighted Graph: A graph with Weight (value) assign to edges is called weighted graph
6. Regular Graph: A graph in which each vertex has same degree is called regular graph
B
C D
A
B
C E
A D2 5
79
6
4
Note: k is degree of each vertex
hence it is called k regular graph
7. Types of Graph
By Prof. Raj Sarode 7
7. Complete Graph: A graph in which each vertex is connected to every other vertices
called complete graph
8. Strongly connected Graph: In Directed graph each vertex is connected to every
other vertices called Strongly connected graph
B
C D
A
B
C D
A
8. Types of Graph
By Prof. Raj Sarode 8
9. Weakly connected Graph: In Directed graph weakly connected if at least two
vertices are not connected.
B
C D
A
10. Isomorphic Graph: Two graph in which there is one to one correspondence
between their vertices and their edges is said to be isomorphic graph .
B
C D
A
F
E
G H
9. Representation of Graph
By Prof. Raj Sarode 9
1. Sequential Representation using adjacency matrix
B
C E
A D
A
B
C
D
E
A
0
1
1
0
0
B
1
0
1
1
0
C
1
1
0
0
1
D
0
1
0
0
1
E
0
0
1
1
0
Adjacency Matrix of
Undirected Graph
B
C E
A D
A
B
C
D
E
A
0
0
1
0
0
B
1
0
1
0
0
C
0
0
0
0
0
D
0
1
0
0
1
E
0
0
1
0
0
Adjacency Matrix of
Directed Graph
10. Representation of Graph
By Prof. Raj Sarode 10
2. Linked Representation using adjacency List
B
C E
A DB
C E
A D
Name
Link to
next node
Link to another list
of adjacent node
Structure of Node List
Adjacent
node
Link
Structure of Edge List
A
B
C
D
E
Node List
B C X
A C D X
A B E X
B E X
C D X
A
B
C
D X
E
Node List
B X
D X
A B E X
D X
Adjacency List of undirected Graph Adjacency List of directed Graph
E.g. 1 E.g. 2
11. Traversal of Graph
By Prof. Raj Sarode 11
1. Breadth First Search ( BFS)
•First we visit the starting vertex and then all the vertices adjacent to starting
vertex.
•After this we pick these adjacent vertices one by one and visit their adjacent
vertices and this process goes on.
•In BFS we use the concept of Queue & any vertex will be in one of the three state-
(initial, waiting, visited),
The procedure for BFS is as follow
1. Initially the queue is empty, and all the vertices are in initial state.
2. Insert the starting vertex into the queue, change its state to waiting.
3. Delete front element from queue and visit it, change its state to visited.
4. Look for adjacent vertices of deleted element, and insert only those vertices
into queue which are in the initial state.
5. Change the state of all inserted vertices from initial to waiting.
6. Repeat step 3,4,5 until the queue is empty.
12. Traversal of Graph
By Prof. Raj Sarode 12
2. Depth First Search ( DFS)
•First we visit the starting vertex and we will pick up any path that start from
starting vertex and visit all the vertices in the path till Dead end occur
•In DFS we use the concept of Stack & any vertex will be in one of the two state-
(initial, visited),
The procedure for DFS is as follow
1. Initially the Stack is empty, and all the vertices are in initial state.
2. PUSH starting vertex into the stack.
3. POP a vertex from stack.
4. If the Popped vertex is in initial state, visit it and change its state to visited
5. PUSH all the unvisited vertex adjacent to the popped vertex.
6. Repeat step 3,4,5 until the stack is empty.