def dict_graph_to_python_graph(d): g = pg.digraph() g.add_nodes(d.keys()) for u, edges in d.iteritems(): for edge in edges: e_v, e_c = edge g.add_edge((u, e_v), wt=e_c) return g
def __init__(self, graph): self.flow = col.defaultdict(int) self.graph = graph self.original_graph = pg.digraph() self.original_graph.add_nodes(self.graph.nodes()) for edge in self.graph.edges(): self.original_graph.add_edge(edge, wt=self.graph.edge_weight(edge))
def test_directed_connected_components(self): digr = digraph() digr.add_nodes(["a", "b", "c", "d", "e", "f", "g", "h", "i"]) digr.add_edges([("b", "a"), ("a", "c"), ("c", "b"), ("d", "b")]) digr.add_edges([("d", "f"), ("f", "e"), ("e", "d"), ("g", "e")]) digr.add_edges([("g", "h"), ("h", "i"), ("i", "g")]) self.assertEqual(len(directed_connected_components(digr)), 3) digr2 = digraph() digr2.add_nodes( ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]) digr2.add_edges([("a", "b"), ("b", "c"), ("c", "a"), ("b", "d"), ("d", "e")]) digr2.add_edges([("e", "f"), ("f", "g"), ("g", "e"), ("d", "g"), ("i", "f")]) digr2.add_edges([("h", "g"), ("c", "h"), ("c", "k"), ("h", "i"), ("i", "j")]) digr2.add_edges([("h", "j"), ("j", "k"), ("k", "h")]) self.assertEqual(len(directed_connected_components(digr2)), 4)
def test_topological_ordering(self): dag = digraph() # directed acyclic graph dag.add_nodes(["a", "b", "c", "d", "e", "f", "g", "h"]) dag.add_edges([("a", "b"), ("a", "c"), ("a", "e"), ("d", "a")]) dag.add_edges([("g", "b"), ("g", "f"), ("f", "e"), ("h", "f"), ("h", "a")]) order = {o[0]: o[1] for o in topological_ordering(dag)} self.assertEqual(sum([order[u] < order[v] for (u, v) in dag.edges()]), len(dag.edges())) # all comparisons are True
def test_topological_ordering(self): dag = digraph() # directed acyclic graph dag.add_nodes(["a", "b", "c", "d", "e", "f", "g", "h"]) dag.add_edges([("a", "b"), ("a", "c"), ("a", "e"), ("d", "a")]) dag.add_edges( [("g", "b"), ("g", "f"), ("f", "e"), ("h", "f"), ("h", "a")]) order = {o[0]: o[1] for o in topological_ordering(dag)} self.assertEqual(sum([order[u] < order[v] for (u, v) in dag.edges()]), len(dag.edges())) # all comparisons are True
def test_directed_connected_components(self): digr = digraph() digr.add_nodes(["a", "b", "c", "d", "e", "f", "g", "h", "i"]) digr.add_edges([("b", "a"), ("a", "c"), ("c", "b"), ("d", "b")]) digr.add_edges([("d", "f"), ("f", "e"), ("e", "d"), ("g", "e")]) digr.add_edges([("g", "h"), ("h", "i"), ("i", "g")]) self.assertEqual(len(directed_connected_components(digr)), 3) digr2 = digraph() digr2.add_nodes( ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]) digr2.add_edges( [("a", "b"), ("b", "c"), ("c", "a"), ("b", "d"), ("d", "e")]) digr2.add_edges( [("e", "f"), ("f", "g"), ("g", "e"), ("d", "g"), ("i", "f")]) digr2.add_edges( [("h", "g"), ("c", "h"), ("c", "k"), ("h", "i"), ("i", "j")]) digr2.add_edges([("h", "j"), ("j", "k"), ("k", "h")]) self.assertEqual(len(directed_connected_components(digr2)), 4)
def create_graph(infile_name): infile = open(infile_name, "r") g = pg.digraph() for line in infile: u, edges = extract_key_value(line) if not g.has_node(str(u)): g.add_node(str(u)) for v, e_c in edges: if not g.has_node(str(v)): g.add_node(str(v)) g.add_edge((str(u),str(v)), wt=e_c) return g
def create_graph(infile_name): infile = open(infile_name, "r") g = pg.digraph() for line in infile: u, edges = extract_key_value(line) if not g.has_node(str(u)): g.add_node(str(u)) for v, e_c in edges: if not g.has_node(str(v)): g.add_node(str(v)) g.add_edge((str(u), str(v)), wt=e_c) return g
def file_to_graph(graph_file_path): graph_file = open(graph_file_path, "r") g = pg.digraph() for line in graph_file: u, edges = extract_key_value(line) if not g.has_node(u): g.add_node(u) for edge in edges: v, e_c = edge if not g.has_node(v): g.add_node(v) g.add_edge((u, v), wt=e_c) return g
def setUp(self): self.gr = graph() self.gr.add_nodes(["s", "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l"]) self.gr.add_edges([("s", "a"), ("s", "b"), ("a", "c"), ("c", "e")]) self.gr.add_edges([("e", "d"), ("d", "b"), ("a", "b"), ("c", "d")]) self.gr.add_edges([("g", "h"), ("f", "g")]) self.gr.add_edges([("j", "k"), ("j", "l")]) self.digr = digraph() self.digr.add_nodes(['s', 'a', 'b', 'c', 'd', 'e', 'f']) self.digr.add_edges([("s", "a"), ("a", "b"), ("b", "a"), ("c", "b")]) self.digr.add_edges([("b", "s"), ("s", "d"), ("d", "e"), ("e", "d")]) self.digr.add_edges([("b", "f"), ("e", "f")])
def setUp(self): self.gr = graph() self.gr.add_nodes( ["s", "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l"]) self.gr.add_edges([("s", "a"), ("s", "b"), ("a", "c"), ("c", "e")]) self.gr.add_edges([("e", "d"), ("d", "b"), ("a", "b"), ("c", "d")]) self.gr.add_edges([("g", "h"), ("f", "g")]) self.gr.add_edges([("j", "k"), ("j", "l")]) self.digr = digraph() self.digr.add_nodes(['s', 'a', 'b', 'c', 'd', 'e', 'f']) self.digr.add_edges([("s", "a"), ("a", "b"), ("b", "a"), ("c", "b")]) self.digr.add_edges([("b", "s"), ("s", "d"), ("d", "e"), ("e", "d")]) self.digr.add_edges([("b", "f"), ("e", "f")])
def __init__(self): self.printDebug("[->] batchHandler.__init__()") self.db_parent_batchid = None self.runID = None self.parentTask = "" self.currentTask = "" self.status = "DNE" self.hasRun = False self.hasCallback = False self.theCallback = None self.batchFileName = "" self.batchFilePath = "" self.batchFileTree = None self.testQueue = Queue.Queue() self.testGraph = digraph.digraph() ##self.debug_on() self.printDebug("[<-] batchHandler.__init__()")
def test_shortest_path_in_directed_graph(self): digr = digraph() digr.add_nodes(["a", "b", "c", "d", "e", "f"]) digr.add_edge(("a", "b"), 7) digr.add_edge(("a", "c"), 9) digr.add_edge(("a", "f"), 14) digr.add_edge(("f", "e"), 9) digr.add_edge(("c", "f"), 2) digr.add_edge(("c", "d"), 11) digr.add_edge(("b", "c"), 10) digr.add_edge(("b", "d"), 15) digr.add_edge(("d", "e"), 6) self.assertEqual(shortest_path(digr, "a")["a"], 0) self.assertEqual(shortest_path(digr, "a")["b"], 7) self.assertEqual(shortest_path(digr, "a")["c"], 9) self.assertEqual(shortest_path(digr, "a")["d"], 20) self.assertEqual(shortest_path(digr, "a")["e"], 20) self.assertEqual(shortest_path(digr, "a")["f"], 11)
def create_graph(infile_name): infile = open(infile_name, "r") g = pg.digraph() # extracts the nodes and edges encoded in each line and # inserts them into the graph g for line in infile: u, edges = extract_key_value(line) if not g.has_node(u): g.add_node(u) for v, e_c in edges: if not g.has_node(v): g.add_node(v) g.add_edge((u, v), wt=e_c) infile.close() return g
def create_graph(infile_name): infile = open(infile_name, "r") g = pg.digraph() # extracts the nodes and edges encoded in each line and # inserts them into the graph g for line in infile: u, edges = extract_key_value(line) if not g.has_node(u): g.add_node(u) for v, e_c in edges: if not g.has_node(v): g.add_node(v) g.add_edge((u,v), wt=e_c) infile.close() return g
def augment_graph(graph, augmented_edges): # copy graph g = pg.digraph() g.add_nodes(graph.nodes()) for edge in graph.edges(): g.add_edge(edge, wt=graph.edge_weight(edge)) # update edge weights based on augmentation for u in g.nodes(): for v in g.neighbors(u): e_id = "{0},{1}".format(u, v) if e_id in augmented_edges: flow = augmented_edges[e_id] # update forward edge f_e = (u, v) residue = g.edge_weight(f_e) - flow g.set_edge_weight(f_e, residue) if residue < 0: print "Fatal error: negative edge residue" sys.exit(-1) # update back edge r_id = "{0},{1}".format(v, u) b_e = (v, u) if g.has_edge(b_e): new_weight = g.edge_weight(b_e) + flow g.set_edge_weight(b_e, new_weight) else: g.add_edge(b_e, wt=flow) # remove edges with zero or less capacity for edge in g.edges(): if g.edge_weight(edge) == 0: g.del_edge(edge) return g
from mpi4py import MPI import numpy as np import time import collections as col import digraph as pg from searching import depth_first_search from find import find if __name__ == '__main__': comm = MPI.COMM_WORLD rank = comm.Get_rank() root_graph = None if rank == 0: root_graph = pg.digraph() root_graph.add_nodes(["s", "t"]) root_graph.add_edge(("s", "t")) root_graph = {} root_graph[1] = 2 graph = comm.bcast(root_graph, root=0) print "{0}: {1}".format(rank, graph)
def setUp(self): self.gr = digraph() self.gr.add_nodes(["a", "b", "c", "d", "e", "f"]) self.gr.add_edges([("a", "b"), ("b", "c"), ("a", "d"), ("d", "e"), ("d", "f")]) self.gr.add_edge(("f", "b"))
# new_paths = dijkstra_queue(new_digraph) # filename = 'dijkstra_queue.npy' # vertices_paths = np.load(filename) # for vertice in vertices_paths: # print('Start Vertice: ' + vertice['start_vertice'].name) # for path in vertice['shortest_paths']: # print('End Vertice: ' + path['end_vertice'].name) # print('Distance: ' + int(path['distance'])) # pa = '' # for p in path['path']: # pa+=p.name+' ' # print('Shortest Path: ' + pa) new_list = digraph(4, 50) vertices = new_list.get_vertices() for vertice in vertices: print('Vertice name: ' + vertice.name) for linked_vertice in vertice.linked_vertices: print('Linked Vertice Name: ' + linked_vertice['vertice'].name) print('Length: ' + str(linked_vertice['length'])) print('\n') start = raw_input("Start: ") dfs = dfs(vertices, start) bfs = bfs(vertices, start)
def cut_tree(igraph, caps = None): """ Construct a Gomory-Hu cut tree by applying the algorithm of Gusfield. @type igraph: graph @param igraph: Graph @type caps: dictionary @param caps: Dictionary specifying a maximum capacity for each edge. If not given, the weight of the edge will be used as its capacity. Otherwise, for each edge (a,b), caps[(a,b)] should be given. @rtype: dictionary @return: Gomory-Hu cut tree as a dictionary, where each edge is associated with its weight """ #maximum flow needs a digraph, we get a graph #I think this conversion relies on implementation details outside the api and may break in the future graph = digraph() graph.add_graph(igraph) #handle optional argument if not caps: caps = {} for edge in graph.edges(): caps[edge] = igraph.edge_weight(edge) #temporary flow variable f = {} #we use a numbering of the nodes for easier handling n = {} N = 0 for node in graph.nodes(): n[N] = node N = N + 1 #predecessor function p = {}.fromkeys(range(N),0) p[0] = None for s in range(1,N): t = p[s] S = [] #max flow calculation (flow,cut) = maximum_flow(graph,n[s],n[t],caps) for i in range(N): if cut[n[i]] == 0: S.append(i) value = cut_value(graph,flow,cut) f[s] = value for i in range(N): if i == s: continue if i in S and p[i] == t: p[i] = s if p[t] in S: p[s] = p[t] p[t] = s f[s] = f[t] f[t] = value #cut tree is a dictionary, where each edge is associated with its weight b = {} for i in range(1,N): b[(n[i],n[p[i]])] = f[i] return b
def copy_graph(graph): new_graph = pg.digraph() new_graph.add_nodes(graph.nodes()) for edge in graph.edges(): new_graph.add_edge(edge, wt=graph.edge_weight(edge)) return new_graph