Skip to content

Commit 641590d

Browse files
author
petergu
committed
Added Jianhui Ma 2018 datastructure course lab, most written in C++
1 parent 44ca553 commit 641590d

36 files changed

+103234
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Dijkstra最短路模拟
2+
用Python matplotlib作为GUI
3+
似乎因为某些方法过时绘图功能有些问题
+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#!/usr/bin/env python3
2+
# Dijkstra shortest path, a simple navigation software, use matploblib as GUI
3+
4+
import math
5+
import copy
6+
7+
8+
class City:
9+
def __init__(self, name, longtitude, latitude):
10+
self.name = name
11+
self.longtitude = longtitude
12+
self.latitude = latitude
13+
14+
15+
class Graph:
16+
def __init__(self):
17+
self.vexnum = 0
18+
self.arcnum = 0
19+
self.vexs = []
20+
self.arcs = []
21+
22+
def getnodeidx(self, node):
23+
idx = -1
24+
for i in range(self.vexnum):
25+
if self.vexs[i].name == node:
26+
idx = i
27+
if idx == -1:
28+
print("Node not found!")
29+
return idx
30+
31+
def addarc(self, n1, n2, d):
32+
# if d == -1:
33+
# d = math.inf
34+
# idx1 = n1
35+
# idx2 = n2
36+
idx1 = self.getnodeidx(n1)
37+
idx2 = self.getnodeidx(n2)
38+
assert idx1 != -1, "No such node %s" % n1
39+
assert idx2 != -1, "No such node %s" % n2
40+
assert idx1 != idx2, "Wrong arc!"
41+
self.arcs[idx1][idx2] = d
42+
self.arcs[idx2][idx1] = d
43+
44+
def shortestpath(self, n1, n2, usestr=0):
45+
if usestr:
46+
n1 = self.getnodeidx(n1)
47+
n2 = self.getnodeidx(n2)
48+
if n1 == -1 or n2 == -1:
49+
print("No such node!")
50+
return -1, []
51+
short = [self.arcs[n1][i] for i in range(self.vexnum)]
52+
path = [[] for i in range(self.vexnum)]
53+
final = [False for i in range(self.vexnum)]
54+
for i in range(self.vexnum):
55+
short[i] = self.arcs[n1][i]
56+
if short[i] < math.inf:
57+
path[i].append(n1)
58+
path[i].append(i)
59+
short[n1] = 0
60+
final[n1] = True
61+
for i in range(self.vexnum - 1):
62+
mininum = math.inf
63+
v = -1
64+
for w in range(self.vexnum):
65+
if not final[w] and short[w] < mininum:
66+
v = w
67+
mininum = short[w]
68+
final[v] = True
69+
if v == n2:
70+
break
71+
for w in range(self.vexnum):
72+
if not final[w] and mininum + self.arcs[v][w] < short[w]:
73+
short[w] = mininum + self.arcs[v][w]
74+
path[w] = copy.deepcopy(path[v])
75+
path[w].append(w)
76+
return short[n2], path[n2]
77+
78+
79+
if __name__ == '__main__':
80+
import matplotlib.pyplot as plt
81+
graph = Graph()
82+
fin = open('./graph.txt', 'r')
83+
graph.vexnum = int(fin.readline())
84+
graph.arcnum = int(fin.readline())
85+
graph.arcs = [[math.inf for i in range(graph.vexnum)] for j in range(graph.vexnum)]
86+
for i in range(graph.vexnum):
87+
name, lo, li = fin.readline().split()
88+
lo = float(lo)
89+
li = float(li)
90+
graph.vexs.append(City(name, lo, li))
91+
for i in range(graph.arcnum):
92+
city1, city2, dist = fin.readline().split()
93+
dist = float(dist)
94+
graph.addarc(city1, city2, dist)
95+
fin.close()
96+
# sp = graph.shortestpath("p", "s", usestr=1)
97+
# print(sp)
98+
# sp = graph.shortestpath("a", "d", usestr=1)
99+
# print(sp)
100+
# sp = graph.shortestpath("a", "g", usestr=1)
101+
# print(sp)
102+
plt.figure()
103+
plt.title("Map")
104+
plt.xlabel("Longtitude")
105+
plt.ylabel("Latitude")
106+
for i in range(graph.vexnum):
107+
for j in range(i):
108+
if graph.arcs[i][j] != math.inf:
109+
v1x = graph.vexs[i].longtitude
110+
v2x = graph.vexs[j].longtitude
111+
v1y = graph.vexs[i].latitude
112+
v2y = graph.vexs[j].latitude
113+
plt.plot([v1x, v2x], [v1y, v2y], color='cyan')
114+
plt.text((v1x + v2x) / 2.0, (v1y + v2y) / 2.0, '%d' % graph.arcs[i][j],
115+
ha='center', va='center', fontsize=7, color='blue')
116+
for i in range(graph.vexnum):
117+
x = graph.vexs[i].longtitude
118+
y = graph.vexs[i].latitude
119+
plt.scatter(x, y, color='cyan')
120+
plt.text(x, y, '%s' % graph.vexs[i].name, ha='center', va='center', color='black', fontsize=13)
121+
plt.show(0)
122+
lines = []
123+
while True:
124+
try:
125+
start = input('Enter source: ')
126+
end = input('Enter destination: ')
127+
except ValueError:
128+
print('Wrong input')
129+
except EOFError:
130+
break
131+
else:
132+
for i in lines:
133+
i.remove()
134+
lines = []
135+
short, path = graph.shortestpath(start, end, usestr=1)
136+
plt.title('Shortest path: %f' % short)
137+
for i in range(len(path) - 1):
138+
v1x = graph.vexs[path[i]].longtitude
139+
v2x = graph.vexs[path[i + 1]].longtitude
140+
v1y = graph.vexs[path[i]].latitude
141+
v2y = graph.vexs[path[i + 1]].latitude
142+
lines.append(plt.plot([v1x, v2x], [v1y, v2y], color='orange')[0])
143+
plt.show(0)
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
25
2+
30
3+
a 87.68333 43.76667
4+
b 101.75000 36.56667
5+
c 102.73333 25.05000
6+
d 104.06667 30.66667
7+
e 103.73333 36.03333
8+
f 106.71667 26.56667
9+
g 108.19 22.48
10+
h 109.24 23.19
11+
i 108.95000 34.26667
12+
j 111.41 40.48
13+
k 113.09 27.51
14+
l 113.23333 23.16667
15+
m 114.31667 30.51667
16+
n 113.65000 34.76667
17+
o 114.06667 22.61667
18+
p 116.41667 39.91667
19+
q 115.90000 28.68333
20+
r 117.11 34.15
21+
s 117.20000 39.13333
22+
t 119.30000 26.08333
23+
u 121.43333 34.50000
24+
v 121.36 38.55
25+
w 123.38333 41.80000
26+
x 125.35000 43.88333
27+
y 126.63333 45.75000
28+
a e 1892
29+
b e 216
30+
c d 1100
31+
c f 639
32+
d i 842
33+
d f 967
34+
e i 676
35+
e j 1145
36+
f h 607
37+
f k 902
38+
g h 255
39+
h k 672
40+
i n 511
41+
j p 668
42+
k l 675
43+
k m 409
44+
k q 367
45+
l o 140
46+
m n 534
47+
n p 695
48+
n r 349
49+
p s 137
50+
q u 825
51+
q t 622
52+
r u 651
53+
r s 674
54+
s w 704
55+
w v 397
56+
w x 305
57+
x y 242
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env pypy3
2+
3+
import dijkstra as dij
4+
import math
5+
6+
7+
if __name__ == '__main__':
8+
graph = dij.Graph()
9+
# fin = open('test.txt', 'r')
10+
# graph.vexnum = int(fin.readline())
11+
# start, end = [int(i) for i in fin.readline().split()]
12+
# fin = open('t()est.txt', 'r')
13+
# fin.close
14+
graph.vexnum = int(input())
15+
start, end = [int(i) for i in input().split()]
16+
for i in range(graph.vexnum):
17+
graph.arcs.append([int(j) if int(j) != -1 else math.inf for j in input().split()])
18+
# graph.arcs.append([int(j) if int(j) != -1 else math.inf for j in fin.readline().split()])
19+
for j in range(i):
20+
graph.arcs[i][j] = graph.arcs[j][i]
21+
# print(graph.arcs)
22+
short, path = graph.shortestpath(start, end)
23+
print("Min=%f" % short)
24+
print("Path ", end='')
25+
for i in path:
26+
print(i, end=' ')
27+
print()
28+
pass
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
5
2+
0 4
3+
0 1 2 3 4
4+
0 0 -1 1 -1
5+
0 0 0 -1 2
6+
0 0 0 0 2
7+
0 0 0 0 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
6
2+
0 1
3+
0 -1 10 -1 30 100
4+
0 0 5 -1 -1 -1
5+
0 0 0 50 -1 -1
6+
0 0 0 0 20 10
7+
0 0 0 0 0 60
8+
0 0 0 0 0 0
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# cmake_minimum_required(VERSION <specify CMake version here>)
2+
project(Elevator)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
include_directories(.)
7+
8+
add_executable(Elevator
9+
elevator.cpp
10+
elevator.hpp
11+
event.hpp
12+
main.cpp
13+
person.hpp)
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 电梯模拟
2+
3+
```
4+
mkdir e
5+
cd e
6+
cmake ..
7+
make
8+
cd ..
9+
./e/Elevator
10+
```
11+
12+
然后按回车查看效果
13+
14+
建议使用xterm等速度快的终端效果更好

0 commit comments

Comments
 (0)