Skip to content

Dfs #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed

Dfs #38

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions allalgorithms/graph/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .dfs import dfs_paths
20 changes: 20 additions & 0 deletions allalgorithms/graph/dfs.py
Original file line number Diff line number Diff line change
@@ -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]))
38 changes: 38 additions & 0 deletions docs/graph/dfs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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.

## Install

```
pip install allalgorithms
```

## Usage

```py
from allalgorithms.graph import dfs_path

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
23 changes: 23 additions & 0 deletions tests/test_graph.py
Original file line number Diff line number Diff line change
@@ -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()