def __init__(self): """ Initialize a hypergraph. """ self.node_links = {} # Pairing: Node -> Hyperedge self.edge_links = {} # Pairing: Hyperedge -> Node self.graph = graph() # Ordinary graph
def test_add_spanning_tree(self): gr = graph() st = {0: None, 1: 0, 2: 0, 3: 1, 4: 2, 5: 3} gr.add_spanning_tree(st) for each in st: self.assertTrue((each, st[each]) in gr.edges() or (each, st[each]) == (0, None)) self.assertTrue((st[each], each) in gr.edges() or (each, st[each]) == (0, None))
def test_add_empty_graph(self): gr1 = testlib.new_graph() gr1c = copy(gr1) gr2 = graph() gr1.add_graph(gr2) self.assertTrue(gr1.nodes() == gr1c.nodes()) self.assertTrue(gr1.edges() == gr1c.edges())
def test_complete_graph(self): gr = graph() gr.add_nodes(xrange(10)) gr.complete() for i in xrange(10): for j in range(10): self.assertTrue((i, j) in gr.edges() or i == j)
def test_raise_exception_on_duplicate_node_addition(self): gr = graph() gr.add_node("a_node") try: gr.add_node("a_node") except AdditionError: pass else: fail()
def read(string): """ Read a graph from a string in Dot language and return it. Nodes and edges specified in the input will be added to the current graph. @type string: string @param string: Input string in Dot format specifying a graph. @rtype: graph @return: Graph """ dotG = pydot.graph_from_dot_data(string) if dotG.get_type() == "graph": G = graph() elif dotG.get_type() == "digraph": G = digraph() else: raise InvalidGraphType # Read nodes... # Note: If the nodes aren't explicitly listed, they need to be for each_node in dotG.get_nodes(): G.add_node(each_node.get_name()) for each_attr_key, each_attr_val in each_node.get_attributes().items(): G.add_node_attribute(each_node.get_name(), (each_attr_key, each_attr_val)) # Read edges... for each_edge in dotG.get_edges(): # Check if the nodes have been added if not dotG.get_node(each_edge.get_source()): G.add_node(each_edge.get_source()) if not dotG.get_node(each_edge.get_destination()): G.add_node(each_edge.get_destination()) # See if there's a weight if "weight" in each_edge.get_attributes().keys(): _wt = each_edge.get_attributes()["weight"] else: _wt = 1 # See if there is a label if "label" in each_edge.get_attributes().keys(): _label = each_edge.get_attributes()["label"] else: _label = "" G.add_edge(each_edge.get_source(), each_edge.get_destination(), wt=_wt, label=_label) for each_attr_key, each_attr_val in each_edge.get_attributes().items(): if not each_attr_key in ["weight", "label"]: G.add_edge_attribute( each_edge.get_source(), each_edge.get_destination(), (each_attr_key, each_attr_val) ) return G
def generate(num_nodes, num_edges, directed=False, weight_range=(1, 1)): """ Create a random graph. @type num_nodes: number @param num_nodes: Number of nodes. @type num_edges: number @param num_edges: Number of edges. @type directed: bool @param directed: Whether the generated graph should be directed or not. @type weight_range: tuple @param weight_range: tuple of two integers as lower and upper limits on randomly generated weights (uniform distribution). """ # Graph creation if directed: random_graph = digraph() else: random_graph = graph() # Nodes nodes = xrange(num_nodes) random_graph.add_nodes(nodes) # Build a list of all possible edges edges = [] edges_append = edges.append for x in nodes: for y in nodes: if ((directed and x != y) or (x > y)): edges_append((x, y)) # Randomize the list for i in xrange(len(edges)): r = randint(0, len(edges)-1) edges[i], edges[r] = edges[r], edges[i] # Add edges to the graph min_wt = min(weight_range) max_wt = max(weight_range) for i in xrange(num_edges): each = edges[i] random_graph.add_edge(each[0], each[1], wt = randint(min_wt, max_wt)) return random_graph
def read(string): """ Read a graph from a XML document and return it. Nodes and edges specified in the input will be added to the current graph. @type string: string @param string: Input string in XML format specifying a graph. @rtype: graph @return: Graph """ dom = parseString(string) if dom.getElementsByTagName("graph"): G = graph() elif dom.getElementsByTagName("digraph"): G = digraph() else: raise InvalidGraphType # Read nodes... for each_node in dom.getElementsByTagName("node"): G.add_node(each_node.getAttribute('id')) for each_attr in each_node.getElementsByTagName("attribute"): G.add_node_attribute(each_node.getAttribute('id'), (each_attr.getAttribute('attr'), each_attr.getAttribute('value'))) # Read edges... for each_edge in dom.getElementsByTagName("edge"): G.add_edge(each_edge.getAttribute('from'), each_edge.getAttribute('to'), \ wt = float(each_edge.getAttribute('wt')), label = each_edge.getAttribute('label')) for each_attr in each_edge.getElementsByTagName("attribute"): attr_tuple = (each_attr.getAttribute('attr'), each_attr.getAttribute('value')) if (attr_tuple not in graph.edge_attributes(each_edge.getAttribute('from'), \ each_edge.getAttribute('to'))): G.add_edge_attribute(each_edge.getAttribute('from'), \ each_edge.getAttribute('to'), attr_tuple) return G
def test_find_cycle_on_graph_withot_cycles(self): gr = testlib.new_graph() st, pre, post = depth_first_search(gr) gr = graph() gr.add_spanning_tree(st) assert find_cycle(gr) == []
def testbfs_in_empty_graph(self): gr = graph() st, lo = breadth_first_search(gr, filter=filters.radius(2)) assert st == {} assert lo == []
def test_dfs_in_empty_graph(self): gr = graph() st, pre, post = depth_first_search(gr) assert st == {} assert pre == [] assert post == []
def test_dfs_in_empty_graph(self): gr = graph() st, pre, post = depth_first_search(gr, filter=filters.radius(2)) assert st == {} assert pre == [] assert post == []
def test_add_empty_spanning_tree(self): gr = graph() st = {} gr.add_spanning_tree(st) self.assertTrue(gr.nodes() == []) self.assertTrue(gr.edges() == [])
def setUp(self): self.G = graph() nations_of_the_world(self.G)
def test_complete_empty_graph(self): gr = graph() gr.complete() self.assertTrue(gr.nodes() == []) self.assertTrue(gr.edges() == [])
def test_complete_graph_with_one_node(self): gr = graph() gr.add_node(0) gr.complete() self.assertTrue(gr.nodes() == [0]) self.assertTrue(gr.edges() == [])
def test_invert_empty_graph(self): gr = graph() inv = gr.inverse() self.assertTrue(gr.nodes() == []) self.assertTrue(gr.edges() == [])
def test_remove_node_with_edge_to_itself(self): gr = graph() gr.add_node(0) gr.add_edge(0, 0) gr.del_node(0)
def test_remove_edge_from_node_to_same_node(self): gr = graph() gr.add_node(0) gr.add_edge(0, 0) gr.del_edge(0, 0)