21
An Analytical Approach to the BFS vs. DFS Algorithm Selection Problem 1 Tom Everitt Marcus Hutter Australian National University September 3, 2015 Everitt, T. and Hutter, M. (2015a). Analytical Results on the BFS vs. DFS Algorithm Selection Problem. Part I: Tree Search. In 28th Australian Joint Conference on Artificial Intelligence Everitt, T. and Hutter, M. (2015b). Analytical Results on the BFS vs. DFS Algorithm Selection Problem. Part II: Graph Search. In 28th Australian Joint Conference on Artificial Intelligence 1 BFS=Breadth-first search, DFS=Depth-first search Tom Everitt , Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 1 / 21

An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

An Analytical Approach to the BFS vs. DFS AlgorithmSelection Problem1

Tom Everitt Marcus Hutter

Australian National University

September 3, 2015

Everitt, T. and Hutter, M. (2015a). Analytical Results on the BFS vs.DFS Algorithm Selection Problem. Part I: Tree Search.In 28th Australian Joint Conference on Artificial Intelligence

Everitt, T. and Hutter, M. (2015b). Analytical Results on the BFS vs.DFS Algorithm Selection Problem. Part II: Graph Search.In 28th Australian Joint Conference on Artificial Intelligence

1BFS=Breadth-first search, DFS=Depth-first searchTom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 1 / 21

Page 2: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

Outline

1 Motivation and Background

2 Simple modelExpected RuntimesDecision Boundary

3 More General Models

4 Experimental Results

5 Conclusions

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 2 / 21

Page 3: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

Motivation

(Graph) search is a fundamental AI problem:planning, learning, problem solving

Hundreds of algorithms have been developed, including metaheuristicssuch as simulated annealing, genetic algorithms.

These are often heuristically motivated, lacking solid theoreticalfooting.

For theoretical approach, return to basics: BFS and DFS.

So far, mainly worst-case results have been available (we focus onaverage/expected runtime).

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 3 / 21

Page 4: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

Breadth-first Search (BFS)

Korf et al. (2001) found a clever wayto analyse IDA*, which essentially isa generalisation of BFS.

Later generalised by Zahavi et al.(2010).

Both are essentially worst-case re-sults.

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 4 / 21

Page 5: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

Depth-first Search (DFS)

Knuth (1975) developed a way to estimate search tree size and DFSworst-case performance.

s0

Assume the same number of childrenin other branches.Estimate ≈ 2 · 3 · 3 · 2 = 36 leaves.

Refinements and applications

Purdom (1978): Use severalbranches instead of one

Chen (1992): Use stratifiedsampling

Kilby et al. (2006): Theestimates can be used to selectbest SAT algorithm

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 5 / 21

Page 6: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

Potential gains

We focus on average or expected runtime of BFS and DFS rather thanworst-case.

Selling points:

Good to have an idea how long a search might take

Useful for algorithm selection (Rice, 1975)

May be used for constructing meta-heuristics

Precise understanding of basics often useful

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 6 / 21

Page 7: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

BFS and DFS

BFS and DFS are opposites.

BFS

1

2

4

8 9

5

10 11

3

6

12 13

7

14 15

focuses near the start node

DFS

1

2

3

4 5

6

7 8

9

10

11 12

13

14 15

focuses far from the start node

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 7 / 21

Page 8: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

Formal setups

We analyse BFS and DFS expected runtime in a sequence of increasinglygeneral models.

1 Tree with a single level of goals

2 Tree with multiple levels of goals

3 General graph

Increasingly coarse approximations are required

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 8 / 21

Page 9: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

Simplest model - Tree with Single Goal Level

Our simplest model assumes a complete tree with:

D = 3, g = 2, p = 1/3

A max search depth D ∈ N,

A goal level g ∈ {0, . . . , D}Nodes on level g are goals withgoal probability p ∈ [0, 1] (iid).

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 9 / 21

Page 10: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

BFS Runtime

1

2

4

8 9

5

10 11

3

6

12 13

7

14 15

Expected BFS search time is

E[tBFS] = 2g − 1 + 1/p

Proof. The position Y of the firstgoal is geometrically distributed withE[Y ] = 1/p.

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 10 / 21

Page 11: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

DFS Runtime

1

2

3

4 5

6

7 8

9

10

11 12

13

14 15

Expected DFS search time is

E[tDFS] ≈ (1/p− 1)︸ ︷︷ ︸number ofsubtrees

2D−g+1︸ ︷︷ ︸size ofsubtrees

Proof. There are (1/p− 1) red mini-trees of size ≈ 2D−g+1. It turns outthat the blue nodes do not substan-tially affect the count in most cases.

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 11 / 21

Page 12: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

4 6 8 10 12 14 16

102

103

104

g

exp

ecte

dse

arch

tim

e

DFSBFS

Expected BFS and DFS search time as a function of goal depth in a treeof depth D = 15, and goal probability p = 0.07.The initially high expectation of BFS is because likely no goal exists whole tree searched (artefact of model).

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 12 / 21

Page 13: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

BFS vs. DFS

Combining the runtime estimates yields an elegant decision boundary forwhen BFS is better:

E[tBFS]− E[tDFS] < 0︸ ︷︷ ︸BFS Better

⇐⇒ g < D/2 + γ

where γ = log2(1−pp )/2 is inversely related to p

(γ small when p not very close to 0 or 1).

Observations:

BFS is better when goal near start node (expected)

DFS benefits when p is large

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 13 / 21

Page 14: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

BFS vs. DFS

4 6 8 10 12 14 162

4

6

8

10

12

14

16

D

g

DFS winsBFS winsBFS=DFS

E[tBFS] = E[tDFS]

Plot of BFS vs. DFS decision boundary with goal level g and goalprobability p = 0.07. The decision boundary gets 79% of the winnerscorrect.

Time to generalise.

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 14 / 21

Page 15: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

Tree with Multiple Goal Levels

As before, assume a complete tree with:

D = 3, p = [0, 13 ,13 ,

13 ]

A maximum search depth D

Instead of goal level g and goalprobability p:Use a goal probability vectorp = [p0, . . . , pD]. Nodes onlevel k are goals with iidprobability pk.

This is arguably much more realistic :) ways to estimate the goalprobabilities is an important future question.

Both BFS and DFS analysis can be carried back to the single goallevel case with some hacks.BFS analysis is fairly straightforwardDFS requires approximation of geometric distribution withexponential distribution

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 15 / 21

Page 16: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

Decision Boundary

10−2 10−1 100 101 102

6

8

10

12

14

σ2

µ

DFS winsBFS wins

tBFSMGL=t

DFSMGL

The goal probabilities are highest at a peak level µ, and decays around itdepending on σ2.Some takeaways:

BFS still likes goals close to the root

BFS likes larger spread more than DFS does (increases probability ofreally easy goal)

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 16 / 21

Page 17: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

General graphs

BFS1

2

4

8 9

5

10 11

3

6

12 13

7

14 15

DFS1

2

3

4 5

7

6 8

13

9

10 11

14

12 15

We capture the various topological properties of graphs in a collection ofparameters called the descendant counter.

Similarly to before, we get approximate expressions for BFS and DFSexpected runtime given a goal probability vector.

We analytically derive the descendant counter for two concrete grammarproblems (it could potentially be inferred empirically in other cases).

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 17 / 21

Page 18: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

One observation is that DFS can spend an even greater fraction of theinitial search time far away from the root.

5 10 15 20

102

104

106

g

Complete Binary Tree

tBFSSGL

tDFSSGL

5 10 15 20

102

104

106

g

Binary Grammar

tBFSBG

tDFSBG

tDFSBGL

tDFSBGU

So BFS will be better for a wider range of goal levels in graph search thanin tree search.

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 18 / 21

Page 19: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

Experimental results

We randomly generate graphs according to a wide range of parametersettings.

BFS always accurate.

DFS in trees:Usually within 10% error; in some corner cases up to 50% error.

DFS in binary grammar problem (non-tree graph):Mostly within 20% error; 35% at worst.

More detailed results in paper.

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 19 / 21

Page 20: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

Conclusions

With our model of goal distribution, we can predict expected search timeof BFS and DFS (instead of only worst-case), given goal probabilities forall distances.

Further work needed to automatically infer parameters.

This theoretical understanding can hopefully be useful when:

Choosing search method

Constructing meta-heuristics

Analysing performance of more complex search algorithms (forexample, A* is a generalisation of BFS, and Beam Search is ageneralisation of DFS).

Choosing graph representation of search problem.

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 20 / 21

Page 21: An Analytical Approach to the BFS vs. DFS Algorithm ... · Choosing search method Constructing meta-heuristics Analysing performance of more complex search algorithms (for example,

References

Chen, P. C. (1992). Heuristic Sampling: A Method for Predicting the Performance ofTree Searching Programs. SIAM Journal on Computing, 21(2):295–315.

Everitt, T. and Hutter, M. (2015a). Analytical Results on the BFS vs. DFS AlgorithmSelection Problem. Part I: Tree Search. In 28th Australian Joint Conference onArtificial Intelligence.

Everitt, T. and Hutter, M. (2015b). Analytical Results on the BFS vs. DFS AlgorithmSelection Problem. Part II: Graph Search. In 28th Australian Joint Conference onArtificial Intelligence.

Kilby, P., Slaney, J., Thiebaux, S., and Walsh, T. (2006). Estimating Search Tree Size.In Proc. of the 21st National Conf. of Artificial Intelligence, AAAI, Menlo Park.

Knuth, D. E. (1975). Estimating the efficiency of backtrack programs. Mathematics ofComputation, 29(129):122–122.

Korf, R. E., Reid, M., and Edelkamp, S. (2001). Time complexity ofiterative-deepening-A*. Artificial Intelligence, 129(1-2):199–218.

Purdom, P. W. (1978). Tree Size by Partial Backtracking. SIAM Journal on Computing,7(4):481–491.

Rice, J. R. (1975). The algorithm selection problem. Advances in Computers, 15:65–117.

Zahavi, U., Felner, A., Burch, N., and Holte, R. C. (2010). Predicting the performanceof IDA* using conditional distributions. Journal of Artificial Intelligence Research,37:41–83.

Tom Everitt, Marcus Hutter (ANU) BFS vs. DFS September 3, 2015 21 / 21