Skip to content

Commit a07725a

Browse files
majitomojitoAjmera, Mahita  SI/HZR-IDSAcclauss
authored
Unit tests (#12125)
* unit test for graphs_floyd_warshall * cleaning up * capturing print output * editing doctest * editing doctest * editing doctest * editing doctest * removing blank lines from print statement * removing tabs from print statement * commenting out print statement * changing pytest * fixing doctest * including print statement and output in doctest * editing doctest * changing tab to double spaces to capture in doctest * changing end character in print statement * fixing doctests * chagning tab to double space for doctest * updating DIRECTORY.md * Improve output formatting in Floyd Warshall example Added a blank line for better readability in the output. --------- Co-authored-by: Ajmera, Mahita SI/HZR-IDSA <ajmermhi@schaeffler.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent 9ea9f2c commit a07725a

3 files changed

Lines changed: 66 additions & 2 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@
640640
* [Strongly Connected Components](graphs/strongly_connected_components.py)
641641
* [Tarjans Scc](graphs/tarjans_scc.py)
642642
* Tests
643+
* [Test Graphs Floyd Warshall](graphs/tests/test_graphs_floyd_warshall.py)
643644
* [Test Johnson](graphs/tests/test_johnson.py)
644645
* [Test Min Spanning Tree Kruskal](graphs/tests/test_min_spanning_tree_kruskal.py)
645646
* [Test Min Spanning Tree Prim](graphs/tests/test_min_spanning_tree_prim.py)

graphs/graphs_floyd_warshall.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ def _print_dist(dist, v) -> None:
99
print("\nThe shortest path matrix using Floyd Warshall algorithm\n")
1010
for i in range(v):
1111
for j in range(v):
12+
end_char = "" if j == v - 1 else " "
1213
if dist[i][j] != float("inf"):
13-
print(int(dist[i][j]), end="\t")
14+
print(int(dist[i][j]), end=end_char)
1415
else:
15-
print("INF", end="\t")
16+
print("INF", end=end_char)
1617
print()
1718

1819

@@ -31,6 +32,28 @@ def floyd_warshall(graph, v):
3132
4. The above is repeated for each vertex k in the graph.
3233
5. Whenever distance[i][j] is given a new minimum value, next vertex[i][j] is
3334
updated to the next vertex[i][k].
35+
36+
37+
>>> graph = [
38+
... [0, 3, float('inf')],
39+
... [2, 0, float('inf')],
40+
... [float('inf'), 7, 0]
41+
... ]
42+
43+
>>> expected = [
44+
... [0, 3, float('inf')],
45+
... [2, 0, float('inf')],
46+
... [9, 7, 0]
47+
... ]
48+
>>> dist, _ = floyd_warshall(graph, 3)
49+
<BLANKLINE>
50+
The shortest path matrix using Floyd Warshall algorithm
51+
<BLANKLINE>
52+
0 3 INF
53+
2 0 INF
54+
9 7 0
55+
>>> dist == expected
56+
True
3457
"""
3558

3659
dist = [[float("inf") for _ in range(v)] for _ in range(v)]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import pytest
2+
3+
from graphs.graphs_floyd_warshall import floyd_warshall
4+
5+
6+
def test_no_edges():
7+
graph = [
8+
[0, float("inf"), float("inf")],
9+
[float("inf"), 0, float("inf")],
10+
[float("inf"), float("inf"), 0],
11+
]
12+
expected = [
13+
[0, float("inf"), float("inf")],
14+
[float("inf"), 0, float("inf")],
15+
[float("inf"), float("inf"), 0],
16+
]
17+
dist, _ = floyd_warshall(graph, 3)
18+
assert dist == expected
19+
20+
21+
def test_with_edges():
22+
graph = [[0, 3, float("inf")], [2, 0, float("inf")], [float("inf"), 7, 0]]
23+
expected = [[0, 3, float("inf")], [2, 0, float("inf")], [9, 7, 0]]
24+
dist, _ = floyd_warshall(graph, 3)
25+
assert dist == expected
26+
27+
28+
def test_unreachable_vertices():
29+
graph = [
30+
[0, 1, float("inf")],
31+
[float("inf"), 0, 2],
32+
[float("inf"), float("inf"), 0],
33+
]
34+
expected = [[0, 1, 3], [float("inf"), 0, 2], [float("inf"), float("inf"), 0]]
35+
dist, _ = floyd_warshall(graph, 3)
36+
assert dist == expected
37+
38+
39+
if __name__ == "__main__":
40+
pytest.main()

0 commit comments

Comments
 (0)