def test_dijkstra():
    g = get_graph()

    single_path = sp.dijkstra(g, 0, 5)
    assert single_path.weight == 62.0
    assert single_path.start_vertex == 0
    assert single_path.end_vertex == 5
    assert list(single_path.edges) == [2, 3, 5]

    single_path = sp.dijkstra(g, 0, 5, use_bidirectional=False)
    assert single_path.weight == 62.0
    assert single_path.start_vertex == 0
    assert single_path.end_vertex == 5
    assert list(single_path.edges) == [2, 3, 5]

    from_paths = sp.dijkstra(g, 0)
    assert from_paths.source_vertex == 0
    single_path = from_paths.get_path(5)
    assert single_path.weight == 62.0
    assert single_path.start_vertex == 0
    assert single_path.end_vertex == 5
    assert list(single_path.edges) == [2, 3, 5]

    single_path = from_paths.get_path(3)
    assert single_path.weight == 103.0
    assert single_path.start_vertex == 0
    assert single_path.end_vertex == 3
    assert list(single_path.edges) == [0, 1]
def test_dijkstra():
    g = get_graph()

    single_path = sp.dijkstra(g, 0, 5)
    assert single_path.weight == 62.0
    assert single_path.start_vertex == 0
    assert single_path.end_vertex == 5
    assert list(single_path.edges) == [2, 3, 5]
    assert [e for e in single_path] == [2, 3, 5]

    single_path = sp.dijkstra(g, 0, 5, use_bidirectional=False)
    assert single_path.weight == 62.0
    assert single_path.start_vertex == 0
    assert single_path.end_vertex == 5
    assert list(single_path.edges) == [2, 3, 5]

    from_paths = sp.dijkstra(g, 0)
    repr(from_paths)
    assert from_paths.source_vertex == 0
    single_path = from_paths.get_path(5)
    assert single_path.weight == 62.0
    assert single_path.start_vertex == 0
    assert single_path.end_vertex == 5
    assert list(single_path.edges) == [2, 3, 5]

    single_path = from_paths.get_path(3)
    repr(single_path)
    assert single_path.weight == 103.0
    assert single_path.start_vertex == 0
    assert single_path.end_vertex == 3
    assert list(single_path.edges) == [0, 1]

    # test no path
    g.add_vertex(100)
    nopath = sp.dijkstra(g, 0, 100)
    assert nopath is None
# %%
# We also assign some random weights from [0, 100) to the edges.

rng = random.Random(17)
for e in g.edges:
    g.set_edge_weight(e, 100 * rng.random())

# %%
# Let us print the graph

print(g)

# %%
# Then, we execute Dijkstra starting from vertex 6.

tree = sp.dijkstra(g, source_vertex=6)

# %%
# The result represents all shortest paths starting from the source vertex. They
# are instances of :py:class:`~jgrapht.types.SingleSourcePaths`.
# To build specific paths to target vertices you call method :py:meth:`~jgrapht.types.SingleSourcePaths.get_path`.
# None is returned if no such path exists.

path = tree.get_path(8)

# %%
# Paths are instances of :py:class:`~jgrapht.types.GraphPath`.

print('path start: {}'.format(path.start_vertex))
print('path end: {}'.format(path.end_vertex))
print('path edges: {}'.format(path.edges))