def newDijkstra(graph, s): """ Crea una busqueda Dijkstra para un digrafo y un vertice origen """ prime = nextPrime (g.numVertex(graph) * 2) search = {'graph':graph, 's':s, 'visitedMap':None, 'minpq':None} search['visitedMap'] = map.newMap(capacity=prime, maptype='PROBING', comparefunction=graph['comparefunction']) vertices = g.vertices (graph) itvertices = it.newIterator (vertices) while (it.hasNext (itvertices)): vert = it.next (itvertices) map.put (search['visitedMap'], vert, {'marked':False,'edgeTo':None,'distTo':math.inf}) map.put(search['visitedMap'], s, {'marked':True,'edgeTo':None,'distTo':0}) pq = minpq.newIndexMinPQ(g.numVertex(graph), comparenames) search['minpq'] = pq minpq.insert(search['minpq'], s, 0) while not minpq.isEmpty(search['minpq']): v = minpq.delMin(pq) if not g.containsVertex(graph,v): raise Exception("Vertex ["+v+"] is not in the graph") elif g.containsVertex(graph, v): adj = g.adjacents(graph,v) adj_it = it.newIterator(adj) while it.hasNext(adj_it): w = it.next(adj_it) edge = g.getEdge(graph, v, w) relax(search, edge) # obtener los enlaces adjacentes de v # Iterar sobre la lista de enlaces # Relajar (relax) cada enlace return search
def test_addEdges(self): graph = g.newGraph(7, self.comparenames) g.insertVertex(graph, 'Bogota') g.insertVertex(graph, 'Yopal') g.insertVertex(graph, 'Cali') g.insertVertex(graph, 'Medellin') g.insertVertex(graph, 'Pasto') g.insertVertex(graph, 'Barranquilla') g.insertVertex(graph, 'Manizales') g.addEdge(graph, 'Bogota', 'Yopal') g.addEdge(graph, 'Bogota', 'Medellin') g.addEdge(graph, 'Bogota', 'Pasto') g.addEdge(graph, 'Bogota', 'Cali') g.addEdge(graph, 'Yopal', 'Medellin') g.addEdge(graph, 'Medellin', 'Pasto') g.addEdge(graph, 'Cali', 'Pasto') g.addEdge(graph, 'Cali', 'Barranquilla') g.addEdge(graph, 'Barranquilla', 'Manizales') g.addEdge(graph, 'Pasto', 'Manizales') self.assertEqual(g.numEdges(graph), 10) self.assertEqual(g.numVertex(graph), 7) lst = g.vertices(graph) self.assertEqual(lt.size(lst), 7) lst = g.edges(graph) self.assertEqual(lt.size(lst), 10) degree = g.degree(graph, 'Bogota') self.assertEqual(degree, 4) edge = g.getEdge(graph, 'Bogota', 'Medellin') lst = g.adjacents(graph, 'Bogota') self.assertEqual(lt.size(lst), 4)