Esempio n. 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(capacity=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")
        elif g.containsVertex(graph, v):
            adj = g.adjacents(graph,v)
            adj_it = it.newIterator(adj)
            while it.hasNext(adj_it):
                w = it.next(adj_it)
                edge = g.getEdge(graph, v, w)
                relax(search, edge)
        # obtener los enlaces adjacentes de v
        # Iterar sobre la lista de enlaces
        # Relajar (relax) cada enlace
    return search
Esempio n. 2
0
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'])
Esempio n. 3
0
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'])
Esempio n. 4
0
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'])
Esempio n. 5
0
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'])
Esempio n. 6
0
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'])
Esempio n. 7
0
    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)
Esempio n. 8
0
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)
Esempio n. 9
0
def camino_menos_pesado(graph, source, dst):
    if g.containsVertex(graph, source) and g.containsVertex(graph, dst):
        dijks = newDijkstra(graph, source)
        if hasPathTo(dijks, dst):
            path = pathTo(dijks, dst)
        else:
            path = 'No hay camino'
    else:
        path = 'No existen los vértices'

    return path
Esempio n. 10
0
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'])
Esempio n. 11
0
def getShortestPath(catalog, source, dst):
    """
    Retorna el camino de menor costo entre vertice origen y destino, si existe 
    """
    graph = catalog['directed_Graph']
    print("vertices: ", source, ", ", dst)
    if g.containsVertex(graph, source) and g.containsVertex(graph, dst):
        dijks = dk.newDijkstra(graph, source)
        if dk.hasPathTo(dijks, dst):
            path = dk.pathTo(dijks, dst)
        else:
            path = 'No hay camino'
    else:
        path = 'No existen los vértices'

    return path
Esempio n. 12
0
def newDijkstra(graph, s):
    """
    Crea una busqueda Dijkstra para un digrafo y un vertice origen
    """
    prime = 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), compareByKey)
    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")
        v_list=g.adjacentEdges(v)
        for i in range(1, lt.size(v_list)+1):
            enlace= lt.getElement(v_list,i)
            relax(search, enlace)
        # obtener los enlaces adjacentes de v
        # Iterar sobre la lista de enlaces
        # Relajar (relax) cada enlace
    return search
Esempio n. 13
0
def getPath(catalog, source, dest, strct):
    """
    Retorna el camino, si existe, entre vertice origen y destino
    """
    path = None
    if g.containsVertex(catalog['reviewGraph'], source) and g.containsVertex(
            catalog['reviewGraph'], dest):
        #print("vertices: ",source,", ", dest)
        if strct == 'dfs':
            search = dfs.newDFS(catalog['reviewGraph'], source)
            path = dfs.pathTo(search, dest)
        if strct == 'bfs':
            search = bfs.newBFS(catalog['reviewGraph'], source)
            path = bfs.pathTo(search, dest)
    # ejecutar dfs desde source
    # obtener el camino hasta dst
    # retornar el camino

    return path
Esempio n. 14
0
    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)
Esempio n. 15
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'])
Esempio n. 16
0
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'])
Esempio n. 17
0
def addFlightNode_user(catalog, vertice):
    if not g.containsVertex(catalog['flightGraph'], vertice):
        g.insertVertex(catalog['flightGraph'], vertice)
        return False
    else:
        return True
Esempio n. 18
0
def DFS(catalog, vertice_fuente):
    if not g.containsVertex(catalog['flightGraph'], vertice_fuente):
        return False
    else:
        return dfs.newDFS(catalog["flightGraph"], vertice_fuente)