def test1(): G = {} G[1] = {2, 3} G[2] = {3, 4} G[3] = {4} G[4] = {} W = {} W[(1, 2)] = 1 W[(1, 3)] = 4 W[(2, 3)] = 2 W[(2, 4)] = 6 W[(3, 4)] = 3 A = dijkstra.shortest_paths(G, W, 1) assert A == {1: 0, 2: 1, 3: 3, 4: 6}
def run_shortest_paths(): edges = [] with open('data/dijkstraData.txt', 'r') as file: for line in file: vertices = line.split('\t') tail = int(vertices[0]) - 1 for i in range(1, len(vertices) - 1): head_data = vertices[i].split(',') head = int(head_data[0]) - 1 weight = int(head_data[1]) edges.append([tail, head, weight]) paths = shortest_paths(200, edges) indices = [7, 37, 59, 82, 99, 115, 133, 165, 188, 197] answer = [] for index in indices: path = paths[index - 1] if path == 0: path = 10**6 answer.append(path) print('correct = %s' % are_lists_equal(answer, [2599, 2610, 2947, 2052, 2367, 2399, 2029, 2442, 2505, 3068]))
import dijkstra ifs = sys.stdin ofs = sys.stdout def numbers_from_line(d=' '): return [int(s) for s in ifs.readline().strip().split(d) if len(s.strip()) > 0] n, m = numbers_from_line() G = {v: [] for v in range(n)} W = {} for __ in range(m): a, b, w = numbers_from_line() a -= 1 b -= 1 G[a].append(b) # Rosalind input data contains multi-graph if (a, b) not in W: W[(a, b)] = w else: W[(a, b)] = min(w, W[(a, b)]) A = dijkstra.shortest_paths(G, W, 0) D = [A[v] if A[v] < math.inf else -1 for v in range(n)] ofs.write('%s\n' % (' '.join(str(a) for a in D)))
OpenStreetMap of Ann Arbor, MI https://www.openstreetmap.org/export#map=15/42.2758/-83.7501 """ import gzip from xml.sax import parse from xml.sax.handler import ContentHandler from openstreetmap import graph_from_openstreetmap from dijkstra import shortest_paths, print_path # load Ann Arbor as a graph graph = graph_from_openstreetmap( gzip.open('openstreetmap_ann_arbor_mi.xml.gz')) # find all paths from your starting location start = ('Crest Avenue', 'West Washington Street') paths = shortest_paths(graph, start) # Confirm it is right for known spots -- nearby coffee shops # Argus print("\nArgus @ ('Second Street', 'West Liberty Street')") print_path(paths, start, (u'Second Street', u'West Liberty Street')) # Big City Small World Bakery print("\nBig City Small World Bakery @ ('Miller Avenue', 'Spring Street')") print_path(paths, start, (u'Miller Avenue', u'Spring Street')) # Jefferson Cakery print("\nJefferson Cakery @ ('Fifth Street', 'West Jefferson Street')") print_path(paths, start, (u'Fifth Street', u'West Jefferson Street'))
"""Shortest path example Example from Wikipedia: https://en.wikipedia.org/wiki/Shortest_path_problem """ from dijkstra import shortest_paths, print_path # make up an example graph graph = { 'A': [('B', 4), ('C', 2)], 'B': [('C', 5), ('D', 10)], 'C': [('E', 3)], 'D': [('F', 11)], 'E': [('D', 4)], 'F': [], } # find all paths from your starting location paths = shortest_paths(graph, 'A') print_path(paths, 'A', 'F')
def test_correctness(self): edges = [[1, 2, 1], [1, 3, 4], [2, 3, 2], [2, 4, 6], [3, 4, 3]] self.assertListEqual(shortest_paths(4, edges), [0, 1, 3, 6])