-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbusquedas.py
228 lines (210 loc) · 4.43 KB
/
busquedas.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import networkx as nx
def dfs(G, v):
print("Inicio DFS")
P = []
R = [v]
estado = {}
for w in G.nodes:
estado[w] = 0
estado[v] = 1
P.append(v)
print("====")
print("P", P)
print("Vértice añadido", v)
print("Vértice eliminado", "-")
print("R", R)
print("====")
while not not P:
w = P[-1]
neighbours = sorted(list(G.neighbors(w)))
u = neighbours[0]
for v in neighbours:
if estado[v] == 0:
u = v
break
if estado[u] == 0:
P.append(u)
estado[u] = 1
R.append(u)
print("P", P)
print("Vértice añadido", u)
print("Vértice eliminado", "-")
else:
e = P.pop()
print("P", P)
print("Vértice añadido", "-")
print("Vértice eliminado", e)
print("R", R)
print("====")
print("Fin DFS")
return R
def dfs_aristas(G, v):
print("Inicio DFS aristas")
P = []
S = []
estado = {}
for w in G.nodes:
estado[w] = 0
estado[v] = 1
P.append(v)
print("====")
print("P", P)
print("Arista añadida", "-")
print("S", S)
print("====")
while not not P:
w = P[-1]
neighbours = sorted(list(G.neighbors(w)))
u = neighbours[0]
for v in neighbours:
if estado[v] == 0:
u = v
break
if estado[u] == 0:
P.append(u)
estado[u] = 1
S.append([w, u])
print("P", P)
print("Arista añadida", [w, u])
else:
e = P.pop()
print("P", P)
print("Arista añadida", "-")
print("S", S)
print("====")
print("Fin DFS aristas")
return S
def bfs(G, v):
print("Inicio BFS")
Q = []
R = [v]
estado = {}
for w in G.nodes:
estado[w] = 0
estado[v] = 1
Q.append(v)
print("====")
print("Q", Q)
print("Vértice añadido", v)
print("Vértice eliminado", "-")
print("R", R)
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
R.append(u)
print("Q", Q)
print("Vértice añadido", u)
print("Vértice eliminado", "-")
print("R", R)
e = Q[0]
Q = Q[1:]
print("Q", Q)
print("Vértice añadido", "-")
print("Vértice eliminado", e)
print("R", R)
print("====")
print("Fin BFS")
return R
def bfs_aristas(G, v):
print("Inicio BFS aristas")
Q = []
S = []
estado = {}
for w in G.nodes:
estado[w] = 0
estado[v] = 1
Q.append(v)
print("====")
print("Q", Q)
print("Arista añadida", "-")
print("S", S)
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
S.append([w, u])
print("Q", Q)
print("Arista añadida", [w, u])
print("S", S)
Q = Q[1:]
print("Q", Q)
print("Arista añadida", "-")
print("S", S)
print("Fin BFS aristas")
return S
if __name__ == '__main__':
dod = {
'A': {
'B': {"weight": 150},
'C': {"weight": 320},
'E': {"weight": 100},
},
'B': {
'C': {"weight": 150},
'D': {"weight": 175},
},
'C': {
'D': {"weight": 49},
'E': {"weight": 130},
},
}
adjs = {
1: [2, 3, 4],
2: [1, 5],
3: [1, 6],
4: [1],
5: [2, 6],
6: [3, 5, 7],
7: [6]
}
G = nx.from_dict_of_dicts(dod)
#dfs(G, "A")
#bfs(G, "A")
dfs_aristas(G, "A")
bfs_aristas(G, "A")
Galt = nx.from_dict_of_lists(adjs)
#dfs(Galt, 1)
#bfs(Galt, 1)
dfs_aristas(Galt, 1)
bfs_aristas(Galt, 1)
# EXAMEN ANTERIOR
g = {
'A': ['B', 'D'],
'B': ['A', 'C', 'D'],
'C': ['B', 'F', 'E'],
'D': ['A', 'B'],
'E': ['C', 'F'],
'F': ['C', 'E']
}
G = nx.from_dict_of_lists(g)
#bfs(G, 'C')
#dfs(G, 'C')
g = {
'A': {
'B': {"weight": 5},
'D': {"weight": 4}
},
'B': {
'C': {"weight": 2},
'D': {"weight": 10},
},
'C': {
'E': {"weight": 3},
'F': {"weight": 10}
},
'F': {
'E': {"weight": 6}
},
}
G = nx.from_dict_of_dicts(g)
from distances import floyd
floyd(G)