def addVertex(catalog, row): ''' Función que añade los vértices al grafo de viajes si no existen ''' if not g.containsVertex(catalog['tripsGraph'], row['src']): g.insertVertex(catalog['tripsGraph'], row['src']) if not g.containsVertex(catalog['tripsGraph'], row['dst']): g.insertVertex(catalog['tripsGraph'], row['dst'])
def addReviewNode(catalog, row): """ Adiciona un nodo para almacenar un libro o usuario """ if not g.containsVertex(catalog['reviewGraph'], row['book_id']): g.insertVertex(catalog['reviewGraph'], row['book_id']) if not g.containsVertex(catalog['reviewGraph'], row['user_id']): g.insertVertex(catalog['reviewGraph'], row['user_id'])
def addstationNode(catalog, row): """ Adiciona un nodo para almacenar una biblioteca """ if not g.containsVertex(catalog['grafo'], row['src']): g.insertVertex(catalog['grafo'], row['src']) if not g.containsVertex(catalog['grafo'], row['dst']): g.insertVertex(catalog['grafo'], row['dst'])
def addLibraryNode (catalog, row): """ Adiciona un nodo para almacenar una biblioteca """ if not g.containsVertex(catalog['librariesGraph'], row['ID_src']): g.insertVertex (catalog['librariesGraph'], row['ID_src']) if not g.containsVertex(catalog['librariesGraph'], row['ID_dst']): g.insertVertex (catalog['librariesGraph'], row['ID_dst'])
def addReviewNode_directed(catalog, row): """ Adiciona un nodo para almacenar un libro o usuario """ if not g.containsVertex(catalog['directed_Graph'], row['SOURCE']): g.insertVertex(catalog['directed_Graph'], row['SOURCE']) if not g.containsVertex(catalog['directed_Graph'], row['DEST']): g.insertVertex(catalog['directed_Graph'], row['DEST'])
def test_connectedcomponents1(self): graph = g.newGraph(1, self.comparenames) g.insertVertex(graph, 'Laura') cc = dfs.countCC(graph) self.assertEqual(cc, 1)
def addDirectedNode (catalog, row): """ Adiciona un nodo para almacenar un libro o usuario """ source = row['src'] dest = row['dst'] if not g.containsVertex(catalog['GraphDirected'], source): g.insertVertex (catalog['GraphDirected'], source) if not g.containsVertex(catalog['GraphDirected'], dest): g.insertVertex (catalog['GraphDirected'], dest)
def addLibraryNode (catalog, row): """ Adiciona un nodo para almacenar una biblioteca """ if not g.containsVertex(catalog['librariesGraph'], row['SOURCE']): g.insertVertex (catalog['librariesGraph'], row['SOURCE']) if not g.containsVertex(catalog['librariesGraph'], row['DEST']): g.insertVertex (catalog['librariesGraph'], row['DEST']) if not g.containsVertex(catalog['delayGraph'], row['SOURCE']): g.insertVertex (catalog['delayGraph'], row['SOURCE']) if not g.containsVertex(catalog['delayGraph'], row['DEST']): g.insertVertex (catalog['delayGraph'], row['DEST'])
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_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 test_connectedcomponents(self): graph = g.newGraph(7, self.comparenames) g.insertVertex(graph, 'Laura') g.insertVertex(graph, 'Eduardo') g.insertVertex(graph, 'Andres') g.insertVertex(graph, 'Camila') g.insertVertex(graph, 'Antonio') g.insertVertex(graph, 'Luis') g.insertVertex(graph, 'Lina') g.addEdge(graph, 'Laura', 'Luis') g.addEdge(graph, 'Eduardo', 'Laura') g.addEdge(graph, 'Antonio', 'Laura') g.addEdge(graph, 'Camila', 'Lina') cc = dfs.countCC(graph) self.assertEqual(cc, 3)
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 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')
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)
def newGraph(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') return graph
def test_bfs(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', 'Barranquilla') g.addEdge(graph, 'Barranquilla', 'Medellin') g.addEdge(graph, 'Bogota', 'Cali') g.addEdge(graph, 'Cali', 'Medellin') search = bfs.newBFS(graph, 'Bogota') response = '' path = bfs.pathTo(search, 'Medellin') 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 = bfs.pathTo(search, 'Pasto') self.assertIsNone(path2)
def test_degrees(self): graph = g.newGraph(7, self.comparenames, directed=True) 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, 'Cali', 'Bogota') g.addEdge(graph, 'Yopal', 'Medellin') g.addEdge(graph, 'Medellin', 'Pasto') g.addEdge(graph, 'Pasto', 'Bogota') 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), 12) self.assertEqual(g.numVertex(graph), 7) degree = g.indegree(graph, 'Bogota') self.assertEqual(degree, 2) degree = g.indegree(graph, 'Barranquilla') self.assertEqual(degree, 1) degree = g.outdegree(graph, 'Barranquilla') self.assertEqual(degree, 1) degree = g.outdegree(graph, 'Bogota') self.assertEqual(degree, 4) degree = g.outdegree(graph, 'Manizales') self.assertEqual(degree, 0)
def loadgraph(self, graph): """ Crea el grafo con la informacion de prueba """ g.insertVertex(graph, 'Pedro') g.insertVertex(graph, 'Maria') g.insertVertex(graph, 'Carol') g.insertVertex(graph, 'Laura') g.insertVertex(graph, 'Felipe') g.insertVertex(graph, 'Jose') g.insertVertex(graph, 'Martin') g.insertVertex(graph, 'Camila') g.insertVertex(graph, 'Gloria') g.insertVertex(graph, 'Luz') g.insertVertex(graph, 'Tere') g.insertVertex(graph, 'Susana') g.addEdge(graph, 'Pedro', 'Jose') g.addEdge(graph, 'Jose', 'Felipe') g.addEdge(graph, 'Felipe', 'Laura') g.addEdge(graph, 'Laura', 'Carol') g.addEdge(graph, 'Carol', 'Maria') g.addEdge(graph, 'Maria', 'Pedro') g.addEdge(graph, 'Camila', 'Jose') g.addEdge(graph, 'Camila', 'Martin') g.addEdge(graph, 'Martin', 'Gloria') g.addEdge(graph, 'Gloria', 'Camila') g.addEdge(graph, 'Gloria', 'Luz') g.addEdge(graph, 'Luz', 'Tere') g.addEdge(graph, 'Tere', 'Susana') g.addEdge(graph, 'Susana', 'Luz')
def test_dijkstra(self): graph = g.newGraph(7, self.comparenames, directed=True) 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', 4) g.addEdge(graph, 'Bogota', 'Medellin', 6) g.addEdge(graph, 'Bogota', 'Pasto', 10) g.addEdge(graph, 'Bogota', 'Cali', 8) g.addEdge(graph, 'Cali', 'Bogota', 7) g.addEdge(graph, 'Yopal', 'Medellin', 9) g.addEdge(graph, 'Medellin', 'Pasto', 5) g.addEdge(graph, 'Pasto', 'Bogota', 10) g.addEdge(graph, 'Cali', 'Pasto', 3) g.addEdge(graph, 'Cali', 'Barranquilla', 10) g.addEdge(graph, 'Barranquilla', 'Manizales', 9) g.addEdge(graph, 'Pasto', 'Manizales', 6) search1 = dijkstra.newDijkstra(graph, 'Bogota') has1 = dijkstra.hasPathTo(search1, 'Manizales') self.assertEqual(has1, True) path1 = dijkstra.pathTo(search1, 'Manizales') self.assertEqual(lt.size(path1), 2) search2 = dijkstra.newDijkstra(graph, 'Manizales') has2 = dijkstra.hasPathTo(search2, 'Yopal') self.assertEqual(has2, False)
def test_insertVertex2(self): graph = g.newGraph(7, self.comparenames, directed=True) 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)
def test_dijkstra(self): graph = g.newGraph(7, self.comparenames, directed=True) 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', 2.5) g.addEdge(graph, 'Bogota', 'Medellin', 0.1) g.addEdge(graph, 'Bogota', 'Pasto', 16) g.addEdge(graph, 'Bogota', 'Cali', 3.2) g.addEdge(graph, 'Cali', 'Bogota', 4.8) g.addEdge(graph, 'Yopal', 'Medellin', 9.1) g.addEdge(graph, 'Medellin', 'Pasto', 7.1) g.addEdge(graph, 'Pasto', 'Bogota', 9.3) g.addEdge(graph, 'Cali', 'Pasto', 4.7) g.addEdge(graph, 'Cali', 'Barranquilla', 1.2) g.addEdge(graph, 'Barranquilla', 'Pasto', 0.0) dis = dk.newDijkstra(graph, 'Bogota') path1 = dk.pathTo(dis, 'Pasto') path2 = dk.pathTo(dis, 'Medellin') path3 = dk.pathTo(dis, 'Manizales') totalDist1 = 0 while not stk.isEmpty(path1): step = stk.pop(path1) totalDist1 += step['weight'] totalDist2 = 0 while not stk.isEmpty(path2): step = stk.pop(path2) totalDist2 += step['weight'] self.assertEqual(totalDist1, 4.4) self.assertEqual(totalDist2, 0.1) self.assertIsNone(path3)
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 addFlightNode(catalog, row): """ Adiciona un nodo para almacenar un vuelo. """ if not g.containsVertex(catalog['flightGraph'], row['VERTEX']): g.insertVertex(catalog['flightGraph'], row['VERTEX'])
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, True) 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) g.addEdge(graph, 'Yopal', 'Medellin', 1.0) g.addEdge(graph, 'Medellin', 'Pasto', 1.0)
def test_topological(self): graph = g.newGraph(10, self.comparenames, directed=True) pre = q.newQueue() post = q.newQueue() reversepost = s.newStack() marked = m.newMap(10, comparefunction=self.comparenames) g.insertVertex(graph, 'Calculo1') g.insertVertex(graph, 'Calculo2') g.insertVertex(graph, 'Diseno1') g.insertVertex(graph, 'Diseno2') g.insertVertex(graph, 'Electiva') g.insertVertex(graph, 'Fisica1') g.insertVertex(graph, 'Ingles') g.insertVertex(graph, 'IP1') g.insertVertex(graph, 'IP2') g.insertVertex(graph, 'ProyectoFinal') g.addEdge(graph, 'Calculo1', 'Calculo2') g.addEdge(graph, 'Calculo2', 'IP2') g.addEdge(graph, 'Calculo2', 'Fisica1') g.addEdge(graph, 'Diseno1', 'Diseno2') g.addEdge(graph, 'Diseno2', 'ProyectoFinal') g.addEdge(graph, 'Electiva', 'ProyectoFinal') g.addEdge(graph, 'Fisica1', 'Diseno2') g.addEdge(graph, 'Ingles', 'ProyectoFinal') g.addEdge(graph, 'IP1', 'Diseno1') g.addEdge(graph, 'IP1', 'IP2') self.assertEqual(g.numEdges(graph), 10) self.assertEqual(g.numVertex(graph), 10) #DFO lstvert = g.vertices(graph) vertiterator = it.newIterator(lstvert) while it.hasNext(vertiterator): vert = it.next(vertiterator) if not (m.contains(marked, vert)): self.dfs(graph, vert, marked, pre, post, reversepost) self.printTopological(reversepost)
def addFlightNode_user(catalog, vertice): if not g.containsVertex(catalog['flightGraph'], vertice): g.insertVertex(catalog['flightGraph'], vertice) return False else: return True
def test_adjacents(self): graph = g.newGraph(7, self.comparenames, directed=True) 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, 'Cali', 'Bogota') g.addEdge(graph, 'Yopal', 'Medellin') g.addEdge(graph, 'Medellin', 'Pasto') g.addEdge(graph, 'Pasto', 'Bogota') 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), 12) self.assertEqual(g.numVertex(graph), 7) lst = g.adjacents(graph, 'Bogota') self.assertEqual(lt.size(lst), 4) self.assertTrue(lt.isPresent(lst, 'Cali', self.comparelst)) self.assertTrue(lt.isPresent(lst, 'Yopal', self.comparelst)) self.assertTrue(lt.isPresent(lst, 'Pasto', self.comparelst)) self.assertTrue(lt.isPresent(lst, 'Medellin', self.comparelst)) self.assertFalse(lt.isPresent(lst, 'Barranquilla', self.comparelst)) lst = g.adjacents(graph, 'Manizales') self.assertEqual(lt.size(lst), 0)
def addNode(catalog, row): """ Adiciona un nodo para almacenar un libro o usuario """ if not g.containsVertex(catalog['Graph'], row['VERTEX']): g.insertVertex(catalog['Graph'], row['VERTEX'])
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)