data_structures/graphs/searches.py
def bfs_list(g, s):
    """
    Breadth First Search algorithm for adjacency lists.
    
    :param g: the graph to visit. It is assumed the list only contains int data type. 
    :type g: list(list(int))
    :param s: the vertex to start from.
    :type s: int
    :return: (V,D,P), where V is the list of visited vertices in order,
    D is the list of the distance of each vertex from the start and
    P is the list of the predecessors of each vertex
    """

    # This list contains the distances from the start vertex.
    distances = [INFINITY for k in range(len(g))]
    distances[s] = 0

    # This list contains the predecessor of each vertex.
    predecessors = [-1 for k in range(len(g))]

    # Queue of vertices to visit
    q = queue.Queue()
    q.put(s)  # Start with the vertex given as argument
    visited_vertices = []  # List of visited vertices.

    while not q.empty():
        v = q.get()  # Vertex being visited

        for u in g[v]:
            if u not in visited_vertices:
                distances[u] = distances[v] + 1  # Update the distance
                predecessors[u] = v  # Update the predecessor
                q.put(u)
        visited_vertices.append(v)

    return visited_vertices, distances, predecessors

    Breadth First Search algorithm for adjacency lists.
    
    :param g: the graph to visit. It is assumed the list only contains int data type. 
    :type g: list(list(int))
    :param s: the vertex to start from.
    :type s: int
    :return: (V,D,P), where V is the list of visited vertices in order,
    D is the list of the distance of each vertex from the start and
    P is the list of the predecessors of each vertex
    
Breadth First Search algorithm for adjacency lists.
Breadth First Search algorithm for adjacency lists .
def bfs_list(g, s):
    distances = [INFINITY for k in range(len(g))]
    distances[s] = 0
    predecessors = [-1 for k in range(len(g))]
    q = queue.Queue()
    q.put(s)  
    visited_vertices = []  
    while not q.empty():
        v = q.get()  
        for u in g[v]:
            if u not in visited_vertices:
                distances[u] = distances[v] + 1  
                predecessors[u] = v  
                q.put(u)
        visited_vertices.append(v)
    return visited_vertices, distances, predecessors
def bfs_list ( g , s ) : distances = [ INFINITY for k in range ( len ( g ) ) ] distances [ s ] = 0 predecessors = [ - 1 for k in range ( len ( g ) ) ] q = queue . Queue ( ) q . put ( s ) visited_vertices = [ ] while not q . empty ( ) : v = q . get ( ) for u in g [ v ] : if u not in visited_vertices : distances [ u ] = distances [ v ] + 1 predecessors [ u ] = v q . put ( u ) visited_vertices . append ( v ) return visited_vertices , distances , predecessors
Breadth First Search algorithm for adjacency lists.
Breadth First Search algorithm for adjacency lists .
"""
    Breadth First Search algorithm for adjacency lists.
    
    :param g: the graph to visit. It is assumed the list only contains int data type. 
    :type g: list(list(int))
    :param s: the vertex to start from.
    :type s: int
    :return: (V,D,P), where V is the list of visited vertices in order,
    D is the list of the distance of each vertex from the start and
    P is the list of the predecessors of each vertex
    """ # This list contains the distances from the start vertex.
 # This list contains the predecessor of each vertex.
 # Queue of vertices to visit
 # Start with the vertex given as argument
 # List of visited vertices.
 # Vertex being visited
 # Update the distance
 # Update the predecessor

(V,D,P), where V is the list of visited vertices in order,
D is the list of the distance of each vertex from the start and
P is the list of the predecessors of each vertex
( V D P ) where V is the list of visited vertices in order D is the list of the distance of each vertex from the start and P is the list of the predecessors of each vertex
g
the graph to visit. It is assumed the list only contains int data type.
the graph to visit . It is assumed the list only contains int data type .
s
the vertex to start from.
the vertex to start from .