forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Place tarjan's graph helpers at graph.py for reuse * Transforms tarjan algorithm into class * Add tarjan's examples as test cases * Comment out testing lines
- Loading branch information
1 parent
8a51db2
commit 063b3f4
Showing
4 changed files
with
168 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ __pycache__/ | |
*.py[cod] | ||
.idea/ | ||
.cache/ | ||
.pytest_cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from tarjan import Tarjan | ||
|
||
import unittest | ||
|
||
|
||
class TestTarjan(unittest.TestCase): | ||
""" | ||
Test for the file tarjan.py | ||
Arguments: | ||
unittest {[type]} -- [description] | ||
""" | ||
|
||
def test_tarjan_example_1(self): | ||
# Graph from https://en.wikipedia.org/wiki/File:Scc.png | ||
example = { | ||
'A': ['B'], | ||
'B': ['C', 'E', 'F'], | ||
'C': ['D', 'G'], | ||
'D': ['C', 'H'], | ||
'E': ['A', 'F'], | ||
'F': ['G'], | ||
'G': ['F'], | ||
'H': ['D', 'G'] | ||
} | ||
|
||
g = Tarjan(example) | ||
self.assertEqual(g.sccs, [['F', 'G'], ['H', 'D', 'C'], ['E', 'B', 'A']]) | ||
|
||
def test_tarjan_example_2(self): | ||
# Graph from https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm#/media/File:Tarjan%27s_Algorithm_Animation.gif | ||
example = { | ||
'A': ['E'], | ||
'B': ['A'], | ||
'C': ['B', 'D'], | ||
'D': ['C'], | ||
'E': ['B'], | ||
'F': ['B', 'E', 'G'], | ||
'G': ['F', 'C'], | ||
'H': ['G', 'H', 'D'] | ||
} | ||
|
||
g = Tarjan(example) | ||
self.assertEqual(g.sccs, [['B', 'E', 'A'], ['D', 'C'], ['G', 'F'], ['H']]) |