コード例 #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
コード例 #2
0
ファイル: test_graph.py プロジェクト: dlmanrique/Lab7_202010
    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)
コード例 #3
0
 def dfo(self, graph, marked, pre, post, reversepost):
     """
      Implementación del recorrido Depth First Order
     """
     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)
コード例 #4
0
ファイル: comp_conec.py プロジェクト: nCaicedo789/Lab7_202010
def countConnectedComponents (Graph, Mapa_de_marcar):
    """
    Retorna la cantidad de componentes conectados del grafo de revisiones
    """
    counter=0
    list_nodes=g.vertices(Graph)
    for i in range(1,lt.size(list_nodes)+1):
        node=lt.getElement(list_nodes,i)
        if not map.contains(Mapa_de_marcar, node):
            depth_first_search(Graph, Mapa_de_marcar, node)
            counter+=1
    return counter
コード例 #5
0
ファイル: model.py プロジェクト: susme2020/Lab7_202010
def countConnectedComponents(catalog):
    """
    Retorna la cantidad de componentes conectados de un grafo.
    """
    grafo = catalog["flightGraph"]
    vertices = g.vertices(grafo)
    componentes_conectados = 0
    iterator = it.newIterator(vertices)
    while it.hasNext(iterator):
        element = it.next(iterator)
        if g.degree(grafo, element) > 0:
            componentes_conectados += 1
    return componentes_conectados
コード例 #6
0
ファイル: test_graph.py プロジェクト: dlmanrique/Lab7_202010
    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)
コード例 #7
0
ファイル: model.py プロジェクト: JeOs714/Lab8_202010
def countConnectedComponents(catalog):
    """
    Retorna la cantidad de componentes conectados de un grafo.

    Aquí en necesario usar DFS para saber cuántos están conectados y luego interar sobre este DFS para contarlos¿?
    """
    grafo = catalog["flightGraph"]
    vertices = g.vertices(grafo)
    componentes_conectados = 0
    iterator = it.newIterator(vertices)
    while it.hasNext(iterator):
        element = it.next(iterator)
        if g.degree(grafo, element) > 0:
            componentes_conectados += 1
    return componentes_conectados
コード例 #8
0
ファイル: model.py プロジェクト: danielhmahecha/Reto4
def componentes_conectados(catalog):
    counter = 0
    grafo = catalog['non_directed_Graph']
    vertices = g.vertices(grafo)
    graph_iter = it.newIterator(vertices)
    m = map.newMap(capacity=55681,
                   maptype='CHAINING',
                   comparefunction=grafo['comparefunction'])
    while (it.hasNext(graph_iter)):
        n = it.next(graph_iter)
        visited_w = map.get(m, n)
        if visited_w == None:
            dfs.newDFS_2(grafo, n, m)
            counter += 1
    return counter
コード例 #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
コード例 #10
0
ファイル: model.py プロジェクト: dlmanrique/Lab7_202010
def componentes_conectados(catalog):
    counter = 0
    grafo = catalog['reviewGraph']
    vertices = g.vertices(grafo)
    graph_iter = it.newIterator(vertices)
    m = map.newMap(
        capacity=55681,
        maptype='CHAINING',
        comparefunction=grafo['comparefunction']
    )  # Se asume que hay 111353 nodos y asi se pone la capacidad de la tabla.
    while (it.hasNext(graph_iter)):
        n = it.next(graph_iter)
        visited_w = map.get(m, n)
        if visited_w == None:
            newDFS_2(grafo, n, m)
            counter += 1
    return counter
コード例 #11
0
ファイル: model.py プロジェクト: JeOs714/Lab9_202010
def ComponentesConectados (grafo):
    dicc_vertices = {}
    contador_componentes = 1
    vertices = g.vertices(grafo)
    if len(vertices) == 0:
        return 0
    else:
        iterator = it.newIterator(vertices)
        vertice = it.next(iterator)
        dicc_vertices = contar_conectados(grafo, dicc_vertices, vertice)
        while  it.hasNext(iterator):
            vertice_siguiente = it.next(iterator)
            esta_visitado = dicc_vertices.get(vertice_siguiente)
            if esta_visitado != "visited":
                dicc_vertices[vertice_siguiente] = "visited"
                contador_componentes += 1
                dicc_vertices = contar_conectados(grafo, dicc_vertices, vertice_siguiente)
        return contador_componentes
コード例 #12
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)
コード例 #13
0
    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
コード例 #14
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)