-
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.
feat: add minimum spanning tree tests
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 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
44 changes: 44 additions & 0 deletions
44
python-algorithm/algorithm/graph/test/test_minimum_spanning_tree.py
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 @@ | ||
import matplotlib.pyplot as plt | ||
import networkx | ||
import pytest | ||
|
||
from algorithm.graph.test.graph_data_utils import create_weighted_city_graph | ||
|
||
city_graph = create_weighted_city_graph() | ||
|
||
|
||
@pytest.mark.skip(reason='This test is for visualization only') | ||
def test_graph_mst_temp(): | ||
mst = networkx.minimum_spanning_tree(city_graph, algorithm='kruskal') | ||
pos = networkx.spring_layout(mst) | ||
networkx.draw_networkx_nodes(mst, pos) | ||
networkx.draw_networkx_edges(mst, pos, width=1) | ||
networkx.draw_networkx_labels(mst, pos, font_size=10) | ||
plt.show() | ||
|
||
|
||
city_edges = {('Seattle', 'San Francisco'), ('San Francisco', 'Los Angeles'), ('Los Angeles', 'Riverside'), | ||
('Riverside', 'Phoenix'), ('Phoenix', 'Dallas'), ('Dallas', 'Houston'), ('Houston', 'Atlanta'), | ||
('Atlanta', 'Miami'), ('Atlanta', 'Washington'), ('Washington', 'Philadelphia'), | ||
('Philadelphia', 'New York'), ('New York', 'Boston'), ('Washington', 'Detroit'), ('Detroit', 'Chicago')} | ||
|
||
|
||
@pytest.mark.benchmark(group='graph_minimum_spanning_tree') | ||
@pytest.mark.parametrize( | ||
argnames='graph, algorithm, expected_total_weight, expected_edges', | ||
argvalues=[ | ||
(city_graph, 'kruskal', 5372, city_edges), | ||
(city_graph, 'prim', 5372, city_edges), | ||
(city_graph, 'boruvka', 5372, city_edges), | ||
], | ||
ids=['kruskal', 'prim', 'boruvka']) | ||
def test_graph_mst(benchmark, graph, algorithm, expected_total_weight, expected_edges): | ||
mst = benchmark(networkx.minimum_spanning_tree, graph, algorithm=algorithm) | ||
|
||
mst_total_weight = sum(d['weight'] for u, v, d in mst.edges(data=True)) | ||
assert expected_total_weight == mst_total_weight | ||
|
||
mst_edges = set((u, v) for u, v, d in mst.edges(data=True)) | ||
normalized_set1 = {tuple(sorted(edge)) for edge in expected_edges} | ||
normalized_set2 = {tuple(sorted(edge)) for edge in mst_edges} | ||
assert normalized_set1 == normalized_set2 |