Exemplo n.º 1
0
    def readFile():
        f1 = open('medellin.txt','r') #Format -> ID  CoordX  CoordY  Name
        IDs = []
        n = [i for i in range(310153)]
        nodes = [] #Info
        for line in f1:
            lineL1 = line.split(' ')
            nodes.append(City.nodeObj(lineL1[0],float(lineL1[1]),float(lineL1[2]),lineL1[3]))
            IDs.append(float(lineL1[0]))
        IDs_Index = dict(zip(IDs,n))

        G = DigraphAL(310153) #Initialize the graph
        f2 = open('connection.rtf','r') #Format -> ID ID distancia nombre
        for line in f2:
            lineL2 = line.split(' ')
            G.insert(IDs_Index[float(lineL2[0])], IDs_Index[float(lineL2[1])], float(lineL2[2]), Name=lineL2[3])
Exemplo n.º 2
0
 def test_punto2(self):
     g = DigraphAL.digraphAL(5)
     g.add_arc(0, 1, 3)
     g.add_arc(0, 1, 4)
     g.add_arc(0, 2, 1)
     g.add_arc(2, 3, 15)
     g.add_arc(1, 3, 3)
     self.assertEqual(punto2(g, 0, 3), 6)
     self.assertEqual(punto2(g, 0, 1), 3)
Exemplo n.º 3
0
 def test_punto1_6(self):
     g = DigraphAL.digraphAL(5)
     g.add_arc(0, 1, 2)
     g.add_arc(1, 0, 2)
     g.add_arc(0, 2, 2)
     g.add_arc(2, 0, 1)
     f = DigraphAL.digraphAL(5)
     f.add_arc(0, 1, 1)
     f.add_arc(1, 2, 1)
     f.add_arc(2, 3, 1)
     f.add_arc(3, 4, 1)
     h = DigraphAL.digraphAL(5)
     h.add_arc(0, 1, 1)
     h.add_arc(0, 0, 1)
     h.add_arc(1, 1, 1)
     h.add_arc(1, 2, 1)
     self.assertEqual(punto1_6(g), True)
     self.assertEqual(punto1_6(f), False)
     self.assertEqual(punto1_6(h), True)
Exemplo n.º 4
0
 def prueba_punto1(self):
     graph2 = DigraphAL.digraphAL(4)
     graph2.add_arc(0, 1, 20)
     graph2.add_arc(0, 2, 42)
     graph2.add_arc(0, 3, 35)
     graph2.add_arc(1, 0, 20)
     graph2.add_arc(1, 2, 30)
     graph2.add_arc(1, 3, 34)
     graph2.add_arc(2, 0, 42)
     graph2.add_arc(2, 1, 30)
     graph2.add_arc(2, 3, 12)
     graph2.add_arc(3, 0, 35)
     graph2.add_arc(3, 1, 34)
     graph2.add_arc(3, 2, 12)
     self.assertEqual(prueba_punto1(graph2, 0), [0, 1, 2, 3, 0])
     return
Exemplo n.º 5
0
 def prueba_punto1_3(self):
     g = DigraphAL.digraphAL(12)
     g.add_arc(5, 11, 1)
     g.add_arc(11, 2, 1)
     g.add_arc(11, 9, 1)
     g.add_arc(11, 10, 1)
     g.add_arc(7, 8, 1)
     g.add_arc(7, 11, 1)
     g.add_arc(8, 9, 1)
     g.add_arc(3, 8, 1)
     g.add_arc(3, 10, 1)
     solution_7 = [7, 8, 11, 9, 2, 10]
     solution_5 = [5, 11, 2, 9, 10]
     solution_3 = [3, 8, 10, 9]
     self.assertEqual(punto1_3(g, 7), solution_7)
     self.assertEqual(punto1_3(g, 5), solution_5)
     self.assertEqual(punto1_3(g, 3), solution_3)
Exemplo n.º 6
0
    def tests():
        graph1AL = DigraphAL(3)
        graph1AL.insert(0, 1, 5)
        graph1AL.insert(1, 2, 10)
        graph1AL.insert(2, 0, 40)

        graph1AM = DigraphAM(3)
        graph1AM.insert(0, 1, 5)
        graph1AM.insert(1, 2, 10)
        graph1AM.insert(2, 0, 40)

        graph2AL = DigraphAL(3)
        graph2AL.insert(0, 1, 3)
        graph2AL.insert(1, 2, 50)

        graph2AM = DigraphAM(3)
        graph2AM.insert(0, 1, 3)
        graph2AM.insert(1, 2, 50)

        if GraphAlgorithms.dijkstra(graph1AL, 0) == GraphAlgorithms.dijkstra(graph1AM, 0):
            print("First test passed!")
        else:
            print("First test failed!")

        if GraphAlgorithms.dijkstra(graph2AL, 0) == GraphAlgorithms.dijkstra(graph2AM, 0):
            print("Second test passed!")
        else:
            print("Second test failed!")
Exemplo n.º 7
0
        new_bits= bits & ~(1 << parent)
        _, parent = C[(bits, parent)]
        bits = new_bits
    path.append(0)
    return opt, list(reversed(path))



#Metodo implementado para generar la matriz de distancias de un grafo
def gen_dist(graph):
    size = graph.size
    matriz = [[0]*size for i in range(size)]
    for i in range(len(matriz)):
        for j in range(len(matriz)):
            matriz[i][j] = graph.get_weight(i, j)
    return matriz
 

graph2 = DigraphAL.digraphAL(4)
graph2.add_arc(0, 1, 7)
graph2.add_arc(0, 2, 15)
graph2.add_arc(0, 3, 6)
graph2.add_arc(1, 0, 2)
graph2.add_arc(1, 2, 7)
graph2.add_arc(1, 3, 3)
graph2.add_arc(2, 0, 9)
graph2.add_arc(2, 1, 6)
graph2.add_arc(2, 3, 12)
graph2.add_arc(3, 0, 10)
graph2.add_arc(3, 1, 4)
graph2.add_arc(3, 2, 8)
Exemplo n.º 8
0
"""

#Generamos un grafo de matriz y lo llenamos
gmatriz = DigraphAM.digraphAM(11)
gmatriz.add_arc(5, 11)
gmatriz.add_arc(11, 2)
gmatriz.add_arc(11, 9)
gmatriz.add_arc(11, 10)
gmatriz.add_arc(7, 11)
gmatriz.add_arc(7, 8)
gmatriz.add_arc(8, 9)
gmatriz.add_arc(3, 8)
gmatriz.add_arc(3, 10)

#Generamos un grafo de lista y lo llenamos
glista = DigraphAL.digraphAL(11)
glista.add_arc(5, 11)
glista.add_arc(11, 2)
glista.add_arc(11, 9)
glista.add_arc(11, 10)
glista.add_arc(7, 11)
glista.add_arc(7, 8)
glista.add_arc(8, 9)
glista.add_arc(3, 8)
glista.add_arc(3, 10)


def punto1_2(grafo):
    #Recorro el total de vertices de el grafo
    for i in range(grafo.size):
        #Comparo cual vertice tiene mayor longitud en sus lsitas y lo declaro como maximo
Exemplo n.º 9
0
import DigraphAL


grafo_lista = DigraphAL.digraphAL(11)
grafo_lista.add_arc(5,11)
grafo_lista.add_arc(11,2)
grafo_lista.add_arc(11,9)
grafo_lista.add_arc(11,10)
grafo_lista.add_arc(7,11)
grafo_lista.add_arc(7,8)
grafo_lista.add_arc(8,9)
grafo_lista.add_arc(3,8)
grafo_lista.add_arc(3,10)



def dfs_aux(grafo, vertice, recorrido, resultado):
    recorrido+=[vertice]
    if len(grafo.get_successors(vertice))==0:
        resultado.append(recorrido)
    else:
        for i in grafo.get_successors(vertice):
             if i not in recorrido:
                dfs_aux(grafo,i,recorrido[:],resultado)




def dfs(grafo, vertice):
    resultado = []
    dfs_aux(grafo,vertice,[],resultado)
Exemplo n.º 10
0
import DigraphAL

grafo = DigraphAL.digraphAL(4)
grafo.add_arc(2, 3)
grafo.add_arc(3, 2)
grafo.add_arc(1, 3)
grafo.add_arc(3, 1)
grafo.add_arc(1, 4)
grafo.add_arc(4, 1)


def FunnyGame(grafo):
    visitados = [False] * grafo.size
    vuelo = -1
    for i in range(grafo.size):
        if FunnyGame_aux(grafo, i + 1, visitados) == True:
            vuelo = i + 1
            break
    if vuelo > -1:
        print("First player wins flying to airport ", vuelo)
    else:
        print("First player loses")


def FunnyGame_aux(grafo, nodo, visitados):
    if all(visitados[:nodo - 1] + visitados[nodo:]):
        visitados[nodo - 1] = True
    vuelos = grafo.get_successors(nodo)
    for i in vuelos:
        if visitados[i - 1] == False:
            visitados[nodo - 1] = True