Ejemplo n.º 1
0
def resistencia(graph, tiempo, estacion_inicio):
    rutas = lt.newList(comparar_data)
    borrar = lt.newList(comparar_data)
    estaciones = adj.adjacents(graph['grafo'], estacion_inicio)
    ite = it.newIterator(estaciones)
    no_repetir = m.newMap(numelements=17,
                          prime=109345121,
                          maptype='CHAINING',
                          loadfactor=0.5,
                          comparefunction=comparar_data)
    m.put(no_repetir, estacion_inicio, 1)
    while (it.hasNext(ite)):
        est = it.next(ite)
        edg = adj.getEdge(graph['grafo'], estacion_inicio, est)
        duracion = edg["weight"]
        if duracion < (tiempo * 60) and m.contains(no_repetir, est) == False:
            ruta = {}
            ruta["Estacion_final"] = est
            ruta["Estacion_inicio"] = estacion_inicio
            ruta["tiempo"] = duracion
            lt.addLast(rutas, ruta)
            lt.addLast(borrar, ruta)
            m.put(no_repetir, est, 1)
    ite2 = it.newIterator(borrar)
    while (it.hasNext(ite2)):
        est2 = it.next(ite2)
        estaciones2 = adj.adjacents(graph['grafo'], est2["Estacion_final"])
        ite3 = it.newIterator(estaciones2)
        while (it.hasNext(ite3)):
            est3 = it.next(ite3)
            edg2 = adj.getEdge(graph['grafo'], est2["Estacion_final"], est3)
            if m.contains(no_repetir, est3) == False:
                if "Tiempo_acumulado" not in est2.keys():
                    tiempo_acumulado = edg2["weight"] + est2["tiempo"]
                    if tiempo_acumulado < (tiempo * 60):
                        ruta = {}
                        ruta["Estacion_final"] = est3
                        ruta["Estacion_inicio"] = est2["Estacion_final"]
                        ruta["tiempo"] = edg2["weight"]
                        ruta["tiempo_acumulado"] = tiempo_acumulado
                        lt.addLast(rutas, ruta)
                        lt.addLast(borrar, ruta)
                        m.put(no_repetir, est3, 1)
                else:
                    tiempo_acumulado = edg2["weight"] + est2["tiempo_acumulado"]
                    if tiempo_acumulado < (tiempo * 60):
                        ruta = {}
                        ruta["Estacion_final"] = est3
                        ruta["Estacion_inicio"] = est2["Estacion_final"]
                        ruta["tiempo"] = edg2["weight"]
                        ruta["tiempo_acumulado"] = tiempo_acumulado
                        lt.addLast(rutas, ruta)
                        lt.addLast(borrar, ruta)
                        m.put(no_repetir, est3, 1)
            lt.removeFirst(borrar)
    return rutas
def dfs_extra(search, graph, vertex, components, path, cycles, weights):
    """
    Funcion auxiliar para calcular un recorrido DFS
    Args:
        search: Estructura para almacenar el recorrido
        vertex: Vertice de inicio del recorrido.
    Returns:
        Una estructura para determinar los vertices
        conectados a source
    Raises:
        Exception
    """
    try:
        if vertex is not None:
            stk.push(path, vertex)
        if vertex is not None:
            cycles[-1].append(vertex)
        adjlst = g.adjacents(graph, vertex)
        adjslstiter = it.newIterator(adjlst)
        while (it.hasNext(adjslstiter)):
            w = it.next(adjslstiter)
            if w is not None:
                visited = map.get(search['visited'], w)
                vertex_comp = map.get(components, vertex)['value']
                w_comp = map.get(components, w)['value']
                # if aun no he visitado todos mis hijos
                if visited is None and vertex_comp == w_comp:
                    map.put(search['visited'], w, {
                        'marked': True,
                        'edgeTo': vertex
                    })
                    edge = g.getEdge(graph, vertex, w)['weight']
                    current_weight = edge + 20
                    weights[-1] += current_weight
                    dfs_extra(search, graph, w, components, path, cycles,
                              weights)
                if g.outdegree(graph, vertex) > 1:
                    new_arr = []
                    new_arr.append(path['last']['info'])
                    dummy = path['first']
                    while dummy['next'] is not None:
                        new_arr.append(dummy['info'])
                        dummy = dummy['next']
                    if new_arr != cycles[-1]:
                        cycles.append(new_arr)
                        weights.append(weights[-1])

        map.put(search['visited'], vertex, None)
        stk.pop(path)
    except Exception as exp:
        error.reraise(exp, 'dfs:dfsVertex')
Ejemplo n.º 3
0
def getEdge(graph, vertexa, vertexb):
    """
    Retorna el arco asociado a los vertices vertexa ---- vertexb
    Args:
        graph: El grafo sobre el que se ejecuta la operacion
        vertexa: Vertice de inicio
        vertexb: Vertice destino
    Returns:
        El grado el arco
    Raises:
        Exception
    """
    if (graph['type'] == "ADJ_LIST"):
        return alt.getEdge(graph, vertexa, vertexb)