Page 1
© 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/
Page 2
© 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.
Page 3
© 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
Page 4
© 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.
Page 5
© 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.
Page 6
© 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.
Page 7
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Dijkstra’s Algorithm
Dijkstra’s AlgorithmSingle Source Multiple Destination
Shortest Path Algorithm
Page 8
© 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
Page 9
© 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)
Page 10
© 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
Page 11
© 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
Page 12
© 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
Page 13
© 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
Page 14
© 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
Page 15
© 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
Page 16
© 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
Page 17
© 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
Page 18
© 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
Page 19
© 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
Page 20
© 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
Page 21
© 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
Page 22
© 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
Page 23
© 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
Page 24
© 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
Page 25
© 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
Page 26
© 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
Page 27
© 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)
Page 28
© 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
Page 29
© 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
Page 30
© 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
Page 31
© 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
Page 32
© 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
Page 33
© 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
Page 34
© 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
Page 35
© 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
Page 36
© 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
Page 37
© 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
Page 38
© 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
Page 39
© 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
Page 40
© 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
Page 41
© 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
Page 42
© 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
Page 43
© 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
Page 44
© 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
Page 45
© 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
Page 46
© 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
Page 47
© 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
Page 48
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE
Exercise-MST
Page 49
© 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
Page 50
© 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
Page 51
© 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
Page 52
© 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
Page 53
© 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
Page 54
© 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
Page 55
© 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
Page 56
© 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
Page 57
© 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
Page 58
© 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
Page 59
© 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
Page 60
© 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
Page 61
© 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
Page 62
© 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
Page 63
© 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
Page 64
© 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
Page 65
© 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
Page 66
© 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
Page 67
© 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
Page 68
© 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
Page 69
© 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
Page 70
© 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
Page 71
© 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
Page 72
© 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
Page 73
© 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
Page 74
© 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
Page 75
© 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
Page 76
© 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
Page 77
© 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
Page 78
© 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
Page 79
© 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
Page 80
© 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
Page 81
© 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
Page 82
© 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
Page 83
© 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
Page 84
© 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
Page 85
© 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
Page 86
© 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
Page 87
© 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
Page 88
© 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
Page 89
© 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
Page 90
© 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.
Page 91
© 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
Page 92
© 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
Page 93
© 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
Page 94
© 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
Page 95
© 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
Page 96
© 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
Page 97
© 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
Page 98
© 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
Page 99
© 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
Page 100
© 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
Page 101
© 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
Page 102
© 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:
Page 103
© 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
Page 104
© 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
Page 105
© 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
Page 106
© 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
Page 107
© 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
Page 108
© 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
Page 109
© 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
Page 110
© 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
Page 111
© 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