BVB Source Codes

Algorithm-Implementations Show johnson.py Source code

Return Download Algorithm-Implementations: download johnson.py Source code - Download Algorithm-Implementations Source code - Type:.py
  1. def initialize(G, s):
  2.     """Initialize graph G and vertex s."""
  3.     V, E = G
  4.     d = {v: float('inf') for v in V}
  5.     p = {v: None for v in V}
  6.     d[s] = 0
  7.     return d, p
  8.  
  9.  
  10. def bellman_ford(G, w, s):
  11.     """Bellman-Ford's algorithm for shortest-path search. Expects oriented graph.
  12.    Parameter G is a graph represented as a tuple of vertexes and edges. The
  13.    parametr w represents weights of each edge of the graph G (w: E -> R).
  14.    """
  15.     d, p = initialize(G, s)
  16.     V, E = G
  17.     for _ in range(len(V)-1):
  18.         for (u, v) in E:
  19.             if d[v] > d[u] + w[u, v]:
  20.                 d[v] = d[u] + w[u, v]
  21.                 p[v] = u
  22.     for (u, v) in E:
  23.         if d[v] > d[u] + w[u, v]:
  24.             raise RuntimeError('Graph contains negative cycles.')
  25.     return d, p
  26.  
  27.  
  28. def dijkstra(G, w, s):
  29.     """Dijkstra's algorithm for shortest-path search."""
  30.     d, p = initialize(G, s)
  31.     V, E = G
  32.     S = set(V)
  33.     while S:
  34.         u = min(S, key=lambda x: d[x])
  35.         S = S - {u}
  36.         for (t, v) in E:
  37.             if t == u and d[v] > d[u] + w[u, v]:
  38.                 d[v] = d[u] + w[u, v]
  39.                 p[v] = u
  40.     return d, p
  41.  
  42.  
  43. def johnson(G, w):
  44.     """Johnson's algorithm for shortest-path search between all vertexes of
  45.    the graph G. G is represented with an adjacancy list. The parameter w
  46.    represents weights of edges.
  47.    """
  48.     V, E = G
  49.     G_ = (V + ['S'], E + [('S', v) for v in V])
  50.     V_, E_ = G_
  51.     w_ = dict(w.items() + [((u, v), 0) for (u, v) in E_ if u == 'S'])
  52.     d, p = bellman_ford(G_, w_, 'S')
  53.     h = {}
  54.     for v in V:
  55.         h[v] = d[v]
  56.     w__ = {}
  57.     for (u, v) in E:
  58.         w__[u, v] = w[u, v] + h[u] - h[v]
  59.     D = {(u, v): None for u in V for v in V}
  60.     for u in V:
  61.         d_, p_ = dijkstra(G, w__, u)
  62.         for v in V:
  63.             D[u, v] = d_[v] + h[v] - h[u]
  64.     return D
  65.  
  66.  
  67. if __name__ == '__main__':
  68.     V = ['A', 'B', 'C', 'D']  # vertexes
  69.     E = [('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'B')]  # edges
  70.     w = {('A', 'B'): 1, ('B', 'C'): 3, ('B', 'D'): 1,
  71.          ('C', 'D'): 8, ('D', 'B'): -2}  # weights
  72.     print johnson((V, E), w)
  73.  
downloadjohnson.py Source code - Download Algorithm-Implementations Source code
Related Source Codes/Software:
raty - 2017-04-22
RDVTabBarController - Highly customizable tabBar and tabBarController fo... 2017-04-22
material-icon-lib - Library containing over 1500 material vector icons... 2017-04-21
httpdiff - Perform the same request against two HTTP servers ... 2017-04-21
jquerytools - The missing UI library for the Web ... 2017-04-21
mcrouter - Mcrouter is a memcached protocol router for scalin... 2017-04-22
dynomite - A generic dynamo implementation for different k-v ... 2017-04-22
kityminder - Baidu brain figure 2017-04-22
llvm - Mirror of official llvm git repository located at ... 2017-04-22
RBBAnimation - Block-based animations made easy, comes with easin... 2017-04-22
ied - 2017-04-29
Nimble - A Matcher Framework for Swift and Objective-C 2017-04-29
MHVideoPhotoGallery - A Photo and Video Gallery 2017-04-29
shoulda-matchers - Collection of testing matchers extracted from Shou... 2017-04-29
Android-SlideExpandableListView - A better ExpandableListView, with animated expanda... 2017-04-29
AppSales-Mobile - App Sales allows iPhone and Mac App Store develope... 2017-04-29
react-templates - Light weight templates for react ... 2017-04-28
afterglow-theme - A minimal dark Theme for Sublime Text 2 and 3 2017-04-28
jwt-go - Golang implementation of JSON Web Tokens (JWT) 2017-04-28
DeerResume - Tool MarkDown online resume, online preview, edit,... 2017-04-28

 Back to top