def main(argv): n, m, edges = files.read_weighted_graph(argv[0]) nodes = [n for n in xrange(1, n + 1)] D = graphs.dijkstra(1, nodes, edges) print ' '.join( str(D[node]) if D[node] != float('inf') else '-1' for node in nodes)
def sssp(bot, update): """ Applies Dijkstra or Ford-Bellman algorithm for this graph """ user_id = update.message.from_user.id start_node = update.message.text.split()[1:] update.message.reply_text(graphs.dijkstra(user_id, start_node))
def main(argv): k, Gs = files.read_weighted_graphs(argv[0]) D = [] for G in Gs: n, m = G[:2] edges = G[2] nodes = [n for n in xrange(1, n + 1)] t, h, w = edges[0] D.append(graphs.dijkstra(h, nodes, edges)[t] + w) print ' '.join(str(d) if d != float('inf') else '-1' for d in D)
#!/usr/bin/env python3 from base import pointSetSpace, setToLists, Vec2D from delaunay import toGraph from graphs import dijkstra, plotGraph import pylab as pl import plot_opt import matplotlib.animation as anim import numpy as np n = 180 V = [Vec2D(0.5, 0.5)] + pointSetSpace(0, 1, 0, 1, n - 1, 0.8 / n**0.5) E = toGraph(V) ds, pred = dijkstra(V, E, V[0]) fig = pl.figure() pl.xlim(0, 1) pl.ylim(0, 1) plotGraph(V, E) es = [] for v in V[1:]: es.append((ds[v], v)) es.sort() es = [setToLists([e[1], pred[e[1]]]) for e in es] ls = [pl.plot([], [], color='red', linewidth=1.6)[0] for _ in range(n - 1)] nc = 7 dd = 1 / (nc - 1) x, y = np.mgrid[slice(0, 1 + dd / 2, dd), slice(0, 1 + dd / 2, dd)] z = [[[0, 0] for _ in range(nc)] for _ in range(nc)]
import sys,graphs,itertools def prepare_graph(): diff_by_1 = lambda (x,y): len(x) == len(y) and sum(1 for a,b in zip(x,y) if a != b) == 1 graph = {} for x,y in filter(diff_by_1,itertools.product(words,repeat=2)): if x not in graph: graph[x] = [] if y not in graph: graph[y] = [] graph[x].append( ( y , 1) ) graph[y].append( ( x , 1) ) return graph if __name__ == '__main__': if len(sys.argv) >= 3 and len(sys.argv[1]) == len(sys.argv[2]): src,dst = sys.argv[1:] words = filter(lambda w : len(w) == len(src), sys.stdin.read().split()) #only consider those words with same length as parameter if src in words and dst in words: print graphs.dijkstra(prepare_graph(),src,dst) else: print "Both words should be present in the dictionary." else: print "Missing command line parameters."
import sys, graphs, itertools def prepare_graph(): diff_by_1 = lambda (x, y): len(x) == len(y) and sum(1 for a, b in zip(x, y) if a != b) == 1 graph = {} for x, y in filter(diff_by_1, itertools.product(words, repeat=2)): if x not in graph: graph[x] = [] if y not in graph: graph[y] = [] graph[x].append((y, 1)) graph[y].append((x, 1)) return graph if __name__ == '__main__': if len(sys.argv) >= 3 and len(sys.argv[1]) == len(sys.argv[2]): src, dst = sys.argv[1:] words = filter( lambda w: len(w) == len(src), sys.stdin.read().split( )) #only consider those words with same length as parameter if src in words and dst in words: print graphs.dijkstra(prepare_graph(), src, dst) else: print "Both words should be present in the dictionary." else: print "Missing command line parameters."
def test_dijkstra_four(): dists = {0: 18, 1: 10, 2: 17, 3: 8, 4: 0} preds = {0: 2, 1: 4, 2: 1, 3: 4, 4: -1} assert dijkstra(adj, 4) == (dists, preds)
def test_dijkstra_three(): dists = {0: 11, 1: 3, 2: 10, 3: 0, 4: 8} preds = {0: 2, 1: 3, 2: 1, 3: -1, 4: 3} assert dijkstra(adj, 3) == (dists, preds)
def test_dijkstra_two(): dists = {0: 1, 1: 7, 2: 0, 3: 10, 4: 17} preds = {0: 2, 1: 2, 2: -1, 3: 1, 4: 1} assert dijkstra(adj, 2) == (dists, preds)
def test_dijkstra_one(): dists = {0: 8, 1: 0, 2: 7, 3: 3, 4: 10} preds = {0: 2, 1: -1, 2: 1, 3: 1, 4: 1} assert dijkstra(adj, 1) == (dists, preds)
def test_dijkstra_zero(): dists = {0: 0, 1: 8, 2: 1, 3: 11, 4: 18} preds = {0: -1, 1: 2, 2: 0, 3: 1, 4: 1} assert dijkstra(adj, 0) == (dists, preds)