advanced dfs, bfs, graph modeling

60

Click here to load reader

Upload: kort

Post on 08-Jan-2016

93 views

Category:

Documents


7 download

DESCRIPTION

Advanced DFS, BFS, Graph Modeling. 19/2/2005. Introduction. Depth-first search (DFS) Breadth-first search (BFS) Graph Modeling Model a graph from a problem, ie. transform a problem into a graph problem. vertex. edge. What is a graph?. A set of vertices and edges. ancestors. root. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Advanced DFS, BFS, Graph Modeling

1

Advanced DFS, BFS, Graph Modeling

19/2/2005

Page 2: Advanced DFS, BFS, Graph Modeling

2

Introduction

• Depth-first search (DFS)

• Breadth-first search (BFS)

• Graph Modeling– Model a graph from a problem, ie.

transform a problem into a graph problem

Page 3: Advanced DFS, BFS, Graph Modeling

3

What is a graph?• A set of vertices and edges

vertex

edge

Page 4: Advanced DFS, BFS, Graph Modeling

4

Trees and related terms

root

siblings

descendents children

ancestors

parent

Page 5: Advanced DFS, BFS, Graph Modeling

5

Graph Traversal• Given: a graph• Goal: visit all (or some) vertices and edges of

the graph using some strategy (the order of visit is systematic)

• DFS, BFS are examples of graph traversal algorithms

• Some shortest path algorithms and spanning tree algorithms have specific visit order

Page 6: Advanced DFS, BFS, Graph Modeling

6

Intuition of DFS of BFS• This is a brief idea of DFS, BFS

• DFS: continue visiting next vertex whenever there is a road, go back if no road (ie. visit to the depth of current path)– Example: a human want to visit a place, but do not know the

path

• BFS: go through all the adjacent vertices before going further (ie. spread among next vertices)– Example: set a house on fire, the fire will spread through the

house

Page 7: Advanced DFS, BFS, Graph Modeling

7

DFS (pseudo code)

DFS (vertex u) {mark u as visitedfor each vertex v directly reachable from u

if v is unvisitedDFS (v)

}

• Initially all vertices are marked as unvisited

Page 8: Advanced DFS, BFS, Graph Modeling

8

F

A

BC

D

E

DFS (Demonstration)

unvisited

visited

Page 9: Advanced DFS, BFS, Graph Modeling

9

“Advanced” DFS

• Apart from just visiting the vertices, DFS can also provide us with valuable information

• DFS can be enhanced by introducing:– birth time and death time of a vertex

• birth time: when the vertex is first visited• death time: when we retreat from the vertex

– DFS tree– parent of a vertex

Page 10: Advanced DFS, BFS, Graph Modeling

10

DFS spanning tree / forest• A rooted tree• The root is the start vertex• If v is first visited from u, then u is the parent of v in

the DFS tree• Edges are those in forward direction of DFS, ie. when

visiting vertices that are not visited before

• If some vertices are not reachable from the start vertex, those vertices will form other spanning trees (1 or more)

• The collection of the trees are called forest

Page 11: Advanced DFS, BFS, Graph Modeling

11

DFS (pseudo code)

DFS (vertex u) {

mark u as visited

time time+1

for each vertex v directly reachable from u

if v is unvisited

DFS (v)

time time+1

}

Page 12: Advanced DFS, BFS, Graph Modeling

12

A

F

B

C

D

E

GH

DFS forest (Demonstration)

unvisited

visited

visited (dead)

A B C D E F G H

birth

death

parent

A

B

C

F

E

D

G

1 2 3 13 10 4 14

12 9 8 16 11 5 15

H

6

7

- A B - A C D C

Page 13: Advanced DFS, BFS, Graph Modeling

13

Classification of edges

• Tree edge• Forward edge• Back edge• Cross edge

• Question: which type of edges is always absent in an undirected graph?

A

B

C

F

E

D

G

H

Page 14: Advanced DFS, BFS, Graph Modeling

14

Determination of edge types• How to determine the type of an arbitrary

edge (u, v) after DFS?• Tree edge

– parent [v] = u

• Forward edge– not a tree edge; and– birth [v] > birth [u]; and– death [v] < death [u]

• How about back edge and cross edge?

Page 15: Advanced DFS, BFS, Graph Modeling

15

Determination of edge types

Tree edge

Forward Edge Back Edge Cross Edge

parent [v] = u

not a tree edge

birth[v] > birth[u]

death[v] < death[u]

birth[v] < birth[u]

death[v] > death[u]

birth[v] > birth[u]

death[v] >death[u]

Page 16: Advanced DFS, BFS, Graph Modeling

16

Applications of DFS Forests

• Topological sorting (Tsort)

• Strongly-connected components (SCC)

• Some more “advanced” algorithms

Page 17: Advanced DFS, BFS, Graph Modeling

17

Example: Tsort• Topological order: A numbering of the

vertices of a directed acyclic graph such that every edge from a vertex numbered i to a vertex numbered j satisfies i<j

• Tsort: Number the vertices in topological order 1

2

3

4 5

6

7

Page 18: Advanced DFS, BFS, Graph Modeling

18

Tsort Algorithm• If the graph has more then one vertex that

has indegree 0, add a vertice to connect to all indegree-0 vertices

• Let the indegree 0 vertice be s• Use s as start vertice, and compute the DFS

forest• The death time of the vertices represent the

reverse of topological order

Page 19: Advanced DFS, BFS, Graph Modeling

19

S

D

B

E F

C

G

A

S A B C D E F G

birth 1 2 3 4 12 13 8 5

death 16 11 10 7 15 14 9 6

G C F B A E D D E A B F C G

Page 20: Advanced DFS, BFS, Graph Modeling

20

Example: SCC• A graph is strongly-connected if

– for any pair of vertices u and v, one can go from u to v and from v to u.

• Informally speaking, an SCC of a graph is a subset of vertices that– forms a strongly-connected subgraph– does not form a strongly-connected

subgraph with the addition of any new vertex

Page 21: Advanced DFS, BFS, Graph Modeling

21

SCC (Illustration)

Page 22: Advanced DFS, BFS, Graph Modeling

22

SCC (Algorithm)• Compute the DFS forest of the graph G to

get the death time of the vertices• Reverse all edges in G to form G’• Compute a DFS forest of G’, but always

choose the vertex with the latest death time when choosing the root for a new tree

• The SCCs of G are the DFS trees in the DFS forest of G’

Page 23: Advanced DFS, BFS, Graph Modeling

23

A

F

B

C

D

GH

SCC (Demonstration)

A

F

B

C

D

E

GH

A B C D E F G H

birth

death

parent

1 2 3 13 10 4 14

12 9 8 16 11 5 15

6

7

- A B - A C D C

D

G

A E B

F

C

H

Page 24: Advanced DFS, BFS, Graph Modeling

24

SCC (Demonstration)

D

G

A E B

F

C

H

A

F

B

C

D

GH

E

Page 25: Advanced DFS, BFS, Graph Modeling

25

DFS Summary

• DFS spanning tree / forest

• We can use birth time and death time in DFS spanning tree to do varies things, such as Tsort, SCC

• Notice that in the previous slides, we related birth time and death time. But in the discussed applications, birth time and death time can be independent, ie. birth time and death time can use different time counter

Page 26: Advanced DFS, BFS, Graph Modeling

26

DFS (vertex u) {

mark u as visited

birthtime birthtime + 1

for each vertex v directly reachable from u

if v is unvisited

DFS (v)

deathtime deathtime + 1

}

time time+1

time time+1

Page 27: Advanced DFS, BFS, Graph Modeling

27

Breadth-first search (BFS)

• In order to “spread”, we need to makes use of a data structure, queue ,to remember just visited vertices

• Revised:• DFS: continue visiting next

vertex whenever there is a road, go back if no road (ie. visit to the depth of current path)

• BFS: go through all the adjacent vertices before going further (ie. spread among next vertices)

Page 28: Advanced DFS, BFS, Graph Modeling

28

BFS (Pseudo code)while queue not empty

dequeue the first vertex u from queue

for each vertex v directly reachable from u

if v is unvisited

enqueue v to queue

mark v as visited

• Initially all vertices except the start vertex are marked as unvisited and the queue contains the start vertex only

Page 29: Advanced DFS, BFS, Graph Modeling

29

A

B

C

D

E

F

G

H

I

J

BFS (Demonstration)

unvisited

visited

visited (dequeued)

Queue: A B C F D E H G J I

Page 30: Advanced DFS, BFS, Graph Modeling

30

Applications of BFS

• Shortest paths finding

• Flood-fill (can also be handled by DFS)

Page 31: Advanced DFS, BFS, Graph Modeling

31

Comparisons of DFS and BFS

DFS BFS

Depth-first Breadth-first

Stack Queue

Does not guarantee shortest paths

Guarantees shortest paths

Page 32: Advanced DFS, BFS, Graph Modeling

32

Bidirectional search (BDS)

• Searches simultaneously from both the start vertex and goal vertex

• Commonly implemented as bidirectional BFS

start goal

Page 33: Advanced DFS, BFS, Graph Modeling

33

BDS Example: Bomber Man (1 Bomb)

• find the shortest path from the upper-left corner to the lower-right corner in a maze using a bomb. The bomb can destroy a wall.

S

E

Page 34: Advanced DFS, BFS, Graph Modeling

34

Bomber Man (1 Bomb)

S

E

1 2 3

4

1234

5

4

Shortest Path length = 7

5

6 67 78 8

9

9

10

10

11

11

12

12 12

13

13

What will happen if we stop once we find a path?

Page 35: Advanced DFS, BFS, Graph Modeling

35

S 1 2 3 4 5 6 7 8 9

10

21 11

20 12

19 13

18 14

17 17 16 15

11 12 13 14 15 16

10

9 8 7 6 5 4 3 2 1 E

Example

Page 36: Advanced DFS, BFS, Graph Modeling

36

Iterative deepening search (IDS)

• Iteratively performs DFS with increasing depth bound

• Shortest paths are guaranteed

Page 37: Advanced DFS, BFS, Graph Modeling

37

IDS

Page 38: Advanced DFS, BFS, Graph Modeling

38

IDS (pseudo code)DFS (vertex u, depth d) {

mark u as visitedif (d>0)

for each vertex v directly reachable from uif v is unvisited

DFS (v,d-1)}

i=0Do {

DFS(start vertex,i)Increment i

}While (target is not found)

Page 39: Advanced DFS, BFS, Graph Modeling

39

IDS Complexity (the details can be skipped)

• ( )=bm

• ( ) =bm

1

0

1

1

nnk

k

rr

r

1

(1 )

1

nnk

k

r rr

r

1

0

1

1

ddk

dk

bt b

b

0

1

0

1

2

1

1

1 (1 )

1 (1 )

m

ii

im

i

m

t

b

b

m b b

b b

0

m

ii

t

- b is branching factor- td is the number of vertices visited for depth d

11

1

m

m

bt

b

mt

Page 40: Advanced DFS, BFS, Graph Modeling

40

IDS

• Conclusion: the complexity of IDS is the same as DFS

• ( )=bm

• ( ) =bm0

m

ii

tmt

- b is branching factor- td is the number of vertices visited for depth d

Page 41: Advanced DFS, BFS, Graph Modeling

41

Summary of DFS, BFS

• We learned some variations of DFS and BFS – Bidirectional search (BDS) – Iterative deepening search (IDS)

Page 42: Advanced DFS, BFS, Graph Modeling

42

What is graph modeling?

• Conversion of a problem into a graph problem

• Sometimes a problem can be easily solved once its underlying graph model is recognized

• Graph modeling appears almost every year in NOI or IOI

Page 43: Advanced DFS, BFS, Graph Modeling

43

Basics of graph modeling

• A few steps:– identify the vertices and the edges– identify the objective of the problem– state the objective in graph terms– implementation:

• construct the graph from the input instance• run the suitable graph algorithms on the graph• convert the output to the required format

Page 44: Advanced DFS, BFS, Graph Modeling

44

Simple examples (1)• Given a grid maze with obstacles, find a

shortest path between two given points

start

goal

Page 45: Advanced DFS, BFS, Graph Modeling

45

Simple examples (2)

• A student has the phone numbers of some other students

• Suppose you know all pairs (A, B) such that A has B’s number

• Now you want to know Alan’s number, what is the minimum number of calls you need to make?

Page 46: Advanced DFS, BFS, Graph Modeling

46

Simple examples (2)

• Vertex: student

• Edge: whether A has B’s number

• Add an edge from A to B if A has B’s number

• Problem: find a shortest path from your vertex to Alan’s vertex

Page 47: Advanced DFS, BFS, Graph Modeling

47

Complex examples (1)• Same settings as simple example 1

• You know a trick – walking through an obstacle! However, it can be used for only once (Bomber Man with 1 bomb)

• What should a vertex represent?– your position only?– your position + whether you have used the

trick

Page 48: Advanced DFS, BFS, Graph Modeling

48

Complex examples (1)

• A vertex is in the form (position, used)

• The vertices are divided into two groups– trick used– trick not used

Page 49: Advanced DFS, BFS, Graph Modeling

49

Complex examples (1)

start

goal

unused

used

start goal

goal

Page 50: Advanced DFS, BFS, Graph Modeling

50

Complex examples (1)

• How about you can walk through obstacles for k times?

Page 51: Advanced DFS, BFS, Graph Modeling

51

Complex examples (2)• The famous 8-puzzle

• Given a state, find the moves that bring it to the goal state

1 2 3

4 5 6

7 8

Page 52: Advanced DFS, BFS, Graph Modeling

52

Complex examples (2)• What does a vertex represent?

– the position of the empty square?– the number of tiles that are in wrong

positions?– the state (the positions of the eight tiles)

• What are the edges?

• What is the equivalent graph problem?

Page 53: Advanced DFS, BFS, Graph Modeling

53

Complex examples (2)1 2 34 5 67 8

1 2 34 5 67 8

1 2 34 57 8 6

1 2 34 67 5 8

1 2 34 5 6

7 8

1 24 5 37 8 6

1 2 34 57 8 6

Page 54: Advanced DFS, BFS, Graph Modeling

54

Complex examples (3)• Theseus and Minotaur

– http://www.logicmazes.com/theseus.html– Extract:

• Theseus must escape from a maze. There is also a mechanical Minotaur in the maze. For every turn that Theseus takes, the Minotaur takes two turns. The Minotaur follows this program for each of his two turns:

• First he tests if he can move horizontally and get closer to Theseus. If he can, he will move one square horizontally. If he can’t, he will test if he could move vertically and get closer to Theseus. If he can, he will move one square vertically. If he can’t move either horizontally or vertically, then he just skips that turn.

Page 55: Advanced DFS, BFS, Graph Modeling

55

Complex examples (3)• What does a vertex represent?

– Theseus’ position– Minotaur’s position– Both

Page 56: Advanced DFS, BFS, Graph Modeling

56

Some more examples• How can the followings be modeled?

– Tilt maze (Single-goal mazes only)• http://www.clickmazes.com/newtilt/ixtilt2d.htm

– Double title maze• http://www.clickmazes.com/newtilt/ixtilt.htm

– No-left-turn maze• http://www.clickmazes.com/noleft/ixnoleft.htm

– Same as complex example 1, but you can use the trick for k times

Page 57: Advanced DFS, BFS, Graph Modeling

57

Competition problems• HKOI2000 S – Wormhole Labyrinth• HKOI2001 S – A Node Too Far• HKOI2004 S – Teacher’s Problem *• TFT2001 – OIMan * • TFT2002 – Bomber Man *• NOI2001 – cung1 ming4 dik7 daa2 zi6 jyun4• NOI2001 – Equation *• IOI2000 – Walls • IOI2002 – Troublesome Frog• IOI2003 – Amazing Robots

Page 58: Advanced DFS, BFS, Graph Modeling

58

Teacher’s Problem

• Question: A teacher wants to distribute sweets to students in an order such that, if student u tease student v, u should not get the sweet before v

• Vertex: student• Edge: directed, (v,u) is a directed edge if

student v tease u• Algorithm: Tsort

Page 59: Advanced DFS, BFS, Graph Modeling

59

OI Man• Question: OIMan (O) has 2 kinds of form:

H- and S-form. He can transform to S-form for m minutes by battery. He can only kill monsters (M) and virus (V) in S-form. Given the number of battery and m, find the minimum time needed to kill the virus.

• Vertex: position, form, time left for S-form, number of batteries left

• Edge: directed– Move to P in H-form (position)– Move to P/M/V in S-form (position, S-form time)– Use battery and move (position, form, S-form

time, number of batteries)

PWWPPPPPPWWPOMMV

Page 60: Advanced DFS, BFS, Graph Modeling

60

Equation

• Question: Find the number of solution of xi, given ki,pi. 1<=n<=6, 1<=xi<=150

• Vertex: possible values of – k1x1

p1 , k1x1p1 + k2x2

p2 , k1x1p1 + k2x2

p2 + k3x3p3 , k4x4

p4 , k4x4

p4 + k5x5p5 , k4x4

p4 + k5x5p5 + k6x6

p6