-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdistances.py
174 lines (158 loc) · 3.95 KB
/
distances.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import networkx as nx
from numpy import Inf
def print_nodes(G):
print("Vértices:|", end="")
for n in G.nodes():
print(n, "|", end='')
print("\n")
def print_Ubitmap(U, G):
print("U:|", end="")
for n in G.nodes():
if n in U:
print(1, "|", end='')
else:
print(0, "|", end='')
print("\n")
def print_etiquetas(dist):
print("(dist(·), ·):|", end="")
for d in dist.values():
print(d, "|", end='')
print("\n")
def dijkstra(G, s):
U = set()
dist = {}
etiquetas = {}
for v in G.nodes:
dist[v] = Inf
etiquetas[v] = (dist[v], s)
dist[s] = 0
etiquetas[s] = (dist[s], s)
print("====")
print_nodes(G)
print_Ubitmap(U, G)
print_etiquetas(etiquetas)
print("====")
while len(U) != len(G.nodes):
can_choose = []
for node in G.nodes():
if node not in U:
can_choose.append(node)
current_node_weight = Inf
for node in can_choose:
if dist[node] < current_node_weight:
current_node_weight = dist[node]
next_node = node
print("next_node", next_node)
U.add(next_node)
print_nodes(G)
print_Ubitmap(U, G)
for v in sorted(list(G.neighbors(next_node))):
if (dist[next_node] + G.get_edge_data(next_node, v)['weight']) < dist[v]:
dist[v] = dist[next_node] + G.get_edge_data(next_node, v)['weight']
etiquetas[v] = (dist[v], next_node)
print_etiquetas(etiquetas)
print("====")
return dist
def distancias_no_ponderado(G, s):
Q = []
estado={}
dist={}
for w in G.nodes():
estado[w] = 0
dist[w] = Inf
estado[s] = 1
dist[s] = 0
Q.append(s)
print("====")
print("Q", Q)
print("Vértice añadido", s)
print("Vértice eliminado", "-")
print("dist", list(dist.values()))
print("====")
while not not Q:
w = Q[0]
neighbours = sorted(list(G.neighbors(w)))
for u in neighbours:
if estado[u] == 0:
Q.append(u)
estado[u] = 1
dist[u] = dist[w] + 1
print("Q", Q)
print("Vértice añadido", u)
print("Vértice eliminado", "-")
print("dist", list(dist.values()))
e = Q[0]
Q = Q[1:]
print("Q", Q)
print("Vértice añadido", "-")
print("Vértice eliminado", e)
print("dist", list(dist.values()))
return dist
if __name__ == '__main__':
adjs = {
'V': {
'M': {"weight": 6},
'CS': {"weight": 5},
'SM': {"weight": 2},
},
'M': {
'T': {"weight": 7},
'CS': {"weight": 3},
},
'CS': {
'PV': {"weight": 4},
},
'SM' : {
'F': {"weight": 1},
},
'F' : {
'PV': {"weight": 4},
'VS': {"weight":4},
},
'VS' : {
'SR' : {"weight": 2},
},
'SR' : {
'PV': {"weight": 4},
'T': {"weight": 3},
}
}
adjtwo = {
1 : {
2: {'weight' : 2},
3: {'weight' : 4},
4: {'weight' : 1},
},
2:{
4: {'weight':3},
5: {'weight':10},
},
3:{
4: {'weight':2},
6:{'weight':5},
},
4:{
5: {'weight':2},
6:{'weight':8},
7:{'weight':4},
},
5:{
7:{'weight':6},
},
6:{
7:{'weight':1}
}
}
G = nx.from_dict_of_dicts(adjtwo)
#dijkstra(G, 1)
adjs = {
1: [2, 3, 4],
2: [1, 5],
3: [1, 6],
4: [1],
5: [2, 6],
6: [3, 5, 7],
7: [6]
}
Galt = nx.from_dict_of_lists(adjs)
distancias_no_ponderado(Galt, 1)