|
| 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 |
| 33 | + |
| 34 | + |
| 35 | +city_graph = create_city_graph() |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.benchmark(group='graph_neighbors') |
| 39 | +@pytest.mark.parametrize( |
| 40 | + argnames='graph, source, expected', |
| 41 | + 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"]) |
| 57 | + ]) |
| 58 | +def test_graph_neighbors(benchmark, graph, source, expected): |
| 59 | + result = benchmark(graph.neighbors, source) |
| 60 | + assert sorted(expected) == sorted(result) |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.benchmark(group='graph_breadth_first_search') |
| 64 | +@pytest.mark.parametrize( |
| 65 | + argnames='graph, source, distance, expected', |
| 66 | + 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"]), |
| 70 | + ], |
| 71 | + ids=["distance1", "distance2", "distance3"]) |
| 72 | +def test_graph_breadth_first_search(benchmark, graph, source, distance, expected): |
| 73 | + bfs_generator = benchmark(networkx.bfs_layers, graph, source) |
| 74 | + path = [] |
| 75 | + for i, layer in enumerate(bfs_generator): |
| 76 | + if i == distance: |
| 77 | + path = list(layer) |
| 78 | + assert path == expected |
0 commit comments