spath.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Dijkstra's algorithm for shortest paths
  2. # David Eppstein, UC Irvine, 4 April 2002
  3. # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/117228
  4. from utils.priodict import priorityDictionary
  5. def Dijkstra(G,start,end=None):
  6. """
  7. Find shortest paths from the start vertex to all vertices nearer than or equal to the end.
  8. The input graph G is assumed to have the following representation:
  9. A vertex can be any object that can be used as an index into a dictionary.
  10. G is a dictionary, indexed by vertices. For any vertex v, G[v] is itself a dictionary,
  11. indexed by the neighbors of v. For any edge v->w, G[v][w] is the length of the edge.
  12. This is related to the representation in <http://www.python.org/doc/essays/graphs.html>
  13. where Guido van Rossum suggests representing graphs as dictionaries mapping vertices
  14. to lists of outgoing edges, however dictionaries of edges have many advantages over lists:
  15. they can store extra information (here, the lengths), they support fast existence tests,
  16. and they allow easy modification of the graph structure by edge insertion and removal.
  17. Such modifications are not needed here but are important in many other graph algorithms.
  18. Since dictionaries obey iterator protocol, a graph represented as described here could
  19. be handed without modification to an algorithm expecting Guido's graph representation.
  20. Of course, G and G[v] need not be actual Python dict objects, they can be any other
  21. type of object that obeys dict protocol, for instance one could use a wrapper in which vertices
  22. are URLs of web pages and a call to G[v] loads the web page and finds its outgoing links.
  23. The output is a pair (D,P) where D[v] is the distance from start to v and P[v] is the
  24. predecessor of v along the shortest path from s to v.
  25. Dijkstra's algorithm is only guaranteed to work correctly when all edge lengths are positive.
  26. This code does not verify this property for all edges (only the edges examined until the end
  27. vertex is reached), but will correctly compute shortest paths even for some graphs with negative
  28. edges, and will raise an exception if it discovers that a negative edge has caused it to make a mistake.
  29. """
  30. D = {} # dictionary of final distances
  31. P = {} # dictionary of predecessors
  32. Q = priorityDictionary() # estimated distances of non-final vertices
  33. Q[start] = 0
  34. for v in Q:
  35. D[v] = Q[v]
  36. if v == end: break
  37. for w in G[v]:
  38. vwLength = D[v] + G[v][w]
  39. if w in D:
  40. if vwLength < D[w]:
  41. raise ValueError("Dijkstra: found better path to already-final vertex")
  42. elif w not in Q or vwLength < Q[w]:
  43. Q[w] = vwLength
  44. P[w] = v
  45. return (D,P)
  46. def shortestPath(G,start,end):
  47. """
  48. Find a single shortest path from the given start vertex to the given end vertex.
  49. The input has the same conventions as Dijkstra().
  50. The output is a list of the vertices in order along the shortest path.
  51. """
  52. D,P = Dijkstra(G,start,end)
  53. Path = []
  54. while 1:
  55. Path.append(end)
  56. if end == start: break
  57. end = P[end]
  58. Path.reverse()
  59. return Path
  60. class Graph:
  61. def __init__(self):
  62. self.graph = {}
  63. def add_vertex(self, vertex):
  64. self.graph[vertex] = {}
  65. def del_vertex(self, vertex):
  66. del self.graph[vertex]
  67. def is_vertex(self, vertex):
  68. if vertex in self.graph:
  69. return True
  70. else:
  71. return False
  72. def add_edge(self, vertex_start, vertex_end, weight, data):
  73. self.graph[vertex_start][vertex_end] = weight
  74. def del_edge(self, vertex_start, vertex_end):
  75. del self.graph[vertex_start][vertex_end]
  76. def is_edge(self, vertex_start, vertex_end):
  77. if vertex_start in self.graph and vertex_end in self.graph[vertex_start]:
  78. return True
  79. else:
  80. return False
  81. def get_graph(self):
  82. return self.graph
  83. def __getitem__(self, key):
  84. return self.graph.get(key, {})
  85. def __len__(self):
  86. return len(self.graph)
  87. # example, CLR p.528
  88. # G = {'s': {'u':10, 'x':5},
  89. # 'u': {'v':1, 'x':2},
  90. # 'v': {'y':4},
  91. # 'x':{'u':3,'v':9,'y':2},
  92. # 'y':{'s':7,'v':6}}
  93. #
  94. # print Dijkstra(G,'s')
  95. # print shortestPath(G,'s','v')