コード例 #1
0
from dfs import dfs
from graph_class import Graph
from check_connected_dfs import find_all_group, check_connected_num_dfs

G1 = Graph(13)
print(G1.nodes)
print(G1.adj_of_node)
G1.build_undirected_edge(0, 1)
G1.build_undirected_edge(0, 9)
G1.build_undirected_edge(1, 0)
G1.build_undirected_edge(1, 8)
G1.build_undirected_edge(2, 3)
G1.build_undirected_edge(3, 2)
G1.build_undirected_edge(3, 4)
G1.build_undirected_edge(3, 5)
G1.build_undirected_edge(3, 7)
G1.build_undirected_edge(4, 3)
G1.build_undirected_edge(5, 3)
G1.build_undirected_edge(5, 6)
G1.build_undirected_edge(6, 5)
G1.build_undirected_edge(6, 7)
G1.build_undirected_edge(7, 3)
G1.build_undirected_edge(7, 8)
G1.build_undirected_edge(7, 10)
G1.build_undirected_edge(7, 11)
G1.build_undirected_edge(8, 1)
G1.build_undirected_edge(8, 7)
G1.build_undirected_edge(8, 9)
G1.build_undirected_edge(9, 0)
G1.build_undirected_edge(9, 8)
G1.build_undirected_edge(10, 7)
コード例 #2
0
    path = []
    G.nodes_visited = [False for i in range(G.nodes_num)]
    dfs_util(G, start, path)
    return path


def dfs_util(G, start, path):
    G.nodes_visited[start] = True
    path.append(start)
    for neighbor in G.adj_of_node[start]:
        if G.nodes_visited[neighbor] == False:
            dfs_util(G, neighbor, path)


G1 = Graph(13)
G1.build_undirected_edge(0, 1)
G1.build_undirected_edge(0, 9)
G1.build_undirected_edge(1, 8)
G1.build_undirected_edge(2, 3)
G1.build_undirected_edge(3, 4)
G1.build_undirected_edge(3, 5)
G1.build_undirected_edge(3, 7)
G1.build_undirected_edge(5, 6)
G1.build_undirected_edge(6, 7)
G1.build_undirected_edge(7, 8)
G1.build_undirected_edge(7, 10)
G1.build_undirected_edge(7, 11)
G1.build_undirected_edge(8, 9)
G1.build_undirected_edge(10, 7)
G1.build_undirected_edge(10, 11)
print(dfs(G1, 0))