© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
THEORY AND PROBLEMS OF DISCRETE MATHEMATICS (3RD EDITION)-SEYMOUR LIPSCHUTZ
Chapter 8: Graph Theory
Biplab C. Debnath
Lecturer
Dept. of Computer Science and Engineering
https://www.facebook.com/groups/bcdcse/
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Hamiltonian Graphs
A finite connected graph is Eulerian if and only if each
vertex has even degree.
Eulerian circuit traverses every edge exactly once, but
may repeat vertices.
Hamiltonian circuit visits each vertex exactly once but
may repeat edges.
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
PLANAR GRAPHS
A graph or multigraph which
can be drawn in the plane so
that its edges do not cross is
said to be planar.
hence K4 is planar
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Maps, Regions
A particular planar representation of a finite planar
multigraph is called a map.
A given map divides the plane into various regions.
Six vertices and nine edges divides the plane into five
regions.
The sum of the degrees of the regions of a map is equal
to twice the number of edges.
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Euler’s Formula
Theorem 8.8 (Euler): V − E + R = 2.
Euler’s formula, p − q + r = 2.
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Nonplanar Graphs, Kuratowski’s Theorem
We give two examples of nonplanar graphs. Consider first
the utility graph; that is, three houses A1, A2, A3 are to be
connected to outlets for water, gas and electricity, B1, B2,
B3
Theorem 8.10: (Kuratowski) A graph is nonplanar if and
only if it contains a subgraph homeomorphic to K3,3 or
K5.
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Dijkstra’s Algorithm
Dijkstra’s AlgorithmSingle Source Multiple Destination
Shortest Path Algorithm
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Shortest Paths
8
Dijkstra’s Algorithm
The distance of a vertex
v from a vertex s is the
length of a shortest path
between s and v
Dijkstra’s algorithm
computes the distances
of all the vertices from a
given start vertex s
Assumptions:
– the graph is connected
– the edges are
undirected
– the edge weights are
nonnegative
We grow a “cloud” of vertices,
beginning with s and eventually
covering all the vertices
We store with each vertex v a
label d(v) representing the
distance of v from s in the
subgraph consisting of the cloud
and its adjacent vertices
At each step
– We add to the cloud the vertex
u outside the cloud with the
smallest distance label, d(u)
– We update the labels of the
vertices adjacent to u
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Shortest Paths
9
Dijkstra’s Algorithm
A priority queue stores the vertices outside the cloud
– Key: distance
– Element: vertex
Locator-based methods
– insert(k,e) returns a locator
– replaceKey(l,k)changes the key of an item
We store two labels with each vertex:
– Distance (d(v) label)
– locator in priority queue
Algorithm DijkstraDistances(G, s)
Q new heap-based priority queue
for all v G.vertices()
if v = s
setDistance(v, 0)
else
setDistance(v, )
l Q.insert(getDistance(v), v)
setLocator(v,l)
while Q.isEmpty()
u Q.removeMin()
for all e G.incidentEdges(u)
{ relax edge e }
z G.opposite(u,e)
r getDistance(u) + weight(e)
if r < getDistance(z)
setDistance(z,r)
Q.replaceKey(getLocator(z),r)
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Requirements
Works with directed and undirected graphs
Works with weighted and unweighted graphs
Rare type of algorithm
A greedy algorithm that produces an optimal
solution
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
25
A
H
B
F
E
D
C
G
Walk-Through
9
7
2
10
18
34
3
7
5
8
9
4
3
10
Initialize array
K dv pv
A F
B F
C F
D F
E F
F F
G F
H F
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
25
A
H
B
F
E
D
C
G
9
7
2
10
18
34
3
7
5
8
9
4
3
10
Start with G
K dv pv
A
B
C
D
E
F
G T 0
H
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
25
A
H
B
F
E
D
C
G
9
7
2
10
18
34
3
7
5
8
9
4
3
10
Update unselected nodes
K dv pv
A
B
C
D 2 G
E
F
G T 0
H 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
25
A
H
B
F
E
D
C
G
9
7
2
10
18
34
3
7
5
8
9
4
3
10
Select minimum distance
K dv pv
A
B
C
D T 2 G
E
F
G T 0
H 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
25
A
H
B
F
E
D
C
G
9
7
2
10
18
34
3
7
5
8
9
4
3
10
Update unselected nodes
K dv pv
A
B
C
D T 2 G
E 27 D
F 20 D
G T 0
H 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
25
A
H
B
F
E
D
C
G
9
7
2
10
18
34
3
7
5
8
9
4
3
10
Select minimum distance
K dv pv
A
B
C
D T 2 G
E 27 D
F 20 D
G T 0
H T 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
25
A
H
B
F
E
D
C
G
9
7
2
10
18
34
3
7
5
8
9
4
3
10
Update unselected nodes
K dv pv
A 7 H
B 12 H
C
D T 2 G
E 27 D
F 20 D
G T 0
H T 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
25
A
H
B
F
E
D
C
G
9
7
2
10
18
34
3
7
5
8
9
4
3
10
Select minimum distance
K dv pv
A T 7 H
B 12 H
C
D T 2 G
E 27 D
F 20 D
G T 0
H T 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
25
A
H
B
F
E
D
C
G
9
7
2
10
18
34
3
7
5
8
9
4
3
10
Update unselected nodes
K dv pv
A T 7 H
B 12 H
C
D T 2 G
E 27 D
F 17 A
G T 0
H T 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
25
A
H
B
F
E
D
C
G
9
7
2
10
18
34
3
7
5
8
9
4
3
10
Select minimum distance
K dv pv
A T 7 H
B T 12 H
C
D T 2 G
E 27 D
F 17 A
G T 0
H T 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
25
A
H
B
F
E
D
C
G
9
7
2
10
18
34
3
7
5
8
9
4
3
10
Update unselected nodes
K dv pv
A T 7 H
B T 12 H
C 16 B
D T 2 G
E 22 B
F 17 A
G T 0
H T 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
25
A
H
B
F
E
D
C
G
9
7
2
10
18
34
3
7
5
8
9
4
3
10
Select minimum distance
K dv pv
A T 7 H
B T 12 H
C T 16 B
D T 2 G
E 22 B
F 17 A
G T 0
H T 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
25
A
H
B
F
E
D
C
G
9
7
2
10
18
34
3
7
5
8
9
4
3
10
Update unselected nodes
K dv pv
A T 7 H
B T 12 H
C T 16 B
D T 2 G
E 22 B
F 17 A
G T 0
H T 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
25
A
H
B
F
E
D
C
G
9
7
2
10
18
34
3
7
5
8
9
4
3
10
Select minimum distance
K dv pv
A T 7 H
B T 12 H
C T 16 B
D T 2 G
E 22 B
F T 17 A
G T 0
H T 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
25
A
H
B
F
E
D
C
G
9
7
2
10
18
2
4
3
7
5
8
9
4
3
10
Update unselected nodes
K dv pv
A T 7 H
B T 12 H
C T 16 B
D T 2 G
E 19 F
F T 17 A
G T 0
H T 3 G
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
25
A
H
B
F
E
D
C
G
9
7
2
10
18
2
4
3
7
5
8
9
4
3
10
Select minimum distance
K dv pv
A T 7 H
B T 12 H
C T 16 B
D T 2 G
E T 19 F
F T 17 A
G T 0
H T 3 G
Done
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Order of Complexity
Analysis
– findMin() takes O(V) time
– outer loop iterates (V-1) times
O(V2) time
Optimal for dense graphs, i.e., |E| = O(V2)
Suboptimal for sparse graphs, i.e., |E| = O(V)
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Minimum Spanning Tree (MST)
A Minimum Spanning Tree (MST) is a subgraph
of an undirected graph such that the subgraph
spans (includes) all nodes, is connected, is
acyclic, and has minimum total edge weight
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Algorithm Characteristics
Both Prim’s and Kruskal’s Algorithms work with undirected graphs
Both work with weighted and unweighted graphs but are more interesting when edges are weighted
Both are greedy algorithms that produce optimal solutions
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Prim’s Algorithm
Similar to Dijkstra’s Algorithm except that dv
records edge weights, not path lengths
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Walk-Through
Initialize array
K dv pv
A F
B F
C F
D F
E F
F F
G F
H F
4
25
A
H
B
F
E
D
C
G7
2
10
18
34
3
7
8
9
3
10
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
4
25
A
H
B
F
E
D
C
G7
2
10
18
34
3
7
8
9
3
10
Start with any node, say D
K dv pv
A
B
C
D T 0
E
F
G
H
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
4
25
A
H
B
F
E
D
C
G7
2
10
18
34
3
7
8
9
3
10
Update distances of adjacent,
unselected nodes
K dv pv
A
B
C 3 D
D T 0
E 25 D
F 18 D
G 2 D
H
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
4
25
A
H
B
F
E
D
C
G7
2
10
18
34
3
7
8
9
3
10
Select node with
minimum distance
K dv pv
A
B
C 3 D
D T 0
E 25 D
F 18 D
G T 2 D
H
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
4
25
A
H
B
F
E
D
C
G7
2
10
18
34
3
7
8
9
3
10
Update distances of adjacent,
unselected nodes
K dv pv
A
B
C 3 D
D T 0
E 7 G
F 18 D
G T 2 D
H 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
4
25
A
H
B
F
E
D
C
G7
2
10
18
34
3
7
8
9
3
10
Select node with
minimum distance
K dv pv
A
B
C T 3 D
D T 0
E 7 G
F 18 D
G T 2 D
H 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
4
25
A
H
B
F
E
D
C
G7
2
10
18
34
3
7
8
9
3
10
Update distances of
adjacent, unselected nodes
K dv pv
A
B 4 C
C T 3 D
D T 0
E 7 G
F 3 C
G T 2 D
H 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
4
25
A
H
B
F
E
D
C
G7
2
10
18
34
3
7
8
9
3
10
Select node with
minimum distance
K dv pv
A
B 4 C
C T 3 D
D T 0
E 7 G
F T 3 C
G T 2 D
H 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
4
25
A
H
B
F
E
D
C
G7
2
10
18
34
3
7
8
9
3
10
Update distances of
adjacent, unselected nodes
K dv pv
A 10 F
B 4 C
C T 3 D
D T 0
E 2 F
F T 3 C
G T 2 D
H 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
4
25
A
H
B
F
E
D
C
G7
2
10
18
34
3
7
8
9
3
10
Select node with
minimum distance
K dv pv
A 10 F
B 4 C
C T 3 D
D T 0
E T 2 F
F T 3 C
G T 2 D
H 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
4
25
A
H
B
F
E
D
C
G7
2
10
18
34
3
7
8
9
3
10
Update distances of
adjacent, unselected nodes
K dv pv
A 10 F
B 4 C
C T 3 D
D T 0
E T 2 F
F T 3 C
G T 2 D
H 3 G
2
Table entries
unchanged
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
4
25
A
H
B
F
E
D
C
G7
2
10
18
34
3
7
8
9
3
10
Select node with
minimum distance
K dv pv
A 10 F
B 4 C
C T 3 D
D T 0
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
4
25
A
H
B
F
E
D
C
G7
2
10
18
34
3
7
8
9
3
10
Update distances of
adjacent, unselected nodes
K dv pv
A 4 H
B 4 C
C T 3 D
D T 0
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
4
25
A
H
B
F
E
D
C
G7
2
10
18
34
3
7
8
9
3
10
Select node with
minimum distance
K dv pv
A T 4 H
B 4 C
C T 3 D
D T 0
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
4
25
A
H
B
F
E
D
C
G7
2
10
18
34
3
7
8
9
3
10
Update distances of adjacent,
unselected nodes
K dv pv
A T 4 H
B 4 C
C T 3 D
D T 0
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
Table entries
unchanged
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
4
25
A
H
B
F
E
D
C
G7
2
10
18
34
3
7
8
9
3
10
Select node with
minimum distance
K dv pv
A T 4 H
B T 4 C
C T 3 D
D T 0
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
4
A
H
B
F
E
D
C
G
2
34
3
3
Cost of Minimum
Spanning Tree = dv = 21
K dv pv
A T 4 H
B T 4 C
C T 3 D
D T 0
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
Done
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Exercise-MST
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Kruskal’s Algorithm
• Work with edges, rather than nodes
• Two steps:
– Sort edges by increasing edge weight
– Select the first |V| – 1 edges that do not
generate a cycle
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Walk-ThroughConsider an undirected, weight
graph
5
1
A
H
B
F
E
D
C
G3
2
4
6
34
3
4
8
4
3
10
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Sort the edges by increasing edge weight
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G3
2
4
6
34
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Select first |V|–1 edges which do
not generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G3
2
4
6
34
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Select first |V|–1 edges which do
not generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G3
2
4
6
34
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Select first |V|–1 edges which do
not generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G3
2
4
6
34
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Accepting edge (E,G) would create a
cycle
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Select first |V|–1 edges which do
not generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G3
2
4
6
34
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Select first |V|–1 edges which do
not generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G3
2
4
6
34
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Select first |V|–1 edges which do
not generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G3
2
4
6
34
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Select first |V|–1 edges which do
not generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G3
2
4
6
34
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Select first |V|–1 edges which do
not generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G3
2
4
6
34
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Select first |V|–1 edges which do
not generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G3
2
4
6
34
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Select first |V|–1 edges which do
not generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G3
2
4
6
34
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Select first |V|–1 edges which do
not generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G3
2
4
6
34
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Select first |V|–1 edges which do
not generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G
2
3
3
3
edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Done
Total Cost = dv = 21
4
} not
considere
d
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Backtracking Algorithm Depth-First Search
Also called Depth-First Search
Can be used to attempt to visit all nodes of a
graph in a systematic manner
Works with directed and undirected graphs
Works with weighted and unweighted graphs
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A
B
C
D
E
F
G
H
Task: Conduct a depth-first search of the
graph starting with node D
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A
B
C
D √
E
F
G
H
Visit D
D
The order nodes are visited:
D
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A
B
C
D √
E
F
G
H
Consider nodes adjacent to D, decide
to visit C first (Rule: visit adjacent
nodes in alphabetical order)
D
The order nodes are visited:
D
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A
B
C √
D √
E
F
G
H
Visit C
C
D
The order nodes are visited:
D, C
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A
B
C √
D √
E
F
G
H
No nodes adjacent to C; cannot
continue backtrack, i.e., pop
stack and restore previous state
C
D
The order nodes are visited:
D, C
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A
B
C √
D √
E
F
G
H
Back to D – C has been visited,
decide to visit E next
D
The order nodes are visited:
D, C
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A
B
C √
D √
E √
F
G
H
Back to D – C has been visited,
decide to visit E next
E
D
The order nodes are visited:
D, C, E
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A
B
C √
D √
E √
F
G
H
Only G is adjacent to E
E
D
The order nodes are visited:
D, C, E
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A
B
C √
D √
E √
F
G √
H
Visit G
G
E
D
The order nodes are visited:
D, C, E, G
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A
B
C √
D √
E √
F
G √
H
Nodes D and H are adjacent to
G. D has already been
visited. Decide to visit H.
G
E
D
The order nodes are visited:
D, C, E, G
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A
B
C √
D √
E √
F
G √
H √
Visit H
H
G
E
D
The order nodes are visited:
D, C, E, G, H
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A
B
C √
D √
E √
F
G √
H √
Nodes A and B are adjacent to F.
Decide to visit A next.
H
G
E
D
The order nodes are visited:
D, C, E, G, H
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A √
B
C √
D √
E √
F
G √
H √
Visit A
A
H
G
E
D
The order nodes are visited:
D, C, E, G, H, A
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A √
B
C √
D √
E √
F
G √
H √
Only Node B is adjacent to A.
Decide to visit B next.
A
H
G
E
D
The order nodes are visited:
D, C, E, G, H, A
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A √
B √
C √
D √
E √
F
G √
H √
Visit B
B
A
H
G
E
D
The order nodes are visited:
D, C, E, G, H, A, B
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A √
B √
C √
D √
E √
F
G √
H √
No unvisited nodes adjacent to
B. Backtrack (pop the stack).
A
H
G
E
D
The order nodes are visited:
D, C, E, G, H, A, B
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A √
B √
C √
D √
E √
F
G √
H √
No unvisited nodes adjacent to
A. Backtrack (pop the stack).
H
G
E
D
The order nodes are visited:
D, C, E, G, H, A, B
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A √
B √
C √
D √
E √
F
G √
H √
No unvisited nodes adjacent to
H. Backtrack (pop the
stack).
G
E
D
The order nodes are visited:
D, C, E, G, H, A, B
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A √
B √
C √
D √
E √
F
G √
H √
No unvisited nodes adjacent to
G. Backtrack (pop the
stack).
E
D
The order nodes are visited:
D, C, E, G, H, A, B
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A √
B √
C √
D √
E √
F
G √
H √
No unvisited nodes adjacent to
E. Backtrack (pop the stack).
D
The order nodes are visited:
D, C, E, G, H, A, B
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A √
B √
C √
D √
E √
F
G √
H √
F is unvisited and is adjacent to
D. Decide to visit F next.
D
The order nodes are visited:
D, C, E, G, H, A, B
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A √
B √
C √
D √
E √
F √
G √
H √
Visit F
F
D
The order nodes are visited:
D, C, E, G, H, A, B, F
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A √
B √
C √
D √
E √
F √
G √
H √
No unvisited nodes adjacent to
F. Backtrack.
D
The order nodes are visited:
D, C, E, G, H, A, B, F
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A √
B √
C √
D √
E √
F √
G √
H √
No unvisited nodes adjacent to
D. Backtrack.
The order nodes are visited:
D, C, E, G, H, A, B, F
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughVisited Array
A √
B √
C √
D √
E √
F √
G √
H √
Stack is empty. Depth-first
traversal is done.
The order nodes are visited:
D, C, E, G, H, A, B, F
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Consider Trees
1. What depth-first traversals do you know?
2. How do the traversals differ?
3. In the walk-through, we visited a node just as
we pushed the node onto the stack. Is there
another time at which you can visit the node?
4. Conduct a depth-first search of the same
graph using the strategy you came up with in
#3.
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Breadth-First Search
Can be used to attempt to visit all nodes of a
graph in a systematic manner
Works with directed and undirected graphs
Works with weighted and unweighted graphs
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Overview
Task: Conduct a breadth-first search of
the graph starting with node D
Breadth-first search starts
with given node
0
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Overview
Nodes visited: D
Breadth-first search starts
with given node
Then visits nodes adjacent
in some specified order
(e.g., alphabetical)
Like ripples in a pond
0
1
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Overview
Nodes visited: D, C
Breadth-first search starts
with given node
Then visits nodes adjacent
in some specified order
(e.g., alphabetical)
Like ripples in a pond
0
1
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Overview
Nodes visited: D, C, E
Breadth-first search starts
with given node
Then visits nodes adjacent
in some specified order
(e.g., alphabetical)
Like ripples in a pond
0
1
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Overview
Nodes visited: D, C, E, F
Breadth-first search starts
with given node
Then visits nodes adjacent
in some specified order
(e.g., alphabetical)
Like ripples in a pond
0
1
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Overview
Nodes visited: D, C, E, F, G
When all nodes in ripple
are visited, visit nodes in
next ripples
0
2 1
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Overview
Nodes visited: D, C, E, F, G, H
When all nodes in ripple
are visited, visit nodes in
next ripples
0
2 1
3
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Overview
Nodes visited: D, C, E, F, G, H, A
When all nodes in ripple
are visited, visit nodes in
next ripples
0
2 1
3
4
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Overview
Nodes visited: D, C, E, F, G, H, A, B
When all nodes in ripple
are visited, visit nodes in
next ripples
0
2 1
3
4
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughEnqueued Array
A
B
C
D
E
F
G
H
How is this accomplished? Simply replace the stack
with a queue! Rules: (1) Maintain an enqueued
array. (2) Visit node when dequeued.
Q
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughEnqueued Array
A
B
C
D √
E
F
G
H
Enqueue D. Notice, D not yet visited.
Q D
Nodes visited:
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughEnqueued Array
A
B
C √
D √
E √
F √
G
H
Dequeue D. Visit D. Enqueue unenqueued nodes
adjacent to D.
Q C E F
Nodes visited: D
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughEnqueued Array
A
B
C √
D √
E √
F √
G
H
Dequeue C. Visit C. Enqueue unenqueued nodes
adjacent to C.
Q E F
Nodes visited: D, C
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughEnqueued Array
A
B
C √
D √
E √
F √
G
H
Dequeue E. Visit E. Enqueue unenqueued nodes
adjacent to E.
Q F G
Nodes visited: D, C, E
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughEnqueued Array
A
B
C √
D √
E √
F √
G √
H
Dequeue F. Visit F. Enqueue unenqueued nodes
adjacent to F.
Q G
Nodes visited: D, C, E, F
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughEnqueued Array
A
B
C √
D √
E √
F √
G √
H √
Dequeue G. Visit G. Enqueue unenqueued nodes
adjacent to G.
Q H
Nodes visited: D, C, E, F, G
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughEnqueued Array
A √
B √
C √
D √
E √
F √
G √
H √
Dequeue H. Visit H. Enqueue unenqueued nodes
adjacent to H.
Q A B
Nodes visited: D, C, E, F, G, H
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughEnqueued Array
A √
B √
C √
D √
E √
F √
G √
H √
Dequeue A. Visit A. Enqueue unenqueued nodes
adjacent to A.
Q B
Nodes visited: D, C, E, F, G, H, A
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughEnqueued Array
A √
B √
C √
D √
E √
F √
G √
H √
Dequeue B. Visit B. Enqueue unenqueued nodes
adjacent to B.
Q empty
Nodes visited: D, C, E, F, G, H,
A, B
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
A
H
B
F
E
D
C
G
Walk-ThroughEnqueued Array
A √
B √
C √
D √
E √
F √
G √
H √
Q empty. Algorithm done.
Q empty
Nodes visited: D, C, E, F, G, H,
A, B
Top Related