From 3a7b2d2da67146e52d1d44b5ed076a261db228aa Mon Sep 17 00:00:00 2001 From: Sanders Lin <45224617+SandersLin@users.noreply.github.com> Date: Thu, 16 Apr 2020 15:10:13 +0800 Subject: [PATCH 01/15] Update dfs.py --- graphs/dfs.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/graphs/dfs.py b/graphs/dfs.py index f183eae73fef..a1b4d6a14858 100644 --- a/graphs/dfs.py +++ b/graphs/dfs.py @@ -15,19 +15,14 @@ def dfs(graph, start): behaviour precisely using a stack of iterators. Instead of recursively calling with a node, we'll push an iterator to the node's children onto the iterator stack. When the iterator at the top of the stack terminates, we'll pop it off the stack.""" - explored, stack = set(), [start] + explored, stack = set(start), [start] while stack: v = ( stack.pop() ) # one difference from BFS is to pop last element here instead of first one - - if v in explored: - continue - - explored.add(v) - for w in graph[v]: if w not in explored: + explored.add(w) stack.append(w) return explored From de64f33456c96588f90d60da78452a4b7905360f Mon Sep 17 00:00:00 2001 From: John Law Date: Fri, 17 Apr 2020 11:51:22 +0200 Subject: [PATCH 02/15] Add type hints, rearrange doc-strings and comments --- graphs/dfs.py | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/graphs/dfs.py b/graphs/dfs.py index a1b4d6a14858..aabe0ff944a5 100644 --- a/graphs/dfs.py +++ b/graphs/dfs.py @@ -1,25 +1,33 @@ -"""pseudo-code""" +"""The DFS function simply calls itself recursively for every unvisited child of +its argument. We can emulate that behaviour precisely using a stack of iterators. +Instead of recursively calling with a node, we'll push an iterator to the node's +children onto the iterator stack. When the iterator at the top of the stack +terminates, we'll pop it off the stack. +Pseudocode: + all nodes initially unexplored + mark s as explored + for every edge (s, v): + if v unexplored: + DFS(G, v) """ -DFS(graph G, start vertex s): -// all nodes initially unexplored -mark s as explored -for every edge (s, v): - if v unexplored: - DFS(G, v) -""" +from typing import Set, Dict + + +def dfs(graph: Dict, start: str) -> Set[int]: + """Depth First Search on Graph -def dfs(graph, start): - """The DFS function simply calls itself recursively for every unvisited child of its argument. We can emulate that - behaviour precisely using a stack of iterators. Instead of recursively calling with a node, we'll push an iterator - to the node's children onto the iterator stack. When the iterator at the top of the stack terminates, we'll pop - it off the stack.""" + :param graph: directed graph in dictionary format + :param vertex: starting vectex as a string + :returns: the trace of the search + """ explored, stack = set(start), [start] while stack: v = ( stack.pop() - ) # one difference from BFS is to pop last element here instead of first one + ) + # one difference from BFS is to pop last element here instead of first one for w in graph[v]: if w not in explored: explored.add(w) @@ -36,4 +44,5 @@ def dfs(graph, start): "F": ["C", "E"], } -print(dfs(G, "A")) +if __name__ == "__main__": + print(dfs(G, "A")) From e185f4f225906e9a9aeb18934e72e190332aa0d8 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 17 Apr 2020 09:52:30 +0000 Subject: [PATCH 03/15] fixup! Format Python code with psf/black push --- graphs/dfs.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/graphs/dfs.py b/graphs/dfs.py index aabe0ff944a5..b6a01cc3de06 100644 --- a/graphs/dfs.py +++ b/graphs/dfs.py @@ -24,9 +24,7 @@ def dfs(graph: Dict, start: str) -> Set[int]: """ explored, stack = set(start), [start] while stack: - v = ( - stack.pop() - ) + v = stack.pop() # one difference from BFS is to pop last element here instead of first one for w in graph[v]: if w not in explored: From da7c95e5118a458828f7384dcff19c3680b9f21c Mon Sep 17 00:00:00 2001 From: John Law Date: Fri, 17 Apr 2020 12:49:23 +0200 Subject: [PATCH 04/15] dfs -> depth_first_search Co-Authored-By: Christian Clauss --- graphs/dfs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/dfs.py b/graphs/dfs.py index b6a01cc3de06..dc4d5a2506a0 100644 --- a/graphs/dfs.py +++ b/graphs/dfs.py @@ -15,7 +15,7 @@ from typing import Set, Dict -def dfs(graph: Dict, start: str) -> Set[int]: +def depth_first_search(graph: Dict, start: str) -> Set[int]: """Depth First Search on Graph :param graph: directed graph in dictionary format From 07c21eefae4776df82a40eae4528cc620b245486 Mon Sep 17 00:00:00 2001 From: John Law Date: Fri, 17 Apr 2020 12:50:47 +0200 Subject: [PATCH 05/15] dfs -> depth_first_search --- graphs/dfs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/dfs.py b/graphs/dfs.py index dc4d5a2506a0..767b88688a75 100644 --- a/graphs/dfs.py +++ b/graphs/dfs.py @@ -43,4 +43,4 @@ def depth_first_search(graph: Dict, start: str) -> Set[int]: } if __name__ == "__main__": - print(dfs(G, "A")) + print(depth_first_search(G, "A")) From 398d00be274d4e740ac4d76a87b1c7823e68ce98 Mon Sep 17 00:00:00 2001 From: John Law Date: Fri, 17 Apr 2020 19:03:48 +0200 Subject: [PATCH 06/15] Add doctest for DFS --- graphs/dfs.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/graphs/dfs.py b/graphs/dfs.py index 767b88688a75..2abe99278d32 100644 --- a/graphs/dfs.py +++ b/graphs/dfs.py @@ -21,6 +21,15 @@ def depth_first_search(graph: Dict, start: str) -> Set[int]: :param graph: directed graph in dictionary format :param vertex: starting vectex as a string :returns: the trace of the search + >>> G = { "A": ["B", "C", "D"], "B": ["A", "D", "E"], + ... "C": ["A", "F"], "D": ["B", "D"], "E": ["B", "F"], + ... "F": ["C", "E", "G"], "G": ["F"] } + >>> start = "A" + >>> output_G = list({'A', 'B', 'C', 'D', 'E', 'F', 'G'}) + >>> all(x in output_G for x in list(depth_first_search(G, "A"))) + True + >>> all(x in output_G for x in list(depth_first_search(G, "G"))) + True """ explored, stack = set(start), [start] while stack: @@ -33,14 +42,11 @@ def depth_first_search(graph: Dict, start: str) -> Set[int]: return explored -G = { - "A": ["B", "C"], - "B": ["A", "D", "E"], - "C": ["A", "F"], - "D": ["B"], - "E": ["B", "F"], - "F": ["C", "E"], -} +G = { "A": ["B", "C", "D"], "B": ["A", "D", "E"], + "C": ["A", "F"], "D": ["B", "D"], "E": ["B", "F"], + "F": ["C", "E", "G"], "G": ["F"] } if __name__ == "__main__": + import doctest + doctest.testmod() print(depth_first_search(G, "A")) From 5b56b19e6bb96f53a41a1ea05222666d44d284a6 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 17 Apr 2020 17:04:34 +0000 Subject: [PATCH 07/15] fixup! Format Python code with psf/black push --- graphs/dfs.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/graphs/dfs.py b/graphs/dfs.py index 2abe99278d32..1206d5ae9252 100644 --- a/graphs/dfs.py +++ b/graphs/dfs.py @@ -42,11 +42,18 @@ def depth_first_search(graph: Dict, start: str) -> Set[int]: return explored -G = { "A": ["B", "C", "D"], "B": ["A", "D", "E"], - "C": ["A", "F"], "D": ["B", "D"], "E": ["B", "F"], - "F": ["C", "E", "G"], "G": ["F"] } +G = { + "A": ["B", "C", "D"], + "B": ["A", "D", "E"], + "C": ["A", "F"], + "D": ["B", "D"], + "E": ["B", "F"], + "F": ["C", "E", "G"], + "G": ["F"], +} if __name__ == "__main__": import doctest + doctest.testmod() print(depth_first_search(G, "A")) From e6d68ab868cce0207ca8aa66ccc160419fcfd692 Mon Sep 17 00:00:00 2001 From: John Law Date: Fri, 17 Apr 2020 19:29:45 +0200 Subject: [PATCH 08/15] Rename dfs.py to depth_first_search_dictionary.py --- graphs/{dfs.py => depth_first_search_dictionary.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename graphs/{dfs.py => depth_first_search_dictionary.py} (100%) diff --git a/graphs/dfs.py b/graphs/depth_first_search_dictionary.py similarity index 100% rename from graphs/dfs.py rename to graphs/depth_first_search_dictionary.py From 377482b684ce733a0f6540056f9c4ece1fbf6b41 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 17 Apr 2020 17:30:09 +0000 Subject: [PATCH 09/15] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 0f7363d7e9ab..c13a10f4c76f 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -212,7 +212,7 @@ * [Check Bipartite Graph Bfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/check_bipartite_graph_bfs.py) * [Check Bipartite Graph Dfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/check_bipartite_graph_dfs.py) * [Depth First Search](https://github.com/TheAlgorithms/Python/blob/master/graphs/depth_first_search.py) - * [Dfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/dfs.py) + * [Depth First Search Dictionary](https://github.com/TheAlgorithms/Python/blob/master/graphs/depth_first_search_dictionary.py) * [Dijkstra](https://github.com/TheAlgorithms/Python/blob/master/graphs/dijkstra.py) * [Dijkstra 2](https://github.com/TheAlgorithms/Python/blob/master/graphs/dijkstra_2.py) * [Dijkstra Algorithm](https://github.com/TheAlgorithms/Python/blob/master/graphs/dijkstra_algorithm.py) From 9d7e9b7ff5bf1e77842b25165af42f1bf4002cc4 Mon Sep 17 00:00:00 2001 From: John Law Date: Fri, 17 Apr 2020 19:34:30 +0200 Subject: [PATCH 10/15] Rename depth_first_search_dictionary.py to depth_first_search_dfs.py --- ...depth_first_search_dictionary.py => depth_first_search_dfs.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename graphs/{depth_first_search_dictionary.py => depth_first_search_dfs.py} (100%) diff --git a/graphs/depth_first_search_dictionary.py b/graphs/depth_first_search_dfs.py similarity index 100% rename from graphs/depth_first_search_dictionary.py rename to graphs/depth_first_search_dfs.py From fb5b3ee1714ae1b6dc3769a85b86f22d3a5148fb Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 17 Apr 2020 17:34:48 +0000 Subject: [PATCH 11/15] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index c13a10f4c76f..52c96e28e32f 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -212,7 +212,7 @@ * [Check Bipartite Graph Bfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/check_bipartite_graph_bfs.py) * [Check Bipartite Graph Dfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/check_bipartite_graph_dfs.py) * [Depth First Search](https://github.com/TheAlgorithms/Python/blob/master/graphs/depth_first_search.py) - * [Depth First Search Dictionary](https://github.com/TheAlgorithms/Python/blob/master/graphs/depth_first_search_dictionary.py) + * [Depth First Search Dfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/depth_first_search_dfs.py) * [Dijkstra](https://github.com/TheAlgorithms/Python/blob/master/graphs/dijkstra.py) * [Dijkstra 2](https://github.com/TheAlgorithms/Python/blob/master/graphs/dijkstra_2.py) * [Dijkstra Algorithm](https://github.com/TheAlgorithms/Python/blob/master/graphs/dijkstra_algorithm.py) From 14034a33a92d3136bb6a3b44bf6e3fc58382a955 Mon Sep 17 00:00:00 2001 From: John Law Date: Fri, 17 Apr 2020 19:36:02 +0200 Subject: [PATCH 12/15] Rename depth_first_search.py to depth_first_search_2.py --- graphs/{depth_first_search.py => depth_first_search_2.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename graphs/{depth_first_search.py => depth_first_search_2.py} (100%) diff --git a/graphs/depth_first_search.py b/graphs/depth_first_search_2.py similarity index 100% rename from graphs/depth_first_search.py rename to graphs/depth_first_search_2.py From 51e06cf078eb4421594767c287f19ce46ef65510 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 17 Apr 2020 17:36:19 +0000 Subject: [PATCH 13/15] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 52c96e28e32f..bcc0e0c9134d 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -211,7 +211,7 @@ * [Breadth First Search](https://github.com/TheAlgorithms/Python/blob/master/graphs/breadth_first_search.py) * [Check Bipartite Graph Bfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/check_bipartite_graph_bfs.py) * [Check Bipartite Graph Dfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/check_bipartite_graph_dfs.py) - * [Depth First Search](https://github.com/TheAlgorithms/Python/blob/master/graphs/depth_first_search.py) + * [Depth First Search 2](https://github.com/TheAlgorithms/Python/blob/master/graphs/depth_first_search_2.py) * [Depth First Search Dfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/depth_first_search_dfs.py) * [Dijkstra](https://github.com/TheAlgorithms/Python/blob/master/graphs/dijkstra.py) * [Dijkstra 2](https://github.com/TheAlgorithms/Python/blob/master/graphs/dijkstra_2.py) From 4b8d1c677f002be637072e3c43125fdfce9b6b49 Mon Sep 17 00:00:00 2001 From: John Law Date: Fri, 17 Apr 2020 19:37:51 +0200 Subject: [PATCH 14/15] Rename depth_first_search_dfs.py to depth_first_search.py --- graphs/{depth_first_search_dfs.py => depth_first_search.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename graphs/{depth_first_search_dfs.py => depth_first_search.py} (100%) diff --git a/graphs/depth_first_search_dfs.py b/graphs/depth_first_search.py similarity index 100% rename from graphs/depth_first_search_dfs.py rename to graphs/depth_first_search.py From a329f4e40d324c5b7627cbc9f6574c279e5c1a95 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 17 Apr 2020 17:38:14 +0000 Subject: [PATCH 15/15] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index bcc0e0c9134d..9e7e46d0be40 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -211,8 +211,8 @@ * [Breadth First Search](https://github.com/TheAlgorithms/Python/blob/master/graphs/breadth_first_search.py) * [Check Bipartite Graph Bfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/check_bipartite_graph_bfs.py) * [Check Bipartite Graph Dfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/check_bipartite_graph_dfs.py) + * [Depth First Search](https://github.com/TheAlgorithms/Python/blob/master/graphs/depth_first_search.py) * [Depth First Search 2](https://github.com/TheAlgorithms/Python/blob/master/graphs/depth_first_search_2.py) - * [Depth First Search Dfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/depth_first_search_dfs.py) * [Dijkstra](https://github.com/TheAlgorithms/Python/blob/master/graphs/dijkstra.py) * [Dijkstra 2](https://github.com/TheAlgorithms/Python/blob/master/graphs/dijkstra_2.py) * [Dijkstra Algorithm](https://github.com/TheAlgorithms/Python/blob/master/graphs/dijkstra_algorithm.py)