def dijkstra(gr, s): dist = {} paths = {} for u in gr.getNodes(): dist[u.name] = sys.maxint paths[u.name] = pathlist.pathlist() #dist[s] = 0 nodeQ = gr.getNodes() nodeQ.remove(gr.getNode(s)) print nodeQ while nodeQ: #nd = min(dist,key = lambda m: dist.get(m)) nd = getNodeWithMinDist(dist) print nd u = gr.getNode(nd) print u nodeQ.remove(u) for v in u.getNeighbours(): alt = dist[u.name] + gr.operations.getEdgeBetween(u.name, v).weight if alt < dist[v]: dist[v] = alt if not paths[u].getPaths(): p = path.path([v]) paths[v].addPath(p) for p in paths[u].getPaths(): q = path.path(p.append(v)) paths[v] = paths[v].addPath(q) elif alt == dist[v]: p = path.path(paths[u.name].append(v)) paths[v] = paths[v].addPath(p) return dist
def bfsBasedShortestPaths(gr, nd, allsp = False): paths = {} bfsqueue = [] for v in gr.getNodes(): paths[v.name] = pathlist.pathlist() bfsqueue.append(nd) paths[nd].addPath(path.path([nd])) while bfsqueue: u = bfsqueue.pop(0) u = gr.getNode(u) u.visited = True for v in u.getNeighbours(): v = gr.getNode(v) if not v.visited: if not allsp: v.visited = True plen = sys.maxint if paths[v.name].getPathList(): plen = paths[v.name].getPath().getPathLength() for p in paths[u.name].getPathList(): if p.getPathLength() >= plen: break q = [] for n in p.getPathString().split(): q.append(n) q.append(v.name) paths[v.name].addPath(path.path(q)) if v.name not in bfsqueue: bfsqueue.append(v.name) paths[nd].reset() del paths[nd] gr.resetNodeVisitedFlags() return paths
def bellmanFord(gr, s, sponly = True, allsp = False): dist = {} paths = {} for u in gr.getNodeList(): dist[u] = sys.maxint/2 paths[u] = pathlist.pathlist() dist[s] = 0 paths[s].addPath(path.path([s], weight = dist[s])) nodes = gr.getNodes() edges = gr.getEdges() for i in range(gr.getNumNodes()): for ed in edges: u = ed.head v = ed.tail if sponly == False: altPaths(v, paths[u], paths[v]) if not gr.directed: u = ed.tail v = ed.head altPaths(v, paths[u], paths[v]) else: alt = dist[u] + ed.weight if alt < dist[v]: relaxEdge(alt, v, dist, paths[u], paths[v]) if allsp: if alt == dist[v]: altPaths(v, paths[u], paths[v]) if not gr.directed: u = ed.tail v = ed.head alt = dist[u] + ed.weight if alt < dist[v]: relaxEdge(alt, v, dist, paths[u], paths[v]) if allsp: if alt == dist[v]: altPaths(v, paths[u], paths[v]) paths[s].reset() del paths[s] if sponly: for ed in edges: u = ed.head v = ed.tail if not gr.directed: if dist[v] > dist[u] + ed.weight and dist[u] > dist[v] + ed.weight: print "Error: Graph contains a negative cycle!" else: if dist[v] > dist[u] + ed.weight: print ed.name print "Error: Graph contains a negative cycle!" return paths
def __loadXml(self, in_buildXmlFilePath): try: document = xml.dom.minidom.parse(in_buildXmlFilePath) except: self.logError( "load document failed " + in_buildXmlFilePath + " " + str(sys.exc_value), True) return self.logMessage("") self.__pathList = pathlist.pathlist(document, self) self.__builderList = builderlist.builderlist(document, self) self.__actionList = actionlist.actionlist(document, self)
def getAPSP(gr, allsp = False): apsp = {} nodes = gr.getNodeList() for nd in nodes: apsp[nd] = pathlist.pathlist() paths = None if not gr.weighted: for nd in nodes: paths = bfsbased.bfsBasedShortestPaths(gr, nd, allsp = False) for v in paths.values(): for p in v.getPathList(): apsp[nd].addPath(p) else: for nd in nodes: paths = bellman_ford.bellmanFord(gr, nd, allsp = False) for v in paths.values(): for p in v.getPathList(): apsp[nd].addPath(p) #apsp[nd] = paths return apsp