UNIT- 6
EXPLORING GRAPHS
A NON-PRIMITIVE NON-LINEAR DATA STRUCTURE
ANALYSIS AND DESIGN OF ALGORITHMS
3150703
Prepared By,
Shraddha Modi
Assistant Professor
CE Dept, LDCE
TOPICS TO BE COVERED
Introduction
Undirected-Directed Graph and Terminology
Traversing Graphs
Depth First Search,
Breath First Search,
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
An edge with no specific direction is called undirected edge.
If x ϵ E and it joins two vertices p and q, where p,q ϵ V then p
and q are called adjacent nodes. x is called incident to nodes p
and q whether x is a directed or undirected edge.
Directed Undirected Edge
Edge p q
p q Prepared By,
Shraddha Modi(LDCE)
GRAPH : DIFFERENT TERMINOLOGIES
A graph with some of edges directed and some undirected, is
called a mixed graph.
Let x ϵ E is a directed edge associated with ordered pair of
nodes (p,q). Here, edge x is said to be initiating or originating
in the node p and terminating or ending in node q.
Node p and q are called Initial and terminal nodes
respectively.
Mixed Graph
p q
Initial Node Terminal Node
Prepared By,
Shraddha Modi(LDCE)
GRAPH : VARIOUS TERMINOLOGIES loop
An edge of a graph that joins a node to itself is called a
loop (sling).
A loop can be considered either as a directed or an
undirected edge (direction is not significant in loop).
e1
In case of directed edges, the two possible edges e2
between a pair of nodes which are opposite in direction e1 and e2 are
are considered distinct. distinct edges
If in a directed or undirected graph, there are more than
e1
one edge between a pair of nodes, then such edges are e2
called parallel edge. e3
e1, e2 and e3
A graph that contains some parallel edges, is called are parallel
edges
multigraph.
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 graph in which weights are assigned to every edge, is called a weighted
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
The total degree of a loop is 2
The total degree of an isolated node is zero Prepared By,
Shraddha Modi(LDCE)
GRAPH : VARIOUS TERMINOLOGIES
A path is a sequence of edges of a digraph (directed graph) such that the
terminal node of any edge in the sequence is the initial node of the edge (if
any), appearing next in the sequence.
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
Paths : P1 = ((1,2),(2,5),(5,6) P2=((1,3)(3,5),(5,4)) P3=((1,2),(2,4),(4,6))
A path is said to traverse through nodes appearing in the sequence,
originating in the initial node of the first edge and ending in the terminal
node of the last edge in the sequence.
The number of edges appearing in the sequence of a path is called the
length of the path.
Prepared By,
Shraddha Modi(LDCE)
GRAPH : VARIOUS TERMINOLOGIES
A path in a digraph in which the edges are distinct is called a
simple path (edge simple)
A path in which all the nodes through which it traverses are
distinct is called an elementary path (node simple)
Path P1 ((1,2), (2,5), (5,6)) is a simple path.
Path P2 ((4,4),(4,6)) is simple (with distinct edge), but not
elementary (as node 2 is traversed for twice due to loop)
2 4
Note that: 1
P
P 2
Every elementary path of a digraph is simple. 1 6
3 5
Every simple path may or may not be elementary
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
Search for a certain node or traverse all nodes in the graph
Visiting each node exactly once.
1) Breadth First Search (BFS)
Start several paths at a time, and advance in each one step
at a time.
2) Depth First Search (DFS)
Once a possible path is found, continue the search until
the end of the path.
Prepared By,
Shraddha Modi(LDCE)
IMPORTANT POINT – BFS
BFS places discovered vertices in FIFO queue, exploring vertices in
the order discovered.
Example : Queue: [A]
Queue: [B, D]
Queue: [D, C, E]
Queue: [C, E]
Queue: [E]
Queue: [].
Now, the explored order is A, B, D, C, E.
Prepared By,
Shraddha Modi(LDCE)
IMPORTANT POINT – DFS
DFS places discovered vertices in LIFO stack, exploring vertices as
discovered.
Example : Stack: [A]
Stack: [A, B],
Stack: [A, B, C]
Stack: [A, B],
Stack: [A, B, E]
Stack: [A, B, E, D], Stack: [A,B,E]
Stack: [A,B], Stack: [A] Prepared By,
Shraddha Modi(LDCE)
BREADTH FIRST SEARCH (BFS)
Each vertex of the graph is in one of three states:
1. Undiscovered;
2. Discovered but not fully explored;
3. Fully explored.
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 |
What is the structure of the grey vertices?
What do they represent?
DEPTH-FIRST SEARCH EXAMPLE
source
vertex
d f
1 | 8 | |
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
Tree edges Back 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 Back edges Forward 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 Back edges Forward edges Cross edges
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
C DFS on C D Call DFS on D
B DFS on B B B Return to call on B
A DFS on A A A A
G Call DFS on G found destination - done!
D Path is implicitly stored in DFS recursion
B Path is: A, B, D, G
A
Prepared By,
Shraddha Modi(LDCE)
DFS VS. BFS
F B A start
E
BFS Process
G D C
destination
rear front rear front rear front rear front
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
G found destination - done!
Dequeue D Path must be stored separately
Add G
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
A topological sort or topological ordering of a directed acyclic
graph is a linear ordering of its vertices such that for every
directed edge (𝒖, 𝒗) from vertex 𝒖 to vertex 𝒗, the vertex
𝒖 comes before the vertex 𝒗 in the ordering.
Applicable on DAG.(Directed Acyclic Graph)
There can be multiple valid topological orderings, not just
one.
Prepared By,
Shraddha Modi(LDCE)
APPLICATIONS OF TOPOLOGICAL SORTING
Scheduling tasks that have dependencies, such as project
management and task scheduling.
Compilers and build systems, where the order of compilation
or linking must respect dependencies.
Job scheduling in operating systems, it ensures that jobs are
executed in the correct order.
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
Strongly connected graph
Articulation Point/Cut vertex and Bridge/Cut edge
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
Solution: The connected components are the
graphs with vertices {a, b, c, d}, {e}, {f}, {i, g, h, 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
Definition:An directed graph is
a
weakly connected if every vertex is
b
not reachable from every other
d
vertex.
c
Prepared By,
Shraddha Modi(LDCE)
CUT VERTICES AND EDGES
If one can remove a vertex (and all incident
edges) and produce a graph with more
components, the vertex is called a cut vertex or
articulation point.
Similarly if removal of an edge creates more
components the edge is called a cut edge or
bridge.
Prepared By,
Shraddha Modi(LDCE)
123
In the star network the
center vertex is a cut vertex.
All edges are cut edges.
In the graphs G1 and G2 every edge is a cut edge.
In the union, no edge is a cut edge.
Vertex e is a cut vertex in all graphs.
WP(3160713) – CO2
Prepared By,
124
Shraddha Modi(LDCE)