Skip to content

Commit ec155ae

Browse files
committed
Add 015
1 parent 5d15575 commit ec155ae

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

014LRU缓存.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,6 @@ class LRUCache(object):
6767
else:
6868
self.data[key] = value
6969
self.keys.insert(0, key)
70-
```
70+
```
71+
72+
这是本章内容,希望对你有所帮助。[进入下一章](./015图.md)

015图.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
2+
# 实现图数据结构
3+
4+
## 什么是图
5+
6+
图是类似树这样的数据结构,与树不同的是它可以没有根节点,而且根据类型不同可分为有向图和无向图等。
7+
8+
在现实中可能存在这样的图,满足以下条件。
9+
10+
```
11+
A -> B
12+
A -> C
13+
B -> C
14+
B -> D
15+
C -> D
16+
D -> C
17+
E -> F
18+
F -> C
19+
```
20+
21+
这样的图用Python可以使用字典来表达这样的数据结构,如{A: [B, C], B: [C, D], C: [D], D: [C], E: [F], F: [C]}。
22+
23+
## 如何实现图
24+
25+
要实现图的遍历等功能,以上面的图为例,我们首先要定义数据结构和函数。因为要递归实现,我们需要把start、end传进去,而且还要给历史走过的path。
26+
27+
```
28+
def find_path(graph, start, end, path=[]):
29+
pass
30+
31+
def find_all_paths(graph, start, end, path=[]):
32+
pass
33+
34+
def find_shortest_path(graph, start, end, path=[]):
35+
pass
36+
37+
graph = {'a':['b', 'c'], 'b': ['c', 'd'], 'c': ['d'], 'd': ['c'], 'e': ['f'], 'f': ['c']}
38+
```
39+
40+
首先我们来实现find_path函数,用到了回溯法,从起点开始,如果起点就是终点那先返回,然后遍历每个子节点,注意要确保不是环路而且找了非空的路径才返回。
41+
42+
```
43+
def find_path(graph, start, end, path=[]):
44+
path = path + [start]
45+
46+
if start == end:
47+
return [path]
48+
49+
for node in graph[start]:
50+
if node not in path:
51+
new_path = find_path(graph, node, end, path)
52+
if new_path:
53+
return new_path
54+
55+
graph = {'a':['b', 'c'], 'b': ['c', 'd'], 'c': ['d'], 'd': ['c'], 'e': ['f'], 'f': ['c']}
56+
print(find_path(graph, 'a', 'd'))
57+
# Print [['a', 'b', 'c', 'd']]
58+
```
59+
60+
然后我们来实现find_all_paths函数,
61+
62+
```
63+
def find_all_paths(graph, start, end, path=[]):
64+
path = path + [start]
65+
66+
if start == end:
67+
return [path]
68+
69+
all_paths = []
70+
for node in graph[start]:
71+
if node not in path:
72+
new_paths = find_all_paths(graph, node, end, path)
73+
for new_path in new_paths:
74+
all_paths.append(new_path)
75+
76+
return all_paths
77+
78+
graph = {'a':['b', 'c'], 'b': ['c', 'd'], 'c': ['d'], 'd': ['c'], 'e': ['f'], 'f': ['c']}
79+
print(find_all_paths(graph, 'a', 'd'))
80+
# Print [['a', 'b', 'c', 'd'], ['a', 'b', 'd'], ['a', 'c', 'd']]
81+
```
82+
83+
最后来实现获取最短路径。
84+
85+
```
86+
def find_shortest_path(graph, start, end, path=[]):
87+
path = path + [start]
88+
89+
if start == end:
90+
return path
91+
92+
result_path = None
93+
for node in graph[start]:
94+
if node not in path:
95+
new_path = find_shortest_path(graph, node, end, path)
96+
if result_path == None:
97+
result_path = new_path
98+
elif len(result_path) > len(new_path):
99+
result_path = new_path
100+
101+
return result_path
102+
103+
graph = {'a':['b', 'c'], 'b': ['c', 'd'], 'c': ['d'], 'd': ['c'], 'e': ['f'], 'f': ['c']}
104+
print(find_shortest_path(graph, 'a', 'd'))
105+
# Print ['a', 'b', 'd']
106+
```
107+
108+
这是本章内容,希望对你有所帮助。[进入下一章](./015图.md)

0 commit comments

Comments
 (0)