|
| 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) |
0 commit comments