data_structures/graphs/connectivity.py
def same_connected_component(g, u, v):
    """
    Check if two vertices belong to the same connected component.
    Use a dfs from u and stop if v is visited along the way.
    
    :type g: adjacency list of an unweighted graph
    :type u: int
    :type v: int
    :return: bool
    """

    visites_vertices = []  # List of visited vertices
    Q = []  # Stack
    Q.append(u)  # Start the DFS from vertex u

    while Q != []:
        i = Q.pop()
        
        if i not in visited_vertices:
            if i == v:
                return True
            else:
                visited_verticed.append(i)
                for neighbor in g[i]:
                    Q.append(neighbor)
                    
    return False

    Check if two vertices belong to the same connected component.
    Use a dfs from u and stop if v is visited along the way.
    
    :type g: adjacency list of an unweighted graph
    :type u: int
    :type v: int
    :return: bool
    
Check if two vertices belong to the same connected component.
Use a dfs from u and stop if v is visited along the way.
Check if two vertices belong to the same connected component . Use a dfs from u and stop if v is visited along the way .
def same_connected_component(g, u, v):
    visites_vertices = []  
    Q = []  
    Q.append(u)  
    while Q != []:
        i = Q.pop()
        if i not in visited_vertices:
            if i == v:
                return True
            else:
                visited_verticed.append(i)
                for neighbor in g[i]:
                    Q.append(neighbor)
    return False
def same_connected_component ( g , u , v ) : visites_vertices = [ ] Q = [ ] Q . append ( u ) while Q != [ ] : i = Q . pop ( ) if i not in visited_vertices : if i == v : return True else : visited_verticed . append ( i ) for neighbor in g [ i ] : Q . append ( neighbor ) return False
Check if two vertices belong to the same connected component.
Check if two vertices belong to the same connected component .
"""
    Check if two vertices belong to the same connected component.
    Use a dfs from u and stop if v is visited along the way.
    
    :type g: adjacency list of an unweighted graph
    :type u: int
    :type v: int
    :return: bool
    """ # List of visited vertices # Stack # Start the DFS from vertex u
None
g
u
v