data_structures/graphs/shortest_path.py
def distances_from(g, s):
    """
    Returns the list of distances from vertex s using a bfs.
    :type g: list(list(int))
    :type s: int
    :param g: unweighted graph as an adjacency list
    :param s: vertex to start from
    :return: A list of distances such that distances[i] is the distance from s to vertex i
    """

    # This list contains the distances from the start vertex.
    distances = [INFINITY for k in range(len(g))]  # Distances
    visited_vertices = []  # List of visited vertices

    # Queue of vertices to visit
    q = queue.Queue()
    q.put((s, 0))

    while not q.empty():
        current_vertex, current_distance = q.get()

        if current_vertex not in visited_vertices:
            visited_vertices += [current_vertex]
            for v in g[current_vertex]:
                q.put((v, current_distance + 1))
            distances[current_vertex] = current_distance
    return distances

    Returns the list of distances from vertex s using a bfs.
    :type g: list(list(int))
    :type s: int
    :param g: unweighted graph as an adjacency list
    :param s: vertex to start from
    :return: A list of distances such that distances[i] is the distance from s to vertex i
    
Returns the list of distances from vertex s using a bfs.
Returns the list of distances from vertex s using a bfs .
def distances_from(g, s):
    distances = [INFINITY for k in range(len(g))]  
    visited_vertices = []  
    q = queue.Queue()
    q.put((s, 0))
    while not q.empty():
        current_vertex, current_distance = q.get()
        if current_vertex not in visited_vertices:
            visited_vertices += [current_vertex]
            for v in g[current_vertex]:
                q.put((v, current_distance + 1))
            distances[current_vertex] = current_distance
    return distances
def distances_from ( g , s ) : distances = [ INFINITY for k in range ( len ( g ) ) ] visited_vertices = [ ] q = queue . Queue ( ) q . put ( ( s , 0 ) ) while not q . empty ( ) : current_vertex , current_distance = q . get ( ) if current_vertex not in visited_vertices : visited_vertices += [ current_vertex ] for v in g [ current_vertex ] : q . put ( ( v , current_distance + 1 ) ) distances [ current_vertex ] = current_distance return distances
Returns the list of distances from vertex s using a bfs.
Returns the list of distances from vertex s using a bfs .
"""
    Returns the list of distances from vertex s using a bfs.
    :type g: list(list(int))
    :type s: int
    :param g: unweighted graph as an adjacency list
    :param s: vertex to start from
    :return: A list of distances such that distances[i] is the distance from s to vertex i
    """ # This list contains the distances from the start vertex.
 # Distances
 # List of visited vertices
 # Queue of vertices to visit

A list of distances such that distances[i] is the distance from s to vertex i
A list of distances such that distances [ i ] is the distance from s to vertex i
g
unweighted graph as an adjacency list
unweighted graph as an adjacency list
s
vertex to start from
vertex to start from