From e0f8ae7298cc67e972b9ccaf84f55b91f88e763c Mon Sep 17 00:00:00 2001 From: Glyphack Date: Wed, 2 Oct 2019 17:14:32 +0330 Subject: [PATCH 1/7] Add graph path finding using dfs algotrithm. --- allalgorithms/graph/dfs.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 allalgorithms/graph/dfs.py diff --git a/allalgorithms/graph/dfs.py b/allalgorithms/graph/dfs.py new file mode 100644 index 0000000..19ae62b --- /dev/null +++ b/allalgorithms/graph/dfs.py @@ -0,0 +1,20 @@ +# -*- coding: UTF-8 -*- +# +# find path from a node to another node using dfs algorithm. +# The All â–²lgorithms library for python +# Code credit: https://github.com/eddmann +# Contributed by: glyphack +# Github: @glyphack +# +from typing import Dict, Union + + +def dfs_paths(graph: Dict[str, set], start: str, destination: str) -> Union[list, None]: + stack = [(start, [start])] + while stack: + (vertex, path) = stack.pop() + for next in graph[vertex] - set(path): + if next == destination: + yield path + [next] + else: + stack.append((next, path + [next])) From 94cc7f21995760ac74832ffe8baad7f003001f6d Mon Sep 17 00:00:00 2001 From: Glyphack Date: Wed, 2 Oct 2019 17:14:48 +0330 Subject: [PATCH 2/7] Add dfs algorithm doc --- docs/graph/dfs.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 docs/graph/dfs.md diff --git a/docs/graph/dfs.md b/docs/graph/dfs.md new file mode 100644 index 0000000..566780e --- /dev/null +++ b/docs/graph/dfs.md @@ -0,0 +1,38 @@ +# Breath first search + +[Depth-first search](https://en.wikipedia.org/wiki/Depth-first_search) (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking. + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.graph import dfs + +graph = {'A': set(['B', 'C']), + 'B': set(['A', 'D', 'E']), + 'C': set(['A', 'F']), + 'D': set(['B']), + 'E': set(['B', 'F']), + 'F': set(['C', 'E'])} + +list(dfs_paths(graph, 'A', 'F')) +# -> [['A', 'C', 'F'], ['A', 'B', 'E', 'F']] + +``` + +## API + +def dfs_paths(graph: Dict[str, set], start: str, destination: str) -> Union[list, None]: + +> Return list of lists that contain path to destination from start, if there is not any returns `None` + +##### Params: + +- `graph`: Dictionary containing the graph to be searched +- `start` : Name of the start node +- `destination` : Name of the destination node From f67243939b450cb7ccd18c717d71e7b5cc3464e4 Mon Sep 17 00:00:00 2001 From: Glyphack Date: Wed, 2 Oct 2019 17:15:13 +0330 Subject: [PATCH 3/7] Remove redundant comment. --- allalgorithms/graph/dfs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/allalgorithms/graph/dfs.py b/allalgorithms/graph/dfs.py index 19ae62b..2992c91 100644 --- a/allalgorithms/graph/dfs.py +++ b/allalgorithms/graph/dfs.py @@ -5,7 +5,7 @@ # Code credit: https://github.com/eddmann # Contributed by: glyphack # Github: @glyphack -# + from typing import Dict, Union From 201ed3fa5872111e51915bd9afd96abbdb1f78eb Mon Sep 17 00:00:00 2001 From: Glyphack Date: Thu, 3 Oct 2019 09:13:52 +0330 Subject: [PATCH 4/7] import dfs_path. --- allalgorithms/graph/__init__.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 allalgorithms/graph/__init__.py diff --git a/allalgorithms/graph/__init__.py b/allalgorithms/graph/__init__.py new file mode 100644 index 0000000..2104df2 --- /dev/null +++ b/allalgorithms/graph/__init__.py @@ -0,0 +1 @@ +from .dfs import dfs_paths \ No newline at end of file From 7674cae4f22d9d5b2ea9cf7a7765acf66f332c38 Mon Sep 17 00:00:00 2001 From: Glyphack Date: Thu, 3 Oct 2019 09:14:00 +0330 Subject: [PATCH 5/7] fix function name. --- docs/graph/dfs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/graph/dfs.md b/docs/graph/dfs.md index 566780e..5d89b4a 100644 --- a/docs/graph/dfs.md +++ b/docs/graph/dfs.md @@ -11,7 +11,7 @@ pip install allalgorithms ## Usage ```py -from allalgorithms.graph import dfs +from allalgorithms.graph import dfs_path graph = {'A': set(['B', 'C']), 'B': set(['A', 'D', 'E']), From 47e31cad75e1063461187171620261fb2d028216 Mon Sep 17 00:00:00 2001 From: Glyphack Date: Thu, 3 Oct 2019 09:14:12 +0330 Subject: [PATCH 6/7] Add test for dfs_path. --- tests/test_graph.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tests/test_graph.py diff --git a/tests/test_graph.py b/tests/test_graph.py new file mode 100644 index 0000000..029231d --- /dev/null +++ b/tests/test_graph.py @@ -0,0 +1,23 @@ +import unittest + +from allalgorithms.graph import dfs_paths + + +class TestGraph(unittest.TestCase): + def setUp(self): + super().setUp() + self.test_graph = { + "A": set(["B", "C"]), + "B": set(["A", "D", "E"]), + "C": set(["A", "F"]), + "D": set(["B"]), + "E": set(["B", "F"]), + "F": set(["C", "E"]), + } + + def test_dfs(self): + self.assertEqual([['A', 'B', 'E', 'F'], ['A', 'C', 'F']], list(dfs_paths(self.test_graph, 'A', 'F'))) + + +if __name__ == "__main__": + unittest.main() From 76075d4c435e9c55e4cd7ca9b8c1793649be6361 Mon Sep 17 00:00:00 2001 From: Glyphack Date: Thu, 3 Oct 2019 09:40:39 +0330 Subject: [PATCH 7/7] Fix typo. --- docs/graph/dfs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/graph/dfs.md b/docs/graph/dfs.md index 5d89b4a..aefc899 100644 --- a/docs/graph/dfs.md +++ b/docs/graph/dfs.md @@ -1,4 +1,4 @@ -# Breath first search +# Depth first search [Depth-first search](https://en.wikipedia.org/wiki/Depth-first_search) (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking.