111
© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE THEORY AND PROBLEMS OF DISCRETE MATHEMATICS (3 RD EDITION)-SEYMOUR LIPSCHUTZ Chapter 8: Graph Theory Biplab C. Debnath Lecturer Dept. of Computer Science and Engineering https://www.facebook.com/groups/bcdcse/

Graph theory discrete mathmatics

Embed Size (px)

Citation preview

Page 1: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE

Dijkstra’s Algorithm

Dijkstra’s AlgorithmSingle Source Multiple Destination

Shortest Path Algorithm

Page 8: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© Biplab C. Debnath CSE 105 Discrete Mathematics Lecturer, CSE

Exercise-MST

Page 49: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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: Graph theory discrete mathmatics

© 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