-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.py
153 lines (115 loc) · 3.45 KB
/
graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# coding=utf-8
"""
图的邻接表
"""
from collections import deque
class Node(object):
def __init__(self, index, weight, next=None):
self.index = index
self.weight = weight
self.next = next
class AdjacentList(object):
def __init__(self, number):
self.number = number
self.list = [None]*self.number
def insert(self, origin, index, weight=1):
# 头插法
node = Node(index, weight, self.list[origin-1])
self.list[origin-1] = node
class AdjacentMatrix(object):
def __init__(self, number):
self.number = number
self.list = [[None]*number for i in range(number)]
def insert(self, origin, index, weight=1):
self.list[origin-1][index-1] = weight
class Vertex(object):
def __init__(self, key):
self.id = key
self.connected_to = {}
def add_neighbor(self, nbr, weight=1):
self.connected_to[nbr] = weight
def get_neighbors(self):
return self.connected_to.keys()
def get_id(self):
return self.id
def get_weight(self, nbr):
return self.connected_to[nbr]
class Graph(object):
def __init__(self):
self.vertex_list = []
self.vertex_num = 0
def add_vertex(self, key):
# 创建新节点
vertex = Vertex(key)
# 把新节点添加到邻接表里
self.vertex_list[key] = vertex
# 节点个数加1
self.vertex_num += 1
return vertex
def get_vertex(self, n):
return self.vertex_list.get(n, None)
def add_edge(self, origin, tail, weight=1):
if origin not in self.vertex_list:
self.add_vertex(origin)
if tail not in self.vertex_list:
self.add_vertex(tail)
self.vertex_list[origin].add_neighbor(self.vertex_list[tail], weight)
def dfs_traverse(graph):
res = []
def dfs(vertex):
if vertex not in graph:
return
res.append(vertex)
for neighbor in graph[vertex]:
if neighbor not in res:
res.append(neighbor)
dfs(neighbor)
for vertex in graph:
if vertex not in res:
#res.append(vertex)
dfs(vertex)
return res
def dfs_traverse_1(vertex_num, graph):
visited = [0] * vertex_num
def dfs(i):
print(i)
visited[i] = 1
for nei in graph[i]:
if not visited[nei]:
dfs(nei)
for i in range(vertex_num):
if not visited[i]:
dfs(i)
def bfs_traverse(vertex_num, graph):
# 邻接表bfs遍历
visited = [0] * vertex_num
queue = deque()
for vertex in graph:
if not visited[vertex]:
visited[vertex] = 1
print(vertex)
queue.append(vertex)
while queue:
current = queue.popleft()
for nei in graph.get(current, []):
if not visited[nei]:
visited[nei] = 1
print(nei)
queue.append(nei)
def dfs_iter(graph, source):
res = [source]
stack = [source]
while stack:
node = stack.pop()
for nei in graph[node]:
if nei not in res:
stack.append(nei)
res.append(nei)
if __name__ == '__main__':
graph = {0: [1, 2],
1: [2, 3],
2: [3],
3: [2, 6, 7],
4: [5],
5: [2]}
print(bfs_traverse(8, graph))