forked from Arayve/TP3-GRAFO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrafo.py
74 lines (72 loc) · 2.25 KB
/
grafo.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
from random import choice
class Grafo:
def __init__(self,grafo_dirigido):
self.dic_vertice={}
self.dirigido=grafo_dirigido
def agregar_vertice(self,vertice):
if vertice in self.dic_vertice:
return False
self.dic_vertice[vertice]={}
return True
def borrar_vertice(self,vertice):
if not vertice in self.dic_vertice:
return False
self.dic_vertice.pop(vertice)
for dic_arista in self.dic_vertice.values():
if vertice in dic_arista:
dic_arista.pop(vertice)
return True
def adyacentes_vertice(self,vertice):
lista=[]
if not vertice in self.dic_vertice:
return None
dic_arista=self.dic_vertice[vertice]
for w in dic_arista.keys():
lista.append(w)
return lista
def agregar_arista(self,vertice_a,vertice_b,peso):
if not (vertice_a in self.dic_vertice and vertice_b in self.dic_vertice):
return False
dic_arista_a=self.dic_vertice[vertice_a]
if vertice_b in dic_arista_a:
return False
dic_arista_a[vertice_b]=peso
if not self.dirigido:
dic_arista_b=self.dic_vertice[vertice_b]
dic_arista_b[vertice_a]=peso
return True
def borrar_arista(self,vertice_a,vertice_b):
if not (vertice_a in self.dic_vertice and vertice_b in self.dic_vertice):
return None
dic_arista_a = self.dic_vertice[vertice_a]
if not vertice_b in dic_arista_a:
return None
peso = dic_arista_a.pop(vertice_b)
if not self.dirigido:
dic_arista_b=self.dic_vertice[vertice_b]
dic_arista_b.pop(vertice_a)
return peso
def pertenece_vertice(self,vertice):
return vertice in self.dic_vertice
def peso_arista(self,vertice_a,vertice_b):
if not (vertice_a in self.dic_vertice and vertice_b in self.dic_vertice):
return None
dic_arista_a=self.dic_vertice[vertice_a]
if not vertice_b in dic_arista_a:
return None
return dic_arista_a[vertice_b]
def obtener_vertice_aleatorio(self):
lista=[]
for vertice in self.dic_vertice.keys():
lista.append(vertice)#QUISE hacer un random al diccionario pero aveces daba error
return choice(lista)
def obtener_todos_vertices(self):
"""Devuelve tdos los vertice en una lista"""
lista=[]
for vertice in self.dic_vertice.keys():
lista.append(vertice)
return lista
def cantidad_vertice(self):
return len(self.dic_vertice)
def __str__(self):
return "{}".format(self.dic_vertice)