def test_undirected_graph_node_add_edge(): # Make graph g = make_g1() assert out_degree(u, g) == 0 assert num_edges(g) == 0 # Add e1 (e1, added1) = add_edge(u, v, g) assert added1 (e, found) = edge(u, v, g) assert found assert e == e1 assert out_degree(u, g) == 1 assert num_edges(g) == 1 # No arc (e, found) = edge(u, w, g) assert not found assert {e for e in out_edges(u, g)} == {e1} # Add e2 (e2, added2) = add_edge(u, w, g) assert added2 assert {e for e in out_edges(u, g)} == {e1, e2} assert out_degree(u, g) == 2 assert num_edges(g) == 2
def test_undirected_graph_add_vertex(): g = make_g2() assert num_vertices(g) == 3 assert num_edges(g) == 5 # Add vertex x x = add_vertex(g) assert num_vertices(g) == 4 # Add edge (v -> x) add_edge(v, x, g) assert num_edges(g) == 6 assert out_degree(v, g) == 4
def test_undirected_graph_remove_edge(): g = make_g2() assert num_edges(g) == 5 (e, found) = edge(v, w, g) assert found remove_edge(e, g) assert num_edges(g) == 4 (e, found) = edge(w, w, g) print(g.adjacencies) assert found remove_edge(e, g) assert num_edges(g) == 3
def test_undirected_graph_remove_vertex(): g = make_g2() assert num_vertices(g) == 3 assert num_edges(g) == 5 remove_vertex(v, g) assert num_vertices(g) == 2 assert num_edges(g) == 2 remove_vertex(w, g) assert num_vertices(g) == 1 assert num_edges(g) == 0 remove_vertex(u, g) assert num_vertices(g) == 0 assert num_edges(g) == 0
def test_all(): for directed in [True, False]: for (i, g) in enumerate([make_g1(directed), make_g2(directed)]): print("Processing G%s (directed = %s)" % (i, directed)) vis = MyDepthFirstSearchVisitor(verbose=False) map_color = defaultdict(int) depth_first_search(0, g, make_assoc_property_map(map_color), vis) n = num_vertices(g) m = num_edges(g) n_ = vis.num_vertices m_ = vis.num_edges # Our graph are connected, so these assertion should be verified assert n_ == n, "Visited %s/%s vertices" % (n_, n) if directed: assert m_ == m, "Visited %s/%s edges" % (m_, m) else: # Undirected edges are visited forward and backward, so they are visited "twice" # Not that (u -> u) arc would be only visited once. assert m_ == 2 * m, "Visited %s/%s edges" % (m_, m) # Finally, all vertices should all be BLACK for u in vertices(g): assert map_color[u] == BLACK
def test_undirected_graph(): g = UndirectedGraph() assert num_vertices(g) == 0 u = add_vertex(g) assert num_vertices(g) == 1 v = add_vertex(g) assert num_vertices(g) == 2 w = add_vertex(g) assert num_vertices(g) == 3 assert num_edges(g) == 0 e_uv, _ = add_edge(u, v, g) assert num_edges(g) == 1 e_uv1, _ = add_edge(v, u, g) assert num_edges(g) == 2 e_uw, _ = add_edge(u, w, g) assert num_edges(g) == 3 e_vw, _ = add_edge(v, w, g) assert num_edges(g) == 4 print("Edges = %s" % {e for e in edges(g)}) print("In-edges(%s) = %s" % (u, {e for e in in_edges(u, g)})) assert in_degree(u, g) == 3 assert in_degree(v, g) == 3 assert in_degree( w, g) == 2, "in_edges(%s) = %s" % (w, {u for u in in_edges(w, g)}) print("Out-edges(%s) = %s" % (u, {e for e in out_edges(u, g)})) assert out_degree(u, g) == 3 assert out_degree(v, g) == 3 assert out_degree(w, g) == 2 print("Removing %s" % e_uv) remove_edge(e_uv, g) assert num_edges(g) == 3 print("Removing %s" % e_uv1) remove_edge(e_uv1, g) assert num_edges(g) == 2 print("Removing %s" % v) remove_vertex(v, g) assert num_vertices(g) == 2 print("Edges = %s" % {e for e in edges(g)}) assert num_edges(g) == 1 print("Out-edges(%s) = %s" % (u, {e for e in out_edges(u, g)})) assert out_degree(u, g) == 1 assert out_degree(w, g) == 1 assert in_degree(u, g) == 1 assert in_degree(w, g) == 1
def test_rpn_deque_ast(): tokenized = tokenizer_re("(a?b)*?c+d") ast = Ast() output = RpnDequeAst(map_operators=MAP_OPERATORS_RE, ast=ast) ret = shunting_yard_postfix(tokenized, MAP_OPERATORS_RE, output=output) assert num_vertices(ast) == 11 assert num_edges(ast) == 10 [root] = ret assert root == 10 from pybgl.graphviz import graph_to_html graph_to_html(ast)
def check_graph_size(g: Graph, n_expected: int, m_expected: int): """ Tests whether a Graph has the expected size. Args: n_expected: The expected number of vertices. m_expected: The expected number of edges. """ n = num_vertices(g) m = num_edges(g) assert n == n_expected, "Expected %s vertices, got %s" % (n_expected, n) assert m == m_expected, "Expected %s edges, got %s" % (m_expected, m)
def test_shunting_yard_ast(): tokenized = tokenizer_re("(a?b)*?c+d") (ast, root) = shunting_yard_ast(tokenized, MAP_OPERATORS_RE) assert num_vertices(ast) == 11 assert num_edges(ast) == 10 assert root == 10
def test_graph_copy_small(threshold :int = 50): g = DirectedGraph(5) (e01, _) = add_edge(0, 1, g) (e02, _) = add_edge(0, 2, g) (e04, _) = add_edge(0, 4, g) (e12, _) = add_edge(1, 2, g) (e23, _) = add_edge(2, 3, g) (e24, _) = add_edge(2, 4, g) (e40, _) = add_edge(4, 0, g) (e44, _) = add_edge(4, 4, g) map_eweight = { e01 : 83, e02 : 3, e04 : 78, e12 : 92, e23 : 7, e24 : 18, e40 : 51, e44 : 84, } pmap_eweight = make_assoc_property_map(map_eweight) pmap_erelevant = make_func_property_map(lambda e: pmap_eweight[e] >= threshold) g_dup = DirectedGraph(0) # Edge duplicate map_eweight_dup = dict() pmap_eweight_dup = make_assoc_property_map(map_eweight_dup) def callback_dup_edge(e, g, e_dup, g_dup): pmap_eweight_dup[e_dup] = pmap_eweight[e] # Vertex mapping map_vertices = dict() pmap_vertices = make_assoc_property_map(map_vertices) map_edges = dict() pmap_edges = make_assoc_property_map(map_edges) graph_copy( 0, g, g_dup, pmap_erelevant = pmap_erelevant, pmap_vertices = pmap_vertices, pmap_edges = pmap_edges, callback_dup_edge = callback_dup_edge ) if in_ipynb(): ori_html = dotstr_to_html( GraphDp( g, dpv = { "label" : make_func_property_map(lambda u: "%s<br/>(becomes %s)" % (u, pmap_vertices[u])) }, dpe = { "color" : make_func_property_map(lambda e : "darkgreen" if pmap_erelevant[e] else "lightgrey"), "style" : make_func_property_map(lambda e : "solid" if pmap_erelevant[e] else "dashed"), "label" : pmap_eweight, } ).to_dot() ) dup_html = dotstr_to_html( GraphDp( g_dup, dpe = { "label" : pmap_eweight_dup, } ).to_dot() ) html( """ <table> <tr> <th>Original</th> <th>Extracted</th> </tr><tr> <td>%s</td> <td>%s</td> </tr> </table> """ % (ori_html, dup_html) ) if threshold == 50: expected_num_edges = 5 assert map_vertices == { 0 : 0, 1 : 1, 2 : 2, 4 : 3 } for e, e_dup in map_edges.items(): u = source(e, g) v = target(e, g) u_dup = source(e_dup, g_dup) v_dup = target(e_dup, g_dup) assert u_dup == pmap_vertices[u], "u_dup = %s ; pmap_vertices[%s] = %s" % (u_dup, u, pmap_vertices[u]) assert v_dup == pmap_vertices[v], "v_dup = %s ; pmap_vertices[%s] = %s" % (v_dup, v, pmap_vertices[v]) assert pmap_eweight[e] == pmap_eweight_dup[e_dup] elif threshold < min([w for w in map_eweight.values()]): expected_num_edges = num_edges(g) elif threshold > max([w for w in map_eweight.values()]): expected_num_edges = 0 assert expected_num_edges == num_edges(g_dup), \ """ Invalid edge number: Expected: %s Obtained: %s """ % (expected_num_edges, num_edges(g_dup))
def test_compile_dfa_ipv6(): dfa = make_dfa_ipv6() assert num_vertices(dfa) == 15 and num_edges(dfa) == 279
def test_compile_nfa_ipv6(): nfa = make_nfa_ipv6() assert num_vertices(nfa) >= 15 and num_edges(nfa) >= 279