-
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.
Merge branch 'feature/graph-algorithms' into develop
resolve conflicts
- Loading branch information
Showing
6 changed files
with
199 additions
and
51 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
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,85 @@ | ||
import networkx | ||
|
||
seattle = 'Seattle' | ||
san_francisco = 'San Francisco' | ||
los_angeles = 'Los Angeles' | ||
riverside = 'Riverside' | ||
phoenix = 'Phoenix' | ||
dallas = 'Dallas' | ||
houston = 'Houston' | ||
atlanta = 'Atlanta' | ||
miami = 'Miami' | ||
chicago = 'Chicago' | ||
detroit = 'Detroit' | ||
boston = 'Boston' | ||
new_york = 'New York' | ||
philadelphia = 'Philadelphia' | ||
washington = 'Washington' | ||
|
||
|
||
def create_city_graph() -> networkx.Graph: | ||
edges = [ | ||
(seattle, chicago), | ||
(seattle, san_francisco), | ||
(san_francisco, riverside), | ||
(san_francisco, los_angeles), | ||
(los_angeles, riverside), | ||
(los_angeles, phoenix), | ||
(riverside, phoenix), | ||
(riverside, chicago), | ||
(phoenix, dallas), | ||
(phoenix, houston), | ||
(dallas, chicago), | ||
(dallas, atlanta), | ||
(dallas, houston), | ||
(houston, atlanta), | ||
(houston, miami), | ||
(atlanta, chicago), | ||
(atlanta, washington), | ||
(atlanta, miami), | ||
(miami, washington), | ||
(chicago, detroit), | ||
(detroit, boston), | ||
(detroit, washington), | ||
(detroit, new_york), | ||
(boston, new_york), | ||
(new_york, philadelphia), | ||
(philadelphia, washington) | ||
] | ||
graph = networkx.Graph() | ||
graph.add_edges_from(edges) | ||
return graph | ||
|
||
|
||
def create_weighted_city_graph() -> networkx.Graph: | ||
weighted_edges = [ | ||
(seattle, chicago, 1737), | ||
(seattle, san_francisco, 678), | ||
(san_francisco, riverside, 386), | ||
(san_francisco, los_angeles, 348), | ||
(los_angeles, riverside, 50), | ||
(los_angeles, phoenix, 357), | ||
(riverside, phoenix, 307), | ||
(riverside, chicago, 1704), | ||
(phoenix, dallas, 887), | ||
(phoenix, houston, 1015), | ||
(dallas, chicago, 805), | ||
(dallas, atlanta, 721), | ||
(dallas, houston, 225), | ||
(houston, atlanta, 702), | ||
(houston, miami, 968), | ||
(atlanta, chicago, 588), | ||
(atlanta, washington, 543), | ||
(atlanta, miami, 604), | ||
(miami, washington, 923), | ||
(chicago, detroit, 238), | ||
(detroit, boston, 613), | ||
(detroit, washington, 396), | ||
(detroit, new_york, 482), | ||
(boston, new_york, 190), | ||
(new_york, philadelphia, 81), | ||
(philadelphia, washington, 123) | ||
] | ||
graph = networkx.Graph() | ||
graph.add_weighted_edges_from(weighted_edges) | ||
return graph |
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,46 @@ | ||
import networkx | ||
import pytest | ||
|
||
from algorithm.graph.test.graph_data_utils import create_weighted_city_graph | ||
|
||
city_graph = create_weighted_city_graph() | ||
|
||
|
||
@pytest.mark.benchmark(group='graph_dijkstra') | ||
@pytest.mark.parametrize( | ||
argnames='graph, source, target, expected_path', | ||
argvalues=[ | ||
(city_graph, 'Los Angeles', 'Boston', ['Los Angeles', 'Riverside', 'Chicago', 'Detroit', 'Boston']), | ||
], | ||
ids=['los_angeles_to_boston']) | ||
def test_graph_dijkstra_path(benchmark, graph, source, target, expected_path): | ||
""" | ||
Find the shortest path between two nodes in a graph using Dijkstra's algorithm. | ||
:param benchmark: benchmark fixture | ||
:param graph: city graph of the United States | ||
:param source: source city node | ||
:param target: target city node | ||
:param expected_path: expected shortest path | ||
""" | ||
shortest_path = benchmark(networkx.dijkstra_path, graph, source, target) | ||
assert shortest_path == expected_path | ||
|
||
|
||
@pytest.mark.benchmark(group='graph_dijkstra') | ||
@pytest.mark.parametrize( | ||
argnames='graph, source, target, expected_distance', | ||
argvalues=[ | ||
(city_graph, 'Los Angeles', 'Boston', 2605), | ||
], | ||
ids=['los_angeles_to_boston']) | ||
def test_graph_dijkstra_distance(benchmark, graph, source, target, expected_distance): | ||
""" | ||
Find the shortest distance between two nodes in a graph using Dijkstra's algorithm. | ||
:param benchmark: benchmark fixture | ||
:param graph: city graph of the United States | ||
:param source: source city node | ||
:param target: target city node | ||
:param expected_distance: expected shortest distance (weight) | ||
""" | ||
total_weight = benchmark(networkx.dijkstra_path_length, graph, source, target) | ||
assert total_weight == expected_distance |
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 |
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