in_q = set([source]) while q: u = q.popleft() in_q.remove(u) # Skip relaxations if any of the predecessors of u is in the queue. if all(pred_u not in in_q for pred_u in pred[u]): dist_u = dist[u] for v, e in dict(G.adjacency())[u].items(): dist_v = dist_u + G_succ[u][v]['weight'] if dist_v > dist.get(v, inf): if v not in in_q: q.append(v) in_q.add(v) count_v = count.get(v, 0) + 1 count[v] = count_v dist[v] = dist_v pred[v] = [u] elif dist.get(v) is not None and dist_v == dist.get(v): pred[v].append(u) return backtrace(pred, source, target) if __name__ == '__main__': fnames = os.listdir('data') G = load('data/%s' % fnames[0]) print("Applying Bellmanford Algorithm") print(bellman_ford(G, 0, len(G.nodes()) - 1))
def parse_file(x: str): return int(x[x.find('_') + 1:x.find('.')]) fnames = os.listdir('data') D_V = dict() B_V = dict() T_V = dict() MB_V = dict() for fname in fnames: print("loading %s" % fname) G = load('data/%s' % fname) G_neg = load('data/%s' % fname, True) print("Applying Modified Topological Sort Algorithm for %s" % fname) start_time = time.time() shortest_path_T = topological_sort(G_neg, topo_order=nx.topological_sort(G))[0] elapsed_time = time.time() - start_time T_V[parse_file(fname)] = float(elapsed_time) print("Applying Modified Bellman Ford Algorithm for %s" % fname) start_time = time.time() shortest_path_B = nx.bellman_ford_path(G_neg, 0, len(G.nodes()) - 1) elapsed_time = time.time() - start_time MB_V[parse_file(fname)] = float(elapsed_time)
from Generator.DAG_Generator import load import os def parse_file(x: str): return int(x[x.find('_') + 1:x.find('.')]) fnames = os.listdir('data') F_V = dict() MF_V = dict() for fname in fnames: print("loading %s" % fname) G = load('data/%s' % fname) for u, v in G.edges(): w = Fuzzy.get_fuzzy_weight() G[u][v]['weight'] = w G[u][v]['fuzzy'] = w print("Applying Fuzzy Algorithm for %s" % fname) start_time = time.time() shortest_path_T = Fuzzy.Fuzzy_CPM(G) elapsed_time = time.time() - start_time print(elapsed_time) F_V[parse_file(fname)] = float(elapsed_time) print("Applying Modified Fuzzy Algorithm for %s" % fname) start_time = time.time() shortest_path_T = Fuzzy.modified_Fuzzy(G)
visited = set() # prioritize nodes from start -> node with the shortest distance! ## elements stored as tuples (distance, node) pq = PriorityQueue() # dist[start] = 0 # dist from start -> start is zero pq.put((dist[start], start)) while 0 != pq.qsize(): curr_cost, curr = pq.get() visited.add(curr) # look at curr's adjacent nodes for neighbor in dict(rev.adjacency()).get(curr): # if we found a shorter path path = max(0, dist[curr] - cost(curr, neighbor)) if path < dist[neighbor]: # update the distance, we found a shorter one! dist[neighbor] = path # update the previous node to be prev on new shortest path prev[neighbor] = curr pq.put((dist[neighbor], neighbor)) return backtrace(prev, start, end), dist if __name__ == "__main__": fnames = os.listdir('data') G = load('data/%s' % fnames[0]) Gi = load('data/%s' % fnames[0], True) print("Applying Dijkstra Algorithm") print(dijkstra(G, 0, len(G.nodes()) - 1))