예제 #1
0
def DFS_bridge(g, u, discovered, ordineVisita):

    for e in g.incident_edges(u):  # for every outgoing edge from u
        v = e.opposite(u)

        if v not in discovered:  # v is an unvisited vertex
            newEdge = Graph.Edge(u, v, None)
            ordineVisita.append(newEdge)
            discovered[v] = e  # e is the tree edge that discovered v
            DFS_bridge(g, v, discovered,
                       ordineVisita)  # recursively explore from v
예제 #2
0
def iterative_dfs(starting_vertex, graph):
    """Perform DFS of the undiscovered portion of Graph g starting at Vertex u.
    It takes O(sum(degree(v)^2) + V)"""
    starting_vertex.discovered = True
    starting_vertex.discovery_edge = Graph.Edge(starting_vertex, None,
                                                None)  # Dummy edge
    walk = starting_vertex

    while walk is not None:
        has_to_go_back = True
        for edge in graph.incident_edges(walk):
            opposite = edge.opposite(walk)
            if not opposite.discovered:
                opposite.discovered = True
                opposite.discovery_edge = edge
                walk = opposite
                has_to_go_back = False
                break

        if has_to_go_back:
            walk = walk.discovery_edge.opposite(walk)

    starting_vertex.discovery_edge = None  # Remove dummy edge