Skip to content

Commit

Permalink
Merge pull request USTC-Resource#19 from ustcpetergu/master
Browse files Browse the repository at this point in the history
Added Jianhui Ma 2018 datastructure course lab, most written in C++
  • Loading branch information
heqin-zhu authored Apr 18, 2020
2 parents 44ca553 + 641590d commit 324771a
Show file tree
Hide file tree
Showing 36 changed files with 103,234 additions and 0 deletions.
3 changes: 3 additions & 0 deletions 数据结构/labs/2018/Dijkstra/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Dijkstra最短路模拟
用Python matplotlib作为GUI
似乎因为某些方法过时绘图功能有些问题
143 changes: 143 additions & 0 deletions 数据结构/labs/2018/Dijkstra/dijkstra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#!/usr/bin/env python3
# Dijkstra shortest path, a simple navigation software, use matploblib as GUI

import math
import copy


class City:
def __init__(self, name, longtitude, latitude):
self.name = name
self.longtitude = longtitude
self.latitude = latitude


class Graph:
def __init__(self):
self.vexnum = 0
self.arcnum = 0
self.vexs = []
self.arcs = []

def getnodeidx(self, node):
idx = -1
for i in range(self.vexnum):
if self.vexs[i].name == node:
idx = i
if idx == -1:
print("Node not found!")
return idx

def addarc(self, n1, n2, d):
# if d == -1:
# d = math.inf
# idx1 = n1
# idx2 = n2
idx1 = self.getnodeidx(n1)
idx2 = self.getnodeidx(n2)
assert idx1 != -1, "No such node %s" % n1
assert idx2 != -1, "No such node %s" % n2
assert idx1 != idx2, "Wrong arc!"
self.arcs[idx1][idx2] = d
self.arcs[idx2][idx1] = d

def shortestpath(self, n1, n2, usestr=0):
if usestr:
n1 = self.getnodeidx(n1)
n2 = self.getnodeidx(n2)
if n1 == -1 or n2 == -1:
print("No such node!")
return -1, []
short = [self.arcs[n1][i] for i in range(self.vexnum)]
path = [[] for i in range(self.vexnum)]
final = [False for i in range(self.vexnum)]
for i in range(self.vexnum):
short[i] = self.arcs[n1][i]
if short[i] < math.inf:
path[i].append(n1)
path[i].append(i)
short[n1] = 0
final[n1] = True
for i in range(self.vexnum - 1):
mininum = math.inf
v = -1
for w in range(self.vexnum):
if not final[w] and short[w] < mininum:
v = w
mininum = short[w]
final[v] = True
if v == n2:
break
for w in range(self.vexnum):
if not final[w] and mininum + self.arcs[v][w] < short[w]:
short[w] = mininum + self.arcs[v][w]
path[w] = copy.deepcopy(path[v])
path[w].append(w)
return short[n2], path[n2]


if __name__ == '__main__':
import matplotlib.pyplot as plt
graph = Graph()
fin = open('./graph.txt', 'r')
graph.vexnum = int(fin.readline())
graph.arcnum = int(fin.readline())
graph.arcs = [[math.inf for i in range(graph.vexnum)] for j in range(graph.vexnum)]
for i in range(graph.vexnum):
name, lo, li = fin.readline().split()
lo = float(lo)
li = float(li)
graph.vexs.append(City(name, lo, li))
for i in range(graph.arcnum):
city1, city2, dist = fin.readline().split()
dist = float(dist)
graph.addarc(city1, city2, dist)
fin.close()
# sp = graph.shortestpath("p", "s", usestr=1)
# print(sp)
# sp = graph.shortestpath("a", "d", usestr=1)
# print(sp)
# sp = graph.shortestpath("a", "g", usestr=1)
# print(sp)
plt.figure()
plt.title("Map")
plt.xlabel("Longtitude")
plt.ylabel("Latitude")
for i in range(graph.vexnum):
for j in range(i):
if graph.arcs[i][j] != math.inf:
v1x = graph.vexs[i].longtitude
v2x = graph.vexs[j].longtitude
v1y = graph.vexs[i].latitude
v2y = graph.vexs[j].latitude
plt.plot([v1x, v2x], [v1y, v2y], color='cyan')
plt.text((v1x + v2x) / 2.0, (v1y + v2y) / 2.0, '%d' % graph.arcs[i][j],
ha='center', va='center', fontsize=7, color='blue')
for i in range(graph.vexnum):
x = graph.vexs[i].longtitude
y = graph.vexs[i].latitude
plt.scatter(x, y, color='cyan')
plt.text(x, y, '%s' % graph.vexs[i].name, ha='center', va='center', color='black', fontsize=13)
plt.show(0)
lines = []
while True:
try:
start = input('Enter source: ')
end = input('Enter destination: ')
except ValueError:
print('Wrong input')
except EOFError:
break
else:
for i in lines:
i.remove()
lines = []
short, path = graph.shortestpath(start, end, usestr=1)
plt.title('Shortest path: %f' % short)
for i in range(len(path) - 1):
v1x = graph.vexs[path[i]].longtitude
v2x = graph.vexs[path[i + 1]].longtitude
v1y = graph.vexs[path[i]].latitude
v2y = graph.vexs[path[i + 1]].latitude
lines.append(plt.plot([v1x, v2x], [v1y, v2y], color='orange')[0])
plt.show(0)
57 changes: 57 additions & 0 deletions 数据结构/labs/2018/Dijkstra/graph.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
25
30
a 87.68333 43.76667
b 101.75000 36.56667
c 102.73333 25.05000
d 104.06667 30.66667
e 103.73333 36.03333
f 106.71667 26.56667
g 108.19 22.48
h 109.24 23.19
i 108.95000 34.26667
j 111.41 40.48
k 113.09 27.51
l 113.23333 23.16667
m 114.31667 30.51667
n 113.65000 34.76667
o 114.06667 22.61667
p 116.41667 39.91667
q 115.90000 28.68333
r 117.11 34.15
s 117.20000 39.13333
t 119.30000 26.08333
u 121.43333 34.50000
v 121.36 38.55
w 123.38333 41.80000
x 125.35000 43.88333
y 126.63333 45.75000
a e 1892
b e 216
c d 1100
c f 639
d i 842
d f 967
e i 676
e j 1145
f h 607
f k 902
g h 255
h k 672
i n 511
j p 668
k l 675
k m 409
k q 367
l o 140
m n 534
n p 695
n r 349
p s 137
q u 825
q t 622
r u 651
r s 674
s w 704
w v 397
w x 305
x y 242
28 changes: 28 additions & 0 deletions 数据结构/labs/2018/Dijkstra/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env pypy3

import dijkstra as dij
import math


if __name__ == '__main__':
graph = dij.Graph()
# fin = open('test.txt', 'r')
# graph.vexnum = int(fin.readline())
# start, end = [int(i) for i in fin.readline().split()]
# fin = open('t()est.txt', 'r')
# fin.close
graph.vexnum = int(input())
start, end = [int(i) for i in input().split()]
for i in range(graph.vexnum):
graph.arcs.append([int(j) if int(j) != -1 else math.inf for j in input().split()])
# graph.arcs.append([int(j) if int(j) != -1 else math.inf for j in fin.readline().split()])
for j in range(i):
graph.arcs[i][j] = graph.arcs[j][i]
# print(graph.arcs)
short, path = graph.shortestpath(start, end)
print("Min=%f" % short)
print("Path ", end='')
for i in path:
print(i, end=' ')
print()
pass
7 changes: 7 additions & 0 deletions 数据结构/labs/2018/Dijkstra/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
5
0 4
0 1 2 3 4
0 0 -1 1 -1
0 0 0 -1 2
0 0 0 0 2
0 0 0 0 0
8 changes: 8 additions & 0 deletions 数据结构/labs/2018/Dijkstra/test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
6
0 1
0 -1 10 -1 30 100
0 0 5 -1 -1 -1
0 0 0 50 -1 -1
0 0 0 0 20 10
0 0 0 0 0 60
0 0 0 0 0 0
Binary file not shown.
13 changes: 13 additions & 0 deletions 数据结构/labs/2018/Elevator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# cmake_minimum_required(VERSION <specify CMake version here>)
project(Elevator)

set(CMAKE_CXX_STANDARD 14)

include_directories(.)

add_executable(Elevator
elevator.cpp
elevator.hpp
event.hpp
main.cpp
person.hpp)
14 changes: 14 additions & 0 deletions 数据结构/labs/2018/Elevator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 电梯模拟

```
mkdir e
cd e
cmake ..
make
cd ..
./e/Elevator
```

然后按回车查看效果

建议使用xterm等速度快的终端效果更好
Loading

0 comments on commit 324771a

Please sign in to comment.