Skip to content

Commit 726b02b

Browse files
Funcao ler_dot() implementada
1 parent 95130c2 commit 726b02b

File tree

1 file changed

+74
-3
lines changed

1 file changed

+74
-3
lines changed

trabalho3.py

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# TRABALHO 3 FUNDAMENTOS DE REDES 1
55

66
from igraph import *
7-
import os
7+
import os, re
88

99
# variavel global para ir armazenando decisoes do usuario
1010
n = 0
@@ -39,14 +39,76 @@ def menu(primeira_vez=True):
3939
while (n < 1 or n > 5):
4040
n = input('Escolha um numero valido (entre 1 e 5):\n')
4141

42+
def ler_dot():
43+
lista = []
44+
vertices = []
45+
vertices_aux = []
46+
n_vertices = 0
47+
48+
# abrir arquivo do usuario e ler cada linha
49+
x = raw_input('Por favor digite o nome do arquivo: (eh preciso ter .dot no final e o PATH adequado)\n')
50+
file = open(x, 'r')
51+
linhas = file.readlines()
52+
pattern = '\w+\s*'
53+
54+
# lista = variavel que guarda par de vertices que possuem aresta
55+
# comeca a ler da linha 1 ao inves da 0, pois a linha zero contem o nome do grafo
56+
for i in range(1, len(linhas)):
57+
lista.append(re.findall(pattern, linhas[i]))
58+
59+
# lista = lista de listas. vertices = lista
60+
for sublista in lista:
61+
for item in sublista:
62+
vertices.append(item)
63+
64+
# caso haja qualquer item vazio, eliminar da lista
65+
# repetir isso ate que nao haja mais item nenhum vazio
66+
while True:
67+
try:
68+
vertices.remove('')
69+
except ValueError:
70+
break
71+
72+
# loop para eliminar duplicatas na lista "vertices"
73+
for i in vertices:
74+
if i not in vertices_aux:
75+
vertices_aux.append(i)
76+
vertices = vertices_aux
77+
78+
# finalmente, vertices se tornou uma lista "limpa"
79+
n_vertices = len(vertices)
80+
g.add_vertices(n_vertices)
81+
g.vs["name"] = vertices
82+
83+
# agora iremos adicionar as arestas no grafo
84+
tupla = ()
85+
arestas = []
86+
for i in range(len(lista)):
87+
if (len(sublista) == 2):
88+
for sublista in lista[i]:
89+
tupla = (sublista[0], sublista[1])
90+
arestas.append(tupla)
91+
g.add_edges(arestas)
92+
g.es["weight"] = [0]*len(vertices)
93+
94+
95+
file.close()
96+
97+
4298
def criar_grafo():
4399
print 'Agora iremos criar seu grafo de testes\n'
44100
y = raw_input('Voce gostaria de carregar um grafo a partir de um arquivo .DOT? (s/n)\n')
45101
if (y == 's'):
46-
print 'kk eae men'
102+
ler_dot()
47103
# fazer algo aqui depois
48104
else:
49-
x = input('Quantos vertices tem seu grafo?\n')
105+
while True:
106+
try:
107+
x = input('Quantos vertices tem seu grafo?\n')
108+
except SyntaxError:
109+
print 'Voce somente apertou ENTER. Por favor, digite um numero valido\n'
110+
continue
111+
break
50112
g.add_vertices(x)
51113
lista = [] # lista auxiliar
52114
for i in range(g.vcount()):
@@ -91,3 +153,12 @@ def main():
91153

92154
if __name__ == "__main__":
93155
main()
156+
157+
158+
159+
160+
161+
162+
163+
164+

0 commit comments

Comments
 (0)