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

Graph

Uploaded by

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

Graph

Uploaded by

sanmith.18171
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Graph

Module 5
Definition

Types of Graph

Representation Methods

• Adjacency Matrix
Introduction • Adjacency List

Traversal Methods

• Breadth First Search (BFS)


• Depth First Search (DFS)

Application

• Topological Sorting
Definition of
Graph
A graph (denoted as
G=(V,E)) consists of a non-
empty set of vertices or
nodes V and a set of edges E.
Let us consider, a Graph is
G=(V,E) where V={a,b,c,d}
and E={{a,b},{a,c},{b,c},
{c,d}}
Terminologies
• Degree of a Vertex
• The degree of a vertex V of a graph G (denoted by deg (V)) is the
number of edges incident with the vertex V.
• Even and Odd Vertex
• If the degree of a vertex is even, the vertex is called an even vertex and
if the degree of a vertex is odd, the vertex is called an odd vertex.
• Degree of a Graph
• The degree of a graph is the largest vertex degree of that graph. For the
above graph the degree of the graph is 3.
• Null Graph: A null graph has no
edges. The null graph of n vertices
Types of Graph is denoted by
• Simple Graph: A graph is called
simple graph/strict graph if the
graph is undirected and does not
contain any loops or multiple
edges.
𝑛
𝑁
Types of Graph
• Multi-Graph: If in a graph multiple edges between
the same set of vertices are allowed, it is called
Multigraph. In other words, it is a graph having at
least one loop or multiple edges.
Types of Graph
• Directed and Undirected Graph:
A graph G=(V,E) is called a directed graph if the
edge set is made of ordered vertex pair and a
graph is called undirected if the edge set is made
of unordered vertex pair.
Types of Graph
• Connected and Disconnected Graph:
A graph is connected if any two vertices of the
graph are connected by a path; while a graph is
disconnected if at least two vertices of the graph
are not connected by a path. If a graph G is
disconnected, then every maximal connected
subgraph of G is called a connected component
of the graph G.
Types of Graph
• Regular Graph: A graph is regular if all the
vertices of the graph have the same degree. In a
regular graph G of degree r, the degree of each
vertex of G is r.
Types of Graph
• Complete Graph: A graph is called complete
graph if every two vertices pair are joined by
exactly one edge. The complete graph with n
vertices is denoted by .
𝑛
𝐾
Types of Graph
• Cycle Graph: If a graph consists of a single cycle,
it is called cycle graph. The cycle graph with n
vertices is denoted by .
𝑛
𝐶
Types of Graph
• Bipartite Graph: If the vertex-set of a graph G
can be split into two disjoint sets, 1 and 2, in
such a way that each edge in the graph joins a
vertex in 1 to a vertex in 2, and there are no
edges in G that connect two vertices in 1 or two
vertices in 2, then the graph G is called a bipartite
graph.
𝑉
𝑉
𝑉
𝑉
𝑉
𝑉
Types of Graph
• Complete Bipartite Graph: A complete bipartite
graph is a bipartite graph in which each vertex in
the first set is joined to every single vertex in the
second set. The complete bipartite graph is
denoted by , where the graph G contains x
vertices in the first set and y vertices in the second
set.
𝑥
𝑦
𝐾
Types of Graph
• Planar graph − A graph G is called a planar graph
if it can be drawn in a plane without any edges
crossed. If we draw graph in the plane without
edge crossing, it is called embedding the graph in
the plane.
Types of Graph
• Non-planar graph − A graph is non-planar if it
cannot be drawn in a plane without graph edges
crossing.
Representation of Graph
There are mainly two ways to represent a graph −
• Adjacency Matrix
• Adjacency List
Adjacency Matrix
• An Adjacency Matrix A[V][V] is a 2D array of
size V×V where V is the number of vertices in a
undirected graph. If there is an edge between
to then the value of A[ ][ ]=1 and A[ ]
[ ]=1, otherwise the value will be zero. And for
a directed graph, if there is an edge between
to , then the value of A[ ][ ]=1, otherwise
the value will be zero.
𝑥
𝑦
𝑥
𝑦
𝑦
𝑥
𝑥
𝑦
𝑥
𝑦
𝑉
𝑉
𝑉
𝑉
𝑉
𝑉
𝑉
𝑉
𝑉
𝑉
Adjacency Matrix of Undirected Graph
a b c d

a 0 1 1 0

b 1 0 1 0

c 1 1 0 1

d 0 0 1 0
Adjacency Matrix for Directed Graph
a b c d

a 0 1 1 0

b 0 0 1 0

c 0 0 0 1

d 0 0 0 0
Adjacency List
• In adjacency list, an array (A[V]) of linked lists is
used to represent the graph G with V number of
vertices. An entry A[ ] represents the linked list
of vertices adjacent to the h vertex.
𝑥
𝑥
𝑉
𝑉
𝑡
Adjacency List of Undirected Graph
Graph Traversal Method (BFS)

Breadth-first search (BFS) is a graph search algorithm that begins at the root node and
explores all the neighboring nodes. Then for each of those nearest nodes, the algorithm
explores their unexplored neighbour nodes, and so on, until it finds the goal.

we need to track the neighbours of the node and guarantee that every node in the graph is
processed, and no node is processed more than once. This is accomplished by using a queue
Step 1: SET STATUS = 1 (ready state ) for each node in G
Step 2: Enqueue the starting node A and set its STATUS =
2( waiting state)
Step 3: Repeat step 3 and 4 until queue is empty BFS
Step 4: Dequeue a node N. process it and set its
STATUS =3 (Processed state)
Methodology
Step 5: Enqueue all the neighbors of N that are in the
ready state ( whose STATUS = 1) and set their
STATUS = 2 (waiting state)
[END OFLOOP]
Step 6: EXIT
Example
FRONT = 0 QUEUE = A
REAR = 0 ORG = \0
Example
FRONT = 1 QUEUE = A B C D
REAR = 3 ORG = \0 A A A
Example
FRONT = 1 QUEUE = A B C D
REAR = 3 ORG = \0 A A A
Example
FRONT = 3 QUEUE = A B C D E G
REAR = 5 ORG = \0 A A A B C
Example
FRONT = 4 QUEUE = A B C D E G
REAR = 5 ORG = \0 A A A B C
Example
FRONT = 5 QUEUE = A B C D E G F
REAR = 6 ORG = \0 A A A B C E
As I is
destination
STOP

Example
FRONT = QUEUE = A B C D E G F H I
6
REAR = 9 ORG = \0 A A A B C E G G
Example
FRONT = QUEUE = A B C D E G F H I
6
REAR = 9 ORG = \0 A A A B C E G G

A C  G  I
BFS Application

Finding the shortest path


Finding all connected
in a weighted directed or
nodes.
undirected graph.
Graph Traversal Method (DFS)

The depth-first search algorithm progresses by expanding the starting node of G and then
going deeper and deeper until the goal node is found, or until a node that has no children is
encountered.

When a dead-end is reached, the algorithm backtracks, returning to the most recent node
that has not been completely explored. This is accomplished by using a stack.
Step SET STATUS = 1 (ready state ) for each node in G
1:
Step Push the starting node A on to the stack and set its
2: STATUS = 2 ( waiting state)
Step
3:
Repeat step 4 and 5 until stack is empty DFS
Step
4:
Pop the top node N. process it and set its STATUS =3
(Processed state)
Methodology
Step Push on the stack all the neighbors of N that are in the
5: ready state ( whose STATUS = 1) and set their STATUS
= 2 (waiting state)
[END OFLOOP]

Step EXIT
6:
Example
Example
STACK H

PRINT
Example
STACK E I

PRINT H
Example
STACK E F

PRINT H I
Example
STACK E C

PRINT H I F
Example
STACK E B G

PRINT H I F C
Example
STACK E B

PRINT H I F C G
Example
STACK E

PRINT H I F C G B
Example
STACK

PRINT H I F C G B E
STOP

Example
FINAL PRINT H, I , F, C, G, B, E
BFS Application

Finding whether the


Computing path
graph is connected or
between any two nodes.
not
Topological sort of a directed acyclic graph (DAG) G is
defined as a linear ordering of its nodes in which each
node comes before all nodes to which it has outbound
edges.

A topological sort of a DAG G is an ordering of the


vertices of G such that if G contains an edge (u, v),
then u appears before v in the ordering.
Topological
Sorting Note that topological sort is possible only on directed
acyclic graphs that do not have any cycles.

Topological sorting is widely used in scheduling


applications, jobs, or tasks.
Topological Sorting
STEP 1: Find the in-degree INDEG(N) of every node in the graph
STEP 2: Enqueue all the nodes with a zero in-degree.
STEP 3: Repeat step 4 and 5 until the QUEUE is empty
STEP 4: Remove the front node N of the QUEUE by setting FRONT = FRONT + 1

STEP 5: Repeat for each neighbor M of node N:


a) Delete the edge from N to M by setting INDEG(M) = INDEG(M) – 1
b)If INDEG(M) = 0, then enqueue M, that is , add M to the rear of the queue.
[END OF INNER LOOP] [END OF LOOP]
STEP 6: EXIT
Topological Sorting
Adjacency List
C A: B
B: C, D, E
A B E
C: E
D D: E

E: F
G F D
G:
In-deg A= 0 B=1 C=1 D=2 E=3 F=1 G=0
FRONT:= 1 REAR:= 2 QUEUE:= A, G
Topological Sorting
Adjacency List
C A: B
B: C, D, E
A B E
C: E
Remove A D E
D:
FRONT = FRONT + 1 F
G E:
Also update In-degree F D
G:
In-deg A= 0 B= C=1 D=2 E=3 F=1 G=0
FRONT:= 2 REAR:= 2 QUEUE:= G, B PRINT:= A
Topological Sorting
Adjacency List
C A: B
B: C, D, E
A B E
C: E
Remove G D E
D:
FRONT = FRONT + 1 F
G E:
Also update In-degree F D
G:
In-deg A= 0 B= C=1 D=1 E=3 F=1 G=0
FRONT:= 3 REAR:= 3 QUEUE:= B PRINT:= A,G
Topological Sorting
Adjacency List
C A: B
B: C, D, E
A B E
C: E
Remove B D E
D:
FRONT = FRONT + 1 F
G E:
Also update In-degree F D
G:
In-deg A= 0 B= C=0 D=0 E=3 F=1 G=0
FRONT:= 4 REAR:= 5 QUEUE:= C, D PRINT:= A,G,B
Topological Sorting
Adjacency List
C A: B
B: C, D, E
A B E
C: E
Remove C D E
D:
FRONT = FRONT + 1 F
G E:
Also update In-degree F D
G:
In-deg A= 0 B= C=0 D=0 E=1 F=1 G=0
FRONT:= 5 REAR:= 5 QUEUE:= D PRINT:= A,G,B,C
Topological Sorting
Adjacency List
C A: B
B: C, D, E
A B E
C: E
Remove D D E
D:
FRONT = FRONT + 1 F
G E:
Also update In-degree F D
G:
In-deg A= 0 B= C=0 D=0 E=0 F=1 G=0
FRONT:= 6 REAR:= 6 QUEUE:= E PRINT:= A,G,B,C,D
Topological Sorting
Adjacency List
C A: B
B: C, D, E
A B E
C: E
Remove E D E
D:
FRONT = FRONT + 1 F
G E:
Also update In-degree F D
G:
In-deg A= 0 B= C=0 D=0 E=0 F=0 G=0
FRONT:= 7 REAR:= 7 QUEUE:= F PRINT:= A,G,B,C,D,E
Topological Sorting
Adjacency List
C A: B
B: C, D, E
A B E
C: E
D D: E
Remove F F
G E:
F D
G:
In-deg A= 0 B= C=0 D=0 E=0 F=0 G=0
FRONT:= -1 REAR:= -1 QUEUE:= PRINT:= A,G,B,C,D,E,F
Topological Sorting

The topological sorting of given graph is:


A, G, B, C, D, E, F

A G B C D E F

You might also like