Data Structures: Fall 2021
Data Structures: Fall 2021
Data Structures
Fall 2021
Graph Traversal
• 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
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
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
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
68
Task : Print cycles
69