0% found this document useful (0 votes)
9 views67 pages

Data Structures: Fall 2021

The document provides an overview of graph theory, including definitions, terminology, and representations of graphs, such as directed and undirected graphs. It discusses key concepts like paths, cycles, and graph traversal methods, specifically Breadth First Search (BFS) and Depth First Search (DFS). Additionally, it covers the pros and cons of different graph representations, including adjacency matrices and adjacency lists.

Uploaded by

fastian.0542
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views67 pages

Data Structures: Fall 2021

The document provides an overview of graph theory, including definitions, terminology, and representations of graphs, such as directed and undirected graphs. It discusses key concepts like paths, cycles, and graph traversal methods, specifically Breadth First Search (BFS) and Depth First Search (DFS). Additionally, it covers the pros and cons of different graph representations, including adjacency matrices and adjacency lists.

Uploaded by

fastian.0542
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 67

CS-2001

Data Structures
Fall 2021
Graph Traversal

Mr. Muhammad Usman Joyia


National University of Computer
and Emerging Sciences,
Faisalabad, Pakistan.
Today’s Lecture

• Graph Definition
• Graph Terminology
• Representation of Graph
• Path and Cycle
• Graph Traversal (BFS-DFS)

2
Graph definitions
• A graph is a collection of nodes (or vertices, singular is
vertex) and edges (or arcs)
– Each node contains an element
– Each edge connects two nodes together (or possibly the same
node to itself) and may contain an edge attribute or weight
• There are two kinds of graphs: directed graphs (sometimes
called digraphs) and undirected graphs
– A directed graph is one in which the edges have a direction
– An undirected graph is one in which the edges do not have a
direction.

3
Directed And Undirected Graphs

start 400
Nantes Paris

350
fill pan take egg 590 120
with water from fridge 340 Grenoble
Lyon
420 320
add salt break egg
to water into pan Bordeaux 510 Monaco

boil Marseille
water

A directed graph An undirected graph

4
Size and Degree
• The size of a graph is the number of edges in it
• The empty graph has size zero (no nodes)
• If two nodes are connected by an edge, they are neighbors
(and the nodes are adjacent to each other)
• The degree of a node is the number of edges it has

5
• For directed graphs,
– In a directed graph vertex v is adjacent to u, if there is an
edge leaving u and coming to v. u
» v is adjacent to u. w
» u is adjacent to w v
– If a directed edge goes from node S to node D, we call S the
source and D the destination of the edge
 The edge is an out-edge of S and an in-edge of D
 S is a predecessor of D, and D is a successor of S S
– The in-degree of a node is the number of in-edges it has
D
– The out-degree of a node is the number of out-edges it has

6
Path and Cycle
• In graph theory, a path in • Example: (Paris, Nantes,
a graph is a sequence of Bordeaux, Lyon) is a path
edges which connect a • Example: (Paris, Nantes,
sequence of vertices. Path Lyon, Paris) is a cycle
has a first vertex, called its • A cyclic graph contains at
start vertex, and a last least one cycle
vertex, called its end • An acyclic graph does not
vertex. Both of them are
contain any cycles
called terminal vertices of
400
the path. Nantes Paris

350
590 120
• A cycle is a path whose 340 Grenoble
Lyon
first and last nodes are the 420 320
same Monaco
Bordeaux 510

Marseille 7
Graph Representation

• Adjacency-matrix
Representation
• Adjacency Lists
Representation

8
Adjacency-matrix representation I
• One simple way of • The adjacency matrix is
representing a graph is the symmetric about the main
adjacency matrix diagonal
• A 2-D array has a mark at
[i][j] if there is an edge
from node i to node j A
B C

D E

AB CD EF G F
G
A
B
C
D • This representation is only
E suitable for small graphs!
F (Why?)
G
9
Adjacency-matrix representation II
• An adjacency matrix can • Again, this is only suitable
equally well be used for for small graphs!
digraphs (directed graphs) • However, the adjacency
• A 2-D array has a mark at matrix for a digraph is
[i][j] if there is an edge usually not symmetric,
from node i to node j why?

AB CD EF G A
A B C
B
D E
C
D F
E G
F
G
10
Adjacency Lists Representation

• A graph of n nodes is represented by a one-dimensional


array L of linked lists, where
– L[i] is the linked list containing all the nodes adjacent to node
i
– The nodes in the list L[i] are in no particular order

11
Adjacency Lists Representation

12
Pros and Cons of Adjacency Matrix
Pros and Cons of Adjacency Lists

• Pros:
– Simple to implement
– Easy and fast to tell if a pair (i,j) is an edge: simply check if
A[i][j] is 1 or 0
• Cons:
– No matter how few edges the graph has, the matrix takes
O(n2) in memory

13
Pros and Cons of Adjacency Lists
• Pros:
– Saves on space (memory): the representation takes as many
memory words as there are nodes and edge.
• Cons:
– It can take up to O(n) time to determine if a pair of nodes (i,j)
is an edge: one would have to search the linked list L[i], which
takes time proportional to the length of L[i].
• Note:
– Adjacency list is better for sparse graphs, adjacency matrix
for dense graphs

14
Graph Traversal
• Graph traversal is the process of writing out all the nodes
of a simple, connected graph G in some organized way.
• Algorithms, that apply for generalize traversal of any
graph. They are:
• Breadth First Search
• Depth First Search

15
Graph Search
• Choice of container
– If a stack is used as the container for adjacent vertices, we
get depth first search.
– If a Queue is used as the container adjacent vertices, we get
breadth first search.

16
Graph Search
• The state of a vertex, u, is stored in a color variable as
follows:
• 1. color[u] = White - for the "undiscovered" state,
2. color [u] = Gray - for the "discovered but not fully
explored" state, and
3. color [u] = Black - for the "fully explored" state.

17
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• Q {s} 5
• While Q   3
• {
• u  head[Q];
• for each v  Adjacent[u]
• if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
18
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white
s 0

2
color[s]  gray
• Q {s} 5
• While Q   3
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q=
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
19
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white
s 0

2
color[s]  gray
• Q {s} 5
• While Q   3
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q=
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
20
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white
s 0

2
color[s]  gray
• Q {s} 5
• While Q   3
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q= 0
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
21
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• Q {s} 5
• While Q   u 3
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q= 0
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
22
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• Q {s} 5
• While Q   u 3
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q= 0 1
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
23
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• Q {s} 5
• While Q   u 3
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q= 0 1 2
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
24
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• Q {s} 5
• While Q   u 3
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q= 0 1 2 3
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
25
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0
1 2
• color[s]  gray
• Q {s} 5
• While Q   u 3
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q = 01 2 3
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
26
Breadth First Algorithm u
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0
1 2
• color[s]  gray
• Q {s} 5
• While Q   3
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q = 01 2 3
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
27
Breadth First Algorithm u
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0
1 2
• color[s]  gray
• Q {s} 5
• While Q   3
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q = 01 2 3 4
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
28
Breadth First Algorithm u
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0
1 2
• color[s]  gray
• Q {s} 5
• While Q   3
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q = 012 3 4
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
29
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V u
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0
1 2
• color[s]  gray
• Q {s} 5
• While Q   3
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q = 012 3 4
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
30
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V u
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0
1 2
• color[s]  gray
• Q {s} 5
• While Q   3
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q = 012 3 4
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
31
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V u
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0
1 2
• color[s]  gray
• Q {s} 5
• While Q   3
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q = 0123 4
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
32
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0
1 2
• color[s]  gray
• Q {s} 5
• While Q   3
• {
• u  head[Q]; u
• for each v  Adjacent[u]
• Q = 0123 4
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
33
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0
1 2
• color[s]  gray
• Q {s} 5
• While Q   3
• {
• u  head[Q]; u
• for each v  Adjacent[u]
• Q = 0123 4 5
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
34
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0
1 2
• color[s]  gray
• Q {s} 5
• While Q   3
• {
• u  head[Q]; u
• for each v  Adjacent[u]
• Q = 01234 5
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
35
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0
1 2
• color[s]  gray
• Q {s} 5
• While Q   3 u
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q = 01234 5
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
36
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0
1 2
• color[s]  gray
• Q {s} 5
• While Q   3 u
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q = 012345
if color[v] = white
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
37
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0
1 2
• color[s]  gray
• Q {s} 5
• While Q   3
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q = 012345
if color[v] = white u
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
38
Breadth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a queue Q 1 4
• For each vertex u  V – {s}
• color[u]  white 0
1 2
• color[s]  gray
• Q {s} 5
• While Q   3
• {
• u  head[Q];
• for each v  Adjacent[u]
• Q=
if color[v] = white  u
• {
• color[v]  gray
• Enqueue(Q,v)
• }
• Dequeue(Q)
• color[u]  black;
• }
39
• Ignore Vertex:
– If an edge goes to completely explored vertex (black)
– If an edge goes to discovered but not completely explored
(Grey)

40
Task – Example of Undirected Graph

r s t u

v w x y
• Root is s

41
• Ignore Vertex:
– If an edge goes to completely explored vertex (black)
– If an edge goes to discovered but not completely explored
(Grey)

43
Graph Search
• Choice of container
– If a stack is used as the container for adjacent vertices, we
get depth first search.
– If a Queue is used as the container adjacent vertices, we get
breadth first search.

44
Depth First Algorithm

• Given graph G=(V,E) and source vertex s  V


• Create a stack S
1 4
• For each vertex u  V – {s} s 0
• color[u]  white 2
• color[s]  gray
• S {s} 3 5
• While S  
• {
• u = Pop(S)


for each v  Adjacent[u]
S=
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }
45
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
• For each vertex u  V – {s}
• color[u]  white
s 0

2
color[s]  gray
• S {s} 5
• While S   3
• {
• u = Pop(S)
• for each v  Adjacent[u]
• S=
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

46
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
• For each vertex u  V – {s}
• color[u]  white
s 0

2
color[s]  gray
• S {s} 5
• While S   3
• {
• u = Pop(S)
• for each v  Adjacent[u]
• S = 0
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

47
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• S {s} 5
• While S  
u 3
• {
• u = Pop(S)
• for each v  Adjacent[u]
• S=
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

48
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• S {s} 5
• While S  
u 3
• {
• u = Pop(S)
• for each v  Adjacent[u]
• S = 1
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

49
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• S {s} 5
• While S  
u 3
• {
• u = Pop(S)
• for each v  Adjacent[u]
• S = 32 1
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

50
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• S {s} 5
• While S  
u 3
• {
• u = Pop(S)
• for each v  Adjacent[u]
• S = 3 2 1
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

51
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• S {s} 5
• While S  
u 3
• {
• u = Pop(S)
• for each v  Adjacent[u]
• S = 3 2 1
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

52
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• S {s} 5
• While S   3
• {
• u = Pop(S) u
• for each v  Adjacent[u]
• S = 32 1
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

53
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• S {s} 5
• While S   3
• {
• u = Pop(S) u
• for each v  Adjacent[u]
• S = 5 32 1
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

54
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• S {s} 5
• While S   3
• {
• u = Pop(S) u
• for each v  Adjacent[u]
• S = 5 32 1
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

55
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• S {s} 5
• While S   3
• {
• u = Pop(S) u
• for each v  Adjacent[u]
• S = 523 1
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

56
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• S {s} 5
• While S   3
• {
• u = Pop(S) u
• for each v  Adjacent[u]
• S = 4 523 1
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

57
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• S {s} 5
• While S   3
• {
• u = Pop(S) u
• for each v  Adjacent[u]
• S = 4 523 1
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

58
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• u
S {s}
3 5
0
• While S  
• {
• u = Pop(S)
• for each v  Adjacent[u]
• S = 4352 1
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

59
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• u
S {s}
3 5
0
• While S  
• {
• u = Pop(S)
• for each v  Adjacent[u]
• S = 4352 1
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

60
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
0
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• u
S {s}
3 5
0
• While S  
• {
• u = Pop(S)
• for each v  Adjacent[u]
• S = 43521
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

61
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
0
• For each vertex u  V – {s}
• color[u]  white 0 2
• color[s]  gray
• u
S {s}
3 5
0
• While S  
• {
• u = Pop(S)
• for each v  Adjacent[u]
• S = 43521
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

62
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1 4
0
• For each vertex u  V – {s}
• color[u]  white 0 2
0
• color[s]  gray
• u
S {s}
3 5
0
• While S  
• {
• u = Pop(S)
• for each v  Adjacent[u]
• S=
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

63
Depth First Algorithm
• Given graph G=(V,E) and source vertex s  V
• Create a stack S 1
0 4
0
• For each vertex u  V – {s}
• color[u]  white 0 2
0
• color[s]  gray
• u
S {s}
3 5
0
• While S  
• {
• u = Pop(S)
• for each v  Adjacent[u]
• S=
if color[v] = white
• {
• color[v]  gray
• Push(S,v)
• }
• color[u]  black;
• }

64
Analysis Of DFS and BFS
• The for-loop inside the while-loop is executed at most |E|
times if G is a directed graph or 2|E| times if G is
undirected. In Directed graph we examine (u, v) only
when u is dequeued. Therefore, every edge examined at
most once if directed, at most twice if undirected. So, we
have O(E).
• Therefore, the total running time for breadth-first search
traversal is O(V + E).
• If adjacency matrix is used as a container analysis will be
O(V2)

65
BASIS FOR
COMPARISON BFS DFS

Basic Vertex-based Edge-based


algorithm algorithm
Data structure used Queue Stack
to store the nodes
Memory Inefficient Efficient
consumption
Structure of the Wide and short Narrow and long
constructed tree
Traversing fashion Oldest unvisited Vertices along the
vertices are explored edge are explored in
at first. the beginning.
Optimality Optimal for finding Not optimal
the shortest
distance, not in cost.
Application Examines bipartite Examines two-edge
graph, connected connected graph, 67
Key Differences Between BFS/DFS
• BFS is vertex-based algorithm while DFS is an edge-based
algorithm.
• Queue data structure is used in BFS. On the other hand,
DFS uses stack or recursion.
• Memory space is efficiently utilized in DFS while space
utilization in BFS is not effective.
• BFS is optimal algorithm while DFS is not optimal.
• DFS constructs narrow and long trees. As against, BFS
constructs wide and short tree.

68
Task : Print cycles

69

You might also like