0% found this document useful (0 votes)
13 views

Unit 5 Graphs

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Unit 5 Graphs

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Unit 5

Graphs - Graph terminology;


Breadth First Traversal; Depth First
Traversal.
Graph
Definition A graph can be defined as group of vertices and edges that
are used to connect these vertices. A graph can be seen as a cyclic tree.
A graph G can be defined as an ordered set G(V, E) where V(G) represents
the set of vertices and E(G) represents the set of edges which are used to
connect these vertices. A graph can be directed or undirected. In a
directed graph, edges form an ordered pair. Edges represent a specific
path from some vertex A to another vertex B. Node A is called initial node
while node B is called terminal node. However, in an undirected graph,
edges are not associated with the directions with them. An undirected
graph is shown in the above figure since its edges are not attached with
any of the
A Graph directions.
G(V, E) with 5 vertices (A,
B, C, D, E) and six edges ((A,B),
(B,C), (C,E), (E,D), (D,B), (D,A)) is
shown in the following figure.
Graph
Terminologies
Vertex Every individual data element is called a vertex or a node.

It is a connecting link between two nodes or vertices. Each edge has two ends and
Edge (Arc)
is represented as (startingVertex, endingVertex).

Undirected Edge It is a bidirectional edge.


Directed Edge It is a unidirectional edge.

Weighted Edge An edge with value (cost) on it.

Degree The total number of edges connected to a vertex in a graph.

Indegree The total number of incoming edges connected to a vertex.

Outdegree The total number of outgoing edges connected to a vertex.

Self-loop An edge is called a self-loop if its two endpoints coincide with each other.
Types of Graph
1.Finite Graph

The graph G=(V, E) is called a finite graph if the number


of vertices and edges in the graph is limited in number

2. Infinite Graph

The graph G=(V, E) is called a finite graph if the number


of vertices and edges in the graph is interminable.

3. Trivial Graph

A graph G= (V, E) is trivial if it contains only a single


vertex and no edges
4. Simple Graph

If each pair of nodes or vertices in a graph G=(V, E) has only one


edge, it is a simple graph.

5. Multi Graph

If there are numerous edges between a pair of vertices in a graph


G= (V, E), the graph is referred to as a multigraph. There are no
self-loops in a Multigraph.

6. Null Graph

It's a reworked version of a trivial graph. If several vertices but no


edges connect them, a graph G= (V, E) is a null graph.
7. Connected Graph

If there is a path between one vertex of a graph data structure and


any other vertex, the graph is connected.

8. Complete Graph

If a graph G= (V, E) is also a simple graph, it is complete. Using the


edges, with n number of vertices must be connected. It's also known
as a full graph because each vertex's degree must be n-1.

9. Weighted Graph

A graph G= (V, E) is called a labeled or weighted graph because


each edge has a value or weight representing the cost of traversing
that edge.

10. Digraph

A directed graph also referred to as a digraph, is a set of nodes


connected by edges, each with a direction.
Ways of representing a graph in the data
An adjacencystructure.
matrix is used to represent the mapping between vertices and edges of the graph.
We can use an adjacency matrix to represent the undirected graph and directed graph
If adj[i][j] = w, it means that there is an edge exists from vertex i to vertex j with weight w.
An entry Aij in the adjacency matrix representation of an undirected graph G will be 1 if an edge
exists between Vi and Vj.

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.

There are two graph traversal techniques

1.BFS (Breadth First Search)


2.DFS (Depth First Search)

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

Algorithm BFS (graph G, int start_node)

Step 1: create a queue Q and set start_node as visited.


Step 2: Q.enqueue(start_node)
Step 3: while (Q is not empty)
curr_node = Q.dequeue( )
for all neighbours adj_node of curr_node in Graph G
if adj_node is not visited
set Q.enqueue(adj_node) and set adj_node as
visited
Step 4: End for While loop
Step 5: End DFS

Q S A B C D E F G
Example for DFS

Algorithm DFS (Vertex V)

Step 1: Algorithm DFS(vertex v)


Step 2: visited[v] = 1
Step 3: for all nodes ‘w’ adjacent to v:
if(visited[w] == 0):
then DFS(w)
C Step 4: End for loop
F Step 5: End DFS
B
E
G
D
A
Stack S
Difference between BFS and 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.

You might also like