def setUp(self): self.iberia = Digraph(V=cities) ikm = {} for i in cities: for j in iberia.succs(i): c1 = coords[i] c2 = coords[j] if c1[0] < c2[0]: self.iberia.E.add((i, j)) ikm[i, j] = km(i, j) self.km = WeightingFunction(ikm, symmetrical=False) self.unconnected = Digraph(E={ 0: [1, 2], 1: [2, 3], 2: [5, 6], 3: [5], 4: [2], 5: [6] }) self.unconnected_weight = WeightingFunction( (((u, v), 1) for (u, v) in self.unconnected.E)) self.iterative = DagShortestPathsFinder() self.memoized = MemoizedDagShortestPathsFinder() self.dijkstra = DijkstraWithPriorityDictShortestPathsFinder()
def test_topsort(self): G = Digraph(E=[(0,1), (1,2), (2,3), (3,4), (4,5)]) topsorter = Topsorter() self.assertEqual(tuple(topsorter.topsorted(G)), tuple(range(6))) int_topsorter = Topsorter(createDepthFirstTraverser= lambda G: DepthFirstTraverser(createSet=lambda V: IntSet(capacity=max(V)+1))) self.assertEqual(tuple(int_topsorter.topsorted(G)), tuple(range(6))) G = Digraph(E={'C': ['C++', 'Objective C', 'Java', 'C#'], 'C++': ['C#', 'Java'], 'Java': ['C#']}) ts = tuple(topsorter.topsorted(G)) for (u,v) in G.E: self.assertTrue(ts.index(u) < ts.index(v))
def squared_graph(G): Gsquared = Digraph(V=G.V) for u in G.V: for v in G.succs(u): for w in G.succs(v): Gsquared.E.add((u, w)) return Gsquared
def datos_fichero(filename): datos = [] aristas = [] aristas_pesos = {} try: f = open(filename, "r", encoding="utf-8") k = int(f.readline()) v_ini = int(f.readline()) nr_aristas = int(f.readline()) for line in f: v_origen, v_destino, arista_peso = line.split(" ") arista = (int(v_origen), int(v_destino)) # añadir aristas a la lista para crear el grafo aristas.append(arista) aristas_pesos[arista] = int(arista_peso) # datos.append([arista, int(arista_peso)]) g = Digraph(E=aristas) d = WeightingFunction(aristas_pesos) f.close() except IOError: print("File cannot be open!") return k, v_ini, nr_aristas, g, d
def setUp(self): self.iberia = UndirectedGraph(V=cities, E=roads) self.unconnected = Digraph(E={ 0: [1, 2], 1: [2, 3], 2: [5, 6], 3: [5], 4: [2], 5: [6] }) self.unconnected_weight = WeightingFunction( (((u, v), 1) for (u, v) in self.unconnected.E))
def setUp(self): self.unconnected = Digraph(E={ 0: [1, 2], 1: [2, 3], 2: [5, 6], 3: [5], 4: [2], 5: [6] }) self.unconnected_weight = WeightingFunction( (((u, v), 1) for (u, v) in self.unconnected.E)) self.f = lambda a, b: 1
def setUp(self): self.edges = [(0,1), (1,2), (2,3), (3,4), (4,5), (5,0), (0,3), (3,0), (0,2), (0,4), (0,7)] self.g1 = Digraph(V=range(8), E=self.edges) self.g1bis = Digraph(E=self.edges) self.g1bis.V.add(6) self.g2 = AdjacencyDigraph(V=range(8), E=self.edges, directed=True, createMap=lambda V: IntKeyMap(capacity=max(V)+1), createSet=lambda V: IntSet(capacity=max(V)+1)) self.g3 = LinkedListSetAdjacencyDigraph(V=range(8), E=self.edges, directed=True, createMap=lambda V: IntKeyMap(capacity=max(V)+1)) self.g4 = ListSetAdjacencyDigraph(V=range(8), E=self.edges, directed=True, createMap=lambda V: IntKeyMap(capacity=max(V)+1)) self.g5 = SetAdjacencyDigraph(V=range(8), E=self.edges, directed=True, createMap=lambda V: IntKeyMap(capacity=max(V)+1)) self.g6 = AdjacencyMatrixDigraph(V=range(8), E=self.edges, directed=True) self.g7 = InvAdjacencyDigraph(V=range(8), E=self.edges, directed=True, createMap=lambda V: IntKeyMap(capacity=max(V)+1), createSet=lambda V: IntSet(capacity=max(V)+1)) edges = self.edges + [(1,1)] self.g8 = InvAdjacencyDigraph(V=range(8), E=edges, directed=True, createMap=lambda V: IntKeyMap(capacity=max(V)+1), createSet=lambda V: IntSet(capacity=max(V)+1))
def recorre_aristas_anchura(grafo: Digraph, v_inical: Vertex) -> List[Edge]: aristas = [] seen = set() queue = Fifo() queue.push((v_inical, v_inical)) seen.add(v_inical) while len(queue) > 0: u, v = queue.pop() aristas.append((u, v)) for suc in grafo.succs(): if suc not in seen: seen.add(suc) queue.push((u, v)) return aristas
def test_strong_components(self): sccf = StrongConnectedComponentsFinder() self.G = Digraph( E={ 0: [1], 1: [2, 3], 2: [0, 3], 3: [6, 4], 4: [5, 6], 5: [4, 8], 6: [7], 7: [6, 8], 8: [] }) ccs = [] for cc in sccf.strong_connected_components(self.G): ccs.append(tuple(sorted(cc))) ccs.sort() self.assertEqual(tuple(ccs), ((0, 1, 2), (3, ), (4, 5), (6, 7), (8, )))
def dijkstra(g: Digraph, d: WeightingFunction, v_inicial, v_final): D = {} added = set() recorrido = [] for v in g.V: D[v] = float("infinity") D[v_inicial] = 0 frontera = {v_inicial: v_inicial} while len(frontera) > 0: v_destino = argmin(frontera.keys(), lambda v: D[v]) added.add(v_destino) v_origen = frontera[ v_destino] # Como en el recorrido de backpointers, aquí el origen del v_destino es el valor de los vertices frontera recorrido.append((v_origen, v_destino)) del frontera[v_destino] if v_destino == v_final: break for i in g.succs(v_destino): if i not in added and D[v_destino] + d(v_destino, i) < D[i]: frontera[i] = v_destino D[i] = D[v_destino] + d(v_destino, i) return recorrido
from algoritmia.datastructures.digraphs import Digraph g = Digraph(E=[(0, 1), (0, 2), (1, 2)]) print(g.V) print(g.succs(0))
if v not in mem: mem[v] = min([(p(d) + w(d, v), d) for d in preds]) return mem[v][0] mem = {} score = p(vf) v = vf decisions = [v] while (v != vi): if v not in mem: break (_, v_prev) = mem[v] decisions.append(v_prev) v = v_prev decisions.reverse() return score, decisions # Programa principal dic_pesos = { (0, 1): 10, (0, 2): 100, (1, 2): 10, (1, 3): 1, (3, 2): 1, (4, 3): 7 } g = Digraph(E=dic_pesos.keys()) w = WeightingFunction(dic_pesos) print(cmc_col(g, w, 0, 3))
#coding: latin1 #< full from algoritmia.datastructures.digraphs import Digraph from algoritmia.problems.shortestpaths.length import BreadthFirstShortestPaths G = Digraph(E=[(0, 1), (0, 3), (1, 4), (2, 4), (2, 5), (3, 0), (3, 1), (4, 3), (5, 5)]) bfsp = BreadthFirstShortestPaths() for (s, t) in (0, 3), (0, 4), (2, 0): print('Camino mínimo entre {} y {}: {}'.format( s, t, bfsp.shortest_path(G, s, t))) #> full
def setUp(self): self.G = Digraph(E={0: [1, 2], 1: [3], 2: [3], 3: [4], 5: [6]}) self.bft = BreadthFirstTraverser() self.rdft = RecursiveDepthFirstTraverser() self.dft = DepthFirstTraverser() seed(0)
#coding: latin1 #< full from algoritmia.datastructures.digraphs import Digraph from algoritmia.problems.connectedcomponents import StrongConnectedComponentsFinder G = Digraph(E=[(0,1),(1,2),(1,3),(2,0),(2,3),(3,4),(3,6),\ (4,5),(4,6),(5,4),(5,8),(6,7),(7,6),(7,8)]) sccf = StrongConnectedComponentsFinder() print([list(comp) for comp in sccf.strong_connected_components(G)]) #> 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
#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
#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
#coding: latin1 from algoritmia.datastructures.digraphs import Digraph #[full from algoritmia.problems.topsort import Topsorter G = Digraph(E=[ ('C', 'C++'), ('C', 'Java'), ('C', 'C#'), ('C', 'ObjC'), #?[(?[¶(? ('C++', 'Java'), ('C++', 'C#'), ('Java', 'C#') ]) #?('C++', 'J?»('C++', 'J? print(list(Topsorter().topsorted(G))) #]full
#coding: latin1 #< full from algoritmia.datastructures.digraphs import Digraph from algoritmia.problems.closures import DigraphTransitiveClosureFinder G = Digraph(E=[(0, 1), (0, 3), (1, 4), (2, 4), (2, 5), (3, 0), (3, 1), (4, 3), (5, 5)]) print(DigraphTransitiveClosureFinder().transitive_closure(G)) G2 = Digraph(E=[ ('C', 'C++'), ('C', 'Java'), ('C', 'C#'), ('C', 'ObjC'), #?[(?[¶(? ('C++', 'Java'), ('C++', 'C#'), ('Java', 'C#') ]) #?('C++', 'J?»('C++', 'J? print(DigraphTransitiveClosureFinder().transitive_closure(G2)) #> full
from algoritmia.datastructures.digraphs import Digraph aristas = [(1, 3), (1, 7), (1, 9), (3, 7)] g = Digraph(E=aristas) print(g.succs(1)) print(g.preds(7)) print(g.V) #Vertices print(g.E) #Aristas
def setUp(self): self.chain = Digraph(E=[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]) self.loop = Digraph(E=[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 0)]) self.islands = Digraph(E=[(0, 1), (1, 2), (2, 0), (3, 4), (4, 3)])