Created
June 17, 2017 13:37
-
-
Save Florencia-97/0bb4f1b0af8361cf54fcb414b9b84ccb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#GRAFO | |
class Grafo(object): | |
def __init__(self): | |
self.grafo={} | |
self.cant_vertices=0 | |
self.cant_aristas=0 | |
def agregar_vertice(self, elemento): | |
self.grafo[elemento]={} | |
self.cant_vertices++ | |
def eliminar_vertice(self,elemento): | |
result=self.grafo.pop(elemento, None) | |
if result==None: | |
return false | |
self.cant_vertices-- | |
return true | |
def agregar_arista(self, elemento1, elemento2): | |
self.grafo[origen][destino] | |
self.grafo[destino][origen] | |
self.cant_aristas+=1 | |
def quitar_arista(self,elemento,arista): | |
del self.grafo[elemento][arista] | |
del self.grafo[arista][elemento] | |
self.cant_aristas-=1 | |
def cant_aristas(self): | |
return self.cant_aristas | |
def son_adyacentes(self,elem1,elem2): | |
return (elem1 in self.grafo[elem2]) | |
def existe_vertice(self,elemento): | |
valor=self.grafo.get(elemento) | |
if valor==None: | |
return False | |
return True | |
def cant_vertices(self): | |
return self.cant_vertices | |
def obtener_adyacentes(self,elemento): | |
return self.grafo[elemento] | |
def obtener_vertices(self): | |
visitados = {} #diccionario | |
for v in self.grafo: | |
if v not in visitados: | |
bfs_visitar(self.grafo, v, visitados) | |
return visitados |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment