-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vertex.py
executable file
·79 lines (61 loc) · 2.28 KB
/
Vertex.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
class Vertex:
def __init__(self, value: str) -> None:
self.__value = value
self.__childrens = {}
self.__parent: Vertex = None
self.__cost = 0
def contains(self, vertex) -> bool:
return self.__childrens.__contains__(vertex)
def add(self, vertexs: dict, directed: bool = False) -> None:
for children, weight in vertexs.items():
self.__childrens.update({children: weight})
if not directed:
children.__childrens.update({self: weight})
def add_costs_for_childrens(self, check=None) -> list:
childrens = []
if self.__childrens:
for vertex, cost in self.get_childrens():
if vertex != check:
new_cost = cost + self.__cost
if vertex.__cost_is_infinite() or new_cost < vertex.__cost:
if vertex != self.__parent:
self.__set_cost_for(vertex, self, new_cost)
childrens.append(vertex)
return childrens
def __set_cost_for(self, vertex, parent, cost: int) -> None:
vertex.__cost = cost
vertex.__parent = parent
def reset(self):
self.__parent = None
self.__cost = 0
def get_childrens(self) -> dict:
return self.__childrens.items()
def get_child_keys(self) -> list:
return list(self.__childrens.keys())
def remove_childrens(self) -> None:
if self.__childrens:
for vertex in self.get_child_keys():
del self.__childrens[vertex]
def get_parents(self) -> list:
result = []
vertex = self
while vertex:
result.append(vertex)
vertex = vertex.__parent
return result
def have_parent(self) -> bool:
return self.__parent
def get_parent(self):
return self.__parent
def get_value(self) -> str:
return self.__value
def get_cost(self) -> float:
return round(self.__cost, 2)
def set_name(self, name: str) -> None:
self.__value = name
def __cost_is_infinite(self) -> bool:
return self.__cost == 0
def __str__(self) -> str:
return self.__value
def __gt__(self, __o: object) -> bool:
return self.__cost > __o.__cost