This repository was archived by the owner on May 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.py
87 lines (64 loc) · 2.53 KB
/
Graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import math
class GraphException(Exception):
def __init__(self, text):
self.txt = text
class Graph:
def __init__(self):
self.__graph: dict = {}
def read_file(self, file: str):
with open(file, "r") as file:
readline = file.read().splitlines()
matrix = []
if(readline[0] == "AM"):
edge_len = len(list(
map(int, filter(None, readline[1].replace(" ", "").split(",")))))
for line in readline[1:]:
format_line = list(
map(int, filter(None, line.replace(" ", "").split(","))))
if edge_len != len(format_line):
raise GraphException(f"Неправильный размер матрицы")
matrix.append(format_line)
else:
raise GraphException(
f"Неправильный параметр матрицы -> {readline[0]}")
for i_item, i in enumerate(matrix):
node_dict = {}
for j_item, j in enumerate(i):
if not j:
continue
else:
node_dict[str(1 + j_item)] = j
self.__graph[str(i_item + 1)] = node_dict
def get_graph(self):
return self.__graph
def dijkstra(self, start, goal):
distance = {}
predecessor = {}
unseenNodes = self.__graph
path = []
for node in unseenNodes:
distance[node] = math.inf
distance[start] = 0
while unseenNodes:
minNode = None
for node in unseenNodes:
if minNode is None:
minNode = node
elif distance[node] < distance[minNode]:
minNode = node
for childNode, weight in self.__graph[minNode].items():
if weight + distance[minNode] < distance[childNode]:
distance[childNode] = weight + \
distance[minNode]
predecessor[childNode] = minNode
unseenNodes.pop(minNode)
currentNode = goal
while currentNode != start:
try:
path.insert(0, currentNode)
currentNode = predecessor[currentNode]
except KeyError:
raise GraphException("Путь недостижим")
path.insert(0, start)
if distance[goal] != math.inf:
return path, distance