Skip to content

Commit e325227

Browse files
committed
完成图和图的遍历markdown 和代码
1 parent acdc0e1 commit e325227

File tree

3 files changed

+148
-3
lines changed

3 files changed

+148
-3
lines changed

docs/07_哈希表/hashtable.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ for number in to_insert:
7070
i = 1
7171
while index in inserted_index_set: # 如果计算发现已经占用,继续计算得到下一个可用槽的位置
7272
print('\th({number}) = {number} % M = {index} collision'.format(number=number, index=index))
73-
index = (first_index + i*i) % M
73+
index = (first_index + i*i) % M # 根据二次方探查的公式重新计算下一个需要插入的位置
7474
i += 1
7575
else:
7676
print('h({number}) = {number} % M = {index}'.format(number=number, index=index))

docs/18_图与图的遍历/graph.md

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,67 @@ def bfs(graph, start):
9393
for node in graph[cur_node]:
9494
search_queue.push(node)
9595

96-
97-
print(list(bfs(GRAPH, 'A'))) # 输出 ['A', 'B', 'F', 'C', 'I', 'G', 'E', 'D', 'H']
96+
print('bfs:')
97+
bfs(GRAPH, 'A')
98+
"""
99+
bfs:
100+
A
101+
B
102+
F
103+
C
104+
I
105+
G
106+
E
107+
D
108+
H
109+
"""
98110
```
99111

100112
![](./bfs.png)
101113

102114
### DFS
115+
深度优先搜索(DFS)是每遇到一个节点,如果没有被访问过,就直接去访问它的邻居节点,不断加深。代码其实很简单:
116+
117+
```
118+
DFS_SEARCHED = set()
119+
120+
121+
def dfs(graph, start):
122+
if start not in DFS_SEARCHED:
123+
print(start)
124+
DFS_SEARCHED.add(start)
125+
for node in graph[start]:
126+
if node not in DFS_SEARCHED:
127+
dfs(graph, node)
128+
129+
130+
print('dfs:')
131+
dfs(GRAPH, 'A')
132+
133+
"""
134+
DFS_SEARCHED = set()
135+
136+
137+
def dfs(graph, start):
138+
if start not in DFS_SEARCHED:
139+
print(start)
140+
DFS_SEARCHED.add(start)
141+
for node in graph[start]:
142+
if node not in DFS_SEARCHED:
143+
dfs(graph, node) # 递归访问邻居节点
144+
145+
146+
print('dfs:')
147+
dfs(GRAPH, 'A')
148+
"""
149+
```
103150

104151

105152
# 思考题
153+
- DFS 中我们使用到了递归,请你用栈来改写这个函数?
106154

107155
# 延伸阅读
156+
图的算法还有很多,这里就不一一列举了,感兴趣的读者可以继续阅读一下材料。
157+
108158
- [数据结构之图](https://www.zybuluo.com/guoxs/note/249812)
109159
- 《算法图解》第六章

docs/18_图与图的遍历/graph.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from collections import deque
4+
5+
6+
GRAPH = {
7+
'A': ['B', 'F'],
8+
'B': ['C', 'I', 'G'],
9+
'C': ['B', 'I', 'D'],
10+
'D': ['C', 'I', 'G', 'H', 'E'],
11+
'E': ['D', 'H', 'F'],
12+
'F': ['A', 'G', 'E'],
13+
'G': ['B', 'F', 'H', 'D'],
14+
'H': ['G', 'D', 'E'],
15+
'I': ['B', 'C', 'D'],
16+
}
17+
18+
19+
class Queue(object):
20+
def __init__(self):
21+
self._deque = deque()
22+
23+
def push(self, value):
24+
return self._deque.append(value)
25+
26+
def pop(self):
27+
return self._deque.popleft()
28+
29+
def __len__(self):
30+
return len(self._deque)
31+
32+
33+
def bfs(graph, start):
34+
search_queue = Queue()
35+
search_queue.push(start)
36+
searched = set()
37+
while search_queue: # 队列不为空就继续
38+
cur_node = search_queue.pop()
39+
if cur_node not in searched:
40+
print(cur_node)
41+
searched.add(cur_node)
42+
for node in graph[cur_node]:
43+
search_queue.push(node)
44+
45+
46+
print('bfs:')
47+
bfs(GRAPH, 'A')
48+
49+
50+
DFS_SEARCHED = set()
51+
52+
53+
def dfs(graph, start):
54+
if start not in DFS_SEARCHED:
55+
print(start)
56+
DFS_SEARCHED.add(start)
57+
for node in graph[start]:
58+
if node not in DFS_SEARCHED:
59+
dfs(graph, node)
60+
61+
62+
print('dfs:')
63+
dfs(GRAPH, 'A')
64+
65+
66+
class Stack(object):
67+
def __init__(self):
68+
self._deque = deque()
69+
70+
def push(self, value):
71+
return self._deque.append(value)
72+
73+
def pop(self):
74+
return self._deque.pop()
75+
76+
def __len__(self):
77+
return len(self._deque)
78+
79+
80+
def dfs_use_stack(graph, start):
81+
stack = Stack()
82+
stack.push(start)
83+
searched = set()
84+
while stack:
85+
cur_node = stack.pop()
86+
if cur_node not in searched:
87+
print(cur_node)
88+
searched.add(cur_node)
89+
# 请读者思考这里我为啥加了 reversed,其实不加上是可以的,你看下代码输出
90+
for node in reversed(graph[cur_node]):
91+
stack.push(node)
92+
93+
94+
print('dfs_use_stack:')
95+
dfs_use_stack(GRAPH, 'A')

0 commit comments

Comments
 (0)