0% found this document useful (0 votes)
23 views28 pages

Graphs

The document provides an overview of graph data structures, including definitions of vertices, edges, and types of graphs. It explains graph representations such as adjacency matrix, incidence matrix, and adjacency list, as well as traversal algorithms like Depth First Search (DFS) and Breadth First Search (BFS). Additionally, it highlights the differences between BFS and DFS in terms of data structure usage, traversal approach, and suitable applications.

Uploaded by

jiyanshi021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views28 pages

Graphs

The document provides an overview of graph data structures, including definitions of vertices, edges, and types of graphs. It explains graph representations such as adjacency matrix, incidence matrix, and adjacency list, as well as traversal algorithms like Depth First Search (DFS) and Breadth First Search (BFS). Additionally, it highlights the differences between BFS and DFS in terms of data structure usage, traversal approach, and suitable applications.

Uploaded by

jiyanshi021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

GRAPH DATA STRUCTURE

GRAPH
Graph is a non linear data structure,
a set of points known as nodes (or vertices)
set of links known as edges (or Arcs)
Graph is a collection of nodes and edges which connects
nodes in the graph
Generally, a graph G is represented as
G=(V,E)
where V is set of vertices and E is set of edges.
EXAMPLE
The following is a graph with 5 vertices and 6 edges.
This graph G can be defined as
G = ( V , E )Where
V = {A,B,C,D,E} and
E ={(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.
GRAPH TERMINOLOGY
1. Vertex
An individual data element of a graph is called as Vertex. Vertex is also
known as node. In above example graph, A, B, C, D & E are known as
vertices.
2. Edge
An edge is a connecting link between two vertices. Edge is also known as
Arc. An edge is represented as (starting Vertex, ending Vertex).
In above example graph, there are 7 edges (i.e., (A,B), (A,C), (A,D), (B,D),
(B,E), (C,D), (D,E)).
Edges are three types.
 Undirected Edge : (A,B) is equal to edge (B,A). Bidirectional
 Directed Edge : (A,B) is not equal to edge (B,A). Unidirectional
 Weighted Edge -An edge with cost on it.
GRAPH TERMINOLOGY
3. Undirected Graph
A graph with only undirected edges
4. Directed Graph
A graph with only directed edges
5. Mixed Graph
A graph with undirected and directed edges
6. Source and Destination
If an edge is directed, its first endpoint is origin and the other
endpoint is destination of the edge.
GRAPH TERMINOLOGY
7. Incoming Edge & Outgoing Edge
A directed edge is said to be outgoing edge on its origin vertex
and incoming edge on its destination vertex.
8. Degree
Total number of edges connected to a vertex
 In degree& Out degree
Total number of incoming edges connected to a vertex is said to
be in degree of that vertex and total number of outgoing edges
is said to be out degree of that vertex.
9. Path
A path is a sequence of alternating vertices and edges that
starts at a vertex and ends at another vertex
GRAPH REPRESENTATIONS
1. Adjacency Matrix
2. Incidence Matrix
3. Adjacency List
ADJACENCY MATRIX
In this representation, graph can be represented using a matrix of size V×V
No matter how few edges the graph has, the matrix takes O(n2) in memory
ADJACENCY MATRIX
Directed graph representation
INCIDENCE MATRIX
In this representation, graph can be represented using a matrix of size
V×E.
This matrix is filled with either 0 or 1 or -1. Here, 0 represents row edge
not connected to vertex, 1 as outgoing edge and -1 as incoming edge
ADJACENCY LIST
Using linked list
It can take up to O(n) time to determine if a pair of nodes (A,B) is an edge

Using array
DEPTH FIRST SEARCH
DFS traversal of a graph, produces a spanning tree as
final result.
Spanning Tree is a graph without any loops. Spanning
tree having all the vertices and connecting edges
without any cycle.
We use Stack data structure with maximum size of total
number of vertices in the graph.
Backtracking is coming back to the vertex from which we
came to current vertex.
ALGORITHM FOR DFS
Step 1:Define a Stack of size total number of vertices
Step 2:Select any vertex as starting point. Visit that vertex and push it on
to the Stack.
Step 3:Visit any one of the adjacent vertex of the vertex which is at top of
the stack which is not visited and push it on to the stack.
Step 4:Repeat step 3 until there are no new vertex to be visit from the
vertex on top of the stack.
Step 5:When there is no new vertex to be visit then use backtracking and
pop one vertex from the stack.
Step 6:Repeat steps 3, 4 and 5 until stack becomes Empty. When stack
becomes Empty, then produce final spanning tree by removing unused
edges from the graph
DEPTH FIRST SEARCH EXAMPLE
DEPTH FIRST SEARCH EXAMPLE
We start from vertex 0, the DFS algorithm starts by putting it in the
Visited list and putting all its adjacent vertices in the stack.
DEPTH FIRST SEARCH EXAMPLE
Next, we visit the element at the top of stack i.e. 1 and go to its
adjacent nodes. Since 0 has already been visited, we visit 2 instead.
DEPTH FIRST SEARCH EXAMPLE
Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the top
of the stack and visit it.
DEPTH FIRST SEARCH EXAMPLE
Next, we visit the element at the top of stack i.e. 4 and go to its adjacent
nodes. Since 2 has already been visited, we visit top of stack instead.
DEPTH FIRST SEARCH EXAMPLE
After we visit the last element 3, it doesn't have any unvisited adjacent
nodes, so we have completed the Depth First Traversal of the graph.
BREADTH FIRST SEARCH TRAVERSAL
BFS traversal of a graph, produces a spanning
tree as final result.
Spanning Tree is a graph without any loops.
We use Queue data structure with maximum
size of total number of vertices in the graph
ALGORITHM FOR BFS
Step 1: start traversing the given graph with the given
number of vertices and edges.
Step 2: insert the starting source vertex S to the queue
and mark it as visited.
Step 3: Repeat
Remove the front node K of the queue and examine it.
Insert all the adjacent nodes of K( which are not visited so
far) to the queue and mark them as visited.
Step 4: Until queue is empty.
Step 5: Exit.
BFS EXAMPLE
BFS EXAMPLE
We start from vertex 0, the BFS algorithm starts by putting it in the
Visited list and putting all its adjacent vertices in the queue.
BFS EXAMPLE
Next, we visit the element at the front of queue i.e. 1 and go to its
adjacent nodes. Since 0 has already been visited, we visit 2 instead.
BFS EXAMPLE
Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the
back of the queue and visit 3, which is at the front of the queue.
BFS EXAMPLE
Next, we visit the element at the front of queue i.e. 3 and go to its
adjacent nodes. Since 0 has already been visited, we visit 4, which is at
the front of the queue.
BFS EXAMPLE
Only 4 remains in the queue since the only adjacent node of 3 i.e. 0 is
already visited. We visit it.
Since the queue is empty, we have completed the Breadth First
Traversal of the graph.
DIFFERENCE BETWEEN BFS AND DFS
Parameters BFS DFS
Stands for BFS stands for Breadth First Search. DFS stands for Depth First Search.
BFS(Breadth First Search) uses Queue data
Data Structure structure for finding the shortest path.
DFS(Depth First Search) uses Stack data structure.

BFS is a traversal approach in which we first DFS is also a traversal approach in which the traverse begins at
Definition walk through all nodes on the same level the root node and proceeds through the nodes as far as
before moving on to the next level. possible until we reach the node with no unvisited nearby nodes.

Conceptual
BFS builds the tree level by level. DFS builds the tree sub-tree by sub-tree.
Difference
Approach It works on the concept of FIFO (First In First
It works on the concept of LIFO (Last In First Out).
used Out).
BFS is more suitable for searching vertices
Suitable for closer to the given source.
DFS is more suitable when there are solutions away from source.

DFS is used in various applications such as acyclic graphs and


BFS is used in various applications such as
Applications bipartite graphs, shortest paths, etc
finding strongly connected components, Topological Sorting,
Cycle Detection, etc.

You might also like