#Induction
        visited[node] = True
        dfs_arr.append(node)
        for i in graph.adjList[node]:
            search(i)

        return None

    search(node)

    for i in graph.nodes:
        if visited[i] == False:
            search(i)

    return dfs_arr


if __name__ == "__main__":

    graph = Graph()

    graph.addEdge(1, 2)
    graph.addEdge(2, 4)
    graph.addEdge(4, 6)
    graph.addEdge(6, 7)
    graph.addEdge(7, 2)
    graph.addEdge(3, 5)

    print(dfs(graph, 1))
        if visited[node] != False:
            return None

        #Induction
        visited[node] = True
        dfs_arr.append(node)
        for i in graph.adjList[node]:
            search(i)
        stack.append(node)
        return None

    for i in graph.nodes:
        if visited[i] == False:
            search(i)

    return stack[::-1]


if __name__ == "__main__":

    graph = Graph(bidir=False)

    graph.addEdge(4, 0)
    graph.addEdge(5, 0)
    graph.addEdge(4, 1)
    graph.addEdge(3, 1)
    graph.addEdge(5, 2)
    graph.addEdge(2, 3)

    print(topo_sort_dfs(graph))
def transpose(graph):
    transpose = Graph(bidir=False)
    for i in graph.adjList:
        for j in graph.adjList[i]:
            transpose.addEdge(j, i)
    return transpose
    distance[x] = 0
    queue.append(x)

    while queue:
        node = queue.pop(0)
        for i in graph.adjList[node]:
            if distance[i] > distance[node] + 1:
                distance[i] = distance[node] + 1
                queue.append(i)

    return distance


if __name__ == "__main__":

    graph = Graph()

    graph.addEdge(0, 1)
    graph.addEdge(1, 3)
    graph.addEdge(0, 3)
    graph.addEdge(1, 2)
    graph.addEdge(2, 6)
    graph.addEdge(3, 4)
    graph.addEdge(4, 5)
    graph.addEdge(5, 6)
    graph.addEdge(6, 7)
    graph.addEdge(7, 8)
    graph.addEdge(6, 8)

    print(shortestPath(graph, 0))
        bruh = dfs_single_directed(transposed_graph, i)
        if bruh:
            scc.append(bruh)
    return scc


def transpose(graph):
    transpose = Graph(bidir=False)
    for i in graph.adjList:
        for j in graph.adjList[i]:
            transpose.addEdge(j, i)
    return transpose


if __name__ == "__main__":

    visited = {}

    graph = Graph(bidir=False)

    graph.addEdge(1, 2)
    graph.addEdge(2, 3)
    graph.addEdge(3, 1)
    graph.addEdge(2, 4)
    graph.addEdge(4, 5)

    for i in graph.nodes:
        visited[i] = False

    print(kosaraju(graph))
示例#6
0
                if low[i] != time[i]:
                    low[node] = low[i]
        if prev != -1:
            if low[node] >= time[prev]:
                vertices.append(prev)

        return None

    search(source, -1)
    vertices.remove(source)
    return vertices


if __name__ == "__main__":
    t = 1
    graph = Graph()

    graph.addEdge(1, 2)
    graph.addEdge(2, 3)
    graph.addEdge(3, 4)
    graph.addEdge(4, 1)
    graph.addEdge(4, 5)
    graph.addEdge(5, 6)
    graph.addEdge(6, 7)
    graph.addEdge(6, 9)
    graph.addEdge(7, 8)
    graph.addEdge(9, 8)
    graph.addEdge(8, 10)
    graph.addEdge(10, 11)
    graph.addEdge(10, 12)
    graph.addEdge(11, 12)
示例#7
0
    #Loop
    while queue:
        node = queue.pop(0)
        counter += 1
        for j in graph.adjList[node]:
            indegree[j] -= 1
            if indegree[j] == 0:
                queue.append(j)

        topo_sort.append(node)

    return counter != len(graph.nodes)


if __name__ == "__main__":

    graph = Graph(bidir=False)

    graph.addEdge(1, 2)
    graph.addEdge(2, 3)
    graph.addEdge(3, 4)
    graph.addEdge(4, 5)
    graph.addEdge(3, 6)
    graph.addEdge(6, 5)
    graph.addEdge(7, 2)
    graph.addEdge(7, 8)
    graph.addEdge(8, 9)
    graph.addEdge(9, 7)

    print(cycle_bfs(graph))
            if i != prev:
                flag = search(i, node)
                ans = max(ans, flag)
        return ans

    for i in graph.nodes:
        if visited[i] == False:
            flag = search(i, -1)
            ans = max(ans, flag)

    return ans


if __name__ == "__main__":

    graph = Graph()

    graph.addEdge(1, 2)
    graph.addEdge(2, 4)
    graph.addEdge(3, 5)
    graph.addEdge(5, 6)
    graph.addEdge(6, 7)
    graph.addEdge(7, 8)
    graph.addEdge(8, 9)
    graph.addEdge(9, 10)
    graph.addEdge(10, 5)
    graph.addEdge(8, 11)

    print(cycle_bfs(graph))
    print(cycle_dfs(graph))
            else:
                if visited[i] == visited[node]:
                    flag = False
                    return flag
        return ans

    for i in graph.nodes:
        if visited[i] == -1:
            flag = search(i, 0)
            ans = min(ans, flag)

    return ans


if __name__ == "__main__":

    graph = Graph()

    graph.addEdge(1, 2)
    graph.addEdge(2, 3)
    #graph.addEdge(2, 4)
    graph.addEdge(3, 4)
    graph.addEdge(4, 5)
    graph.addEdge(5, 6)
    graph.addEdge(6, 7)
    graph.addEdge(7, 2)
    graph.addEdge(5, 8)

    print(bipartite_bfs(graph))
    print(bipartite_dfs(graph))