-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.py
56 lines (46 loc) · 1.18 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
"""
FelipedelosH
2023
"""
class Graph:
"""
set edges and nodes
node is a id, edge is a interconector (destination, weight)
the pivot is to move
"""
def __init__(self) -> None:
self.pivot = None
self.nodes = []
self.edges = {}
def setPivot(self, pivot):
"""
Se a pivot to mouve.
"""
if pivot in self.nodes:
self.pivot = pivot
def addNode(self, x):
"""
Add node key
"""
if x not in self.nodes:
self.nodes.append(x)
def addEdge(self, origin ,destination, weight):
"""
Conect two nodes origin->destination with cost
"""
try:
if not origin in self.edges.keys():
self.edges[origin] = []
# Add
self.edges[origin].append((destination, weight))
except:
pass
def step(self, destination):
"""
Enter a destination and mouve if is hable
"""
if self.pivot != None:
for i in self.edges[self.pivot]:
if i[0] == destination:
self.pivot = i[0]
break