counting all possible dfs paths
TRANSCRIPT
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?