-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
50 lines (42 loc) · 1.31 KB
/
utils.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
from src.Graph import Vertice
from os import listdir
def addVertice(G, v):
u = Vertice(v)
G.vertices[v] = u
G.n += 1
def DSF_visitado(v, u, visitados, arvore):
visitados[u.nome] = True
arvore.append((v.nome, u.nome))
for e in u.arestas:
vi = e['vertice']
if not visitados[vi.nome]:
DSF_visitado(u, vi, visitados, arvore)
def Eciclo(G):
keys = G.vertices.keys()
visitados = dict(zip(keys, [False]*G.n))
keys = list(keys)
vi = G.vertices[keys[0]]
lista = [vi]
visitados[vi.nome] = True
while len(lista) != 0:
vi = lista[0]
for e in vi.arestas:
u = e['vertice']
if not visitados[u.nome]:
visitados[u.nome] = True
lista.append(u)
else:
return True
lista.pop(0)
return False
def salvarGrafo(G, nome):
if f'{nome}.txt' in listdir('src/grafos'):
print(f'Ja existe um grafo com nome {nome}.')
return
with open(f'src/grafos/{nome}.txt', 'w') as f:
f.write(f"{nome} {G.n} {G.m}\n")
f.write('\n')
for key in G.vertices.keys():
v = G.vertices[key]
for e in v.arestas:
f.write(f"{v.nome} {e['vertice'].nome} {e['peso']}\n")