-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_dijkstra.py
46 lines (40 loc) · 1.7 KB
/
test_dijkstra.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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