class HypgerGraph(Graph): """ From mathworld.wolfram.com/Hypergraph.html: "A hypergraph is a graph in which generalized edges (called hyperedges) may connect more than two nodes." Also interesting, from en.wikipedia.org/wiki/Hypergraph "The collection of hypergraphs is a category with hypergraph homomorphisms as morphisms." """ if DEBUG: with Section('Multi-graph'): hypergraph = HypgerGraph(dmkr.random_graph(max_edges=10)) print_h3('Random multi-graph') print(hypergraph) # Multiple edges pointing to each other hypergraph2 = HypgerGraph({ 0: { 'edges': [1, 2, 3], 'val': 'A' }, 1: { 'edges': [0, 3, 2, 1], 'val': 'B' }, 2: { 'edges': [0, 1, 3, 2], 'val': 'C'
class HypgerGraph(Graph): """ From mathworld.wolfram.com/Hypergraph.html: "A hypergraph is a graph in which generalized edges (called hyperedges) may connect more than two nodes." Also interesting, from en.wikipedia.org/wiki/Hypergraph "The collection of hypergraphs is a category with hypergraph homomorphisms as morphisms." """ if DEBUG: with Section('Multi-graph'): hypergraph = HypgerGraph(dmkr.random_graph(max_edges=10)) print_h3('Random multi-graph') print(hypergraph) # Multiple edges pointing to each other hypergraph2 = HypgerGraph({ 0: {'edges': [1, 2, 3], 'val': 'A'}, 1: {'edges': [0, 3, 2, 1], 'val': 'B'}, 2: {'edges': [0, 1, 3, 2], 'val': 'C'}, 3: {'edges': [0, 1, 2, 3], 'val': 'D'}, }) print(hypergraph2) if raw_input('Save graph images? Y/N: ') == 'Y': hypergraph.render_graph('hypergraph-test.png') hypergraph2.render_graph('hypergraph2-test.png')
def __init__(self, vertices={}): for vertex, data in vertices.iteritems(): if vertex in data["edges"]: raise ValueError('Loop "{}" is not allowed on vertices.'.format(vertex)) return super(LooplessMultiGraph, self).__init__(vertices=vertices) def __setitem__(self, *args): key, vertices = args if key in vertices["edges"]: raise ValueError("Loop {} is not allowed on vertices.".format(key)) return super(LooplessMultiGraph, self).__setitem__(*args) if DEBUG: with Section("Multi-graph"): mgraph_rand = MultiGraph(dmkr.random_graph(max_edges=5)) print_h3("Random multi-graph") print(mgraph_rand) # Multiple edges pointing to each other mgraph = MultiGraph( { 0: {"edges": [1, 2, 3], "val": "A"}, 1: {"edges": [0, 3, 2, 1], "val": "B"}, 2: {"edges": [0, 1, 3, 2], "val": "C"}, 3: {"edges": [0, 1, 2, 3], "val": "D"}, } ) print_h3("Specific multi-graph") print(mgraph) mgraph.render_graph("mgraph.png", strict=False)
for vertex, data in vertices.iteritems(): if vertex in data['edges']: raise ValueError( 'Loop "{}" is not allowed on vertices.'.format(vertex)) return super(LooplessMultiGraph, self).__init__(vertices=vertices) def __setitem__(self, *args): key, vertices = args if key in vertices['edges']: raise ValueError('Loop {} is not allowed on vertices.'.format(key)) return super(LooplessMultiGraph, self).__setitem__(*args) if DEBUG: with Section('Multi-graph'): mgraph_rand = MultiGraph(dmkr.random_graph(max_edges=5)) print_h3('Random multi-graph') print(mgraph_rand) # Multiple edges pointing to each other mgraph = MultiGraph({ 0: { 'edges': [1, 2, 3], 'val': 'A' }, 1: { 'edges': [0, 3, 2, 1], 'val': 'B' }, 2: { 'edges': [0, 1, 3, 2], 'val': 'C'