|
1 | | -import pytest, networkx |
2 | | - |
3 | | - |
4 | | -def create_city_graph() -> networkx.Graph: |
5 | | - graph = networkx.Graph() |
6 | | - graph.add_edge("Seattle", "Chicago") |
7 | | - graph.add_edge("Seattle", "San Francisco") |
8 | | - graph.add_edge("San Francisco", "Riverside") |
9 | | - graph.add_edge("San Francisco", "Los Angeles") |
10 | | - graph.add_edge("Los Angeles", "Riverside") |
11 | | - graph.add_edge("Los Angeles", "Phoenix") |
12 | | - graph.add_edge("Riverside", "Phoenix") |
13 | | - graph.add_edge("Riverside", "Chicago") |
14 | | - graph.add_edge("Phoenix", "Dallas") |
15 | | - graph.add_edge("Phoenix", "Houston") |
16 | | - graph.add_edge("Dallas", "Chicago") |
17 | | - graph.add_edge("Dallas", "Atlanta") |
18 | | - graph.add_edge("Dallas", "Houston") |
19 | | - graph.add_edge("Houston", "Atlanta") |
20 | | - graph.add_edge("Houston", "Miami") |
21 | | - graph.add_edge("Atlanta", "Chicago") |
22 | | - graph.add_edge("Atlanta", "Washington") |
23 | | - graph.add_edge("Atlanta", "Miami") |
24 | | - graph.add_edge("Miami", "Washington") |
25 | | - graph.add_edge("Chicago", "Detroit") |
26 | | - graph.add_edge("Detroit", "Boston") |
27 | | - graph.add_edge("Detroit", "Washington") |
28 | | - graph.add_edge("Detroit", "New York") |
29 | | - graph.add_edge("Boston", "New York") |
30 | | - graph.add_edge("New York", "Philadelphia") |
31 | | - graph.add_edge("Philadelphia", "Washington") |
32 | | - return graph |
| 1 | +import networkx |
| 2 | +import pytest |
33 | 3 |
|
| 4 | +from algorithm.graph.test.graph_data_utils import create_city_graph |
34 | 5 |
|
35 | 6 | city_graph = create_city_graph() |
36 | 7 |
|
37 | 8 |
|
38 | 9 | @pytest.mark.benchmark(group='graph_neighbors') |
39 | 10 | @pytest.mark.parametrize( |
40 | | - argnames='graph, source, expected', |
| 11 | + argnames='graph, source, expected_neighbors', |
41 | 12 | argvalues=[ |
42 | | - (city_graph, "Seattle", ["Chicago", "San Francisco"]), |
43 | | - (city_graph, "San Francisco", ["Seattle", "Riverside", "Los Angeles"]), |
44 | | - (city_graph, "Los Angeles", ["San Francisco", "Riverside", "Phoenix"]), |
45 | | - (city_graph, "Riverside", ["San Francisco", "Los Angeles", "Phoenix", "Chicago"]), |
46 | | - (city_graph, "Phoenix", ["Los Angeles", "Riverside", "Dallas", "Houston"]), |
47 | | - (city_graph, "Chicago", ["Seattle", "Riverside", "Dallas", "Atlanta", "Detroit"]), |
48 | | - (city_graph, "Boston", ["Detroit", "New York"]), |
49 | | - (city_graph, "New York", ["Detroit", "Boston", "Philadelphia"]), |
50 | | - (city_graph, "Atlanta", ["Dallas", "Houston", "Chicago", "Washington", "Miami"]), |
51 | | - (city_graph, "Miami", ["Houston", "Atlanta", "Washington"]), |
52 | | - (city_graph, "Dallas", ["Phoenix", "Chicago", "Atlanta", "Houston"]), |
53 | | - (city_graph, "Houston", ["Phoenix", "Dallas", "Atlanta", "Miami"]), |
54 | | - (city_graph, "Detroit", ["Chicago", "Boston", "Washington", "New York"]), |
55 | | - (city_graph, "Philadelphia", ["New York", "Washington"]), |
56 | | - (city_graph, "Washington", ["Atlanta", "Miami", "Detroit", "Philadelphia"]) |
| 13 | + (city_graph, 'Seattle', ['Chicago', 'San Francisco']), |
| 14 | + (city_graph, 'San Francisco', ['Seattle', 'Riverside', 'Los Angeles']), |
| 15 | + (city_graph, 'Los Angeles', ['San Francisco', 'Riverside', 'Phoenix']), |
| 16 | + (city_graph, 'Riverside', ['San Francisco', 'Los Angeles', 'Phoenix', 'Chicago']), |
| 17 | + (city_graph, 'Phoenix', ['Los Angeles', 'Riverside', 'Dallas', 'Houston']), |
| 18 | + (city_graph, 'Chicago', ['Seattle', 'Riverside', 'Dallas', 'Atlanta', 'Detroit']), |
| 19 | + (city_graph, 'Boston', ['Detroit', 'New York']), |
| 20 | + (city_graph, 'New York', ['Detroit', 'Boston', 'Philadelphia']), |
| 21 | + (city_graph, 'Atlanta', ['Dallas', 'Houston', 'Chicago', 'Washington', 'Miami']), |
| 22 | + (city_graph, 'Miami', ['Houston', 'Atlanta', 'Washington']), |
| 23 | + (city_graph, 'Dallas', ['Phoenix', 'Chicago', 'Atlanta', 'Houston']), |
| 24 | + (city_graph, 'Houston', ['Phoenix', 'Dallas', 'Atlanta', 'Miami']), |
| 25 | + (city_graph, 'Detroit', ['Chicago', 'Boston', 'Washington', 'New York']), |
| 26 | + (city_graph, 'Philadelphia', ['New York', 'Washington']), |
| 27 | + (city_graph, 'Washington', ['Atlanta', 'Miami', 'Detroit', 'Philadelphia']) |
57 | 28 | ]) |
58 | | -def test_graph_neighbors(benchmark, graph, source, expected): |
59 | | - result = benchmark(graph.neighbors, source) |
60 | | - assert sorted(expected) == sorted(result) |
| 29 | +def test_graph_neighbors(benchmark, graph, source, expected_neighbors): |
| 30 | + """ |
| 31 | + Find the neighboring city nodes of the source city node in the United States city graph. |
| 32 | + :param benchmark: benchmark fixture |
| 33 | + :param graph: city graph of the United States |
| 34 | + :param source: source city node |
| 35 | + :param expected_neighbors: list of neighboring city nodes |
| 36 | + """ |
| 37 | + neighbor_iterator = benchmark(graph.neighbors, source) |
| 38 | + assert sorted(expected_neighbors) == sorted(neighbor_iterator) |
61 | 39 |
|
62 | 40 |
|
63 | 41 | @pytest.mark.benchmark(group='graph_breadth_first_search') |
64 | 42 | @pytest.mark.parametrize( |
65 | | - argnames='graph, source, distance, expected', |
| 43 | + argnames='graph, source, distance, expected_nodes', |
66 | 44 | argvalues=[ |
67 | | - (city_graph, "Boston", 1, ["Detroit", "New York"]), |
68 | | - (city_graph, "Boston", 2, ["Chicago", "Washington", "Philadelphia"]), |
69 | | - (city_graph, "Boston", 3, ["Seattle", "Riverside", "Dallas", "Atlanta", "Miami"]), |
| 45 | + (city_graph, 'Boston', 1, ['Detroit', 'New York']), |
| 46 | + (city_graph, 'Boston', 2, ['Chicago', 'Washington', 'Philadelphia']), |
| 47 | + (city_graph, 'Boston', 3, ['Seattle', 'Riverside', 'Dallas', 'Atlanta', 'Miami']), |
70 | 48 | ], |
71 | | - ids=["distance1", "distance2", "distance3"]) |
72 | | -def test_graph_breadth_first_search(benchmark, graph, source, distance, expected): |
| 49 | + ids=['distance1', 'distance2', 'distance3']) |
| 50 | +def test_graph_breadth_first_search(benchmark, graph, source, distance, expected_nodes): |
| 51 | + """ |
| 52 | + Find the city nodes at a given distance from the source city node in the United States city graph. |
| 53 | + :param benchmark: benchmark fixture |
| 54 | + :param graph: city graph of the United States |
| 55 | + :param source: source city node |
| 56 | + :param distance: distance from the source city node |
| 57 | + :param expected_nodes: list of city nodes at the given distance from the source city node |
| 58 | + """ |
73 | 59 | bfs_generator = benchmark(networkx.bfs_layers, graph, source) |
74 | | - path = [] |
| 60 | + nodes = [] |
75 | 61 | for i, layer in enumerate(bfs_generator): |
76 | 62 | if i == distance: |
77 | | - path = list(layer) |
78 | | - assert path == expected |
| 63 | + nodes = list(layer) |
| 64 | + assert nodes == expected_nodes |
0 commit comments