Exemple #1
0
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(numelements=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")
        else:
            adjs = g.adjacentEdges(search['graph'],v)
            adjs_iter = it.newIterator (adjs) 
            while (it.hasNext(adjs_iter)):
                w = it.next (adjs_iter)
                relax(search, w)


        # obtener los enlaces adjacentes de v
        # Iterar sobre la lista de enlaces
        # Relajar (relax) cada enlace
    return search
    def test_kosaraju(self):

        graph = g.newGraph(12, self.comparenames, directed=True)
        idscc = m.newMap(12,
                         maptype='PROBING',
                         comparefunction=self.comparenames)

        pre = q.newQueue()
        post = q.newQueue()
        reversepost = s.newStack()
        marked = m.newMap(12, comparefunction=self.comparenames)

        grmarked = m.newMap(12,
                            maptype='PROBING',
                            comparefunction=self.comparenames)
        grpre = q.newQueue()
        grpost = q.newQueue()
        grreversepost = s.newStack()

        # se inicializa el grafo
        self.loadgraph(graph)
        self.assertEqual(g.numVertex(graph), 12)
        self.assertEqual(g.numEdges(graph), 14)

        # Se calcula el grafo reverso de G
        greverse = self.reverse(graph)
        self.assertEqual(g.numVertex(greverse), 12)
        self.assertEqual(g.numEdges(greverse), 14)

        # Se recorre el grafor reverso de G, utilizando DepthFirstOrder.
        self.dfo(greverse, grmarked, grpre, grpost, grreversepost)
        lst = self.lstReversePost(grreversepost)

        #lst contiene los vertices retornados por reversepost (G-reverso)
        #Se recorre el grafo en el orden dado por reverspost (G-reverso)
        iterlst = it.newIterator(lst)
        scc = 1
        while (it.hasNext(iterlst)):
            vert = it.next(iterlst)
            if not m.contains(marked, vert):
                self.sccCount(graph, vert, marked, idscc, scc)
                scc += 1

        self.assertTrue(self.stronglyConnected(idscc, 'Pedro', 'Maria'))
        self.assertTrue(self.stronglyConnected(idscc, 'Martin', 'Gloria'))
        self.assertTrue(self.stronglyConnected(idscc, 'Susana', 'Tere'))

        self.assertFalse(self.stronglyConnected(idscc, 'Pedro', 'Gloria'))
        self.assertFalse(self.stronglyConnected(idscc, 'Camila', 'Jose'))
        self.assertFalse(self.stronglyConnected(idscc, 'Gloria', 'Luz'))
Exemple #3
0
def countNodesEdges(catalog):
    """
    Retorna la cantidad de nodos y enlaces del grafo de revisiones
    """
    nodes = g.numVertex(catalog['Graph'])
    edges = g.numEdges(catalog['Graph'])

    return nodes, edges
Exemple #4
0
def countNodesEdges_directed(catalog):
    """
    Retorna la cantidad de nodos y enlaces del grafo de bibliotecas
    """
    nodes = g.numVertex(catalog['directed_Graph'])
    edges = g.numEdges(catalog['directed_Graph'])

    return nodes, edges
Exemple #5
0
def countNodesEdgesGraph(catalog):
    """
    Retorna la cantidad de nodos y enlaces del grafo de viajes
    """
    tripsGraph = catalog['tripsGraph']
    nodes = g.numVertex(tripsGraph)
    edges = g.numEdges(tripsGraph)

    return nodes, edges
Exemple #6
0
    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_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)
Exemple #8
0
    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)
Exemple #9
0
def countConnectedComponents (catalog):
    """
    Retorna la cantidad de componentes conectados del grafo de revisiones
    """
    counter=0
    list_nodes=g.vertices(catalog['delayGraph'])
    total= g.numVertex(catalog['delayGraph'])
    for i in range(1,lt.size(list_nodes)+1):
        node=lt.getElement(list_nodes,i)
        if not map.contains(catalog['visitedMap'],node):
            depth_first_search(catalog,node)
            counter+=1
        sub_total=map.size(catalog['visitedMap'])
        if sub_total==total:
            break
    return counter
Exemple #10
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 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 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 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)