Skip to main content

Single Source Shortest Path (Unweighted)

Finds the single source shortest path of an unweighted graph.

Usage

sssp_unweighted(graph: Graph, source: int)

Parameters:
      graph : Graph
            Graph Object
      source : int
            Source Vertex

Returns:
      dict
            A dictionary of vertex -> cost

Example

from jellybeans.algos import sssp_unweightedfrom jellybeans.structures import Graphg = Graph()g.add_vertex(0)g.add_vertex(1)g.add_vertex(2)g.add_vertex(3)g.add_vertex(4)g.add_vertex(5)g.add_edge(0, 1)g.add_edge(0, 2)g.add_edge(1, 3)g.add_edge(1, 5)g.add_edge(2, 4)g.add_edge(3, 5)g.add_edge(4, 0)g.add_edge(4, 3)res = sssp_unweighted(g, 0)print(res) # {0: 0, 1: 1, 2: 1, 3: 2, 4: 2, 5: 2}