def dijkstra(self, g, s): initialize_single_source(g, s) S = [] Q = [v for v in g.get_vertices()] while len(Q): u = extract_min(Q) u.set_visitado() S.append(u) for v in u.get_vertices_adjacentes(): if v.get_visitado(): continue relax(u, v)
def bellman_ford(self, g, s): initialize_single_source(g, s) for u in g.get_vertices(): for u, v in g.get_arestas(): if v.get_visitado(): continue relax(u, v) for u, v in g.get_arestas(): if v.get_distancia() > u.get_distancia() + u.get_peso(v): return False return True
def BFSS(self, g, start): initialize_single_source(g, start) start = g.get_vertice(start) Q = list() Q.append(start) while len(Q): u = Q[0] u.set_visitado() Q.remove(u) for v in u.get_vertices_adjacentes(): if not v.get_visitado(): setBusca(v, u) Q.append(v)
def BFS_Proibido(self, g, start, final, p): initialize_single_source(g, start) start = g.get_vertice(start) l = list() Q = list() Q.append(start) # if len(start.get_vertices_adjacentes()) > 1: while len(Q): u = Q[0] u.set_visitado() Q.remove(u) for v in u.get_vertices_adjacentes(): # uri(v, u, final, l) if not v.get_visitado(): setBusca(v, u) Q.append(v) if v.get_id() == final: return v.get_distancia()
def BFS2(self, g, start, final, final2): initialize_single_source(g, start) start = g.get_vertice(start) l = list() Q = list() Q.append(start) # if len(start.get_vertices_adjacentes()) > 1: while len(Q): u = Q[0] u.set_visitado() Q.remove(u) for v in u.get_vertices_adjacentes(): # uri(v, u, final, l) if not v.get_visitado(): setBusca(v, u) Q.append(v) if v.get_id() == final or v.get_id() == final2: return True # else: # l.append(-1) return False
def BFS_Distancia(self, g, start, final): maior = sys.maxsize * -1 initialize_single_source(g, start) start = g.get_vertice(start) l = list() Q = list() Q.append(start) # if len(start.get_vertices_adjacentes()) > 1: while len(Q): u = Q[0] u.set_visitado() Q.remove(u) for v in u.get_vertices_adjacentes(): # uri(v, u, final, l) peso = u.get_peso(v) if peso > maior: maior = peso if not v.get_visitado(): setBusca(v, u) Q.append(v) if v.get_id() == final: return maior