def scc(G): """Strongly Connected Components""" G_reversed = DiGraph() for u in G.adj: for v in G.adj[u]: G_reversed.add_edge(v, u, attr=G.adj[u][v]) # topologically high to low sorted_vertices = topological_sort(G)[::-1] groups = _scc(G_reversed, sorted_vertices) return groups
def get_dfs_tree(self, property, node): if property not in self.properties: raise Exception(property + ' is not a SKOS property') di_graph = DiGraph() for s, p, o in self.vocab.triples(None, SKS[property], None): di_graph.add_edge(s, o) tree_edges = di_graph.dfs_tree(node) di_graph.clear() di_graph.add_edges_from(list(tree_edges)) # label nodes with literals for n1, n2 in di_graph.edges(): di_graph.nodes()[n1]['label'] = self.get_term_from_uri(n1) di_graph.nodes()[n2]['label'] = self.get_term_from_uri(n2) return di_graph
def adjust_route(topology, remove_file, routes, link_order): # get remove_links f = open(remove_file) links = [] line = f.readline() while line: s = line.rstrip().split(' ') s = [int(i) for i in s] ls = s[0] ld = s[1] if ld < ls: tmp = ls ls = ld ld = tmp links.append((ls,ld)) line = f.readline() f.close() # get final topology g = DiGraph() f = open(topology) line = f.readline() line = f.readline() while line: s = line.rstrip().split(' ') lls = int(s[0]) lld = int(s[1]) #g.add_nodes([int(s[0]), int(s[1])]) g.add_edge(lls, lld, link_order[lls,lld][1] / link_order[lls,lld][0]) g.add_edge(lld, lls, link_order[lls,lld][1] / link_order[lls,lld][0]) line = f.readline() f.close() def sort_path(paths): for i in range(len(paths)): for j in range(i, len(paths)): if paths[i][1] < paths[j][1]: tmp = paths[i] paths[i] = paths[j] paths[j] = tmp def calculate_cf(paths, link_order): for p in paths: cf = 100000 for i in range(len(p[0]) - 1): tmp_cf = 0 s = p[0][i] d = p[0][i+1] if s < d: tmp_cf = link_order[s,d][0] / link_order[s,d][1] else: tmp_cf = link_order[d,s][0] / link_order[d,s][1] if tmp_cf < cf: cf = tmp_cf p[1] = cf sort_path(paths) for ls,ld in links: #print(ls,ld) for s,d in routes: route_paths = routes[s,d] remove_route = [] remove_weight = 0 for path, weight in route_paths: for i in range(len(path) - 1): if (path[i] == ls and path[i+1] == ld) or (path[i] == ld and path[i+1] == ls): remove_route.append([path, weight]) remove_weight += weight # remove weight from all the link for pi in range(len(path) - 1): ps = path[pi] pd = path[pi+1] if pd < ps: tmp = pd pd = ps ps = tmp link_order[ps,pd][1] -= weight break if remove_weight != 0: # Find k-shortest-paths between s,d tmp_paths = algorithms.ksp_yen(g, s, d, 20) yen_paths = [[yen_path['path'], 0, 0] for yen_path in tmp_paths] # make sure the proportion of every yen_path steps = 10.0 gap = remove_weight / steps calculate_cf(yen_paths, link_order) for step in range(int(steps)): for yi in range(len(yen_paths[0][0]) - 1): ys = yen_paths[0][0][yi] yd = yen_paths[0][0][yi + 1] if yd < ys: tmp = ys ys = yd yd = tmp link_order[ys,yd][1] += gap yen_paths[0][2] += 1 / steps calculate_cf(yen_paths, link_order) # remain routes for rr in remove_route: route_paths.remove(rr) # merge k-s-p into routes for yen_path, flows, proportion in yen_paths: if proportion == 0: continue exist = False for rp in route_paths: if rp[0] == yen_path: rp[1] += remove_weight * proportion exist = True if not exist: route_paths.append([yen_path, remove_weight * proportion]) routes[s,d] = route_paths return routes
def main(): # Load the graph G = DiGraph("net5") # Get the painting object and set its properties. paint = G.painter() #paint.set_source_sink('ada', 'bob') paint.set_source_sink('s5', 's7') # load the graph dictionary das_ergebnis = joblib.load( "/Users/aditimiglani/Downloads/das_ergebnis.pkl") list_of_edges = das_ergebnis['edges'] list_of_nodes = das_ergebnis['nodes'] for edge in list_of_edges: source = str(edge['source']) target = str(edge['target']) load = float(edge['load']) G.add_edge(source, target, load) G.add_edge(target, source, load) #G.add_edge('ada', 'sue', 3) #G.add_edge('ada', 'john', 2) #G.add_edge('sue', 'bob', 1) #G.add_edge('a', 'b', 1) #G.add_edge('sue', 'john', 1) #G.add_edge('john', 'bob', 1) # Generate the graph using the painter we configured. G.export(False, paint) # Get 5 shortest paths from the graph. items = algorithms.ksp_yen(G, 's5', 's7', 5) all_paths = [] #items = algorithms.ksp_yen(G, 'ada', 'bob', 5) for path in items: print "Cost:%s\t%s" % (path['cost'], "->".join(path['path'])) all_paths.append(path['path']) print all_paths sub_das_ergebnis = {} sub_das_ergebnis['nodes'] = [] sub_das_ergebnis['edges'] = [] for sub_dict in list_of_nodes: for path in all_paths: for i in range(len(path)): if path[i] == sub_dict['id']: if sub_dict not in sub_das_ergebnis['nodes']: if i == 0 or i == len(path) - 1: sub_dict['root'] = True sub_das_ergebnis['nodes'].append(sub_dict) else: sub_dict['root'] = False sub_das_ergebnis['nodes'].append(sub_dict) for sub_dict in list_of_edges: for path in all_paths: for i in range(len(path)): if i < len(path) - 1: if (path[i] == sub_dict['source'] or path[i] == sub_dict['target']) and ( path[i + 1] == sub_dict['source'] or path[i + 1] == sub_dict['target']): if sub_dict not in sub_das_ergebnis['edges']: sub_das_ergebnis['edges'].append(sub_dict) joblib.dump(sub_das_ergebnis, 'sub_das_ergebnis.pkl') print sub_das_ergebnis print 'loading' loaded = joblib.load('sub_das_ergebnis.pkl') print loaded return 0
def main(): G = DiGraph() undershorts = Node("undershorts") pants = Node("pants") belt = Node("belt") shirt = Node("shirt") tie = Node("tie") jacket = Node("jacket") socks = Node("socks") shoes = Node("shoes") watch = Node("watch") G.add_edge(shirt, belt) G.add_edge(shirt, tie) G.add_edge(tie, jacket) G.add_edge(belt, jacket) G.add_edge(pants, belt) G.add_edge(undershorts, shoes) G.add_edge(undershorts, pants) G.add_edge(pants, shoes) G.add_edge(socks, shoes) G.add_edge(watch, watch) vertices_sorted = topological_sort(G) # Can have more than 1 answers print(vertices_sorted)
# interstation cost (mins) isc = 5 interrows = [] for stopA in fullStops: for stopB in fullStops: a, b = spsl(stopA) c, d = spsl(stopB) if (a == c and not (b == d)): interrows.append((stopA, stopB, isc)) GMaster = DiGraph('awesome') for stop in fullStops: GMaster.add_node(stop) for row in rows: GMaster.add_edge(row[0], row[1], row[2]) for irow in interrows: GMaster.add_edge(irow[0], irow[1], irow[2]) print "Calculate Routes..." # Holds all routes in memory to aid filtering fromToDict = {} for stopFrom in fullStops: print "Currently working on:", stopFrom stA, t = spsl(stopFrom) G = copy.deepcopy(GMaster) distances, previous = algorithms.dijkstra(G, stopFrom)
path_len = 0 for path, weight in route_paths: path_len += (len(path) - 1) * weight path_len_routes_base[(alpha, s, d)] = path_len g = DiGraph() f = open(final_topology) line = f.readline() length_graph = int(line.rstrip().split(' ')[0]) line = f.readline() while line: s = line.rstrip().split(' ') lls = int(s[0]) lld = int(s[1]) g.add_edge(lls, lld, 1) g.add_edge(lld, lls, 1) line = f.readline() f.close() for n in range(length_graph): distances = dijkstra(g, length_graph, n) for key in distances: if key == n: continue path_len_shortest[(alpha, n, key)] = distances[key] g = DiGraph() f = open(final_topology_base)
def GenerateGraph(person1,person2): person1_found = 'N' person2_found = 'N' #u'phillip.k': u's2625' print person1 print person2 try: node1 =person_node_mapping[person1.lower()] person1_found = 'Y' except: print "Person 1 Not found" node1 =u's78' #Janel Guerrero try: node2 =person_node_mapping[person2.lower()] person2_found = 'Y' except: print "Person 2 Not found" node2 = u's30018' G = DiGraph("net5") # Get the painting object and set its properties. paint = G.painter() #paint.set_source_sink('ada', 'bob') paint.set_source_sink(node1, node2) # load the graph dictionary list_of_edges = das_ergebnis['edges'] list_of_nodes = das_ergebnis['nodes'] for edge in list_of_edges: source = str(edge['source']) target = str(edge['target']) load = float(edge['load']) G.add_edge(source, target, load) G.add_edge(target, source, load) #G.add_edge('ada', 'sue', 3) #G.add_edge('ada', 'john', 2) #G.add_edge('sue', 'bob', 1) #G.add_edge('a', 'b', 1) #G.add_edge('sue', 'john', 1) #G.add_edge('john', 'bob', 1) # Generate the graph using the painter we configured. #G.export(False, paint) # Get 5 shortest paths from the graph. items = algorithms.ksp_yen(G, node1, node2, 5) all_paths = [] #items = algorithms.ksp_yen(G, 'ada', 'bob', 5) for path in items: print "Cost:%s\t%s" % (path['cost'], "->".join(path['path'])) all_paths.append(path['path']) print all_paths sub_das_ergebnis = {} sub_das_ergebnis['nodes'] = [] sub_das_ergebnis['edges'] = [] for sub_dict in list_of_nodes: for path in all_paths: for i in range(len(path)): if path[i] == sub_dict['id']: if sub_dict not in sub_das_ergebnis['nodes']: if i == 0 or i == len(path) - 1: sub_dict['root'] = True sub_das_ergebnis['nodes'].append(sub_dict) else: sub_dict['root'] = False sub_das_ergebnis['nodes'].append(sub_dict) for sub_dict in list_of_edges: for path in all_paths: for i in range(len(path)): if i < len(path)-1: if (path[i] == sub_dict['source'] or path[i] == sub_dict['target']) and (path[i+1] == sub_dict['source'] or path[i+1] == sub_dict['target']): if sub_dict not in sub_das_ergebnis['edges']: sub_das_ergebnis['edges'].append(sub_dict) #joblib.dump(sub_das_ergebnis, 'sub_das_ergebnis.pkl') nodes =sub_das_ergebnis['nodes'] edges =sub_das_ergebnis['edges'] node_list = [] count =0 for node in nodes: if node['caption'] in enron_table: word = enron_table[node['caption']][1] org = enron_table[node['caption']][0] node_list.append([word,org,node['id']]) count+=1 elif node['caption'] in other_org_table: word = other_org_table[node['caption']][1] org = other_org_table[node['caption']][0] node_list.append([word,org,node['id']]) count+=1 else: continue edge_list = [] connection_exist =[] for edge in edges: source = edge['source'] target = edge['target'] value = edge['load'] value = value/4 value1 = int(value*10) value2 = int(value*200) connection_exist.append(edge['source']) connection_exist.append(edge['target']) edge_list.append([edge['source'],edge['target'],value1,value2]) F = open("WorldCup2014.js","w") F.write("var nodes = [") nodes =[] for node in node_list: num = 1 if node[0] ==person1: num = 2 if node[0] ==person2: num = 3 string = "{id: " + str(node[2][1:]) + ", label: '" + str(node[0].title()) + "', value: " + str(20) +", group: " + str(num)+"}," nodes.append(string) #print string F.write(string) F.write("];") F.write("var edges = [") edges =[] for edge in edge_list: string = "{from: %s, to: %s, value: %s, title: 'Relationship score: %s'}," %(edge[0][1:],edge[1][1:],edge[2],edge[3]) edges.append(string) F.write(string) F.write("];") F.close() return nodes,edges
def adjust_route(topology, remove_file, routes, link_order): # get remove_links f = open(remove_file) links = [] line = f.readline() while line: s = line.rstrip().split(' ') s = [int(i) for i in s] ls = s[0] ld = s[1] if ld < ls: tmp = ls ls = ld ld = tmp links.append((ls, ld)) line = f.readline() f.close() # get final topology g = DiGraph() f = open(topology) line = f.readline() line = f.readline() while line: s = line.rstrip().split(' ') lls = int(s[0]) lld = int(s[1]) #g.add_nodes([int(s[0]), int(s[1])]) g.add_edge(lls, lld, link_order[lls, lld][1] / link_order[lls, lld][0]) g.add_edge(lld, lls, link_order[lls, lld][1] / link_order[lls, lld][0]) line = f.readline() f.close() def sort_path(paths): for i in range(len(paths)): for j in range(i, len(paths)): if paths[i][1] < paths[j][1]: tmp = paths[i] paths[i] = paths[j] paths[j] = tmp def calculate_cf(paths, link_order): for p in paths: cf = 100000 for i in range(len(p[0]) - 1): tmp_cf = 0 s = p[0][i] d = p[0][i + 1] if s < d: tmp_cf = link_order[s, d][0] / link_order[s, d][1] else: tmp_cf = link_order[d, s][0] / link_order[d, s][1] if tmp_cf < cf: cf = tmp_cf p[1] = cf sort_path(paths) for ls, ld in links: #print(ls,ld) for s, d in routes: route_paths = routes[s, d] remove_route = [] remove_weight = 0 for path, weight in route_paths: for i in range(len(path) - 1): if (path[i] == ls and path[i + 1] == ld) or (path[i] == ld and path[i + 1] == ls): remove_route.append([path, weight]) remove_weight += weight # remove weight from all the link for pi in range(len(path) - 1): ps = path[pi] pd = path[pi + 1] if pd < ps: tmp = pd pd = ps ps = tmp link_order[ps, pd][1] -= weight break if remove_weight != 0: # Find k-shortest-paths between s,d tmp_paths = algorithms.ksp_yen(g, s, d, 20) yen_paths = [[yen_path['path'], 0, 0] for yen_path in tmp_paths] # make sure the proportion of every yen_path steps = 10.0 gap = remove_weight / steps calculate_cf(yen_paths, link_order) for step in range(int(steps)): for yi in range(len(yen_paths[0][0]) - 1): ys = yen_paths[0][0][yi] yd = yen_paths[0][0][yi + 1] if yd < ys: tmp = ys ys = yd yd = tmp link_order[ys, yd][1] += gap yen_paths[0][2] += 1 / steps calculate_cf(yen_paths, link_order) # remain routes for rr in remove_route: route_paths.remove(rr) # merge k-s-p into routes for yen_path, flows, proportion in yen_paths: if proportion == 0: continue exist = False for rp in route_paths: if rp[0] == yen_path: rp[1] += remove_weight * proportion exist = True if not exist: route_paths.append( [yen_path, remove_weight * proportion]) routes[s, d] = route_paths return routes
def test_dag_shortest_path(): G = DiGraph() r = Node("r") s = Node("s") t = Node("t") x = Node("x") y = Node("y") z = Node("z") G.add_edge(r, s, w=5) G.add_edge(r, t, w=3) G.add_edge(s, x, w=6) G.add_edge(s, t, w=2) G.add_edge(t, x, w=7) G.add_edge(t, y, w=4) G.add_edge(t, z, w=2) G.add_edge(x, y, w=-1) G.add_edge(x, z, w=1) G.add_edge(y, z, w=-2) dag_shortest_path(G, s) assert [u.d for u in G.node] == [float('inf'), 0, 2, 6, 5, 3]
def test_bellman_ford(): G = DiGraph() s = Node("s") t = Node("t") y = Node("y") x = Node("x") z = Node("z") G.add_edge(s, t, w=6) G.add_edge(s, y, w=7) G.add_edge(t, y, w=8) G.add_edge(t, x, w=5) G.add_edge(t, z, w=-4) G.add_edge(x, t, w=-2) G.add_edge(y, x, w=-3) G.add_edge(y, z, w=9) G.add_edge(z, x, w=7) G.add_edge(z, s, w=2) assert bellman_ford(G, s)
def test_dijkstra(): G = DiGraph() s = Node("s") t = Node("t") y = Node("y") x = Node("x") z = Node("z") G.add_edge(s, t, w=10) G.add_edge(s, y, w=5) G.add_edge(t, y, w=2) G.add_edge(y, t, w=3) G.add_edge(t, x, w=1) G.add_edge(y, x, w=9) G.add_edge(y, z, w=2) G.add_edge(z, s, w=7) G.add_edge(z, x, w=6) G.add_edge(x, z, w=4) dijkstra(G, s) assert [u.d for u in G.node] == [0, 8, 5, 9, 7]
path_len = 0 for path, weight in route_paths: path_len += (len(path) - 1) * weight path_len_routes_base[(alpha, s, d)] = path_len g = DiGraph() f = open(final_topology) line = f.readline() length_graph = int(line.rstrip().split(' ')[0]) line = f.readline() while line: s = line.rstrip().split(' ') lls = int(s[0]) lld = int(s[1]) g.add_edge(lls, lld, 1); g.add_edge(lld, lls, 1); line = f.readline() f.close() for n in range(length_graph): distances = dijkstra(g, length_graph, n) for key in distances: if key == n: continue path_len_shortest[(alpha, n, key)] = distances[key] g = DiGraph() f = open(final_topology_base)
def main(): G = DiGraph() a = Node("a") b = Node("b") c = Node("c") d = Node("d") e = Node("e") f = Node("f") g = Node("g") h = Node("h") G.add_edge(a, b) G.add_edge(e, a) G.add_edge(b, e) G.add_edge(b, f) G.add_edge(b, c) G.add_edge(e, f) G.add_edge(f, g) G.add_edge(g, f) G.add_edge(c, g) G.add_edge(c, d) G.add_edge(d, c) G.add_edge(d, h) G.add_edge(g, h) G.add_edge(h, h) groups = scc(G) print(groups)