counting all possible dfs paths

45
Counting All Possible DFS Paths Ryan Flint and Thomas Beard Computer Science Graduates University of Tennessee April 23, 2020

Upload: others

Post on 17-Mar-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Counting All Possible DFS Paths

Counting All Possible DFS Paths

Ryan Flint and Thomas BeardComputer Science Graduates

University of Tennessee

April 23, 2020

Page 2: Counting All Possible DFS Paths

Questions

1. Who is the person credited with first discovering DFS?

2. What is the runtime of DFSCount?

3. What problem is similar to DFSCount?

Page 3: Counting All Possible DFS Paths

The Presenters – Ryan FlintThe Presenters – Ryan Flint

• Born here in Knoxville TN• BS in CS in 2019 (Currently 5-year MS/BS)• Refereeing youth soccer for 10 years• Not anymore (thanks COVID-19…)

• Playing piano since age 5• Trained classically• I strongly prefer improvisation

Page 4: Counting All Possible DFS Paths

The Presenters – Thomas Beard• Recently accepted a position as a software developer at Cisco• Love all forms of games. Some of my favorites are:• Board Game – 7 Wonders• Video Game – Zelda series• Sports – Basketball & Snowboarding

Page 5: Counting All Possible DFS Paths

Overview

• History

• Explanation of the Algorithm

• Results

• Applications

Page 6: Counting All Possible DFS Paths

History – DFS

• Investigated Charles Pierre Trémaux in the 19th century• He used it to solve mazes

• Trémaux Tree (a.k.a. Normal Spanning Tree)• Every 2 connected nodes must share a

parent/child relationship

• Trémaux’s Algorithm• Used to solve a maze (like IRL)• Early example of DFS

https://www.geeksforgeeks.org/binary-tree-data-structure/

Page 7: Counting All Possible DFS Paths

Connected Components – A Recap

• A Connected Component within a graph is a set of nodes in which there exists a path from one node to every other node• We can have multiple connected

components within a graph• And we can use DFS to find these connected

components

https://en.wikipedia.org/wiki/Component_(graph_theory)

Page 8: Counting All Possible DFS Paths

The Problem

• Given an unweighted, undirected, connected graph

• Determine how many possible DFS orderings there are

• DFS can start at any point in the graph

Page 9: Counting All Possible DFS Paths

Example

O

7 8 9

4 5 6

7 8 9

1 2 3

• 36 permutations

• How do we get this result?

Page 10: Counting All Possible DFS Paths

Initial Musings

O

7 8 9

4 5 6

7 8 9

1 2 3• Assume DFS starts with 0

• DFS can only proceed in three ways

• DFS will visit entire component before proceeding

Page 11: Counting All Possible DFS Paths

Initial Musings

O

7 8 9

4 5 6

7 8 9

1 2 3• Assume DFS starts with 0

• DFS can only proceed in three ways

• DFS will visit entire component before proceeding

Page 12: Counting All Possible DFS Paths

The Solution

• D(component, n)

• D(Red, 1)

• Only one way to traverse this component. Duh.

Page 13: Counting All Possible DFS Paths

The Solution (cont.)

• D(component, n)

• D(Blue, 4)

• One way to go through this one too.

Page 14: Counting All Possible DFS Paths

The Solution (cont.)

• D(component, n)

• D(Green, 7)

• The answer is one. What a surprise!

Page 15: Counting All Possible DFS Paths

The Solution (cont.)

• Each D(component, n) is independent

• D(Red, 1) * D(Blue, 4) * D(Green, 7)

• Assumes that DFS chooses the above ordering

• How to account for all other orderings?

Page 16: Counting All Possible DFS Paths

The Solution (cont.)

O

7 8 9

4 5 6

7 8 9

1 2 3• Recall: There are three

components

• 3! ways to go through the components

• Multiply previous result by 3!

Page 17: Counting All Possible DFS Paths

The Solution (cont.)

• 3! * ( D(Red, 1) * D(Blue, 4) * D(Green, 7) )

• 6 * (1 * 1 * 1) = 6

• D(Yellow, 0) = 6 permutations

• Repeat entire process for each node (1-9) i: D(Yellow, i)

Page 18: Counting All Possible DFS Paths

The Solution (cont.)

O

7 8 9

4 5 6

7 8 9

1 2 3• Assume DFS starts with 1

• DFS can only proceed in two ways

• Continuing with 2 is easy

• We know how to continue from node 0 from the previous slides

Page 19: Counting All Possible DFS Paths

The Solution (cont.)

• Node 0 disconnected

• Two components connected to 0

• D(Blue, 4) = 1

• D(Green, 7) = 1

Page 20: Counting All Possible DFS Paths

The Solution (cont.)• D(Blue, 4) * D(Green, 7)

• Multiply by 2! to account for both orders

• 2! * ( D(Blue, 4) * D(Green, 7) )

• 2 * ( 1 * 1 ) = 2 permutations = D(Teal, 0)

Page 21: Counting All Possible DFS Paths

The Solution (cont.)

O

7 8 9

4 5 6

7 8 9

1 2 3• D(Teal, 0) = 2

• D(Red, 2) = 1

• We can now compute D(Yellow, 1)

Page 22: Counting All Possible DFS Paths

The Solution (cont.)• D(Red, 1) * D(Teal, 0)

• Must account for both orders

• 2! * ( D(Red, 1) * D(Teal, 0) )

• 2 * (1 * 2) = 4

• D(Yellow, 1) = 4 permutations

Page 23: Counting All Possible DFS Paths

The Solution (cont.)

O

7 8 9

4 5 6

7 8 9

1 2 3• D(Yellow, 4) = 4

• Same as D(Yellow, 1)

Page 24: Counting All Possible DFS Paths

The Solution (cont.)

O

7 8 9

4 5 6

7 8 9

1 2 3• D(Yellow, 7) = 4

• Same as D(Yellow, 1)

Page 25: Counting All Possible DFS Paths

The Solution (cont.)

O

7 8 9

4 5 6

7 8 9

1 2 3• D(Yellow, 2) = 4

• Same as D(Yellow, 1)

Page 26: Counting All Possible DFS Paths

The Solution (cont.)

O

7 8 9

4 5 6

7 8 9

1 2 3• D(Yellow, 5) = 4

• Same as D(Yellow, 1)

Page 27: Counting All Possible DFS Paths

The Solution (cont.)

O

7 8 9

4 5 6

7 8 9

1 2 3• D(Yellow, 8) = 4

• Same as D(Yellow, 1)

Page 28: Counting All Possible DFS Paths

The Solution (cont.)

O

7 8 9

4 5 6

7 8 9

1 2 3• D(Yellow, 3) = 2

• Only one component

• Only two possible outputs

Page 29: Counting All Possible DFS Paths

The Solution (cont.)

O

7 8 9

4 5 6

7 8 9

1 2 3• D(Yellow, 6) = 2

• Only one component

• Only two possible outputs

Page 30: Counting All Possible DFS Paths

The Solution (cont.)

O

7 8 9

4 5 6

7 8 9

1 2 3• D(Yellow, 9) = 2

• Only one component

• Only two possible outputs

Page 31: Counting All Possible DFS Paths

The Solution (cont.)• D(Yellow, 0) + … + D(Yellow, 9)

• 6 + 4 + 4 + 2 + 4 + 4 + 2 + 4 + 4 + 2

• = 36 possible DFS outputs

Page 32: Counting All Possible DFS Paths

Generalized D(component, x)

Page 33: Counting All Possible DFS Paths

Generalized D(component, x)• Disconnect node x from the component

Page 34: Counting All Possible DFS Paths

Generalized D(component, x)• Disconnect node x from the component

• Determine components with edges to x (T of these)

Page 35: Counting All Possible DFS Paths

Generalized D(component, x)• Disconnect node x from the component

• Determine components with edges to x (T)

• For each of the T components t

Page 36: Counting All Possible DFS Paths

Generalized D(component, x)• Disconnect node x from the component

• Determine components with edges to x (T)

• For each of the T components t

• For each node o in t

Page 37: Counting All Possible DFS Paths

Generalized D(component, x)• Disconnect node x from the component

• Determine components with edges to x (T)

• For each of the T components t

• For each node o in t

• Call D(t, o) on o if edge to x. Store this value

Page 38: Counting All Possible DFS Paths

Generalized D(component, x)• Disconnect node x from the component

• Determine components with edges to x (T)

• For each of the T components t

• For each node o in t

• Call D(t, o) on o if edge to x. Store this value

• Return T! * product of all D(t, o)

Page 39: Counting All Possible DFS Paths

Running Time Analysis• Disconnect node x from the component 𝑶(𝟏)

• Determine components with edges to x (T) 𝑶(𝒏𝟐)

• For each of the T components t 𝑶(𝒏)

• For each node o in t 𝑶(𝒏)

• Call D(t, o) on o if edge to x. Store this value

• Return T! * product of all D(t, o) 𝑶(𝒏)

Page 40: Counting All Possible DFS Paths

Running Time Analysis (cont.)• Thus far, runtime is 𝑶(𝒏𝟐)

• There are potentially 2n calls to D.

• 𝑶(𝒏𝟐𝟐𝒏)

• THIS IS NASTY!!!!

• Must memoize on the component and starting node

Page 41: Counting All Possible DFS Paths

Performance• Mac OS X 10.15.4• 2.7 GHz Intel Core i5

• Worst case – fully connected• All nodes have edges to all

other nodes

• Best case – minimally connected• All nodes (except first and

last) have only two edges

Page 42: Counting All Possible DFS Paths

Applications• DFSCount• Could be used to find all solutions to a maze• Could be used to test performance of a

computer• Could be used to test your friends

• DFS• Used to solve a maze, find connected

components, topological sort, maze generation, and many more!

https://hackaday.com/2017/10/23/solving-mazes-with-graphics-cards/

Page 43: Counting All Possible DFS Paths

Adjacent Problem – Traveling Salesman• While this algorithm does not solve TSP, it shares some similarities

with TSP• Specifically, enumerating paths in a graph

• Bellman-Held-Karp Algorithm• Also runs in O(n22n)• Uses Dynamic Programming• Gives exact solution

Page 44: Counting All Possible DFS Paths

References• https://en.wikipedia.org/wiki/Depth-first_search• https://en.wikipedia.org/wiki/Tr%C3%A9maux_tree• https://en.wikipedia.org/wiki/Maze_solving_algorithm• http://web.eecs.utk.edu/~jplank/topcoder-writeups/2017/DFSCount/index.html• https://en.wikipedia.org/wiki/Held%E2%80%93Karp_algorithm• https://en.wikipedia.org/wiki/Hamiltonian_path_problem

Page 45: Counting All Possible DFS Paths

Questions

1. Who is the person credited with first discovering DFS?

2. What is the runtime of DFSCount?

3. What problem is similar to DFSCount?