Example #1
0
def test_shortest_path_length_no_target(graphs):
    cugraph_G, nx_G, cupy_df = graphs

    cugraph_path_1_to_all = cugraph.shortest_path_length(cugraph_G, 1)
    nx_path_1_to_all = nx.shortest_path_length(nx_G,
                                               source="1",
                                               weight="weight")
    nx_gpu_path_1_to_all = cugraph.shortest_path_length(nx_G, "1")
    cupy_path_1_to_all = cugraph.shortest_path_length(cupy_df, 1)

    # Cast networkx graph on cugraph vertex column type from str to int.
    # SSSP preserves vertex type, convert for comparison
    nx_gpu_path_1_to_all["vertex"] = \
        nx_gpu_path_1_to_all["vertex"].astype("int32")

    assert cugraph_path_1_to_all == nx_gpu_path_1_to_all
    assert cugraph_path_1_to_all == cupy_path_1_to_all

    # results for vertex 8 and 9 are not returned
    assert cugraph_path_1_to_all.shape[0] == len(nx_path_1_to_all) + 2

    for index in range(cugraph_path_1_to_all.shape[0]):

        vertex = str(cugraph_path_1_to_all["vertex"][index].item())
        distance = cugraph_path_1_to_all["distance"][index].item()

        # verify cugraph against networkx
        if vertex in {'8', '9'}:
            # Networkx does not return distances for these vertexes.
            assert distance == sys.float_info.max
        else:
            assert distance == nx_path_1_to_all[vertex]
Example #2
0
def test_shortest_path_length_no_path(graphs):
    cugraph_G, nx_G, cupy_df = graphs

    path_1_to_8 = cugraph.shortest_path_length(cugraph_G, 1, 8)
    assert path_1_to_8 == sys.float_info.max
    assert path_1_to_8 == cugraph.shortest_path_length(nx_G, "1", "8")
    assert path_1_to_8 == cugraph.shortest_path_length(cupy_df, 1, 8)
Example #3
0
def test_shortest_path_length_invalid_vertexes(graphs):
    cugraph_G, nx_G, cupy_df = graphs

    with pytest.raises(ValueError):
        cugraph.shortest_path_length(cugraph_G, 0, 42)

    with pytest.raises(ValueError):
        cugraph.shortest_path_length(nx_G, "0", "42")

    with pytest.raises(ValueError):
        cugraph.shortest_path_length(cupy_df, 0, 42)
Example #4
0
def test_shortest_path_length_invalid_target(graphs):
    cugraph_G, nx_G, cupy_df = graphs

    with pytest.raises(ValueError):
        cugraph.shortest_path_length(cugraph_G, 1, 10)

    with pytest.raises(ValueError):
        cugraph.shortest_path_length(nx_G, "1", "10")

    with pytest.raises(ValueError):
        cugraph.shortest_path_length(cupy_df, 1, 10)
Example #5
0
def test_shortest_path_length_invalid_source(graphs):
    cugraph_G, nx_G, cupy_df = graphs

    with pytest.raises(ValueError):
        cugraph.shortest_path_length(cugraph_G, -1, 1)

    with pytest.raises(ValueError):
        cugraph.shortest_path_length(nx_G, "-1", "1")

    with pytest.raises(ValueError):
        cugraph.shortest_path_length(cupy_df, -1, 1)
Example #6
0
def test_connected_graph_shortest_path_length(graphs):
    cugraph_G, nx_G, cupy_df = graphs

    path_1_to_1_length = cugraph.shortest_path_length(cugraph_G, 1, 1)
    assert path_1_to_1_length == 0.0
    assert path_1_to_1_length == nx.shortest_path_length(nx_G,
                                                         "1",
                                                         target="1",
                                                         weight="weight")
    assert path_1_to_1_length == cugraph.shortest_path_length(nx_G, "1", "1")
    assert path_1_to_1_length == cugraph.shortest_path_length(cupy_df, 1, 1)

    path_1_to_5_length = cugraph.shortest_path_length(cugraph_G, 1, 5)
    assert path_1_to_5_length == 2.0
    assert path_1_to_5_length == nx.shortest_path_length(nx_G,
                                                         "1",
                                                         target="5",
                                                         weight="weight")
    assert path_1_to_5_length == cugraph.shortest_path_length(nx_G, "1", "5")
    assert path_1_to_5_length == cugraph.shortest_path_length(cupy_df, 1, 5)

    path_1_to_3_length = cugraph.shortest_path_length(cugraph_G, 1, 3)
    assert path_1_to_3_length == 2.0
    assert path_1_to_3_length == nx.shortest_path_length(nx_G,
                                                         "1",
                                                         target="3",
                                                         weight="weight")
    assert path_1_to_3_length == cugraph.shortest_path_length(nx_G, "1", "3")
    assert path_1_to_3_length == cugraph.shortest_path_length(cupy_df, 1, 3)

    path_1_to_6_length = cugraph.shortest_path_length(cugraph_G, 1, 6)
    assert path_1_to_6_length == 2.0
    assert path_1_to_6_length == nx.shortest_path_length(nx_G,
                                                         "1",
                                                         target="6",
                                                         weight="weight")
    assert path_1_to_6_length == cugraph.shortest_path_length(nx_G, "1", "6")
    assert path_1_to_6_length == cugraph.shortest_path_length(cupy_df, 1, 6)