class TestSpanning(unittest.TestCase):
    def setUp(self):
        self.iberia = UndirectedGraph(V=cities, E=roads)
        self.unconnected = UndirectedGraph(E={
            0: [1, 2],
            1: [2, 3],
            2: [4, 5],
            3: [4],
            4: [5],
            6: [7, 8],
            7: [8]
        })
        self.unconnected_weight = WeightingFunction(
            (((u, v), 1) for (u, v) in self.unconnected.E), symmetrical=True)
        self.d = WeightingFunction(
            {
                (0, 1): 0,
                (0, 2): 15,
                (0, 3): 2,
                (1, 3): 3,
                (1, 4): 13,
                (2, 3): 11,
                (2, 5): 4,
                (3, 4): 5,
                (3, 5): 8,
                (3, 6): 12,
                (4, 7): 9,
                (5, 6): 16,
                (5, 8): 10,
                (6, 7): 17,
                (6, 8): 1,
                (6, 9): 6,
                (7, 9): 14,
                (8, 9): 7
            },
            symmetrical=True)
        self.g = UndirectedGraph(E=self.d.keys())
        seed(0)

    def test_breadth_first_spanning_tree(self):
        spf = GraphTraversalSpanningTreeFinder()
        self.assertEqual(count(spf.spanning_tree(self.iberia, 'Madrid')),
                         len(cities) - 1)
        self.assertEqual(count(spf.spanning_tree(self.unconnected, 0)), 5)
        self.assertEqual(count(spf.spanning_tree(self.g, 0)), 9)

    def test_breadth_first_spanning_forest(self):
        sff = GraphTraversalSpanningForestFinder()
        f = tuple(sff.spanning_forest(self.iberia))
        self.assertEqual(len(f), 1)
        self.assertEqual(count(f[0]), len(cities) - 1)
        f = tuple(sff.spanning_forest(self.unconnected))
        self.assertEqual(len(f), 2)
        self.assertEqual(count(f[0]) + count(f[1]), 7)
        forest = tuple(sff.spanning_forest(self.g))
        self.assertEqual(len(forest), 1)
        self.assertEqual(count(forest[0]), 9)
#coding: latin1

#< full
from algoritmia.datastructures.digraphs import Digraph, WeightingFunction
from algoritmia.problems.shortestpaths.kedges import KEdgesDistance

d = WeightingFunction({
    (0, 1): 3,
    (0, 3): 1,
    (1, 0): -2,
    (1, 1): 2,
    (1, 2): 2,
    (1, 4): -2,
    (1, 5): 3,
    (2, 5): 1,
    (3, 1): 1,
    (4, 3): 2,
    (4, 5): 2
})
G = Digraph(E=d.keys())
print('Distancia de {0} a {5}')
for i in range(6):
    print('  con {} aristas: {}'.format(
        i,
        KEdgesDistance().distance(G, d, [0], [5], i)))
#> full
            distan=distancia(vertices[i],vertices[j])
            diccionario[(i,j)]=distan
            diccionario[(j,i)]=distan
            
    return diccionario,vertices

def distancia(punto1,punto2):
    return sqrt(((punto1[0]-punto2[0])*(punto1[0]-punto2[0]))+((punto1[1]-punto2[1])*(punto1[1]-punto2[1])))
 

    

#diccionario,vertices=read_file("pruebas-greedy/p00.vjt")
diccionario,vertices=read_file(sys.argv[1])
d = WeightingFunction(diccionario)
G = UndirectedGraph(E=d.keys())
MST = list(PrimsMinimumSpanningFinder().minimum_spanning_forest(G, d))
grafoPrim=UndirectedGraph(E=MST)

j=0
for inici in grafoPrim.V:
    distan=0
    solucio=list(DepthFirstTraverser().traverse(grafoPrim, inici))
    if (j==0):
        mejorDistancia=0
        for i in range(len(solucio)-1):
            mejorDistancia+=diccionario[(solucio[i],solucio[i+1])]
        mejorDistancia+=diccionario[(solucio[len(solucio)-1],solucio[0])]
        solucion=solucio
    
    else:
Exemple #4
0
#coding: latin1

#< full
from algoritmia.datastructures.digraphs import UndirectedGraph, WeightingFunction
from algoritmia.problems.spanningtrees import PrimsWithPriorityQueueMinimumSpanningTreeFinder

d = WeightingFunction({(0,1): 0, (0,2): 15, (0,3): 2, (1,3): 3, (1,4): 13, (2,3): 11,
                       (2,5): 4, (3,4): 5, (3,5): 8, (3,6): 12, (4,7): 9, (5,6): 16, 
                       (5,8):10, (6,7): 17, (6,8): 1, (6,9): 6, (7,9): 14, (8,9): 7}, 
                       symmetrical=True)
G = UndirectedGraph(E=d.keys())
MST = list(PrimsWithPriorityQueueMinimumSpanningTreeFinder().minimum_spanning_forest(G, d))
print('MST: {} con peso {}.'.format(MST, sum (d(u,v) for (u,v) in MST)))
#> full
#coding: latin1

#< full
from algoritmia.datastructures.digraphs import Digraph, WeightingFunction
from algoritmia.problems.shortestpaths.acyclic import DagShortestPathsFinder

d = WeightingFunction({(0,1): 3, (0,3): -1, (1,2): 5, (1,4): 2, (1,5): 4, #?(0,1?¶(0,1? 
                       (2,4): 3, (2,5): 1, (3,1): 1, (4,5): 1}) #?(2,4?»(2,4?
G, I, F = Digraph(E=d.keys()), [0], [2, 5]
print('Distancia entre {} y {}: {}'.format(
    I, F, DagShortestPathsFinder().some_to_some_distance(G, d, I, F)))
#> full
Exemple #6
0
#coding: latin1

#< full
from algoritmia.datastructures.digraphs import UndirectedGraph, WeightingFunction
from algoritmia.problems.spanningtrees import BaruvkasMinimumSpanningForestFinder

baruvka = BaruvkasMinimumSpanningForestFinder()
d = WeightingFunction({(0,1): 0, (0,2): 15, (0,3): 2, (1,3): 3, (1,4): 13, (2,3): 11, #?{?¶{? #?(0,1?¶(0,1? 
            (2,5): 4, (3,4): 5, (3,5): 8, (3,6): 12, (4,7): 9, (5,6): 16, #?(2,5?»»(2,5?
            (5,8):10, (6,7): 17, (6,8): 1, (6,9): 6, (7,9): 14, (8,9): 7},#?(5,8?»»(5,8? 
            symmetrical=True) #?symm?»symm?
MST = list(baruvka.minimum_spanning_forest(UndirectedGraph(E=d.keys()), d))
print('MST: {} con peso {}.'.format(MST, sum(d(u,v) for (u,v) in MST)))
#> full
Exemple #7
0
#coding: latin1

#< full
from algoritmia.datastructures.digraphs import UndirectedGraph, WeightingFunction

km = WeightingFunction(
    {
        ('Alcúdia', 'Artà'): 36,
        ('Alcúdia', 'Inca'): 25,
        ('Alcúdia', 'Pollença'): 10,
        ('Andratx', 'Calvià'): 14,
        ('Andratx', 'Palma de Mallorca'): 30,
        ('Andratx', 'Sóller'): 56,
        ('Artà', 'Capdepera'): 8,
        ('Artà', 'Manacor'): 17,
        ('Calvià', 'Palma de Mallorca'): 14,
        ('Campos del Port', 'Llucmajor'): 14,
        ('Campos del Port', 'Santanyí'): 13,
        ('Inca', 'Manacor'): 25,
        ('Inca', 'Marratxí'): 12,
        ('Llucmajor', 'Palma de Mallorca'): 20,
        ('Manacor', 'Santanyí'): 27,
        ('Marratxí', 'Palma de Mallorca'): 14,
        ('Pollença', 'Sóller'): 54
    },
    symmetrical=True)
Mallorca = UndirectedGraph(E=km.keys())
#> full
class TestSpanning(unittest.TestCase):
    def setUp(self):
        self.iberia = UndirectedGraph(V=cities, E=roads)
        self.unconnected = UndirectedGraph(E={
            0: [1, 2],
            1: [2, 3],
            2: [4, 5],
            3: [4],
            4: [5],
            6: [7, 8],
            7: [8]
        })
        self.unconnected_weight = WeightingFunction(
            (((u, v), 1) for (u, v) in self.unconnected.E), symmetrical=True)
        self.d = WeightingFunction(
            {
                (0, 1): 0,
                (0, 2): 15,
                (0, 3): 2,
                (1, 3): 3,
                (1, 4): 13,
                (2, 3): 11,
                (2, 5): 4,
                (3, 4): 5,
                (3, 5): 8,
                (3, 6): 12,
                (4, 7): 9,
                (5, 6): 16,
                (5, 8): 10,
                (6, 7): 17,
                (6, 8): 1,
                (6, 9): 6,
                (7, 9): 14,
                (8, 9): 7
            },
            symmetrical=True)
        self.g = UndirectedGraph(E=self.d.keys())
        seed(0)

    def test_baruvka(self):
        sff = BaruvkasMinimumSpanningForestFinder()
        self.assertEqual(count(sff.minimum_spanning_forest(self.iberia, km)),
                         len(cities) - 1)
        self.assertEqual(
            count(
                sff.minimum_spanning_forest(self.unconnected,
                                            self.unconnected_weight)), 7)
        w = sum(
            self.d(u, v)
            for (u, v) in sff.minimum_spanning_forest(self.g, self.d))
        self.assertEqual(w, 45)

    def test_Kruskal(self):
        sff = KruskalsMinimumSpanningForestFinder()
        self.assertEqual(count(sff.minimum_spanning_forest(self.iberia, km)),
                         len(cities) - 1)
        self.assertEqual(
            count(
                sff.minimum_spanning_forest(self.unconnected,
                                            self.unconnected_weight)), 7)
        w = sum(
            self.d(u, v)
            for (u, v) in sff.minimum_spanning_forest(self.g, self.d))
        self.assertEqual(w, 45)

    def test_Prim(self):
        stf = PrimsMinimumSpanningFinder()
        self.assertEqual(
            count(stf.minimum_spanning_tree(self.iberia, km, "Gandia")),
            len(cities) - 1)
        self.assertEqual(
            count(
                stf.minimum_spanning_tree(self.unconnected,
                                          self.unconnected_weight, 0)), 5)
        w = sum(
            self.d(u, v)
            for (u, v) in stf.minimum_spanning_tree(self.g, self.d, 0))
        self.assertEqual(w, 45)

    def test_Prim_with_priority_dict(self):
        stf = PrimsMinimumSpanningFinder()
        self.assertEqual(
            count(stf.minimum_spanning_tree(self.iberia, km, "Gandia")),
            len(cities) - 1)
        self.assertEqual(
            count(
                stf.minimum_spanning_tree(self.unconnected,
                                          self.unconnected_weight, 0)), 5)
        pstf = PrimsWithPriorityQueueMinimumSpanningTreeFinder()
        w = sum(
            self.d(u, v)
            for (u, v) in pstf.minimum_spanning_tree(self.g, self.d, 0))
        self.assertEqual(w, 45)
#coding: latin1

#< full
from algoritmia.datastructures.digraphs import Digraph, WeightingFunction
from algoritmia.problems.shortestpaths.general import BellmanFordShortestPathsFinder

d = WeightingFunction({
    (0, 1): 3,
    (0, 3): 1,
    (1, 0): 1,
    (1, 1): 2,
    (1, 2): 2,
    (1, 4): -2,  #?(0,1?¶(0,1?
    (1, 5): 3,
    (2, 5): 1,
    (3, 1): 1,
    (4, 3): 2,
    (4, 5): 2
})  #?(1,5?»(1,5?
bfsp = BellmanFordShortestPathsFinder()
print('Distancia de 0 a 5:', bfsp.distance(Digraph(E=d.keys()), d, 0, 5))
#> full