def Dijkstra(graph, source):
    """
    Implementa el algoritmo de Dijkstra
    Args:
        graph: El grafo de busqueda
        source: El vertice de inicio

    Returns:
        Un nuevo grafo vacío
    Raises:
        Exception
    """
    try:
        search = initSearch(graph, source)
        while not iminpq.isEmpty(search['iminpq']):
            v = iminpq.delMin(search['iminpq'])
            edges = g.adjacentEdges(graph, v)
            if edges is not None:
                edgesiter = it.newIterator(edges)
                while (it.hasNext(edgesiter)):
                    edge = it.next(edgesiter)
                    relax(search, edge)
        return search
    except Exception as exp:
        error.reraise(exp, 'dks:dijkstra')
示例#2
0
def dfs(graph, search, v):
    """
    DFS
    Args:
        search: La estructura de busqueda
        v: Vertice desde donde se relajan los pesos
    Returns:
        El grafo
    Raises:
        Exception
    """
    try:
        map.put(search['marked'], v, True)
        map.put(search['onStack'], v, True)
        edges = g.adjacentEdges(graph, v)
        for edge in lt.iterator(edges):
            w = e.other(edge, v)
            if (not st.isEmpty(search['cycle'])):
                return search
            elif ((not map.get(search['marked'], w)['value'])):
                map.put(search['edgeTo'], w, edge)
                dfs(graph, search, w)
            elif (map.get(search['onStack'], w)['value']):
                f = edge
                while (e.either(f) != w):
                    st.push(search['cycle'], f)
                    f = map.get(search['edgeTo'], e.either(f))['value']
                st.push(search['cycle'], f)
                return search
        map.put(search['onStack'], v, False)
    except Exception as exp:
        error.reraise(exp, 'cycle:dfs')
示例#3
0
def scan(graph, search, vertex):
    """
    Args:
        search: La estructura de busqueda
        vertex: El vertice destino
    Returns:
        El costo total para llegar de source a
        vertex. Infinito si no existe camino
    Raises:
        Exception
    """
    try:
        map.put(search['marked'], vertex, True)
        edges = g.adjacentEdges(graph, vertex)
        for edge in lt.iterator(edges):
            w = e.other(edge, vertex)
            if (not map.get(search['marked'], w)['value']):
                if (e.weight(edge) < map.get(search['distTo'], w)['value']):
                    map.put(search['distTo'], w, e.weight(edge))
                    map.put(search['edgeTo'], w, edge)
                    if (pq.contains(search['pq'], w)):
                        pq.decreaseKey(search['pq'], w,
                                       map.get(search['distTo'], w)['value'])
                    else:
                        pq.insert(search['pq'], w,
                                  map.get(search['distTo'], w)['value'])
        return search
    except Exception as exp:
        error.reraise(exp, 'bellman:disto')
示例#4
0
def prueba(catalog):
    print(
        gr.adjacentEdges(
            catalog['connections'],
            formatVertex('Barranquilla',
                         'America Movil Submarine Cable System-1 (AMX-1)')))
    print(me.getValue(mp.get(catalog['landing_points'], 'Bogota'))['cables'])
示例#5
0
def relax(graph, search, v):
    """
    Relaja el peso de los arcos del grafo
    Args:
        search: La estructura de busqueda
        v: Vertice desde donde se relajan los pesos
    Returns:
        El grafo con los arcos relajados
    Raises:
        Exception
    """
    try:
        edges = g.adjacentEdges(graph, v)
        if edges is not None:
            for edge in lt.iterator(edges):
                v = e.either(edge)
                w = e.other(edge)
                distv = map.get(search['distTo'], v)['value']
                distw = map.get(search['distTo'], w)['value']
                distweight = distv + e.weight(edge)
                if (distw > distweight):
                    map.put(search['distTo'], w, distweight)
                    map.put(search['edgeTo'], w, edge)
                    if (not map.get(search['onQ'], w)['value']):
                        q.enqueue(search['qvertex'], w)
                        map.put(search['onQ'], w, True)
                cost = search['cost']
                if ((cost % g.numVertices(graph)) == 0):
                    findneg = findNegativeCycle(graph, search)
                    if (hasNegativecycle(findneg)):
                        return
                search['cost'] = cost + 1
        return search
    except Exception as exp:
        error.reraise(exp, 'bellman:relax')
 def req_5(self, lp):
     id = self.name_to_id(lp)
     affected_countries = mp.newMap(numelements=10)
     self.add_affected_country(affected_countries, id, 0)
     edges_it = ll_it.newIterator(gp.adjacentEdges(self.connections_map,
                                                   id))
     while ll_it.hasNext(edges_it):
         edge = ll_it.next(edges_it)
         self.add_affected_country(affected_countries, edge['vertexB'],
                                   edge['weight'])
     respuesta = mp.valueSet(affected_countries)
     respuesta = self.linked_to_array(respuesta)
     mergesort.sort(respuesta, self.req_5_cmpfunc)
     graphing = graphing_helper(self)
     graphing.graph_edges_lp(id)
     graphing.graph_affected_countries(respuesta)
     return respuesta
示例#7
0
def crearSubGrafo(catalog, lista_vertices, tamaño):
    # Con los vértices del SCC más grande, se crea un subgrafo, obteniendo las
    # conexiones del grafo original
    catalog_sub = {"connections": None}
    catalog_sub["connections"] = gr.newGraph(size=2017)

    i = 1
    while i < tamaño:
        vertice = lt.getElement(lista_vertices, i)
        lista_arcos_adyacentes = gr.adjacentEdges(catalog["connections"],
                                                  vertice)
        for arco in lt.iterator(lista_arcos_adyacentes):
            #print(arco)
            vertice1 = arco["vertexA"]
            vertice2 = arco["vertexB"]
            distancia = arco["weight"]
            addDistance(catalog_sub, vertice1, vertice2, distancia)
        i += 1
    subgrafo = catalog_sub["connections"]
    return tamaño, subgrafo
 def graph_edges_lp(self, lp):
     edges_it = ll_it.newIterator(
         gp.adjacentEdges(self.data.connections_map, lp))
     while ll_it.hasNext(edges_it):
         edge = ll_it.next(edges_it)
         self.graph_edge(edge)