Unit 5 Graphs
Unit 5 Graphs
It is a connecting link between two nodes or vertices. Each edge has two ends and
Edge (Arc)
is represented as (startingVertex, endingVertex).
Self-loop An edge is called a self-loop if its two endpoints coincide with each other.
Types of Graph
1.Finite Graph
2. Infinite Graph
3. Trivial Graph
5. Multi Graph
6. Null Graph
8. Complete Graph
9. Weighted Graph
10. Digraph
If an Undirected 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 Vi to Vj} aij = 0
{Otherwise}
It means that, in an adjacency matrix, 0 represents that there is no association exists between the
nodes, whereas 1 represents the existence of a path between two edges. If there is no self-loop
present in the graph, it means that the diagonal entries of the adjacency matrix will be 0.
Graph traversal
Graph traversal is a technique used for searching a vertex in a graph. Using graph traversal we can visit all the
vertices of the graph without getting into looping path.
BFS, Breadth First Search traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph without
loops. BFS puts every vertex of the graph into two categories - visited and non-visited. It is a vertex-based technique
for finding the shortest path in the graph. It uses a Queue data structure that follows first in first
out. In BFS, one vertex is selected at a time when it is visited and marked then its adjacent are
visited and stored in the queue. It is slower than DFS.
DFS, Depth First Search, is an edge-based technique. It uses the Stack data structure and
performs two stages, first visited vertices are pushed into the stack, and second if there are no
vertices then visited vertices are popped.
Example for BFS
Q S A B C D E F G
Example for DFS
S. No.
Parameters BFS DFS
1. Stands for BFS stands for Breadth First Search. DFS stands for Depth First Search.
BFS(Breadth First Search) uses Queue data
2. Data Structure DFS(Depth First Search) uses Stack data structure.
structure for finding the shortest path.
DFS is also a traversal approach in which the traverse
BFS is a traversal approach in which we
begins at the root node and proceeds through the nodes
3. Definition first walk through all nodes on the same
as far as possible until we reach the node with no
level before moving on to the next level.
unvisited nearby nodes.
It works on the concept of FIFO (First In
4. Approach used It works on the concept of LIFO (Last In First Out).
First Out).
Here, siblings are visited before the
5. Visit of Siblings/ Children Here, children are visited before the siblings.
children.
DFS algorithm is a recursive algorithm that uses the idea
6 Backtracking In BFS there is no concept of backtracking.
of backtracking
BFS is used in various applications such as DFS is used in various applications such as acyclic
7 Applications
bipartite graphs, shortest paths, etc. graphs and topological order etc.
8 Memory BFS requires more memory. DFS requires less memory.
9 Optimality BFS is optimal for finding the shortest path. DFS is not optimal for finding the shortest path.
10 Speed BFS is slow as compared to DFS. DFS is fast as compared to BFS.