Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Connected Components

&
Traversal Techniques
Connected Components
• In an undirected graph G, two vertices u and v are
called connected if G contains a path from u to v.
Otherwise, they are called disconnected.
• If the two vertices are additionally connected by a
path of length 1, i.e. by a single edge, the vertices are
called adjacent.
• A graph is said to be connected if every pair of
vertices in the graph is connected.

With vertex 0 this graph is disconnected,


the rest of the graph is connected.
Connected Components

 A directed graph is strongly connected if and only if, for


each pair of vertices v and w, there is a path from v to w.
 A directed graph is called weakly connected if replacing all
of its directed edges with undirected edges produces a
connected (undirected) graph.

 A disconnected graph is made up of connected sub graphs


that are called components.
Connected Components

Strongly connected component


– A strongly connected component of a
digraph G is a maximal strongly connected
sub graph of G.
Graph Search (traversal)
• In computer science, graph traversal (also
known as graph search) refers to the process
of visiting (checking and/or updating) each
vertex in a graph.
• Such traversals are classified by the order in
which the vertices are visited.
• Tree traversal is a special case of graph
traversal.
Graph Search (traversal)

• Two different types of traversal strategies are:


– Breadth First Search (BFS)
– Depth First Search (DFS)
Breadth First Search (BFS)
What is the idea behind BFS?
• In this method, a node from the graph is
selected randomly as the start position.
• Starting from that node then traversal into
next level of graph.
• This process will be recursively done until all
the nodes are visited.
Data structure used
 Queue
Breadth First Search (BFS)

• Rule 1 − Visit adjacent unvisited vertex. Mark


it visited. Display it. Insert it in a queue.
• Rule 2 − If no adjacent vertex found, remove
the first vertex from queue.
• Rule 3 − Repeat Rule 1 and Rule 2 until queue
is empty.
Breadth First Search (BFS)

Current Node Adjacent Nodes Traversed List Queue Status Description


S A,B,C S A,B,C Selected node S as
start node and visit
it.
A,B,C are S’s
unvisited adjacent
nodes. So add them
into a Queue.
Current Node Adjacent Traversed List Queue Status Description
Nodes
A S,D S,A B,C,D Delete the front
item from Queue
that is node A and
traverse it. A’s
adjacent nodes are
S & D. S is already
visited and D is the
only unvisited
node add D into
the Queue.
B S,D S,A,B C,D Delete the front
item from Queue
that is node B and
traverse it. B’s
adjacent nodes are
S & D. Both are
already visited.
Current Node Adjacent Traversed List Queue Status Description
Nodes
C S,D S,A,B,C D Delete the front
item from Queue
that is node C and
traverse it. C’s
adjacent nodes are
S & D. Both are
already visited.
D A,B,C S,A,B,C,D Empty Delete the front
item from Queue
that is node D and
traverse it. D’s all
adjacent nodes are
visited.
Queue become All nodes are Traversed.
empty Traversal Order is S-A-B-C-D
Depth First Search (DFS)
What is the idea behind DFS?
• In this method, a node from the graph is
selected randomly as the start position.
• Starting from that node then travel as far as you can
down a path
• Back up as little as possible when you reach a
"dead end" (i.e., next vertex has been
"marked" or there is no next vertex)
Data structure used
 Stack
Depth First Search (DFS)

• Rule 1 − Visit adjacent unvisited vertex. Mark


it visited. Display it. Push it in a stack.
• Rule 2 − If no adjacent vertex found, pop up a
vertex from stack. (It will pop up all the
vertices from the stack which do not have
adjacent vertices.)
• Rule 3 − Repeat Rule 1 and Rule 2 until stack is
empty.
DFS Example

Current Node Adjacent Node Action Stack Status Traversal Result


S as start node Push S on S
the stack

S A,B,C Pop(S) from S


stack and
traverse it.
Adj(S) are
A,B, C not
visited push
C B A

A,B & C
DFS Example
Current Node Adjacent Node Action Stack Status Traversal Result
S A,B,C Pop(S) from S
stack and
traverse it.
Adj(S) are
A,B, C not

C B A
visited push
A,B & C
A S,D Pop(A) from S, A
stack and
traverse it.
Adj(A) are S
& D. S is
already
visited,
push only D CBD
onto stack
DFS Example
Current Node Adjacent Node Action Stack Status Traversal Result
D A,B, C Pop(D) from stack S,A,D
and traverse it.
Adj(D) are A, B & C.
All are already

C B
visited.
B S,D Pop(B) from stack and S,A,D,B
traverse it.
Adj(B) are S & D. Both
are already visited.

C
C S,D Pop(C) from stack and Empty S,A,D,B,C
traverse it.
Adj(C) are S & D. Both
are already visited.

Stack become All nodes are Traversed. Traversal Order is S-A-D-B-C


empty
DFS Example A

B C D

E
Current Node Adjacent Node Action Stack Status Traversal Result
A as start node Push A on the stack

A
A B,C,D Pop(A) from stack A
and traverse it.
Adj(A) are B,C & D.
All are unvisited push

DCB
into the stack

B - Pop(B) from stack and A,B


traverse it.
No Adjacent node for
B.

DC
C - Pop(C) from stack and A,B,C
traverse it.
No Adjacent node for
C. D
DFS Example
Current Node Adjacent Node Action Stack Status Traversal Result
D E Pop(D) from stack A,B,C,D
and traverse it.
Adj(D) is E push into
the stack

E
E B,C Pop(E) from stack and Empty A,B,C,D,E
traverse it.
Adj(E) are B,C.
Both are already
visited.

Stack become All nodes are Traversed. Traversal Order is A-B-C-D-E


empty

You might also like