Exploring Graphs
Exploring Graphs
EXPLORING GRAPHS
A NON-PRIMITIVE NON-LINEAR DATA STRUCTURE
Prepared By,
Shraddha Modi
Assistant Professor
CE Dept, LDCE
TOPICS TO BE COVERED
Introduction
Traversing Graphs
Topological sort,
Connected components
Prepared By,
Shraddha Modi(LDCE)
APPLICATIONS
Prepared By,
Shraddha Modi(LDCE)
INTRODUCTION
Let V is a set of nodes (vertices points), E is a set of edges
Then, a graph G consists of set of vertices V, set of edges E and
a mapping from E to a set of pairs of elements of V
Directed Graph
G = (V,E)
Any two nodes connected by an edge in the graph are called
adjacent nodes.
In a graph, if an edge is directed from one node to another Undirected
node, then it is called directed edge. Graph
Mixed Graph
p q
Prepared By,
Shraddha Modi(LDCE)
GRAPH : VARIOUS TERMINOLOGIES
If there is no more than one edge between each pair of nodes, (no more
than one directed edge in case of directed graph) then the graph is called a
simple graph.
A node which is not adjacent to any other node, is called an isolated node.
A graph with all the isolated nodes is called a null graph. (set of edges in a
null graph is empty)
Prepared By,
Shraddha Modi(LDCE)
GRAPH : VARIOUS TERMINOLOGIES
P
In a directed graph,
for any node p, the total number of edges for which p is
the initial node, is called the outdegree of the node p. Indegree of
node p is 3
The total number of edges for which p is the terminal
node, is called the indegree of node p. P
The sum of outdegree and indegree of a node p is the total
degree of that node.
Outdegree of
node p is 2
In an undirected graph,
Total Degree of
The total degree (or degree) of a node q is equal to the node P
total number of edges incident with node q. =5
2 4 2 4 P3
2 4
1 P1 1
1
6 6
P2 6
3 5 3 5 3 5
Different Paths in a Diagraph
Prepared By,
Shraddha Modi(LDCE)
GRAPH : VARIOUS TERMINOLOGIES
A path that originates and ends in the same node is called a
cycle (circuit).
A cycle is called elementary if it does not traverse through any
node more than once (except the initiating node which is the
ending node also).
A simple digraph which does not have any cycles is called
acyclic graph.
2 4
Some of cycles are : P2
1 P1
C1=((2,5), (5,3), (3,1), (1,2)) 6
3 5
C2=((4,4))
Prepared By,
Shraddha Modi(LDCE)
TRAVERSING GRAPH – BFS, DFS
Stack: [A, B, C]
Stack: [A, B, E]
Prepared By,
Shraddha Modi(LDCE)
BREADTH-FIRST SEARCH: EXAMPLE
r s t u
v w x y
Prepared By,
Shraddha Modi(LDCE)
BREADTH-FIRST SEARCH: EXAMPLE
r s t u
0
v w x y
Q: s
Prepared By,
Shraddha Modi(LDCE)
BREADTH-FIRST SEARCH: EXAMPLE
r s t u
1 0
1
v w x y
Q: w r
Prepared By,
Shraddha Modi(LDCE)
BREADTH-FIRST SEARCH: EXAMPLE
r s t u
1 0 2
1 2
v w x y
Q: r t x
Prepared By,
Shraddha Modi(LDCE)
BREADTH-FIRST SEARCH: EXAMPLE
r s t u
1 0 2
2 1 2
v w x y
Q: t x v
Prepared By,
Shraddha Modi(LDCE)
BREADTH-FIRST SEARCH: EXAMPLE
r s t u
1 0 2 3
2 1 2
v w x y
Q: x v u
Prepared By,
Shraddha Modi(LDCE)
BREADTH-FIRST SEARCH: EXAMPLE
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: v u y
Prepared By,
Shraddha Modi(LDCE)
BREADTH-FIRST SEARCH: EXAMPLE
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: u y
Prepared By,
Shraddha Modi(LDCE)
BREADTH-FIRST SEARCH: EXAMPLE
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: y
Prepared By,
Shraddha Modi(LDCE)
BREADTH-FIRST SEARCH: EXAMPLE
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: Ø
Prepared By,
Shraddha Modi(LDCE)
BREADTH-FIRST SEARCH: EXAMPLE2
A B C D
E F G H
Prepared By,
Shraddha Modi(LDCE)
DEPTH-FIRST SEARCH EXAMPLE
source
vertex
d f
1 | | |
| |
| | |
DEPTH-FIRST SEARCH EXAMPLE
source
vertex
d f
1 | | |
2 | |
| | |
DEPTH-FIRST SEARCH EXAMPLE
source
vertex
d f
1 | | |
2 | |
3 | | |
DEPTH-FIRST SEARCH EXAMPLE
source
vertex
d f
1 | | |
2 | |
3 | 4 | |
DEPTH-FIRST SEARCH EXAMPLE
source
vertex
d f
1 | | |
2 | |
3 | 4 5 | |
DEPTH-FIRST SEARCH EXAMPLE-DIRECTED
GRAPH
source
vertex
d f
1 | | |
2 | |
3 | 4 5 | 6 |
DEPTH-FIRST SEARCH EXAMPLE
source
vertex
d f
1 | 8 | |
2 | 7 |
3 | 4 5 | 6 |
DEPTH-FIRST SEARCH EXAMPLE
source
vertex
d f
1 | 8 | |
2 | 7 |
3 | 4 5 | 6 |
DEPTH-FIRST SEARCH EXAMPLE
source
vertex
d f
1 | 8 | |
2 | 7 9 |
3 | 4 5 | 6 |
2 | 7 9 |10
3 | 4 5 | 6 |
DEPTH-FIRST SEARCH EXAMPLE
source
vertex
d f
1 | 8 |11 |
2 | 7 9 |10
3 | 4 5 | 6 |
DEPTH-FIRST SEARCH EXAMPLE
source
vertex
d f
1 |12 8 |11 |
2 | 7 9 |10
3 | 4 5 | 6 |
DEPTH-FIRST SEARCH EXAMPLE
source
vertex
d f
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 |
DEPTH-FIRST SEARCH EXAMPLE
source
vertex
d f
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 14|
DEPTH-FIRST SEARCH EXAMPLE
source
vertex
d f
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 14|15
DEPTH-FIRST SEARCH EXAMPLE
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
DFS: KINDS OF EDGES
DFS introduces an important distinction among edges in
the original graph:
Tree edge: encounter new vertex
Back edge: from descendent to ancestor
Forward edge: from ancestor to descendent
Cross edge: all other edges
DEPTH-FIRST SEARCH EXAMPLE
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
Tree edges
DEPTH-FIRST SEARCH EXAMPLE
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
2 | 7 9 |10
3 | 4 5 | 6 14|15
2 | 7 9 |10
3 | 4 5 | 6 14|15
97 10/3/2023
Depth-First Search
Example
u v w
x y z
DFS VS. BFS
F B A start
DFS Process E
G D C
destination
A B C D D
Initial call to BFS on A Dequeue A Dequeue B Dequeue C
Add A to queue Add B Add C, D Nothing to add
rear front
Prepared By,
Shraddha Modi(LDCE)
DEAPTH-FIRST SEARCH: ALGORITHM
procedure dfsearch(G)
for each v Є N do
mark[v] ← not-visited
for each v Є N do
if mark[v] ≠ visited
then dfs(v)
procedure dfs(v)
{Node v has not previously been visited}
mark[v] ← visited
for each node w adjacent to v do
if mark[w] ≠ visited
then dfs(w)
Prepared By,
Shraddha Modi(LDCE)
BREADTH-FIRST SEARCH: ALGORITHM
Prepared By,
Shraddha Modi(LDCE)
TOPOLOGICAL SORTING
Prepared By,
Shraddha Modi(LDCE)
APPLICATIONS OF TOPOLOGICAL SORTING
Prepared By,
Shraddha Modi(LDCE)
EXAMPLE
Topological order
A
BADC Valid
BDCA
ABCD B C
Invalid
D
Prepared By,
Shraddha Modi(LDCE)
ALGORITHM
Prepared By,
Shraddha Modi(LDCE)
EXAMPLE1
0 0
A
2 1
3
2 2
Prepared By,
Shraddha Modi(LDCE)
EXAMPLE1
A 0
1 2 1
2 2
Prepared By,
Shraddha Modi(LDCE)
EXAMPLE1
A
1 1 0
2 2
Prepared By,
Shraddha Modi(LDCE)
EXAMPLE1
A
1 0
2 1
Prepared By,
Shraddha Modi(LDCE)
EXAMPLE1
A
1 0
Prepared By,
Shraddha Modi(LDCE)
EXAMPLE1
A
0 0
Prepared By,
Shraddha Modi(LDCE)
EXAMPLE2 AND SOLUTION
5 4
2 0 1
Valid Order 4, 5, 0, 2, 3, 1
Other order…???
Prepared By,
Shraddha Modi(LDCE)
EXAMPLE3 AND SOLUTION
A
Prepared By,
Shraddha Modi(LDCE)
EXAMPLE4 AND SOLUTION
Prepared By,
Shraddha Modi(LDCE)
CONNECTED COMPONENTS – SELF STUDY
Connected components
Prepared By,
Shraddha Modi(LDCE)
CONNECTIVITY
Let us now look at something new:
Definition: An undirected graph is called connected if
there is a path between every pair of distinct vertices in
the graph.
For example, any two computers in a network can
communicate if and only if the graph of this network is
connected.
Note: A graph consisting of only one vertex is always
connected, because it does not contain any pair of
distinct vertices.
Prepared By,
Shraddha Modi(LDCE)
CONNECTIVITY
Example: Are the following graphs connected?
a
b a
b
e
e
d
d c
c
Yes. No.
a b a
e e
d
c
d f
c
Yes. No.
Prepared By,
Shraddha Modi(LDCE)
CONNECTIVITY
Definition:A graph that is not connected is the union
of two or more connected subgraphs, each pair of which
has no vertex in common. These disjoint connected
subgraphs are called the connected components of the
graph.
Definition:
A connected component of a graph G is a
maximal connected subgraph of G.
E.g., if vertex v in G belongs to a connected
component, then all other vertices in G that is
connected to v must also belong to that component.
Prepared By,
Shraddha Modi(LDCE)
CONNECTIVITY
Example: What are the connected components in the
following graph?
i h
d
a e
g
b c f j
Prepared By,
Shraddha Modi(LDCE)
CONNECTIVITY
a
Definition: An directed graph is b
strongly connected if every vertex is d
reachable from every other vertex.
c
Prepared By,
Shraddha Modi(LDCE)
CUT VERTICES AND EDGES