def test_dfs(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, 'Yopal', 'Cali') search = dfs.newDFS(graph, 'Bogota') response = '' path = dfs.pathTo(search, 'Cali') pathsize = 0 if path: iteraPath = it.newIterator(path) while it.hasNext(iteraPath): Vertex = it.next(iteraPath) response += Vertex + '\n' pathsize += 1 print(response) self.assertEqual(pathsize, 3) path2 = dfs.pathTo(search, 'Pasto') self.assertIsNone(path2)
def newCatalog(): """ Inicializa el catálogo y retorna el catalogo inicializado. """ libgraph = g.newGraph(7235, compareByKey, directed=True) catalog = {'librariesGraph': libgraph} return catalog
def test_connectedcomponents1(self): graph = g.newGraph(1, self.comparenames) g.insertVertex(graph, 'Laura') cc = dfs.countCC(graph) self.assertEqual(cc, 1)
def test_numVertex(self): graph = g.newGraph(7,self.comparenames) g.insertVertex (graph, 'Bogota') g.insertVertex (graph, 'Yopal') g.insertVertex (graph, 'Cali') n=g.numVertex(graph) lst = g.vertices (graph) self.assertEqual (lt.size (lst), n)
def test_containsVertex(self): graph = g.newGraph(7,self.comparenames) g.insertVertex (graph, 'Bogota') g.insertVertex (graph, 'Yopal') g.insertVertex (graph, 'Cali') v1 = g.containsVertex(graph,'Cali') self.assertEqual (True, v1) v2=g.containsVertex(graph,'Tunja') self.assertEqual(False,v2)
def newCatalog(): """ Inicializa el catálogo y retorna el catalogo inicializado. """ capacityMap = map.newMap(maptype='PROBING',comparefunction=compareByKey) stationMap = map.newMap(97,maptype='CHAINING',comparefunction=compareByKey) tree = oms.newMap('RBT') gdir = g.newGraph(97, compareByKey, directed=True) tempHash = map.newMap(179, maptype='CHAINING', comparefunction=compareByKey) tempList = lt.newList('ARRAY_LIST') catalog = {'capacityMap':capacityMap, 'stationMap': stationMap,"dateTree": tree, "GraphDirected":gdir, 'temperatureHash': tempHash, 'tempList':tempList} return catalog
def test_insertVertex (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') self.assertEqual (g.numVertex(graph), 7) lst = g.vertices (graph) self.assertEqual (lt.size (lst), 7)
def reverse(self, graph): """ Retornar el reverso del grafo graph """ greverse = g.newGraph(12, self.comparenames, directed=True) lstvert = g.vertices(graph) itervert = it.newIterator(lstvert) while it.hasNext(itervert): vert = it.next(itervert) g.insertVertex(greverse, vert) itervert = it.newIterator(lstvert) while it.hasNext(itervert): vert = it.next(itervert) lstadj = g.adjacents(graph, vert) iteradj = it.newIterator(lstadj) while it.hasNext(iteradj): adj = it.next(iteradj) g.addEdge(greverse, adj, vert) return greverse
def test_removeVertex(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.removeVertex(graph,'Bogota') g.removeVertex(graph,'Cali') n=g.containsVertex(graph, 'Bogota') o=g.containsVertex(graph, 'Cali') self.assertEqual(False,n) self.assertEqual(False,o)
def test_adjacents(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') lst=g.adjacents(graph,'Bogota') self.assertEqual(lt.size(lst),4)
def resultado(pila): if pila == 'No hay camino' or pila == 'No existen los vértices': print(pila) else: print("El camino de menor costo entre los vertices es:") totalDist = 0 while not stk.isEmpty(pila): step = stk.pop(pila) totalDist += step['weight'] print(step['vertexA'] + "-->" + step['vertexB'] + " costo: " + str(step['weight'])) print("Total: " + str(totalDist)) if __name__ == "__main__": graph = g.newGraph(7, 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.insertVertex(graph, 'Cucuta') g.insertVertex(graph, 'Bucaramanga') g.addEdge(graph, 'Bogota', 'Yopal', 1.0) g.addEdge(graph, 'Bogota', 'Medellin', 1.0) g.addEdge(graph, 'Bogota', 'Pasto', 1.0) g.addEdge(graph, 'Bogota', 'Cali', 1.0)
def test_connectedcomponents0(self): graph = g.newGraph(1, self.comparenames) cc = dfs.countCC(graph) self.assertEqual(cc, 0)