data_structures/graphs/shortest_path.py
def path_from(g, s):
    """
    Returns the path to go to a vertex using a bfs
    :param g: Graph as an adjacency list
    :param s:
    :return: List l such that l[i] is the list of vertices to use to go to i from s
    """

    V = []  # List of visited vertices
    P = [None for k in range(len(g))]  # List of paths
    Q = queue.Queue()
    Q.put((s, []))

    while not Q.empty():
        u, p = Q.get()

        if u not in V:
            V += [u]
            for v in g[u]:
                Q.put((v, p + [u]))
            P[u] = p + [u]
    return P

    Returns the path to go to a vertex using a bfs
    :param g: Graph as an adjacency list
    :param s:
    :return: List l such that l[i] is the list of vertices to use to go to i from s
    
Returns the path to go to a vertex using a bfs
Returns the path to go to a vertex using a bfs
def path_from(g, s):
    V = []  
    P = [None for k in range(len(g))]  
    Q = queue.Queue()
    Q.put((s, []))
    while not Q.empty():
        u, p = Q.get()
        if u not in V:
            V += [u]
            for v in g[u]:
                Q.put((v, p + [u]))
            P[u] = p + [u]
    return P
def path_from ( g , s ) : V = [ ] P = [ None for k in range ( len ( g ) ) ] Q = queue . Queue ( ) Q . put ( ( s , [ ] ) ) while not Q . empty ( ) : u , p = Q . get ( ) if u not in V : V += [ u ] for v in g [ u ] : Q . put ( ( v , p + [ u ] ) ) P [ u ] = p + [ u ] return P
Returns the path to go to a vertex using a bfs
Returns the path to go to a vertex using a bfs
"""
    Returns the path to go to a vertex using a bfs
    :param g: Graph as an adjacency list
    :param s:
    :return: List l such that l[i] is the list of vertices to use to go to i from s
    """ # List of visited vertices
 # List of paths

List l such that l[i] is the list of vertices to use to go to i from s
List l such that l [ i ] is the list of vertices to use to go to i from s
g
Graph as an adjacency list
Graph as an adjacency list
s
None