Counting All Possible DFS Paths

Post on 17-Mar-2022

2 views 0 download

Transcript of Counting All Possible DFS Paths

Counting All Possible DFS Paths

Ryan Flint and Thomas BeardComputer Science Graduates

University of Tennessee

April 23, 2020

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?

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

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

Overview

• History

• Explanation of the Algorithm

• Results

• Applications

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/

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)

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

Example

O

7 8 9

4 5 6

7 8 9

1 2 3

• 36 permutations

• How do we get this result?

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

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

The Solution

• D(component, n)

• D(Red, 1)

• Only one way to traverse this component. Duh.

The Solution (cont.)

• D(component, n)

• D(Blue, 4)

• One way to go through this one too.

The Solution (cont.)

• D(component, n)

• D(Green, 7)

• The answer is one. What a surprise!

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?

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!

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)

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

The Solution (cont.)

• Node 0 disconnected

• Two components connected to 0

• D(Blue, 4) = 1

• D(Green, 7) = 1

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)

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)

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

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)

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)

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)

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)

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)

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

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

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

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

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

• = 36 possible DFS outputs

Generalized D(component, x)

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

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

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

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

• Determine components with edges to x (T)

• For each of the T components t

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

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

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)

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) 𝑶(𝒏)

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

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

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/

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

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

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?