def create_graph(self): graph = Graph(directed=False, multiedged=False) for i, level in enumerate(self._field): for j, cell, in enumerate(level): start = i, j ends = [i for i in self.get_environment(i, j)] for valid_end in (end for end in ends if self.is_valid(*end)): graph.add_edge(start, valid_end) return graph
def setUp(self): self.test_solver = solver.Solver() self.field1 = TriangleField([[1], [1, 0, 2], [3, 0, 3, 0, 2]]) self.field2 = TriangleField([[1], [1, 0, 2], [1, 0, 3, 0, 2], [3, 0, 0, 0, 4, 0, 4]]) self.field3 = TriangleField([[1], [1, 0, 2]]) self.graph = Graph(directed=False) self.graph.add_edge('a', 'b') self.graph.add_edge('a', 'c') self.graph.add_edge('c', 'b') self.graph.add_edge('b', 'd')
def make_graph(self): """ Строит граф шестиугольного поля и возвращает его. """ graph = Graph(directed=False, multiedged=False) middle = len(self.field) // 2 for i, level in enumerate(self.field): for j, cell, in enumerate(level): start = i, j ends = {(i + 1, j), (i, j + 1), (i + 1, j + 1) if i < middle else (i + 1, j - 1)} for valid_end in (end for end in ends if self.is_valid(*end)): graph.add_edge(start, valid_end) return graph
def setUp(self): self.instance_one_solution = HexLink([ [1, 0], # 1 0 [0, 2, 1], # 0 2 1 [0, 2] # 0 2 ]) self.instance_many_solutions = HexLink([ [1, 2], # 1 2 [0, 0, 0], # 0 0 0 [1, 2], # 1 2 ]) self.instance_no_solutions = HexLink([ [1, 2], # 1 2 [0, 0, 0], # 0 0 0 [2, 1] # 2 1 ]) self.graph = Graph(directed=False) self.graph.add_edge("p", "q") # e_1 self.graph.add_edge("p", "r") # e_2 self.graph.add_edge("r", "q") # e_3 self.graph.add_edge("q", "s") # e_4
#!/usr/bin/env python3 from test_more import ok from graph_tools import Graph g = Graph(directed=False) g.add_edge(1, 2) g.add_edge(2, 3) g.add_vertex(4) ok(len(g.components()) == 2) ok(len(g.maximal_component()) == 3)
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=False) g.create_li_maini_graph() print(g)
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=False, multiedged=True) g = g.create_graph('barandom', 10, 20, 2) ok(g) eq(len(g.vertices()), 10) ok(g.is_connected()) g = Graph(directed=False, multiedged=True) g = g.create_graph('barandom', 100, 200, 2) ok(g) eq(len(g.vertices()), 100) ok(g.is_connected())
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=True) g.add_vertices(1, 2, 3) g.add_edge(1, 2) g.add_edge(1, 2) g.add_edge(2, 3) eq(len(g.neighbors(1)), 1) eq(len(g.neighbors(2)), 2) eq(len(g.neighbors(3)), 1) g.delete_vertex(2) eq(len(g.neighbors(1)), 0) eq(len(g.neighbors(3)), 0) g = Graph(directed=False) g.add_vertices(1, 2, 3) g.add_edge(1, 2) g.add_edge(2, 3) g.delete_vertex(1) eq(len(g.vertices()), 2) eq(len(g.neighbors(2)), 1) eq(len(g.neighbors(3)), 1)
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=False, multiedged=True) ok(g) ok(not g.directed()) ok(g.multiedged()) g = Graph(directed=True, multiedged=True) ok(g) ok(g.directed()) ok(g.multiedged())
#!/usr/bin/env python3 from test_more import ok from graph_tools import Graph g = Graph(directed=True) g.add_edge(1, 2) g.add_edge(2, 3) g.add_vertex(4) ok(g.is_reachable(1, 2)) ok(g.is_reachable(2, 3)) ok(g.is_reachable(1, 3)) ok(not g.is_reachable(2, 1)) ok(not g.is_reachable(3, 2)) ok(not g.is_reachable(3, 1))
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=True, multiedged=True) g = g.create_graph('tree', 10) ok(g) eq(len(g.vertices()), 10) eq(len(g.edges()), 10 - 1) g = Graph(directed=True, multiedged=True) g = g.create_graph('tree', 100) ok(g) eq(len(g.vertices()), 100) eq(len(g.edges()), 100 - 1) g = Graph(directed=False, multiedged=True) g = g.create_graph('tree', 10) ok(g) eq(len(g.vertices()), 10) eq(len(g.edges()), 10 - 1) ok(g.is_connected())
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=True) g.add_vertices(1, 2, 3) g.add_edge(1, 2) g.add_edge(1, 2) g.add_edge(2, 3) name, val, val2 = 'foo', 'bar', 'buzz' g.set_graph_attribute(name, val) eq(g.get_graph_attribute(name), val) g.set_vertex_attribute(1, name, val) eq(g.get_vertex_attribute(1, name), val) g.set_edge_attribute_by_id(1, 2, 0, name, val) eq(g.get_edge_attribute_by_id(1, 2, 0, name), val) g.set_edge_attribute_by_id(1, 2, 1, name, val2) eq(g.get_edge_attribute_by_id(1, 2, 1, name), val2) g.set_edge_weight_by_id(1, 2, 0, val) eq(g.get_edge_weight_by_id(1, 2, 0), val) g.set_edge_weight_by_id(1, 2, 1, val2) eq(g.get_edge_weight_by_id(1, 2, 1), val2)
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=False, multiedged=True) g = g.create_graph('unknown') ok(not g) g = Graph(directed=True, multiedged=True) g = g.create_graph('random', 4, 10) ok(g) eq(len(g.vertices()), 4) eq(len(g.edges()), 10) ok(g.is_connected()) g = Graph(directed=True, multiedged=True) g = g.create_graph('random', 40, 100) ok(g) eq(len(g.vertices()), 40) eq(len(g.edges()), 100) ok(g.is_connected()) g = Graph(directed=True, multiedged=True) g = g.create_graph('random', 4, 10) ok(g) eq(len(g.vertices()), 4) eq(len(g.edges()), 10)
#!/usr/bin/env python3 from test_more import ok from graph_tools import Graph g = Graph(directed=True) g.add_edge(1, 2) g.add_edge(2, 3) g.floyd_warshall() ok(g.T[1][2] == 1) ok(g.T[2][3] == 1) ok(g.T[1][3] == 2) ok(not g.T[2].get(1, None)) ok(not g.T[3].get(2, None)) ok(not g.T[3].get(1, None))
#!/usr/bin/env python3 from graph_tools import Graph connections = [(1, 2), (2, 3), (3, 1), (3, 4)] g = Graph(connections) print(g._graph) print("add (1, 5)") g.add(1, 5) print(g._graph) print("remove 5") g.remove(5) print(g._graph) print(g.get_adj(1)) print(f"(1, 2) is connected:", g.is_connected(1, 2)) print(f"(2, 3) is connected:", g.is_connected(2, 3)) print(f"find_path from 1 to 4:", g.find_path(1, 4))
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=False) g.create_configuration_graph([3, 2, 2, 1, 1, 1, 0]) ok(g.degree(1) == 3) ok(g.degree(2) == 2) ok(g.degree(3) == 2) ok(g.degree(4) == 1) ok(g.degree(5) == 1) ok(g.degree(6) == 1) ok(g.degree(7) == 0) g = Graph(directed=False) g.create_configuration_graph([3, 3, 3, 3, 3, 3]) for v in g.vertices(): ok(g.degree(v) == 3) g = Graph(directed=False) g.create_random_regular_graph(10, 3) for v in g.vertices(): ok(g.degree(v) == 3)
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=False) g.add_vertices(1, 2, 3) g.add_edge(1, 2) g.add_edge(2, 3) ok(g.betweenness(1) == 0) ok(g.betweenness(2) == 2) ok(g.betweenness(3) == 0) """ 2 / \ 1 -- 3 --4 """ g = Graph(directed=False) g.add_vertices(1, 2, 3, 4) g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(1, 3) g.add_edge(3, 4) ok(g.betweenness(1) == 0) ok(g.betweenness(2) == 0) ok(g.betweenness(3) == 4) ok(g.betweenness(4) == 0) """ 2 // \
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph """ 2 / \ 1 -- 3 --4 """ g = Graph(directed=False) g.add_vertices(1, 2, 3, 4) g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(1, 3) g.add_edge(3, 4) print(g.adjacency_matrix()) print(g.diagonal_matrix()) print(g.spectral_radius()) print(g.spectral_gap()) print(g.natural_connectivity()) print(g.algebraic_connectivity()) print(g.effective_resistance()) print(g.spanning_tree_count()) """ 2 / \ 1 -- 3 4 """
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=True, multiedged=True) g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(3, 1) g.set_vertex_attribute(1, 'foo', 123) g.set_edge_attribute_by_id(1, 2, 0, 'bar', 456) T = g.copy_graph() ok(T.directed()) ok(T.multiedged()) ok(T.has_edge(1, 2)) ok(not T.has_edge(2, 1)) eq(T.get_vertex_attribute(1, 'foo'), 123) eq(T.get_edge_attribute_by_id(1, 2, 0, 'bar'), 456) g = Graph(directed=False, multiedged=True) g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(3, 1) g.set_vertex_attribute(1, 'foo', 123) g.set_edge_attribute_by_id(1, 2, 0, 'bar', 456) T = g.copy_graph() ok(T.undirected()) ok(T.multiedged()) ok(T.has_edge(1, 2)) ok(T.has_edge(2, 1)) eq(T.get_vertex_attribute(1, 'foo'), 123)
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=False) ok(g) g.add_vertices(1, 2, 3, 4) h = g.complete_graph() ok(h.is_connected()) eq(len(h.vertices()), 4) eq(len(h.edges()), 6) eq(h.vertex_degree(1), 3)
def test_make_graph(self): expected = Graph(directed=False) # Top-Down expected.add_edge((0, 0), (1, 0)) expected.add_edge((1, 0), (2, 0)) expected.add_edge((0, 1), (1, 1)) expected.add_edge((1, 1), (2, 1)) # Left-Right expected.add_edge((0, 0), (0, 1)) expected.add_edge((1, 0), (1, 1)) expected.add_edge((1, 1), (1, 2)) expected.add_edge((2, 0), (2, 1)) # Diagonal expected.add_edge((0, 0), (1, 1)) expected.add_edge((0, 1), (1, 2)) expected.add_edge((2, 0), (1, 1)) expected.add_edge((2, 1), (1, 2)) actual = self.simple_game.make_graph() self.assertSetEqual(set(expected.vertices()), set(actual.vertices())) self.assertSetEqual(set(frozenset(edge) for edge in expected.edges()), set(frozenset(edge) for edge in actual.edges()))
#!/usr/bin/env python3 import re from test_more import ok, eq from graph_tools import Graph g = Graph(directed=True, multiedged=True) buf = """digraph sample { 1; 2; 1 -> 2; } """.splitlines() g.import_graph('dot', buf) eq(len(g.vertices()), 2) eq(len(g.edges()), 1) ok(g.is_directed()) ok(g.has_edge(1, 2)) g = Graph(directed=True, multiedged=True) buf = """// comment here digraph sample { 1; /* another comment here */ 2; 4; 1 -> 2; 1 -> 4; } """.splitlines()
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=False) ok(g) ok(not g.is_directed()) ok(g.is_undirected()) g.add_vertices(1, 2, 3, 4) ok(g.has_vertex(1)) ok(g.has_vertex(2)) ok(g.has_vertex(3)) ok(g.has_vertex(4)) ok(not g.has_vertex(5)) ok(not g.has_vertex(0)) g.add_edge(1, 2) g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(3, 4) ok(g.has_edge(1, 2)) ok(g.has_edge(2, 3)) ok(g.has_edge(2, 1)) ok(not g.has_edge(1, 3)) ok(not g.has_edge(4, 1)) eq(len(g.vertices()), 4) eq(len(g.neighbors(1)), 1) eq(len(g.neighbors(2)), 2)
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=False, multiedged=True) g = g.create_graph('barabasi', 10, 2, 2) ok(g) eq(len(g.vertices()), 10) eq(len(g.edges()), 2 * (2 - 1) / 2 + (10 - 2) * 2) ok(g.is_connected()) g = Graph(directed=False, multiedged=True) g = g.create_graph('barabasi', 100, 3, 4) ok(g) eq(len(g.vertices()), 100) eq(len(g.edges()), 3 * (3 - 1) / 2 + (100 - 3) * 4) ok(g.is_connected()) g = Graph(directed=False, multiedged=True) g = g.create_graph('ba', 10, 2, 2) ok(g) eq(len(g.vertices()), 10) eq(len(g.edges()), 2 * (2 - 1) / 2 + (10 - 2) * 2) ok(g.is_connected())
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=True) eq(len(g.get_multiedge_ids(1, 2)), 0) g.add_edge(1, 2) g.add_edge(2, 1) ok(g.has_edge(1, 2)) ok(g.has_edge(2, 1)) eq(len(g.get_multiedge_ids(1, 2)), 1) eq(len(g.get_multiedge_ids(2, 1)), 1) eq(len(g.neighbors(1)), 1) eq(len(g.neighbors(2)), 1) g.add_edge(1, 2) ok(g.has_edge(1, 2)) ok(g.has_edge(2, 1)) eq(len(g.get_multiedge_ids(1, 2)), 2) eq(len(g.get_multiedge_ids(2, 1)), 1) eq(len(g.neighbors(1)), 1) eq(len(g.neighbors(2)), 1)
class SolverTest(unittest.TestCase): def setUp(self): self.test_solver = solver.Solver() self.field1 = TriangleField([[1], [1, 0, 2], [3, 0, 3, 0, 2]]) self.field2 = TriangleField([[1], [1, 0, 2], [1, 0, 3, 0, 2], [3, 0, 0, 0, 4, 0, 4]]) self.field3 = TriangleField([[1], [1, 0, 2]]) self.graph = Graph(directed=False) self.graph.add_edge('a', 'b') self.graph.add_edge('a', 'c') self.graph.add_edge('c', 'b') self.graph.add_edge('b', 'd') def test_update_neighbors(self): self.method_tester( expected={ 'a': 'b', 'b': 'a', 'c': 'c', 'd': 'd' }, parent_node=Node(self.graph.edges()[0], {v: v for v in self.graph.vertices()}, 1), main_path=list('abcd')) self.method_tester(expected={ 'b': 'c', 'c': 'b', 'd': 'd' }, parent_node=Node(self.graph.edges()[1], { 'a': 'b', 'b': 'a', 'c': 'c', 'd': 'd' }, 1), main_path=list('bcd')) self.method_tester(expected={ 'b': 0, 'd': 'd' }, parent_node=Node(self.graph.edges()[2], { 'b': 'a', 'c': 'c', 'd': 'd' }, 1), main_path=list('bd')) def method_tester(self, expected, parent_node, main_path): actual = self.test_solver.update_main_path( self.test_solver.update_neighbors(parent_node), main_path) self.assertDictEqual(expected, actual) def test_solve(self): expected = [[(0, 0), (1, 1)], [(1, 0), (1, 1)], [(1, 2), (2, 3)], [(2, 0), (2, 1)], [(2, 1), (2, 2)], [(2, 3), (2, 4)]] actual = list(self.test_solver.solve(self.field1)) assert len(actual) == 64 self.assertListEqual(expected, actual[0]) expected = [] actual = list(self.test_solver.solve(self.field2)) self.assertListEqual(expected, actual) actual = list(self.test_solver.solve(self.field3)) self.assertListEqual(expected, actual)
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=True, multiedged=True) g = g.create_graph('ring', 10, 1) ok(g) eq(len(g.vertices()), 10) eq(len(g.edges()), 10) for v in range(1, 11): eq(g.in_degree(v), 1) eq(g.out_degree(v), 1) g = Graph(directed=False, multiedged=True) g = g.create_graph('ring', 10, 1) ok(g) eq(len(g.vertices()), 10) eq(len(g.edges()), 10) for v in range(1, 11): eq(g.degree(v), 2) ok(g.is_connected())
#!/usr/bin/env python3 from graph_tools import Graph from collections import deque import pprint H, W = map(int, input().split()) S = [] for i in range(H): l = str(input()) S.append(list(l)) # print(H, W) # print(S) connections = [] g = Graph(connections) dist = [-1 for i in range(H * W)] dq = deque() # init dq.append(0) dist[0] = 0 while dq != []: v = dq.pop() for v_adj in g.adjacency_list(): # undiscovered vertex if (dist[v_adj] == -1): dist[v_adj] = dist[v] + 1 dq.append(v_adj)
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=True) ok(g) ok(g.is_directed()) ok(not g.is_undirected()) g.add_vertices(1, 2, 3, 4) ok(g.has_vertex(1)) ok(g.has_vertex(2)) ok(g.has_vertex(3)) ok(g.has_vertex(4)) ok(not g.has_vertex(5)) ok(not g.has_vertex(0)) g.add_edge(1, 2) g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(3, 4) ok(g.has_edge(1, 2)) ok(g.has_edge(2, 3)) ok(not g.has_edge(2, 1)) ok(not g.has_edge(1, 3)) ok(not g.has_edge(4, 1)) eq(len(g.vertices()), 4) eq(len(g.successors(1)), 1) eq(len(g.predecessors(1)), 0)
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=True) g.add_vertices(1, 2, 3) g.add_edge(1, 2) g.add_edge(1, 2) g.add_edge(2, 3) eq(len(g.edges()), 3) eq(len(g.get_multiedge_ids(1, 2)), 2) eq(len(g.neighbors(1)), 1) eq(len(g.neighbors(2)), 2) g.delete_edge(1, 2) eq(len(g.edges()), 2) eq(len(g.get_multiedge_ids(1, 2)), 1) eq(len(g.neighbors(1)), 1) eq(len(g.neighbors(2)), 2) g.delete_edge(1, 2) eq(len(g.edges()), 1) eq(len(g.get_multiedge_ids(1, 2)), 0) eq(len(g.neighbors(1)), 0) eq(len(g.neighbors(2)), 1) g.delete_edge(1, 2)