data_structures/graphs/searches.py
def dfs_imp_list(g, s):
    """
    Imperative Depth First Search for adjacency list
    
    :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: list of visited vertices
    """
    visited_vertices = []
    stack = [s]

    while not stack == []:
        v = stack.pop()
        
        if v not in visited_vertices:
            visited_vertices.append(v)
            
            for u in g[v]:
                stack.append(u)

    return visited_vertices

    Imperative Depth First Search for adjacency list
    
    :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: list of visited vertices
    
Imperative Depth First Search for adjacency list
Imperative Depth First Search for adjacency list
def dfs_imp_list(g, s):
    visited_vertices = []
    stack = [s]
    while not stack == []:
        v = stack.pop()
        if v not in visited_vertices:
            visited_vertices.append(v)
            for u in g[v]:
                stack.append(u)
    return visited_vertices
def dfs_imp_list ( g , s ) : visited_vertices = [ ] stack = [ s ] while not stack == [ ] : v = stack . pop ( ) if v not in visited_vertices : visited_vertices . append ( v ) for u in g [ v ] : stack . append ( u ) return visited_vertices
Imperative Depth First Search for adjacency list
Imperative Depth First Search for adjacency list
"""
    Imperative Depth First Search for adjacency list
    
    :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: list of visited vertices
    """
list of visited vertices
list of visited vertices
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 .