def main(): """Main entry point""" args = basic_parser().parse_args() if VERBOSE: print(args.instances) for instance in args.instances: graph = None with open(instance, 'r') as instance_file: graph = Graph(instance_file) graph.name = os.path.splitext(os.path.basename(instance))[0] if VERBOSE: print('-' * 100) print('File: {name}.txt'.format(name=graph.name)) start = time.time() S = iterated_local_search(graph, args.max_iter, args.time_limit, args.exclude_ls) elapsed = time.time() - start if VERBOSE: if S is None: print('! NO SOLUTION FOUND: NO SATISFYING INITIAL !') else: print('O* = {o}'.format(o=IlsObjective()(graph, S, None))) print('All served?', S.all_served(graph.customer_number)) print('Everything satisfied?', satisfies_all_constraints(graph, S)) print('----- PERFORMANCE -----') print('ILS took {some} seconds'.format(some=elapsed)) # visualize(S) print('-' * 100) if S is not None and not args.no_sol: filedir = os.path.dirname(os.path.abspath(__file__)) generate_sol(graph, S, cwd=filedir, prefix='_ils_') return 0
def test(): uno = Node("uno") dos = Node("dos") tres = Node("tres") quatro = Node("quatro") r = Relation("R", directed = True) s = Relation("S", directed = True) t = Relation("T", directed = True) r.add_node(uno) r.add_node(dos) r.add_node(quatro) r.add_edge((uno, dos)) r.add_edge((quatro, dos)) s.add_node(dos) s.add_node(tres) s.add_edge((dos, tres)) t.add_node(tres) t.add_node(quatro) t.add_node(uno) t.add_edge((tres, quatro)) t.add_edge((quatro, uno)) g = Graph("graph", [uno, dos, tres, quatro], [r, s, t]) g.print_all() pr_0 = Rule("pr_0", ["x", "y"], [(r, ("x", "y"))]) pr_1 = Rule("pr_1", ["x", "y"], [(s, ("x", "y"))]) pr_2 = Rule("pr_2", ["x", "y"], [(t, ("x", "y"))]) goal = Rule("Goal", ["x", "y"], [(pr_0, ("x","w")), (pr_1, ("w","z")), (pr_2, ("z","y"))], goal=True) rules = [pr_0, pr_1, pr_2, goal] print("Rules:") for r in rules: print("\t* {}".format(r)) print("") solutions = g.resolve(rules) if len(solutions) != 0: print("Solutions:") for s in solutions: print("\t* ({0} - {1})".format(s[0].id, s[1].id)) else: print("No solution!")
class TestExample1(unittest.TestCase): a, b, c, d = [Vertex(l) for l in 'ABCD'] edges = [ Edge(a, b, 1), Edge(a, c, 1), Edge(c, a, 1), Edge(b, c, 1), Edge(d, c, 1), ] g = Graph([a,b,c,d], edges) def test_graph_connection_cache(self): self.assertEqual(self.g.inV[self.a], set([(self.c, 1)])) self.assertEqual(self.g.inV[self.b], set([(self.a, 1)])) self.assertEqual(self.g.inV[self.c], set([(self.a, 1), (self.b, 1), (self.d, 1)])) self.assertEqual(self.g.inV[self.d], set()) self.assertEqual(self.g.outV[self.a], set([(self.b, 1), (self.c, 1)])) self.assertEqual(self.g.outV[self.b], set([(self.c, 1)])) self.assertEqual(self.g.outV[self.c], set([(self.a, 1)])) self.assertEqual(self.g.outV[self.d], set([(self.c, 1)])) def test_pagerank(self): pr_result = self.g.pagerank() self.assertAlmostEqual(pr_result[self.a], 1.49, places=2) self.assertAlmostEqual(pr_result[self.b], 0.78, places=2) self.assertAlmostEqual(pr_result[self.c], 1.58, places=2) self.assertAlmostEqual(pr_result[self.d], 0.15, places=2)
class TestExample2(unittest.TestCase): home = Vertex('Home') about = Vertex('About') product = Vertex('Product') links = Vertex('Links') a = Vertex('External Site A') b = Vertex('External Site B') c = Vertex('External Site C') d = Vertex('External Site D') edges = [ Edge(home, about, 1), Edge(about, home, 1), Edge(home, product, 1), Edge(product, home, 1), Edge(home, links, 1), Edge(links, home, 1), Edge(links, a, 1), Edge(links, b, 1), Edge(links, c, 1), Edge(links, d, 1), ] g = Graph([home, about, product, links, a, b, c, d], edges) def test_pagerank(self): pr_result = self.g.pagerank() self.assertAlmostEqual(pr_result[self.home], 0.92, places=2) self.assertAlmostEqual(pr_result[self.about], 0.41, places=2) self.assertAlmostEqual(pr_result[self.product], 0.41, places=2) self.assertAlmostEqual(pr_result[self.links], 0.41, places=2) self.assertAlmostEqual(pr_result[self.a], 0.22, places=2) self.assertAlmostEqual(pr_result[self.b], 0.22, places=2) self.assertAlmostEqual(pr_result[self.c], 0.22, places=2) self.assertAlmostEqual(pr_result[self.d], 0.22, places=2)
def testFloyds(self): expected = { 0: {0: 0, 1: 6, 2: 4, 3: 13, 4: 5, 5: 10, 6: 7 }, 1: {0: float('inf'), 1: 0, 2: 3, 3: 12, 4: 4, 5: 7, 6: 6 }, 2: {0: float('inf'), 1: float('inf'), 2: 0, 3: 9, 4: 1, 5: 6, 6: 3 }, 3: {0: float('inf'), 1: float('inf'), 2: float('inf'), 3: 0, 4: 1, 5: 6, 6: 3 }, 4: {0: float('inf'), 1: float('inf'), 2: float('inf'), 3: float('inf'), 4: 0, 5: 5, 6: 2 }, 5: {0: float('inf'), 1: float('inf'), 2: float('inf'), 3: float('inf'), 4: float('inf'), 5: 0, 6: float('inf') }, 6: {0: float('inf'), 1: float('inf'), 2: float('inf'), 3: float('inf'), 4: float('inf'), 5: float('inf'), 6: 0 } } actual = self.g.floyds() self.assertDictEqual(expected, actual) # handy self.assertTrue(p==q for p, q in zip(expected.items(), actual.items())) #does the same self.assertTrue(expected.items() == actual.items()) #does the same """ grph = {0: {0: 0, 1: 1, 2: 4}, 1: {0: inf, 1: 0, 2: 2}, 2: {0: inf, 1: inf, 2: 0} } """ grph = Graph() for v in range(3): grph.addVertex(v) grph.addEdge(0, 1, 1) grph.addEdge(0, 2, 4) grph.addEdge(1, 2, 2) """ {0: {0: 0, 1: 1, 2: 3}, 1: {0: inf, 1: 0, 2: 2}, 2: {0: inf, 1: inf, 2: 0}} """ expected = {0: {0: 0, 1: 1, 2: 3}, 1: {0: float('inf'), 1: 0, 2: 2}, 2: {0: float('inf'), 1: float('inf'), 2: 0} } actual = grph.floyds() self.assertDictEqual(expected, actual) # handy self.assertTrue(p==q for p, q in zip(expected.items(), actual.items())) #does the same self.assertTrue(expected.items() == actual.items()) #does the same
def summarize(text): print("Creating graph...") vertices = [Vertex(s) for s in text_to_sentences(text)] edges = [] for sentence_a, sentence_b in combinations(vertices, 2): score = similarity(sentence_a.data, sentence_b.data) edges.append(Edge(sentence_a, sentence_b, score)) edges.append(Edge(sentence_b, sentence_a, score)) # drop the memoized entries sentence_to_words.cache_clear() g = Graph(vertices, edges) print("Graph initialized: |V|=%d, |E|=%d" % (len(vertices), len(edges))) print("Running pagerank...") return g.sort_pagerank()
def main(): structures = pd.read_csv('../../../input/structures.csv') strs_gp = structures.groupby('molecule_name') bonds = pd.read_csv('../../../input/bonds.csv') bonds_gp = bonds.groupby('molecule_name') train = pd.merge( pd.read_csv('../../../dataset/train.csv'), pd.read_csv('../../../dataset/scalar_coupling_contributions.csv')) train_gp = train.groupby('molecule_name') list_atoms = list(set(structures['atom'])) print(list_atoms) model = TripletUpdateNet(num_layer=4, node_dim=512, edge_dim=256, triplet_dim=128) model.to_gpu() train_charges = pd.read_csv('../../../input/train_ob_charges.csv') train_charges_gp = train_charges.groupby('molecule_name') target1 = 'dsgdb9nsd_000008' g1 = Graph(strs_gp.get_group(target1), bonds_gp.get_group(target1), list_atoms, train_charges_gp.get_group(target1)) y1 = train_gp.get_group(target1) out = model([g1], [y1]) print(out) target2 = 'dsgdb9nsd_000010' g2 = Graph(strs_gp.get_group(target2), bonds_gp.get_group(target2), list_atoms, train_charges_gp.get_group(target2)) y2 = train_gp.get_group(target2) out = model([g2], [y2]) print(out) out = model([g1, g2], [y1, y2]) print(out)
def test_remove_node(self): g = Graph() g.add_edge(0, 1) g.add_edge(1, 2) g.add_edge(2, 3) g.remove_vertex(1) self.assertTrue((0, 1) not in g) self.assertTrue((1, 2) not in g) self.assertTrue((1, 2) not in g) self.assertTrue((2, 3) in g)
def testHavelHakimiAlgorithm(self): self.assertTrue(Graph.isGraphicSequenceIterative([2, 2, 2, 2, 1, 1])) self.assertTrue(Graph.isGraphicSequenceIterative([3, 3, 3, 3, 3, 3])) self.assertTrue(Graph.isGraphicSequenceIterative([3, 3, 2, 1, 1])) self.assertFalse(Graph.isGraphicSequenceIterative([4, 3, 2, 2, 2, 1, 1])) self.assertFalse(Graph.isGraphicSequenceIterative([6, 6, 5, 4, 4, 2, 1])) self.assertFalse(Graph.isGraphicSequenceIterative([3, 3, 3, 1]))
def testErdosGallaiTheorem(self): self.assertTrue(Graph.isGraphicSequence([2, 2, 2, 2, 1, 1])) self.assertTrue(Graph.isGraphicSequence([3, 3, 3, 3, 3, 3])) self.assertTrue(Graph.isGraphicSequence([3, 3, 2, 1, 1])) self.assertFalse(Graph.isGraphicSequence([4, 3, 2, 2, 2, 1, 1])) self.assertFalse(Graph.isGraphicSequence([6, 6, 5, 4, 4, 2, 1])) self.assertFalse(Graph.isGraphicSequence([3, 3, 3, 1]))
def main(): structures = pd.read_csv('../../../input/structures.csv') strs_gp = structures.groupby('molecule_name') bonds = pd.read_csv('../../../input/bonds.csv') bonds_gp = bonds.groupby('molecule_name') train = pd.read_csv('../../../input/train2.csv') train_gp = train.groupby('molecule_name') train_charges = pd.read_csv('../../../input/train_ob_charges.csv') train_charges_gp = train_charges.groupby('molecule_name') list_atoms = list(set(structures['atom'])) print(list_atoms) model = EdgeUpdateNet(num_layer=10, node_dim=512, edge_dim=512) model.to_gpu() target1 = 'dsgdb9nsd_000008' g1 = Graph(strs_gp.get_group(target1), bonds_gp.get_group(target1), list_atoms, train_charges_gp.get_group(target1)) y1 = train_gp.get_group(target1) out = model([g1], [y1]) print(out) target2 = 'dsgdb9nsd_000010' g2 = Graph(strs_gp.get_group(target2), bonds_gp.get_group(target2), list_atoms, train_charges_gp.get_group(target2)) y2 = train_gp.get_group(target2) out = model([g2], [y2]) print(out) out = model([g1, g2], [y1, y2]) print(out)
def __init__(self, builder, changed_method): gtk.ScrolledWindow.__init__(self) self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self.builder = builder self.event_box = EventBox() self.event_box.add_events(gtk.gdk.BUTTON_PRESS_MASK) self.event_box.add_events(gtk.gdk.BUTTON_RELEASE_MASK) self.event_box.add_events(gtk.gdk.MOTION_NOTIFY) self.event_box.add_events(gtk.gdk.BUTTON1_MOTION_MASK) self.event_box.add_events(gtk.gdk.KEY_PRESS_MASK) self.event_box.set_events(gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.POINTER_MOTION_HINT_MASK) self.event_box.connect('motion-notify-event', self.mouse_motion) self.event_box.connect('button-press-event', self.mouse_press) self.event_box.connect('button-release-event', self.mouse_release) self.event_box.connect('scroll-event', self.mouse_scroll) self.action = None self.menu = None self.box = None self.last_vertex_clicked = None self.last_position_clicked = None self.box_selecting = None self.changed = False self.changed_method = changed_method self.graph = GraphController() self.area = GraphArea(self.graph) self.event_box.add(self.area) self.add_with_viewport(self.event_box) self.area.show() self.event_box.show() self.show() # To UNDO and REDO actions self.states = [] self.state_index = None self.add_state() # Algorithm stuff self.algorithm_runner = None self.algorithm_playing = None self.algorithm_paused = None self.algorithm_states = []
def extract_func(self, addr): curr = self.code[addr] gph = Graph(self, addr) rest = [] while 1: if not gph.exists(curr): if is_uncond_jump(curr) and len(curr.operands) > 0: if curr.operands[0].type == X86_OP_IMM: addr = curr.operands[0].value.imm nxt = self.code[addr] gph.set_next(curr, nxt) rest.append(nxt.address) elif is_cond_jump(curr) and len(curr.operands) > 0: if curr.operands[0].type == X86_OP_IMM: nxt_jump = self.code[curr.operands[0].value.imm] direct_nxt = self.code[curr.address + curr.size] gph.set_cond_next(curr, nxt_jump, direct_nxt) rest.append(nxt_jump.address) rest.append(direct_nxt.address) elif is_ret(curr): gph.add_node(curr) else: nxt = self.code[curr.address + curr.size] gph.set_next(curr, nxt) rest.append(nxt.address) try: curr = self.code[rest.pop()] except: break return gph
def setUp(self): self.g = Graph() for v in range(7): self.g.addVertex(v) self.g.addEdge(0, 1, 6) self.g.addEdge(0, 2, 4) self.g.addEdge(1, 2, 3) self.g.addEdge(1, 5, 7) self.g.addEdge(2, 3, 9) self.g.addEdge(2, 4, 1) self.g.addEdge(3, 4, 1) self.g.addEdge(4, 5, 5) self.g.addEdge(4, 6, 2)
def setUp(self): from io import StringIO self.graph = Graph(StringIO(SearchUtilsTests.BASIC_VRP)) super(SearchUtilsTests, self).setUp() def distance(graph, solution, md): """Calculate overall distance""" del md s = 0 for route in solution: s += sum(graph.costs[(route[i], route[i + 1])] for i in range(len(route) - 1)) return s self.obj = distance
def setUp(self): # A brand new graph. self.new = Graph() # A disconnected graph self.disconnected = Graph() self.disconnected.add_edge(1, 2) self.disconnected.add_edge(3, 4) # A complete graph self.complete = Graph() self.complete.add_edge(1, 2, 3) self.complete.add_edge(2, 3, 5) self.complete.add_edge(3, 4, 7) self.complete.add_edge(4, 1, 5) self.complete.add_edge(1, 3, 4) self.complete.add_edge(2, 4, 6)
visited = set() def path_between(start, end): if start == end: return True for node in start.adjacent: if node in visited: next visited.add(node) if path_between(node, end): return True return False graph = Graph(( (1, (2, 6)), (2, (3,)), (3, (5,)), (5, ()), (6, (3,)), (7, (2,)) )) assert path_between(graph.find(1), graph.find(1)) assert path_between(graph.find(1), graph.find(5)) assert not path_between(graph.find(5), graph.find(7))
else: for v in graph.adj[u]: if v not in visited: visited.append(v) queue.append(v) return bfs_len, visited if __name__ == '__main__': filename = "../tests/DiameterGraph.txt" with open(filename) as fd: V = int(fd.readline()) E = int(fd.readline()) g = Graph(V) for lines in range(E): u, v = tuple(map(int, fd.readline().split())) g.add_edge(u, v) maxd = 0 for u in g.adj.iterkeys(): d, vertices = find_diameter(g, u) maxd = max(d, maxd) print "Max diameter {} vertices {}".format(maxd, vertices)
def test(): # Création des noeuds uno = Node("uno") dos = Node("dos") tres = Node("tres") quatro = Node("quatro") # Création des relations r = Relation("R", directed = True) s = Relation("S", directed = True) t = Relation("T", directed = True) # Ajout des noeuds dans les relations, et création des arêtes r.add_node(uno) r.add_node(dos) r.add_node(quatro) r.add_edge((uno, dos)) r.add_edge((quatro, dos)) s.add_node(dos) s.add_node(tres) s.add_edge((dos, tres)) t.add_node(tres) t.add_node(quatro) t.add_node(uno) t.add_edge((tres, quatro)) t.add_edge((quatro, uno)) # Création du graph, qui contient la liste des noeuds créés et la liste des relations utilisant les noeuds g = Graph("graph", [uno, dos, tres, quatro], [r, s, t]) # Méthode permettant de sortir toutes les caractéristiques du graphe g.print_all() # Création des règles! # Définition d'une règle! pr_0 = Rule("pr_0", ["x", "y"], [(r, ("x", "z")), (s, ("z", "y"))]) # Définition du goal, utilisant la règle pr_0! goal = Rule("Goal", ["x", "y"], [(pr_0, ("x","y"))], goal=True) # Ajout des règles dans une liste, afin de pouvoir la passer au graphe pour la résolution rules = [pr_0, goal] # Impression des règles print("Rules:") for r in rules: print("\t* {}".format(r)) print("") # Résolution du problème solutions = g.resolve(rules) # Impression des solutions if len(solutions) != 0: print("Solutions:") for s in solutions: print("\t* ({0} - {1})".format(s[0].id, s[1].id)) else: print("No solution!")
from lib.graph import Graph # Predict expected return for each time step agent = Agent(STATES, ACTIONS, REWARDS, HORIZON, DISCOUNT_FACTOR, POLICY, TRANSITIONS) expected_return_under_policy = agent.expected_return_under_policy() # Get expected rewards at end of episode expected_rewards = {} for state in STATES: expected_rewards[state] = round(expected_return_under_policy[state, HORIZON - 1]) # Simulate an episode to get actual rewards actual_rewards = {} for state in STATES: episode = Episode(ACTIONS, STATES, REWARDS, HORIZON, POLICY, TRANSITIONS, state) actual_rewards[state] = 0 for _ in range(EPISODES): actual_rewards[state] += episode.play() actual_rewards[state] /= EPISODES # Compare predicted vs. actual rewards print('Expected rewards:', expected_rewards) print('Actual rewards: ', actual_rewards) # Draw state-action diagram graph = Graph(STATES, ACTIONS, LABELS) graph.draw()
def testPrims(self): expectedMST = [(0, 2, 4), (2, 4, 1), (4, 3, 1), (4, 6, 2), (2, 1, 3), (4, 5, 5)] actualMST = self.g.mspPrims() for edge in expectedMST: self.assertTrue(edge in actualMST) # second example grph = Graph() for v in range(8): grph.addVertex(v) grph.addEdge(0, 1, 4) grph.addEdge(0, 2, 6) grph.addEdge(0, 3, 16) grph.addEdge(1, 5, 24) grph.addEdge(2, 5, 23) grph.addEdge(2, 4, 5) grph.addEdge(2, 3, 8) grph.addEdge(3, 4, 10) grph.addEdge(3, 7, 21) grph.addEdge(4, 5, 18) grph.addEdge(4, 6, 11) grph.addEdge(4, 7, 14) grph.addEdge(5, 6, 9) grph.addEdge(6, 7, 7) expMST = [(0, 1, 4), (0, 2, 6), (2, 4, 5), (2, 3, 8), (4, 6, 11), (6, 7, 7), (6, 5, 9)] actualMST = grph.mspPrims() for edge in expMST: self.assertTrue(edge in actualMST) # third example grph = Graph() for v in range(7): grph.addVertex(v) grph.addEdge(0, 1, 4) grph.addEdge(0, 2, 8) grph.addEdge(1, 2, 9) grph.addEdge(1, 3, 8) grph.addEdge(1, 4, 10) grph.addEdge(2, 3, 2) grph.addEdge(2, 5, 1) grph.addEdge(3, 4, 7) grph.addEdge(3, 5, 9) grph.addEdge(4, 5, 5) grph.addEdge(4, 6, 6) grph.addEdge(5, 6, 2) expMST = [(0, 1, 4), (0, 2, 8), (2, 5, 1), (2, 3, 2), (5, 6, 2), (5, 4, 5)] actualMST = grph.mspPrims() for edge in expMST: self.assertTrue(edge in actualMST) # fourth example grph = Graph() for v in range(6): grph.addVertex(v) grph.addEdge(0, 1, 2) grph.addEdge(0, 2, 3) grph.addEdge(1, 2, 6) grph.addEdge(1, 3, 5) grph.addEdge(1, 4, 3) grph.addEdge(2, 4, 2) grph.addEdge(3, 4, 1) grph.addEdge(3, 5, 2) grph.addEdge(4, 5, 4) expMST = [(0, 1, 2), (0, 2, 3), (2, 4, 2), (4, 3, 1), (3, 5, 2)] actualMST = grph.mspPrims() for edge in expMST: self.assertTrue(edge in actualMST)
def testDensity(self): g = Graph() vertices = ['a', 'b', 'c', 'd', 'e', 'f'] for v in vertices: g.addVertex(v) """ g = { "a" : ["d","f"], "b" : ["c","b"], "c" : ["b", "c", "d", "e"], "d" : ["a", "c"], "e" : ["c"], "f" : ["a"] } """ g.addEdge('a', 'd') g.addEdge('a', 'f') g.addEdge('b', 'c') g.addEdge('b', 'b') g.addEdge('c', 'c') g.addEdge('c', 'd') g.addEdge('c', 'e') self.assertAlmostEqual(0.466666666667, g.density(), places=7) """ complete_graph = { "a" : ["b","c"], "b" : ["a","c"], "c" : ["a","b"] } """ complete_graph = Graph() vertices = ['a', 'b', 'c'] for v in vertices: complete_graph.addVertex(v) complete_graph.addEdge('a', 'b') complete_graph.addEdge('a', 'c') complete_graph.addEdge('b', 'c') self.assertEqual(1.0, complete_graph.density()) """ isolated_graph = { "a" : [], "b" : [], "c" : [] } """ isolated_graph = Graph() vertices = ['a', 'b', 'c'] for v in vertices: isolated_graph.addVertex(v) self.assertEqual(0.0, isolated_graph.density())
def testFindAllPaths(self): g = Graph() g.addVertex('s') g.addVertex('u') g.addVertex('v') g.addVertex('t') g.addEdge('s', 'u', 20) g.addEdge('s', 'v', 10) g.addEdge('u', 'v', 30) g.addEdge('u', 't', 10) g.addEdge('v', 't', 20) paths = g.find_all_paths('s', 't', []) expectedPaths = [ ['s', 'u', 't'], ['s', 'u', 'v', 't'], ['s', 'v', 't'] ] for path in expectedPaths: self.assertTrue(path in paths)
def testFindShortestPath(self): g = Graph() g.addVertex('s') g.addVertex('u') g.addVertex('v') g.addVertex('t') g.addEdge('s', 'u', 20) g.addEdge('s', 'v', 10) g.addEdge('u', 'v', 30) g.addEdge('u', 't', 10) g.addEdge('v', 't', 20) expectedPath = ['s', 'u', 't'] actualPath = g.find_shortest_path('s', 't', []) for v in expectedPath: self.assertTrue(v in actualPath)
def testFindPath(self): g = Graph() g.addVertex('s') g.addVertex('u') g.addVertex('v') g.addVertex('t') g.addEdge('s', 'u', 20) g.addEdge('s', 'v', 10) g.addEdge('u', 'v', 30) g.addEdge('u', 't', 10) g.addEdge('v', 't', 20) p = g.find_path('s', 't', []) self.assertTrue(all(x in p for x in ['s', 'u', 't']))
def test_BFS(self): ss_g = Graph() for v in range(6): ss_g.addVertex(v) ss_g.addEdge(0, 1) ss_g.addEdge(0, 4) ss_g.addEdge(0, 5) ss_g.addEdge(1, 2) ss_g.addEdge(2, 3) ss_g.addEdge(3, 4) (discovered, parents) = ss_g.BFS(0) expectePath = [0, 1, 2, 3] vPath = [] ss_g.findPath(0, 3, parents, vPath) for (x, y) in zip(expectePath, vPath): self.assertEqual(x, y) # add an edge between 4 and 3 to short cut for testing new path ss_g.addEdge(4, 3) (discovered, parents) = ss_g.BFS(0) expectePath = [0, 4, 3] vPath = [] ss_g.findPath(0, 3, parents, vPath) for (x, y) in zip(expectePath, vPath): self.assertEqual(x, y)
def testIsConnected(self): self.assertTrue(self.g.isConnected()) self.g.addVertex(10) self.assertFalse(self.g.isConnected()) """ g = { "a" : ["d"], "b" : ["c"], "c" : ["b", "c", "d", "e"], "d" : ["a", "c"], "e" : ["c"], "f" : [] } """ g = Graph() vertices = ['a', 'b', 'c', 'd', 'e', 'f'] for v in vertices: g.addVertex(v) g.addEdge('a', 'd') g.addEdge('b', 'c') g.addEdge('c', 'd') g.addEdge('c', 'e') self.assertFalse(g.isConnected('a')) """ g2 = { "a" : ["d","f"], "b" : ["c"], "c" : ["b", "c", "d", "e"], "d" : ["a", "c"], "e" : ["c"], "f" : ["a"] } """ g2 = Graph() vertices = ['a', 'b', 'c', 'd', 'e', 'f'] for v in vertices: g2.addVertex(v) g2.addEdge('a', 'd') g2.addEdge('a', 'f') g2.addEdge('b', 'c') g2.addEdge('c', 'b') g2.addEdge('c', 'd') g2.addEdge('d', 'a') g2.addEdge('d', 'c') g2.addEdge('c', 'e') g2.addEdge('e', 'c') g2.addEdge('f', 'a') self.assertTrue(g2.isConnected('a')) """ g3 = { "a" : ["d","f"], "b" : ["c","b"], "c" : ["b", "c", "d", "e"], "d" : ["a", "c"], "e" : ["c"], "f" : ["a"] } """ g3 = Graph() vertices = ['a', 'b', 'c', 'd', 'e', 'f'] for v in vertices: g3.addVertex(v) g3.addEdge('a', 'd') g3.addEdge('a', 'f') g3.addEdge('b', 'c') g3.addEdge('b', 'b') g3.addEdge('c', 'b') g3.addEdge('c', 'c') g3.addEdge('c', 'd') g3.addEdge('c', 'e') g3.addEdge('d', 'a') g3.addEdge('d', 'c') g3.addEdge('e', 'c') g3.addEdge('f', 'a') self.assertTrue(g3.isConnected('a'))
class Graph(gtk.ScrolledWindow): def __init__(self, builder, changed_method): gtk.ScrolledWindow.__init__(self) self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self.builder = builder self.event_box = EventBox() self.event_box.add_events(gtk.gdk.BUTTON_PRESS_MASK) self.event_box.add_events(gtk.gdk.BUTTON_RELEASE_MASK) self.event_box.add_events(gtk.gdk.MOTION_NOTIFY) self.event_box.add_events(gtk.gdk.BUTTON1_MOTION_MASK) self.event_box.add_events(gtk.gdk.KEY_PRESS_MASK) self.event_box.set_events(gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.POINTER_MOTION_HINT_MASK) self.event_box.connect('motion-notify-event', self.mouse_motion) self.event_box.connect('button-press-event', self.mouse_press) self.event_box.connect('button-release-event', self.mouse_release) self.event_box.connect('scroll-event', self.mouse_scroll) self.action = None self.menu = None self.box = None self.last_vertex_clicked = None self.last_position_clicked = None self.box_selecting = None self.changed = False self.changed_method = changed_method self.graph = GraphController() self.area = GraphArea(self.graph) self.event_box.add(self.area) self.add_with_viewport(self.event_box) self.area.show() self.event_box.show() self.show() # To UNDO and REDO actions self.states = [] self.state_index = None self.add_state() # Algorithm stuff self.algorithm_runner = None self.algorithm_playing = None self.algorithm_paused = None self.algorithm_states = [] def centralize_scroll(self, position=None): """Put both scrolls in center""" vadj = self.get_vadjustment() hadj = self.get_hadjustment() if not position: position = [(vadj.upper / 2), (hadj.upper / 2)] print([int(x) for x in position]) hadj.set_value(position[0] - (hadj.page_size / 2)) vadj.set_value(position[1] - (vadj.page_size / 2)) def zoom_in(self, center=None): if self.area.zoom < 1.7: self.area.zoom *= 1.1 self.do_zoom(lambda v: v * 1.1, center) def zoom_out(self, center=None): if self.area.zoom > 0.2: self.area.zoom /= 1.1 self.do_zoom(lambda v: v / 1.1, center) def zoom_default(self): old = self.area.zoom self.area.zoom = 1 self.do_zoom(lambda v: v / old) def do_zoom(self, op, center=None): width, height = self.area.get_size_request() vadj = self.get_vadjustment() hadj = self.get_hadjustment() width *= self.area.zoom height *= self.area.zoom if not center: offsetx, offsety = self.event_box.get_size_request() x = (hadj.get_value() + offsetx / 2) * self.area.zoom y = (vadj.get_value() + offsety / 2) * self.area.zoom center = [x, y] hadj.set_upper(width) vadj.set_upper(height) # self.centralize_scroll(center) hadj.set_value(op(hadj.get_value())) vadj.set_value(op(vadj.get_value())) self.area.queue_draw() def mouse_scroll(self, widget, event): width, height = self.area.get_size_request() vadj = self.get_vadjustment() hadj = self.get_hadjustment() if not (event.state & gtk.gdk.CONTROL_MASK): return center = [v / self.area.zoom for v in event.get_coords()] if event.direction == gtk.gdk.SCROLL_UP: self.zoom_in(center) elif event.direction == gtk.gdk.SCROLL_DOWN: self.zoom_out(center) width, height = self.area.get_size_request() vadj = self.get_vadjustment() hadj = self.get_hadjustment() def set_changed(self, value): if self.changed != value: self.changed = value self.changed_method(self) def add_vertex(self): self.graph.add_vertex(self.last_position_clicked) self.add_state() self.action = None self.area.queue_draw() def remove_vertex(self): to_be_removed = list(self.graph.selected_vertices()) list(map(lambda vertex: self.graph.remove_vertex(vertex), to_be_removed)) self.add_state() self.action = None self.area.queue_draw() def edit_vertex(self): if len(self.graph.selected_vertices()) == 1: vertex = self.graph.selected_vertices()[0] self.graph.deselect_vertex(vertex) vertex_edit = Vertex(self, vertex) def add_edge(self): if len(self.graph.selected_vertices()) == 1: vertex = self.graph.find_by_position(self.last_position_clicked) if vertex != None and vertex != self.graph.selected_vertices()[0]: self.graph.add_edge(self.graph.selected_vertices()[0], vertex) self.graph.deselect_vertex(self.graph.selected_vertices()[0]) self.add_state() self.action = None self.area.queue_draw() return True if len(self.graph.selected_vertices()) > 1: for i in range(len(self.graph.selected_vertices())): for j in range(i, len(self.graph.selected_vertices())): vertex1 = self.graph.selected_vertices()[i] vertex2 = self.graph.selected_vertices()[j] if vertex1 != vertex2: self.graph.add_edge(vertex1, vertex2) if self.graph.directed: self.graph.add_edge(vertex2, vertex1) selected_vertices = list(self.graph.selected_vertices()) if len(selected_vertices): self.graph.deselect_all() self.add_state() for vertex in selected_vertices: self.graph.select_vertex(vertex) self.action = None self.area.queue_draw() return True self.action = "add_edge" return False def remove_edge(self): # TODO - Handle multiple edges if len(self.graph.selected_vertices()) == 1: vertex = self.graph.find_by_position(self.last_position_clicked) if vertex != None: edge = self.graph.find_edge(self.graph.selected_vertices()[0], vertex) if len(edge) > 0: self.graph.remove_edge(edge[0]) self.graph.deselect_vertex(self.graph.selected_vertices()[0]) self.add_state() self.action = None self.area.queue_draw() return True elif len(self.graph.selected_vertices()) > 1: for vertex1 in self.graph.selected_vertices(): for vertex2 in self.graph.selected_vertices(): if vertex1 != vertex2: edge = self.graph.find_edge(vertex1, vertex2) if len(edge) > 0: self.graph.remove_edge(edge[0]) self.graph.deselect_all() self.add_state() self.action = None self.area.queue_draw() return True return False def edit_edge(self): # TODO - Handle multiple edges if len(self.graph.selected_vertices()) == 2: v1 = self.graph.selected_vertices()[0] v2 = self.graph.selected_vertices()[1] edge = self.graph.find_edge(v1, v2) self.graph.deselect_vertex(v1) self.graph.deselect_vertex(v2) # TODO: This only works for 1 edge. 0 or more than 1 edges: fail. edge_edit = Edge(self, edge[0]) def select_area(self, event, area): if not area: return x, y, w, h = area vertices = self.graph.find_in_area(x, y, w, h) from gtk.gdk import CONTROL_MASK, SHIFT_MASK if not (event.state & CONTROL_MASK or event.state & SHIFT_MASK): self.graph.deselect_all() method = self.graph.select_vertex if (event.state & CONTROL_MASK): method = self.graph.toggle_vertex_selection list(map(lambda vertex: method(vertex), vertices)) def select_vertex(self, event): vertex = self.graph.find_by_position(self.last_position_clicked) from gtk.gdk import CONTROL_MASK, SHIFT_MASK if not (event.state & CONTROL_MASK or event.state & SHIFT_MASK) and (len(self.graph.selected_vertices()) > 0): if not vertex or not vertex.selected: self.graph.deselect_all() if vertex: if vertex.selected and (event.state & CONTROL_MASK or event.state & SHIFT_MASK): self.graph.deselect_vertex(vertex) else: self.graph.select_vertex(vertex) self.last_vertex_clicked = vertex else: self.box_selecting = self.last_position_clicked self.action = None def __block_event_box(self): if self.algorithm_runner: self.event_box.handler_block_by_func(self.mouse_motion) self.event_box.handler_block_by_func(self.mouse_press) self.event_box.handler_block_by_func(self.mouse_release) self.event_box.handler_block_by_func(self.mouse_scroll) else: self.event_box.handler_unblock_by_func(self.mouse_motion) self.event_box.handler_unblock_by_func(self.mouse_press) self.event_box.handler_unblock_by_func(self.mouse_release) self.event_box.handler_unblock_by_func(self.mouse_scroll) def algorithm_play(self): if self.algorithm_runner: self.graph.deselect_all() self.algorithm_playing = True if self.algorithm_paused: self.algorithm_paused = False else: self.algorithm_runner.play() self.queue_draw() gobject.timeout_add(500, self.algorithm_next, True) def algorithm_pause(self): if self.algorithm_runner: if self.algorithm_playing: self.algorithm_runner.pause() self.algorithm_playing = False self.algorithm_paused = True def algorithm_load(self, Algorithm): if self.algorithm_runner: self.algorithm_runner.stop() self.algorithm_runner = Algorithm(self) self.__block_event_box() def algorithm_next(self, auto=False): if self.algorithm_runner: if auto and not self.algorithm_paused: next(self.algorithm_runner) self.queue_draw() if self.algorithm_playing and self.algorithm_runner.is_alive(): return True else: if not self.algorithm_playing or not self.algorithm_runner.is_alive(): next(self.algorithm_runner) self.queue_draw() self.algorithm_playing = False return False def algorithm_prev(self): if self.algorithm_runner: self.algorithm_runner.prev() self.queue_draw() if self.algorithm_playing: self.algorithm_playing = False self.algorithm_prev() def algorithm_stop(self): if self.algorithm_runner: self.algorithm_runner.stop() self.algorithm_runner = None self.queue_draw() self.__block_event_box() def algorithm_layout(self): self.graph.layout_graph(50) self.queue_draw() def add_state(self): state = pickle.dumps(self.graph) if not self.state_index: self.state_index = 0 for place in ["menu_edit_", "toolbutton_"]: self.builder.get_object(place + "undo").set_sensitive(True) if (self.state_index < len(self.states) - 1): self.states = self.states[:self.state_index + 1] for place in ["menu_edit_", "toolbutton_"]: self.builder.get_object(place + "redo").set_sensitive(False) self.states.append(state) if len(self.states) == 1: self.state_index = 0 else: self.state_index += 1 def prev_state(self): if (self.state_index > 0 and len(self.states) > 0): self.state_index -= 1 graph = self.states[self.state_index] state = pickle.loads(graph) if self.state_index == 0: for place in ["menu_edit_", "toolbutton_"]: self.builder.get_object(place + "undo").set_sensitive(False) if len(self.states) > 1: for place in ["menu_edit_", "toolbutton_"]: self.builder.get_object(place + "redo").set_sensitive(True) return state return None def next_state(self): if (self.state_index < len(self.states) - 1): self.state_index += 1 graph = self.states[self.state_index] state = pickle.loads(graph) if self.state_index == len(self.states) - 1: for place in ["menu_edit_", "toolbutton_"]: self.builder.get_object(place + "redo").set_sensitive(False) if len(self.states) > 1: for place in ["menu_edit_", "toolbutton_"]: self.builder.get_object(place + "undo").set_sensitive(True) return state return None def undo(self): graph = self.prev_state() if graph: graph.path = self.graph.path graph.title = self.graph.title self.area.graph = graph self.graph = graph self.set_changed(True) self.queue_draw() def redo(self): graph = self.next_state() if graph: self.area.graph = graph self.graph = graph self.set_changed(True) self.queue_draw() def mouse_press(self, widget, event): self.last_position_clicked = [v / self.area.zoom for v in event.get_coords()] if event.button == 1: if self.action != None: self.set_changed(True) if self.action == None: self.select_vertex(event) if event.type == gtk.gdk._2BUTTON_PRESS: self.edit_vertex() elif self.action == "add_vertex": self.add_vertex() elif self.action == "remove_vertex": self.remove_vertex() elif self.action == "add_edge": if not self.add_edge(): self.select_vertex(event) elif self.action == "remove_edge": if not self.remove_edge(): self.select_vertex(event) elif event.button == 2: if self.action != None: self.set_changed(True) if self.action == None: vertex = self.graph.find_by_position(self.last_position_clicked) if vertex: self.select_vertex(event) self.action = "add_edge" else: self.add_vertex() elif self.action == "add_edge": if not self.add_edge(): self.select_vertex(event) elif event.button == 3: self.right_click_menu(event) self.area.queue_draw() self.mouse_motion(widget, event) def right_click_menu(self, event): vertex = self.graph.find_by_position(self.last_position_clicked) if len(self.graph.selected_vertices()) == 0 and vertex: self.graph.select_vertex(vertex) def execute_action(event, action): action() if not self.menu: self.menu = gtk.Menu() self.menu_add_edge = gtk.MenuItem(_("_Add edge")) self.menu_remove_edge = gtk.MenuItem(_("_Remove edge")) self.menu_add_vertex = gtk.MenuItem(_("_Add vertex")) self.menu_remove_vertex = gtk.MenuItem(_("_Remove vertex")) self.menu_edit_vertex = gtk.MenuItem(_("_Edit vertex settings")) self.menu_edit_edge = gtk.MenuItem(_("_Edit edge settings")) self.menu_add_edge.connect("activate", execute_action, self.add_edge) self.menu_remove_edge.connect("activate", execute_action, self.remove_edge) self.menu_add_vertex.connect("activate", execute_action, self.add_vertex) self.menu_remove_vertex.connect("activate", execute_action, self.remove_vertex) self.menu_edit_vertex.connect("activate", execute_action, self.edit_vertex) self.menu_edit_edge.connect("activate", execute_action, self.edit_edge) self.menu.append(self.menu_add_vertex) self.menu.append(self.menu_remove_vertex) self.menu.append(gtk.SeparatorMenuItem()) self.menu.append(self.menu_add_edge) self.menu.append(self.menu_remove_edge) self.menu.append(gtk.SeparatorMenuItem()) self.menu.append(self.menu_edit_vertex) self.menu.append(self.menu_edit_edge) if vertex: self.menu_add_vertex.set_sensitive(False) self.menu_edit_vertex.set_sensitive(True) self.menu_remove_vertex.set_sensitive(True) self.menu_add_edge.set_sensitive(True) self.menu_remove_edge.set_sensitive(True) self.menu_edit_edge.set_sensitive(False) if len(self.graph.selected_vertices()) == 2: self.menu_edit_edge.set_sensitive(True) else: self.menu_add_vertex.set_sensitive(True) self.menu_edit_vertex.set_sensitive(False) self.menu_remove_vertex.set_sensitive(False) self.menu_add_edge.set_sensitive(len(self.graph.selected_vertices()) > 0) self.menu_remove_edge.set_sensitive(False) self.menu_edit_edge.set_sensitive(False) self.menu.show_all() self.menu.popup(None, None, None, event.button, event.time) def mouse_release(self, widget, event): selected_vertices = list(self.graph.selected_vertices()) if len(selected_vertices) > 0 and self.last_vertex_clicked: self.graph.deselect_all() self.add_state() for vertex in selected_vertices: self.graph.select_vertex(vertex) self.last_vertex_clicked = None self.box_selecting = None self.select_area(event, self.area.selected_area) self.area.selected_area = None self.area.queue_draw() def mouse_motion(self, widget, event): coords = [v / self.area.zoom for v in event.get_coords()] if self.box_selecting: x, y = self.box_selecting w, h = [e - s for e, s in zip(coords, self.box_selecting)] self.area.selected_area = (x, y, w, h) self.area.queue_draw() return selected_vertices = self.graph.selected_vertices() if self.action == "add_edge" and len(selected_vertices) == 1: start = selected_vertices[0].position end = coords self.area.adding_edge = (start, end) self.area.queue_draw() else: self.area.adding_edge = None if len(selected_vertices) > 0 and self.last_vertex_clicked: end_position = coords start_position = self.last_vertex_clicked.position delta_x = end_position[0] - start_position[0] delta_y = end_position[1] - start_position[1] self.set_changed(True) for vertex in selected_vertices: new_position = [vertex.position[0] + delta_x, vertex.position[1] + delta_y] vertex.position = new_position self.area.queue_draw()
def testKrushkals(self): expectedMST = [(2, 4, 1), (3, 4, 1), (4, 6, 2), (1, 2, 3), (0, 2, 4), (4, 5, 5)] actualTree = self.g.mspKrushkals() for edge in expectedMST: self.assertTrue(edge in actualTree) # second example grph = Graph() for v in range(7): grph.addVertex(v) grph.addEdge(0, 1, 5) grph.addEdge(0, 2, 7) grph.addEdge(1, 2, 9) grph.addEdge(1, 3, 6) grph.addEdge(1, 5, 15) grph.addEdge(2, 4, 8) grph.addEdge(2, 5, 7) grph.addEdge(3, 5, 8) grph.addEdge(3, 6, 11) grph.addEdge(4, 5, 5) grph.addEdge(5, 6, 9) expTree = [(0, 1, 5), (4, 5, 5), (1, 3, 6), (0, 2, 7), (2, 5, 7), (5, 6, 9)] actualTree = grph.mspKrushkals() for edge in expTree: self.assertTrue(edge in actualTree)
def get_graph(self, addr): from capstone import CS_OP_IMM, CS_ARCH_MIPS ARCH_UTILS = self.load_arch_module().utils curr = self.lazy_disasm(addr) if curr == None: return None gph = Graph(self, addr) rest = [] start = time.clock() prefetch = None # WARNING: this assume that on every architectures the jump # address is the last operand (operands[-1]) while 1: if not gph.exists(curr): if self.arch == CS_ARCH_MIPS: prefetch = self.__prefetch_inst(curr) if ARCH_UTILS.is_uncond_jump(curr) and len(curr.operands) > 0: if curr.operands[-1].type == CS_OP_IMM: addr = curr.operands[-1].value.imm nxt = self.lazy_disasm(addr) gph.set_next(curr, nxt, prefetch) rest.append(nxt.address) else: # Can't interpret jmp ADDR|reg gph.add_node(curr, prefetch) gph.uncond_jumps_set.add(curr.address) elif ARCH_UTILS.is_cond_jump(curr) and len(curr.operands) > 0: if curr.operands[-1].type == CS_OP_IMM: nxt_jump = self.lazy_disasm(curr.operands[-1].value.imm) if self.arch == CS_ARCH_MIPS: direct_nxt = \ self.lazy_disasm(prefetch.address + prefetch.size) else: direct_nxt = \ self.lazy_disasm(curr.address + curr.size) gph.set_cond_next(curr, nxt_jump, direct_nxt, prefetch) rest.append(nxt_jump.address) rest.append(direct_nxt.address) else: # Can't interpret jmp ADDR|reg gph.add_node(curr, prefetch) gph.cond_jumps_set.add(curr.address) elif ARCH_UTILS.is_ret(curr): gph.add_node(curr, prefetch) else: try: nxt = self.lazy_disasm(curr.address + curr.size) gph.set_next(curr, nxt) rest.append(nxt.address) except: gph.add_node(curr) pass try: curr = self.lazy_disasm(rest.pop()) except IndexError: break if self.binary.type == T_BIN_PE: self.binary.pe_reverse_stripped_symbols(self) elapsed = time.clock() elapsed = elapsed - start debug__("Graph built in %fs" % elapsed) return gph
class GraphTests(unittest.TestCase): def setUp(self): self.g = Graph() for v in range(7): self.g.addVertex(v) self.g.addEdge(0, 1, 6) self.g.addEdge(0, 2, 4) self.g.addEdge(1, 2, 3) self.g.addEdge(1, 5, 7) self.g.addEdge(2, 3, 9) self.g.addEdge(2, 4, 1) self.g.addEdge(3, 4, 1) self.g.addEdge(4, 5, 5) self.g.addEdge(4, 6, 2) def tearDown(self): self.g = None def testBasic(self): for v in self.g.getVertices(): self.assertTrue(v in range(7)) def testNeighbours(self): zero_s_nbrs = [1, 2] for v in zero_s_nbrs: self.assertTrue(v in self.g.getNeighbours(0)) one_s_nbrs = [2, 5] for v in one_s_nbrs: self.assertTrue(v in self.g.getNeighbours(1)) two_s_nbrs = [3, 4] for v in two_s_nbrs: self.assertTrue(v in self.g.getNeighbours(2)) three_s_nbrs = [4] for v in three_s_nbrs: self.assertTrue(v in self.g.getNeighbours(3)) four_s_nbrs = [5, 6] for v in four_s_nbrs: self.assertTrue(v in self.g.getNeighbours(4)) def testEdges(self): expected_edges = [ (0, 1, 6), (0, 2, 4), (1, 2, 3), (1, 5, 7), (2, 3, 9), (2, 4, 1), (3, 4, 1), (4, 5, 5), (4, 6, 2) ] edges = self.g.getEdges() for edge in expected_edges: self.assertTrue(edge in edges) def testIsolatedVertex(self): self.g.addVertex(10) isolated = self.g.findIsolated() self.assertTrue(10 in isolated) def testGetPath(self): path = self.g.getPath(0, 6, []) self.assertEquals(path, [0, 1, 2, 3, 4, 6]) path = self.g.getPath(0, 10, []) self.assertEquals(None, path) def testGetAllPaths(self): expectedPaths = [[0, 1, 2, 3, 4, 6], [0, 1, 2, 4, 6], [0, 2, 3, 4, 6], [0, 2, 4, 6] ] paths = self.g.getAllPaths(0, 6, []) for path in expectedPaths: self.assertTrue(path in paths) def testInDegree(self): self.g.addVertex(10) expectedDegrees = [(0, 0), # 0 vertex has 0 edges going to it (1, 1), (2, 2), (3, 1), (4, 2), (5, 2), (6, 1), (10, 0) ] for (vertex, inDegree) in expectedDegrees: self.assertEqual(self.g.inDegree(vertex), inDegree) def testOutDegree(self): self.g.addVertex(10) expectedDegrees = [(0, 2), # 0 vertex has 2 edges going from it (1, 2), (2, 2), (3, 1), (4, 2), (5, 0), (6, 0), (10, 0) ] for (vertex, outDegree) in expectedDegrees: self.assertEqual(self.g.outDegree(vertex), outDegree) def testDegree(self): self.g.addVertex(10) expectedDegrees = [(0, 2), # 0 vertex has 2 edges (1, 3), (2, 4), (3, 2), (4, 4), (5, 2), (6, 1), (10, 0) ] for (vertex, degree) in expectedDegrees: self.assertEqual(self.g.getDegree(vertex), degree) def testDegreeSumFormula(self): self.assertTrue(self.g.verifyDegreeSumFormula()) def testMinDegreeOfGraphV(self): self.assertEqual(self.g.delta(), 1) #add a isolated vertex and check the degree 0 self.g.addVertex(10) self.assertEqual(self.g.delta(), 0) def testMaxDegreeOfGraphV(self): self.assertEqual(self.g.Delta(), 4) def testDegreeSequence(self): expected = (4, 4, 3, 2, 2, 2, 1) self.assertEqual(self.g.degreeSequence(), expected) def testErdosGallaiTheorem(self): self.assertTrue(Graph.isGraphicSequence([2, 2, 2, 2, 1, 1])) self.assertTrue(Graph.isGraphicSequence([3, 3, 3, 3, 3, 3])) self.assertTrue(Graph.isGraphicSequence([3, 3, 2, 1, 1])) self.assertFalse(Graph.isGraphicSequence([4, 3, 2, 2, 2, 1, 1])) self.assertFalse(Graph.isGraphicSequence([6, 6, 5, 4, 4, 2, 1])) self.assertFalse(Graph.isGraphicSequence([3, 3, 3, 1])) def testHavelHakimiAlgorithm(self): self.assertTrue(Graph.isGraphicSequenceIterative([2, 2, 2, 2, 1, 1])) self.assertTrue(Graph.isGraphicSequenceIterative([3, 3, 3, 3, 3, 3])) self.assertTrue(Graph.isGraphicSequenceIterative([3, 3, 2, 1, 1])) self.assertFalse(Graph.isGraphicSequenceIterative([4, 3, 2, 2, 2, 1, 1])) self.assertFalse(Graph.isGraphicSequenceIterative([6, 6, 5, 4, 4, 2, 1])) self.assertFalse(Graph.isGraphicSequenceIterative([3, 3, 3, 1])) def testDensity(self): g = Graph() vertices = ['a', 'b', 'c', 'd', 'e', 'f'] for v in vertices: g.addVertex(v) """ g = { "a" : ["d","f"], "b" : ["c","b"], "c" : ["b", "c", "d", "e"], "d" : ["a", "c"], "e" : ["c"], "f" : ["a"] } """ g.addEdge('a', 'd') g.addEdge('a', 'f') g.addEdge('b', 'c') g.addEdge('b', 'b') g.addEdge('c', 'c') g.addEdge('c', 'd') g.addEdge('c', 'e') self.assertAlmostEqual(0.466666666667, g.density(), places=7) """ complete_graph = { "a" : ["b","c"], "b" : ["a","c"], "c" : ["a","b"] } """ complete_graph = Graph() vertices = ['a', 'b', 'c'] for v in vertices: complete_graph.addVertex(v) complete_graph.addEdge('a', 'b') complete_graph.addEdge('a', 'c') complete_graph.addEdge('b', 'c') self.assertEqual(1.0, complete_graph.density()) """ isolated_graph = { "a" : [], "b" : [], "c" : [] } """ isolated_graph = Graph() vertices = ['a', 'b', 'c'] for v in vertices: isolated_graph.addVertex(v) self.assertEqual(0.0, isolated_graph.density()) def testIsConnected(self): self.assertTrue(self.g.isConnected()) self.g.addVertex(10) self.assertFalse(self.g.isConnected()) """ g = { "a" : ["d"], "b" : ["c"], "c" : ["b", "c", "d", "e"], "d" : ["a", "c"], "e" : ["c"], "f" : [] } """ g = Graph() vertices = ['a', 'b', 'c', 'd', 'e', 'f'] for v in vertices: g.addVertex(v) g.addEdge('a', 'd') g.addEdge('b', 'c') g.addEdge('c', 'd') g.addEdge('c', 'e') self.assertFalse(g.isConnected('a')) """ g2 = { "a" : ["d","f"], "b" : ["c"], "c" : ["b", "c", "d", "e"], "d" : ["a", "c"], "e" : ["c"], "f" : ["a"] } """ g2 = Graph() vertices = ['a', 'b', 'c', 'd', 'e', 'f'] for v in vertices: g2.addVertex(v) g2.addEdge('a', 'd') g2.addEdge('a', 'f') g2.addEdge('b', 'c') g2.addEdge('c', 'b') g2.addEdge('c', 'd') g2.addEdge('d', 'a') g2.addEdge('d', 'c') g2.addEdge('c', 'e') g2.addEdge('e', 'c') g2.addEdge('f', 'a') self.assertTrue(g2.isConnected('a')) """ g3 = { "a" : ["d","f"], "b" : ["c","b"], "c" : ["b", "c", "d", "e"], "d" : ["a", "c"], "e" : ["c"], "f" : ["a"] } """ g3 = Graph() vertices = ['a', 'b', 'c', 'd', 'e', 'f'] for v in vertices: g3.addVertex(v) g3.addEdge('a', 'd') g3.addEdge('a', 'f') g3.addEdge('b', 'c') g3.addEdge('b', 'b') g3.addEdge('c', 'b') g3.addEdge('c', 'c') g3.addEdge('c', 'd') g3.addEdge('c', 'e') g3.addEdge('d', 'a') g3.addEdge('d', 'c') g3.addEdge('e', 'c') g3.addEdge('f', 'a') self.assertTrue(g3.isConnected('a')) def testCLRDfs(self): paths = self.g.CLR_Dfs() self.assertEqual(len(paths), 1) self.g.addEdge(1, 0, 2) # add a edge from 1 to 0 to make possible paths 2 paths = self.g.CLR_Dfs() self.assertEqual(len(paths), 2) def test_BFS(self): ss_g = Graph() for v in range(6): ss_g.addVertex(v) ss_g.addEdge(0, 1) ss_g.addEdge(0, 4) ss_g.addEdge(0, 5) ss_g.addEdge(1, 2) ss_g.addEdge(2, 3) ss_g.addEdge(3, 4) (discovered, parents) = ss_g.BFS(0) expectePath = [0, 1, 2, 3] vPath = [] ss_g.findPath(0, 3, parents, vPath) for (x, y) in zip(expectePath, vPath): self.assertEqual(x, y) # add an edge between 4 and 3 to short cut for testing new path ss_g.addEdge(4, 3) (discovered, parents) = ss_g.BFS(0) expectePath = [0, 4, 3] vPath = [] ss_g.findPath(0, 3, parents, vPath) for (x, y) in zip(expectePath, vPath): self.assertEqual(x, y) def testFindPath(self): g = Graph() g.addVertex('s') g.addVertex('u') g.addVertex('v') g.addVertex('t') g.addEdge('s', 'u', 20) g.addEdge('s', 'v', 10) g.addEdge('u', 'v', 30) g.addEdge('u', 't', 10) g.addEdge('v', 't', 20) p = g.find_path('s', 't', []) self.assertTrue(all(x in p for x in ['s', 'u', 't'])) def testFindAllPaths(self): g = Graph() g.addVertex('s') g.addVertex('u') g.addVertex('v') g.addVertex('t') g.addEdge('s', 'u', 20) g.addEdge('s', 'v', 10) g.addEdge('u', 'v', 30) g.addEdge('u', 't', 10) g.addEdge('v', 't', 20) paths = g.find_all_paths('s', 't', []) expectedPaths = [ ['s', 'u', 't'], ['s', 'u', 'v', 't'], ['s', 'v', 't'] ] for path in expectedPaths: self.assertTrue(path in paths) def testFindShortestPath(self): g = Graph() g.addVertex('s') g.addVertex('u') g.addVertex('v') g.addVertex('t') g.addEdge('s', 'u', 20) g.addEdge('s', 'v', 10) g.addEdge('u', 'v', 30) g.addEdge('u', 't', 10) g.addEdge('v', 't', 20) expectedPath = ['s', 'u', 't'] actualPath = g.find_shortest_path('s', 't', []) for v in expectedPath: self.assertTrue(v in actualPath) def testPrims(self): expectedMST = [(0, 2, 4), (2, 4, 1), (4, 3, 1), (4, 6, 2), (2, 1, 3), (4, 5, 5)] actualMST = self.g.mspPrims() for edge in expectedMST: self.assertTrue(edge in actualMST) # second example grph = Graph() for v in range(8): grph.addVertex(v) grph.addEdge(0, 1, 4) grph.addEdge(0, 2, 6) grph.addEdge(0, 3, 16) grph.addEdge(1, 5, 24) grph.addEdge(2, 5, 23) grph.addEdge(2, 4, 5) grph.addEdge(2, 3, 8) grph.addEdge(3, 4, 10) grph.addEdge(3, 7, 21) grph.addEdge(4, 5, 18) grph.addEdge(4, 6, 11) grph.addEdge(4, 7, 14) grph.addEdge(5, 6, 9) grph.addEdge(6, 7, 7) expMST = [(0, 1, 4), (0, 2, 6), (2, 4, 5), (2, 3, 8), (4, 6, 11), (6, 7, 7), (6, 5, 9)] actualMST = grph.mspPrims() for edge in expMST: self.assertTrue(edge in actualMST) # third example grph = Graph() for v in range(7): grph.addVertex(v) grph.addEdge(0, 1, 4) grph.addEdge(0, 2, 8) grph.addEdge(1, 2, 9) grph.addEdge(1, 3, 8) grph.addEdge(1, 4, 10) grph.addEdge(2, 3, 2) grph.addEdge(2, 5, 1) grph.addEdge(3, 4, 7) grph.addEdge(3, 5, 9) grph.addEdge(4, 5, 5) grph.addEdge(4, 6, 6) grph.addEdge(5, 6, 2) expMST = [(0, 1, 4), (0, 2, 8), (2, 5, 1), (2, 3, 2), (5, 6, 2), (5, 4, 5)] actualMST = grph.mspPrims() for edge in expMST: self.assertTrue(edge in actualMST) # fourth example grph = Graph() for v in range(6): grph.addVertex(v) grph.addEdge(0, 1, 2) grph.addEdge(0, 2, 3) grph.addEdge(1, 2, 6) grph.addEdge(1, 3, 5) grph.addEdge(1, 4, 3) grph.addEdge(2, 4, 2) grph.addEdge(3, 4, 1) grph.addEdge(3, 5, 2) grph.addEdge(4, 5, 4) expMST = [(0, 1, 2), (0, 2, 3), (2, 4, 2), (4, 3, 1), (3, 5, 2)] actualMST = grph.mspPrims() for edge in expMST: self.assertTrue(edge in actualMST) def testKrushkals(self): expectedMST = [(2, 4, 1), (3, 4, 1), (4, 6, 2), (1, 2, 3), (0, 2, 4), (4, 5, 5)] actualTree = self.g.mspKrushkals() for edge in expectedMST: self.assertTrue(edge in actualTree) # second example grph = Graph() for v in range(7): grph.addVertex(v) grph.addEdge(0, 1, 5) grph.addEdge(0, 2, 7) grph.addEdge(1, 2, 9) grph.addEdge(1, 3, 6) grph.addEdge(1, 5, 15) grph.addEdge(2, 4, 8) grph.addEdge(2, 5, 7) grph.addEdge(3, 5, 8) grph.addEdge(3, 6, 11) grph.addEdge(4, 5, 5) grph.addEdge(5, 6, 9) expTree = [(0, 1, 5), (4, 5, 5), (1, 3, 6), (0, 2, 7), (2, 5, 7), (5, 6, 9)] actualTree = grph.mspKrushkals() for edge in expTree: self.assertTrue(edge in actualTree) def testFloyds(self): expected = { 0: {0: 0, 1: 6, 2: 4, 3: 13, 4: 5, 5: 10, 6: 7 }, 1: {0: float('inf'), 1: 0, 2: 3, 3: 12, 4: 4, 5: 7, 6: 6 }, 2: {0: float('inf'), 1: float('inf'), 2: 0, 3: 9, 4: 1, 5: 6, 6: 3 }, 3: {0: float('inf'), 1: float('inf'), 2: float('inf'), 3: 0, 4: 1, 5: 6, 6: 3 }, 4: {0: float('inf'), 1: float('inf'), 2: float('inf'), 3: float('inf'), 4: 0, 5: 5, 6: 2 }, 5: {0: float('inf'), 1: float('inf'), 2: float('inf'), 3: float('inf'), 4: float('inf'), 5: 0, 6: float('inf') }, 6: {0: float('inf'), 1: float('inf'), 2: float('inf'), 3: float('inf'), 4: float('inf'), 5: float('inf'), 6: 0 } } actual = self.g.floyds() self.assertDictEqual(expected, actual) # handy self.assertTrue(p==q for p, q in zip(expected.items(), actual.items())) #does the same self.assertTrue(expected.items() == actual.items()) #does the same """ grph = {0: {0: 0, 1: 1, 2: 4}, 1: {0: inf, 1: 0, 2: 2}, 2: {0: inf, 1: inf, 2: 0} } """ grph = Graph() for v in range(3): grph.addVertex(v) grph.addEdge(0, 1, 1) grph.addEdge(0, 2, 4) grph.addEdge(1, 2, 2) """ {0: {0: 0, 1: 1, 2: 3}, 1: {0: inf, 1: 0, 2: 2}, 2: {0: inf, 1: inf, 2: 0}} """ expected = {0: {0: 0, 1: 1, 2: 3}, 1: {0: float('inf'), 1: 0, 2: 2}, 2: {0: float('inf'), 1: float('inf'), 2: 0} } actual = grph.floyds() self.assertDictEqual(expected, actual) # handy self.assertTrue(p==q for p, q in zip(expected.items(), actual.items())) #does the same self.assertTrue(expected.items() == actual.items()) #does the same def testReachability(self): #uses floyd's algorithm expected = { 0: {0: True, 1: True, 2: True, 3: True, 4: True, 5: True, 6: True}, 1: {0: False, 1: True, 2: True, 3: True, 4: True, 5: True, 6: True}, 2: {0: False, 1: False, 2: True, 3: True, 4: True, 5: True, 6: True}, 3: {0: False, 1: False, 2: False, 3: True, 4: True, 5: True, 6: True}, 4: {0: False, 1: False, 2: False, 3: False, 4: True, 5: True, 6: True}, 5: {0: False, 1: False, 2: False, 3: False, 4: False, 5: True, 6: False}, 6: {0: False, 1: False, 2: False, 3: False, 4: False, 5: False, 6: True} } actual = self.g.reachability() self.assertDictEqual(expected, actual) def testPathRecoveryFloydWarshall(self): #ToDo: expectedParentMap = { 0: {0: -1, 1: 0, 2: 0, 3: 2, 4: 2, 5: 4, 6: 4}, 1: {0: -1, 1: -1, 2: 1, 3: 2, 4: 2, 5: 1, 6: 4}, 2: {0: -1, 1: -1, 2: -1, 3: 2, 4: 2, 5: 4, 6: 4}, 3: {0: -1, 1: -1, 2: -1, 3: -1, 4: 3, 5: 4, 6: 4}, 4: {0: -1, 1: -1, 2: -1, 3: -1, 4: -1, 5: 4, 6: 4}, 5: {0: -1, 1: -1, 2: -1, 3: -1, 4: -1, 5: -1, 6: -1}, 6: {0: -1, 1: -1, 2: -1, 3: -1, 4: -1, 5: -1, 6: -1} } actualMap = self.g.pathRecoveryFloydWarshall() self.assertDictEqual(expectedParentMap, actualMap) path = [] self.g.getFloydPath(actualMap, 0, 6, path) self.assertEqual([6, 4, 2, 0], path) path = [] self.g.getFloydPath(actualMap, 0, 5, path) #always shortest path self.assertEqual([5, 4, 2, 0], path) path = [] self.g.getFloydPath(actualMap, 0, 4, path) #always shortest path self.assertEqual([4, 2, 0], path) path = [] self.g.getFloydPath(actualMap, 0, 3, path) #always shortest path self.assertEqual([3, 2, 0], path) path = [] self.g.getFloydPath(actualMap, 0, 2, path) #always shortest path self.assertEqual([2, 0], path) path = [] self.g.getFloydPath(actualMap, 0, 1, path) #always shortest path self.assertEqual([1, 0], path) path = [] self.g.getFloydPath(actualMap, 1, 5, path) #always shortest path self.assertEqual([5, 1], path) path = [] self.g.getFloydPath(actualMap, 1, 6, path) #always shortest path self.assertEqual([6, 4, 2, 1], path) path = [] self.g.getFloydPath(actualMap, 2, 6, path) #always shortest path self.assertEqual([6, 4, 2], path) path = [] self.g.getFloydPath(actualMap, 2, 5, path) #always shortest path self.assertEqual([5, 4, 2], path) def testDijkstra(self): g = Graph() g.addVertex('s') g.addVertex('u') g.addVertex('v') g.addVertex('x') g.addVertex('y') g.addEdge('s', 'u', 10) g.addEdge('s', 'x', 5) g.addEdge('u', 'v', 1) g.addEdge('u', 'x', 2) g.addEdge('v', 'y', 4) g.addEdge('x', 'u', 3) g.addEdge('x', 'v', 9) g.addEdge('x', 'y', 2) g.addEdge('y', 's', 7) g.addEdge('y', 'v', 6) expected = ['y', 's'] actual = g.shortestPathDijkstra('y', 's') self.assertEqual(expected, actual) expected = ['s', 'x', 'u', 'v'] actual = g.shortestPathDijkstra('s', 'v') self.assertEqual(expected, actual) g.updateEdge('s', 'u', 1) expected = ['s', 'u', 'v'] actual = g.shortestPathDijkstra('s', 'v') self.assertEqual(expected, actual) def testTarjansSCC(self): expected = [('G',), ('B',), ('E', 'F', 'D'), ('C',), ('A',)] g = Graph() g.addVertex('A') g.addVertex('B') g.addVertex('C') g.addVertex('D') g.addVertex('E') g.addVertex('F') g.addVertex('G') g.addEdge('A', 'C', 1) g.addEdge('A', 'E', 1) g.addEdge('A', 'F', 1) g.addEdge('B', 'G', 1) g.addEdge('C', 'B', 1) g.addEdge('C', 'D', 1) g.addEdge('F', 'E', 1) g.addEdge('D', 'F', 1) g.addEdge('E', 'D', 1) g.addEdge('E', 'G', 1) self.assertEqual(expected, g.strongly_connected_components()) #case -2 expected = [(12, 11, 9, 10, 7, 8), (6, 3), (5, 4, 2), (1,)] g2 = Graph() for v in range(1, 13): g2.addVertex(v) g2.addEdge(1, 2, 1) g2.addEdge(2, 3, 1) g2.addEdge(2, 4, 1) g2.addEdge(2, 5, 1) g2.addEdge(3, 6, 1) g2.addEdge(4, 5, 1) g2.addEdge(4, 7, 1) g2.addEdge(5, 2, 1) g2.addEdge(5, 6, 1) g2.addEdge(5, 7, 1) g2.addEdge(6, 3, 1) g2.addEdge(6, 8, 1) g2.addEdge(7, 8, 1) g2.addEdge(7, 10, 1) g2.addEdge(8, 7, 1) g2.addEdge(9, 7, 1) g2.addEdge(10, 9, 1) g2.addEdge(10, 11, 1) g2.addEdge(11, 12, 1) g2.addEdge(12, 10, 1) self.assertEqual(expected, g2.strongly_connected_components()) """
def get_graph(self, entry_addr): from capstone import CS_OP_IMM, CS_ARCH_MIPS ARCH_UTILS = self.load_arch_module().utils gph = Graph(self, entry_addr) stack = [entry_addr] start = time() prefetch = None # WARNING: this assume that on every architectures the jump # address is the last operand (operands[-1]) # Here each instruction is a node. Blocks will be created in the # function __simplify. while stack: ad = stack.pop() inst = self.lazy_disasm(ad) if inst is None: # Remove all previous instructions which have a link # to this instruction. if ad in gph.link_in: for i in gph.link_in[ad]: gph.link_out[i].remove(ad) for i in gph.link_in[ad]: if not gph.link_out[i]: del gph.link_out[i] del gph.link_in[ad] continue if gph.exists(inst): continue if ARCH_UTILS.is_ret(inst): if self.arch == CS_ARCH_MIPS: prefetch = self.__prefetch_inst(inst) gph.new_node(inst, prefetch, None) elif ARCH_UTILS.is_uncond_jump(inst): if self.arch == CS_ARCH_MIPS: prefetch = self.__prefetch_inst(inst) gph.uncond_jumps_set.add(ad) op = inst.operands[-1] if op.type == CS_OP_IMM: nxt = op.value.imm stack.append(nxt) gph.new_node(inst, prefetch, [nxt]) else: if inst.address in self.jmptables: table = self.jmptables[inst.address].table stack += table gph.new_node(inst, prefetch, table) else: # Can't interpret jmp ADDR|reg gph.new_node(inst, prefetch, None) elif ARCH_UTILS.is_cond_jump(inst): if self.arch == CS_ARCH_MIPS: prefetch = self.__prefetch_inst(inst) gph.cond_jumps_set.add(ad) op = inst.operands[-1] if op.type == CS_OP_IMM: if self.arch == CS_ARCH_MIPS: direct_nxt = prefetch.address + prefetch.size else: direct_nxt = inst.address + inst.size nxt_jmp = op.value.imm stack.append(direct_nxt) stack.append(nxt_jmp) gph.new_node(inst, prefetch, [direct_nxt, nxt_jmp]) else: # Can't interpret jmp ADDR|reg gph.new_node(inst, prefetch, None) else: nxt = inst.address + inst.size stack.append(nxt) gph.new_node(inst, None, [nxt]) if len(gph.nodes) == 0: return None, 0 if self.binary.type == T_BIN_PE: nb_new_syms = self.binary.pe_reverse_stripped_symbols(self) else: nb_new_syms = 0 elapsed = time() elapsed = elapsed - start debug__("Graph built in %fs (%d instructions)" % (elapsed, len(gph.nodes))) return gph, nb_new_syms
You should have received a copy of the GNU General Public License along with RIAAPath. If not, see <http://www.gnu.org/licenses/>. """ import json import sys import os from lib.mbz import MusicBrainz from lib.graph import Graph if __name__ == "__main__": mbz = MusicBrainz() labels = mbz.get_labels() relations = mbz.get_relations() mbz.disconnect() graph = Graph() graph.add_labels(labels) graph.add_relations(relations) tree = graph.generate_riaa_tree() print(tree) if not os.path.isdir("dist"): os.mkdir("dist") with open("dist/riaalabels.js", "w") as output: output.write(json.dumps(tree))
class TestGraph(unittest.TestCase): def setUp(self): # A brand new graph. self.new = Graph() # A disconnected graph self.disconnected = Graph() self.disconnected.add_edge(1, 2) self.disconnected.add_edge(3, 4) # A complete graph self.complete = Graph() self.complete.add_edge(1, 2, 3) self.complete.add_edge(2, 3, 5) self.complete.add_edge(3, 4, 7) self.complete.add_edge(4, 1, 5) self.complete.add_edge(1, 3, 4) self.complete.add_edge(2, 4, 6) def test_has_edge(self): self.assertFalse(self.new.has_edge(1, 2)) self.assertFalse(self.disconnected.has_edge(2, 3)) self.assertFalse(self.disconnected.has_edge(1, 1)) self.assertFalse(self.disconnected.has_edge(5, 1)) self.assertTrue(self.disconnected.has_edge(2, 1)) def test_unweighted_edges(self): self.assertEqual(self.new.unweighted_edges(), set()) self.assertEqual(len(self.disconnected.unweighted_edges()), 2) self.assertEqual(len(self.complete.unweighted_edges()), 6) for graph in self.disconnected, self.complete: edges = graph.unweighted_edges() for (source, target) in edges: self.assertNotIn((target, source), edges) def test_weighted_edges(self): self.assertEqual(self.new.weighted_edges(), set()) self.assertEqual(len(self.disconnected.weighted_edges()), 2) self.assertEqual(len(self.complete.weighted_edges()), 6) edges = self.disconnected.weighted_edges() for (source, target, weight) in edges: self.assertEqual(weight, 1) self.assertNotIn((target, source, weight), edges) edges = self.complete.weighted_edges() for (source, target, weight) in edges: self.assertEqual(weight, source + target) self.assertNotIn((target, source, weight), edges) def test_topological_sort(self): for graph in self.new, self.disconnected, self.complete: self.assertEqual(graph.topological_sort(), []) def test_best_tour(self): self.assertIn(self.complete.best_tour(), [[1, 2, 3, 4, 1], [2, 3, 4, 1, 2], [3, 4, 1, 2, 3], [4, 1, 2, 3, 4]]) def test_minimum_spanning_tree(self): mst = self.complete.minimum_spanning_tree(4) self.assertEqual(mst.nodes(), self.complete.nodes()) self.assertEqual(mst.unweighted_edges(), {(4, 1), (1, 2), (1, 3)})
import socket from lib.graph import Graph, GraphEncoder, GraphDecoder from json import loads, dumps from bsonrpc import JSONRpc from bsonrpc.exceptions import FramingError from bsonrpc.framing import (JSONFramingNetstring, JSONFramingNone, JSONFramingRFC7464) # Cut-the-corners TCP Client: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 50001)) rpc = JSONRpc(s, framing_cls=JSONFramingNone) server = rpc.get_peer_proxy() graph = Graph() graph.add_node("leaf1") graph.add_node("leaf2") graph.add_node( "root", [graph.nodes["leaf1"], graph.nodes["leaf2"], graph.nodes["leaf1"]]) encoder = GraphEncoder() # Execute in server: result = loads(server.increment(encoder.default(graph)), cls=GraphDecoder) result.nodes["root"].show() result = loads(server.increment(encoder.default(result)), cls=GraphDecoder) result.nodes["root"].show() rpc.close() # Closes the socket 's' also
def main(): random.seed(params.seed) np.random.seed(params.seed) tic = time.time() logger = init_logger('_log/log_b{:d}_l{:d}_nd{:d}_seed{:d}'.format( params.batch_size, params.num_layer, params.node_dim, params.seed)) logger.info('parameters') logger.info(vars(params)) train, valid, test, train_moles, valid_moles = load_dataset(params.seed) train_moles = sorted(train_moles) valid_moles = sorted(valid_moles) valid.sort_values('molecule_name', inplace=True) logger.info('train moles: {} ...'.format(train_moles[:5])) logger.info('valid moles: {} ...'.format(valid_moles[:5])) test_moles = sorted(list(set(test['molecule_name']))) test.sort_values('molecule_name', inplace=True) logger.info('train data: {}'.format(train.shape)) logger.info('valid data: {}'.format(valid.shape)) logger.info('test data: {}'.format(test.shape)) structures = pd.read_csv('../../input/structures.csv') structures_groups = structures.groupby('molecule_name') bonds = pd.read_csv('../../input/bonds.csv') bonds_gp = bonds.groupby('molecule_name') train_charges = pd.read_csv('../../input/train_ob_charges.csv') train_charges_gp = train_charges.groupby('molecule_name') test_charges = pd.read_csv('../../input/test_ob_charges.csv') test_charges_gp = test_charges.groupby('molecule_name') train_targets = train.groupby('molecule_name') valid_targets = valid.groupby('molecule_name') test_targets = test.groupby('molecule_name') if params.debug: random.shuffle(train_moles) train_moles = train_moles[:5000] test_moles = test_moles[:1000] valid.sort_values('id', inplace=True) test.sort_values('id', inplace=True) list_atoms = list(set(structures['atom'])) train_graphs = dict() for mole in tqdm(train_moles): train_graphs[mole] = Graph(structures_groups.get_group(mole), bonds_gp.get_group(mole), list_atoms, train_charges_gp.get_group(mole)) valid_graphs = dict() for mole in tqdm(valid_moles): valid_graphs[mole] = Graph(structures_groups.get_group(mole), bonds_gp.get_group(mole), list_atoms, train_charges_gp.get_group(mole)) test_graphs = dict() for mole in tqdm(test_moles): test_graphs[mole] = Graph(structures_groups.get_group(mole), bonds_gp.get_group(mole), list_atoms, test_charges_gp.get_group(mole)) model = EdgeUpdateNet(num_layer=params.num_layer, node_dim=params.node_dim, edge_dim=params.edge_dim, gpu=params.gpu) if params.gpu >= 0: logger.info('transfer model to GPU {}'.format(params.gpu)) model.to_gpu(params.gpu) optimizer = optimizers.Adam(alpha=5e-4) optimizer.setup(model) model.cleargrads() epoch = 2 if params.debug else params.epoch for ep in range(epoch): logger.info('') logger.info('') logger.info('start epoch {}'.format(ep)) logger.info('') # ------------------------- logger.info('') logger.info('training') loss_value = 0 random.shuffle(train_moles) train_batches_moles = generate_batches(structures_groups, train_moles, params.batch_size) random.shuffle(train_batches_moles) for batch_moles in tqdm(train_batches_moles): list_train_X = list() list_train_y = list() for target_mol in batch_moles: list_train_X.append(train_graphs[target_mol]) list_train_y.append(train_targets.get_group(target_mol)) with chainer.using_config('train', ep == 0): loss = model(list_train_X, list_train_y) model.cleargrads() loss.backward() optimizer.update() loss_value += cuda.to_cpu(loss.data) logger.info('train loss: {:.3f}'.format( float(loss_value) / len(train_moles))) # ------------------------- logger.info('') logger.info('validation') valid_df = predicts(structures_groups, valid_moles, valid_graphs, valid_targets, model, params.batch_size) valid_pred = valid_df[['fc', 'sd', 'pso', 'dso']] valid_score = calc_score(valid, valid_pred.values) logger.info('valid score: {:.3f}'.format(valid_score)) # ------------------------- optimizer.alpha = optimizer.alpha * 0.95 logger.info('change learning rate: {:.6f}'.format(optimizer.alpha)) if (ep + 1) % 20 == 0: # ------------------------- # save model dir_model = Path('_model') logger.info('save model') dir_model.mkdir(exist_ok=True) serializers.save_npz( dir_model / 'model_ep{}_seed{}.npz'.format(ep, params.seed), model) # ------------------------- # make submission logger.info('') logger.info('test') test_df = predicts(structures_groups, test_moles, test_graphs, test_targets, model, params.batch_size) make_submission(test_df, ep, valid_score, params.seed, dir_sub=Path('_submission')) make_submission(valid_df, ep, valid_score, params.seed, dir_sub=Path('_valid')) toc = time.time() - tic logger.info('Elapsed time {:.1f} [min]'.format(toc / 60))
def testTarjansSCC(self): expected = [('G',), ('B',), ('E', 'F', 'D'), ('C',), ('A',)] g = Graph() g.addVertex('A') g.addVertex('B') g.addVertex('C') g.addVertex('D') g.addVertex('E') g.addVertex('F') g.addVertex('G') g.addEdge('A', 'C', 1) g.addEdge('A', 'E', 1) g.addEdge('A', 'F', 1) g.addEdge('B', 'G', 1) g.addEdge('C', 'B', 1) g.addEdge('C', 'D', 1) g.addEdge('F', 'E', 1) g.addEdge('D', 'F', 1) g.addEdge('E', 'D', 1) g.addEdge('E', 'G', 1) self.assertEqual(expected, g.strongly_connected_components()) #case -2 expected = [(12, 11, 9, 10, 7, 8), (6, 3), (5, 4, 2), (1,)] g2 = Graph() for v in range(1, 13): g2.addVertex(v) g2.addEdge(1, 2, 1) g2.addEdge(2, 3, 1) g2.addEdge(2, 4, 1) g2.addEdge(2, 5, 1) g2.addEdge(3, 6, 1) g2.addEdge(4, 5, 1) g2.addEdge(4, 7, 1) g2.addEdge(5, 2, 1) g2.addEdge(5, 6, 1) g2.addEdge(5, 7, 1) g2.addEdge(6, 3, 1) g2.addEdge(6, 8, 1) g2.addEdge(7, 8, 1) g2.addEdge(7, 10, 1) g2.addEdge(8, 7, 1) g2.addEdge(9, 7, 1) g2.addEdge(10, 9, 1) g2.addEdge(10, 11, 1) g2.addEdge(11, 12, 1) g2.addEdge(12, 10, 1) self.assertEqual(expected, g2.strongly_connected_components())
return None def infer(self, params): self.calc_dominance_distributions('in', params) self.calc_dominance_distributions('out', params) for user in self.users.iter(): if user['location_point'] == None: user['location_point'] = self.infer_one(user['id'], params) def get_users(self): return self.users if __name__ == '__main__': import sys from lib.users import Users from lib.graph import Graph if len(sys.argv) < 3: print '[usage]: python %s [users file path] [graph file path]' % sys.argv[0] exit() users = Users() users.load_file(sys.argv[1]) graph = Graph() graph.load_file(sys.argv[2]) lmm = LMM(users, graph) lmm.infer() print lmm.get_users()
def get_graph(self, addr): from capstone import CS_OP_IMM ARCH_UTILS = self.load_arch_module().utils curr = self.lazy_disasm(addr) gph = Graph(self, addr) rest = [] start = time.clock() while 1: if not gph.exists(curr): if ARCH_UTILS.is_uncond_jump(curr) and len(curr.operands) > 0: if curr.operands[0].type == CS_OP_IMM: addr = curr.operands[0].value.imm nxt = self.lazy_disasm(addr) gph.set_next(curr, nxt) rest.append(nxt.address) else: # Can't interpret jmp ADDR|reg gph.add_node(curr) gph.uncond_jumps_set.add(curr.address) elif ARCH_UTILS.is_cond_jump(curr) and len(curr.operands) > 0: if curr.operands[0].type == CS_OP_IMM: nxt_jump = self.lazy_disasm(curr.operands[0].value.imm) direct_nxt = self.lazy_disasm(curr.address + curr.size) gph.set_cond_next(curr, nxt_jump, direct_nxt) rest.append(nxt_jump.address) rest.append(direct_nxt.address) else: # Can't interpret jmp ADDR|reg gph.add_node(curr) gph.cond_jumps_set.add(curr.address) elif ARCH_UTILS.is_ret(curr): gph.add_node(curr) else: try: nxt = self.lazy_disasm(curr.address + curr.size) gph.set_next(curr, nxt) rest.append(nxt.address) except: gph.add_node(curr) pass try: curr = self.lazy_disasm(rest.pop()) except IndexError: break if self.binary.type == T_BIN_PE: self.binary.pe_reverse_stripped_symbols(self) elapsed = time.clock() elapsed = elapsed - start debug__("Graph built in %fs" % elapsed) return gph
server = cfg.get("zabbix_server") username = cfg.get("zabbix_user") password = cfg.get("zabbix_pw") graph_path = cfg.get("graph_dir") period = cfg.get('period') stime = cfg.get('start_time') template_name = cfg.get('template_name') client_name = template_name.split('.')[0] template = cfg.get('template_dir') + template_name output_path = cfg.get('output_dir') output_name = client_name + '_' + now + '.docx' zp = ZAPI(server, username, password) gph = Graph(server, username, password) # doc = Document(unicode(template)) doc = Document(template) # pdb.set_trace() for p in doc.paragraphs: print(p.text) if p.text.strip().startswith('zabbix_graph'): m=re.match('^zabbix_graph\|([^|]*)\|([^|]*)$', p.text.strip()) val1, val2 = m.groups() hostname = val1.strip() graph = val2.strip() host_id = zp.get_host_id_by_name(hostname) graph_id = zp.get_graph_id(host_id, graph) graph_name = graph_path + '_'.join([hostname, graph_id, now, '.png']) gph.get_graph(graph_id,period, stime, graph_name)
print '\tkinsella' exit() args = {} for i in range(1, len(sys.argv)): key, value = sys.argv[i].split(':') args[key] = value test_users = Users() test_users.load_file(args['test']) training_users = Users() training_users.load_file(args['training']) ev = Evaluation(test_users) if args['method'] == 'naiveg': graph = Graph() graph.load_file(args['graph']) method = NaiveG(training_users, graph) elif args['method'] == 'naivec': db = DB(args['dbuser'], args['dbpass'], args['dbname']) tweets = Tweets(db) venues = Venues(db) method = NaiveC(training_users, tweets, venues) elif args['method'] == 'li': db = DB(args['dbuser'], args['dbpass'], args['dbname']) tweets = Tweets(db) venues = Venues(db) graph = Graph() graph.load_file(args['graph']) method = UDI(training_users, tweets, venues, graph) elif args['method'] == 'jurgens':
def graph(): return Graph()
import numpy as np import tensorflow as tf from tensorflow.keras.layers import Input from lib.gcn import GraphConv, TemporalConv from lib.graph import Graph skeleton = Graph("sbu", "spatial") input_features = Input([30, skeleton.num_node, 3], dtype="float32") x = tf.keras.layers.Conv2D(64 * 3, (3, 1), padding="same")(input_features) input_A = Input(tensor=tf.keras.backend.constant(skeleton.A)) x, A = GraphConv(64, t_kernels=3)([input_features, input_A]) x = TemporalConv(64, dropout=0.5)(x) x, A = GraphConv(128, t_kernels=3)([x, A]) x = TemporalConv(128, dropout=0.5)(x) print(x.shape)
def test_edge_weight(self): g = Graph() g.add_edge(0, 1, 5) self.assertTrue(g.get_edge_weight(0, 1) == 5) g.increase_edge_weight(0, 1, 1) self.assertTrue(g.get_edge_weight(0, 1) == 6) g.increase_edge_weight(0, 2, 3) self.assertTrue(g.get_edge_weight(0, 2) == 3)
def main(): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers() log_parsers = get_log_parsers() parse_cmd_parser = subparsers.add_parser("parse") parse_cmd_parser.add_argument( "--parser", help=( "The parser used to extract time information from a log. Valid " "options: {0}".format(", ".join(log_parsers.keys())) ) ) parse_cmd_parser.add_argument( "--min-duration", default="00:00:10.000", help="The minimum duration of a valid task" ) parse_cmd_parser.add_argument( "logfile", help=( "Log file containing necessary timing information. See README.md " "for more information" ) ) parse_cmd_parser.set_defaults(which="parse") graph_cmd_parser = subparsers.add_parser("graph") graph_cmd_parser.add_argument( "--imagefile", default="graph.png", help="File location to write image to" ) graph_cmd_parser.add_argument( "--title", default="Performance Profile", help="Title to appear on graph" ) graph_cmd_parser.add_argument( "--subtitle", default="", help="Subtitle to appear on graph" ) graph_cmd_parser.add_argument( "dsvfile", help=( "File containing semicolon-delimited values. See README.md " "for more information" ) ) graph_cmd_parser.set_defaults(which="graph") args = parser.parse_args() if args.which == "parse": if args.parser in log_parsers: log_parsers[args.parser].parse(args.logfile, args.min_duration) else: sys.exit("Please specify a valid parser") elif args.which == "graph": print "Wrote: " + Graph.render( args.dsvfile, args.imagefile, args.title, args.subtitle), print
def test_add_edge(self): g = Graph() g.add_edge(0, 1) self.assertTrue((0, 1) in g) self.assertTrue((1, 0) not in g)
>>> end = "Penzance" >>> cities.weight(cities.shortest_path(start, end)) 284 >>> cities.weighted_distance(start, end) 284 Although all cities are directly connected to each other, the shortest path is not necessarily the direct path. >>> cities.shortest_path(start, end) ['Luton', 'Nuneaton', 'Penzance'] """ from lib.graph import Graph cities = Graph() cities.add_edge("Scunthorpe", "Bridlington", 31) cities.add_edge("Scunthorpe", "Wick", 514) cities.add_edge("Scunthorpe", "Bognor", 252) cities.add_edge("Scunthorpe", "Nuneaton", 111) cities.add_edge("Scunthorpe", "Luton", 117) cities.add_edge("Scunthorpe", "Wrexham", 142) cities.add_edge("Scunthorpe", "Penzance", 318) cities.add_edge("Bridlington", "Luton", 142) cities.add_edge("Bridlington", "Nuneaton", 115) cities.add_edge("Bridlington", "Bognor", 209) cities.add_edge("Bridlington", "Penzance", 426) cities.add_edge("Bridlington", "Wrexham", 162) cities.add_edge("Bridlington", "Wick", 512) cities.add_edge("Luton", "Wick", 627) cities.add_edge("Luton", "Bognor", 112)
] bands = [ Band(id=1, cost=1, frequency_range_from=1, frequency_range_to=384, loss_per_km=0.046), Band(id=2, cost=2, frequency_range_from=385, frequency_range_to=768, loss_per_km=0.055), ] graph = Graph(Graphical(data_xml_root)) demands = read_demands(data_xml_root, graph) problem = Problem(graph, demands, transponders, bands) gen_alg = GeneticAlgorithm(problem, 2000, 5000, 0.01) gen_alg.init_population() period_of_save_result = 10 i = 1 while True: gen_alg.generate_new_population() print("Epoch nr", i) print("Best result:") print(" Cost = ", gen_alg.P[0].get_cost())
def run(): if interactive: print '''Beaver %s''' % __version__ graph = Graph(verbose=args.verbose) for input_file in args.file: try: graph.parse(filename=input_file) except KeyboardInterrupt: print "KeyboardInterrupt" sys.exit() except Exception as e: print e sys.exit() if args.eval: try: graph.parse(text=args.eval) except KeyboardInterrupt: print "KeyboardInterrupt" sys.exit() except Exception as e: print e sys.exit() if interactive: import readline exit = False while not exit: graph.verbose = args.verbose try: next_line = raw_input('>> ').strip() if not next_line: continue if next_line[0] == '-' and next_line.split(' ')[0] in arg_parser._option_string_actions: command = next_line.split(' ')[0] action = arg_parser._option_string_actions[command].dest if len(next_line.split(' ')) > 1: arg = ' '.join(next_line.split(' ')[1:]) try: arg = eval(arg) except: pass else: arg = not getattr(args, action) try: setattr(args, action, arg) except: print 'Illegal argument: %s %s' % (command, arg) elif next_line in ('exit', 'quit'): exit = True else: stmts = graph.parse(text=next_line) if stmts == 0: raise BeaverException('Failed to parse line: %s' % next_line) except EOFError: print exit = True except KeyboardInterrupt: print continue except Exception as e: print e continue if args.out: if args.out is True: filename = None else: filename = args.out if not filename.startswith('<') and filename.endswith('>'): filename = '<%s>' % os.path.abspath(filename) filename = Uri(filename) graph.execute(OutCommand(filename)) if args.draw: graph.draw(args.draw)
def get_graph(self, entry_addr): from capstone import CS_OP_IMM, CS_ARCH_MIPS ARCH_UTILS = self.load_arch_module().utils gph = Graph(self, entry_addr) stack = [entry_addr] start = time() prefetch = None addresses = set() # WARNING: this assume that on every architectures the jump # address is the last operand (operands[-1]) # Here each instruction is a node. Blocks will be created in the # function __simplify. while stack: ad = stack.pop() inst = self.lazy_disasm(ad) if inst is None: # Remove all previous instructions which have a link # to this instruction. if ad in gph.link_in: for i in gph.link_in[ad]: gph.link_out[i].remove(ad) for i in gph.link_in[ad]: if not gph.link_out[i]: del gph.link_out[i] del gph.link_in[ad] continue if gph.exists(inst): continue addresses.add(ad) if ARCH_UTILS.is_ret(inst): if self.arch == CS_ARCH_MIPS: prefetch = self.__prefetch_inst(inst) addresses.add(prefetch.address) gph.new_node(inst, prefetch, None) elif ARCH_UTILS.is_uncond_jump(inst): if self.arch == CS_ARCH_MIPS: prefetch = self.__prefetch_inst(inst) addresses.add(prefetch.address) gph.uncond_jumps_set.add(ad) op = inst.operands[-1] if op.type == CS_OP_IMM: nxt = op.value.imm stack.append(nxt) gph.new_node(inst, prefetch, [nxt]) else: if inst.address in self.jmptables: table = self.jmptables[inst.address].table stack += table gph.new_node(inst, prefetch, table) else: # Can't interpret jmp ADDR|reg gph.new_node(inst, prefetch, None) elif ARCH_UTILS.is_cond_jump(inst): if self.arch == CS_ARCH_MIPS: prefetch = self.__prefetch_inst(inst) addresses.add(prefetch.address) gph.cond_jumps_set.add(ad) op = inst.operands[-1] if op.type == CS_OP_IMM: if self.arch == CS_ARCH_MIPS: direct_nxt = prefetch.address + prefetch.size else: direct_nxt = inst.address + inst.size nxt_jmp = op.value.imm stack.append(direct_nxt) stack.append(nxt_jmp) gph.new_node(inst, prefetch, [direct_nxt, nxt_jmp]) else: # Can't interpret jmp ADDR|reg gph.new_node(inst, prefetch, None) else: nxt = inst.address + inst.size stack.append(nxt) gph.new_node(inst, None, [nxt]) if len(gph.nodes) == 0: return None, 0 if self.binary.type == T_BIN_PE: nb_new_syms = self.binary.pe_reverse_stripped_symbols(self, addresses) else: nb_new_syms = 0 elapsed = time() elapsed = elapsed - start debug__("Graph built in %fs (%d instructions)" % (elapsed, len(gph.nodes))) return gph, nb_new_syms
def testDijkstra(self): g = Graph() g.addVertex('s') g.addVertex('u') g.addVertex('v') g.addVertex('x') g.addVertex('y') g.addEdge('s', 'u', 10) g.addEdge('s', 'x', 5) g.addEdge('u', 'v', 1) g.addEdge('u', 'x', 2) g.addEdge('v', 'y', 4) g.addEdge('x', 'u', 3) g.addEdge('x', 'v', 9) g.addEdge('x', 'y', 2) g.addEdge('y', 's', 7) g.addEdge('y', 'v', 6) expected = ['y', 's'] actual = g.shortestPathDijkstra('y', 's') self.assertEqual(expected, actual) expected = ['s', 'x', 'u', 'v'] actual = g.shortestPathDijkstra('s', 'v') self.assertEqual(expected, actual) g.updateEdge('s', 'u', 1) expected = ['s', 'u', 'v'] actual = g.shortestPathDijkstra('s', 'v') self.assertEqual(expected, actual)