Professional Documents
Culture Documents
Algorithm
Algorithm
&
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.
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.
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
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.