def union(G, H): """ Return a new graph which is the simple union of the node sets and edge sets. Parameters ---------- G,H : Two undirected simple staticgraph Returns ------- GC: A new graph with the same type as G Notes ----- It is mandatory that G and H be both undirected. The node sets of G and H must be same. """ if G.order() != H.order(): msg = "Node sets of the two undirected graphs are not equal!" raise StaticGraphNotEqNodesException(msg) edges = ((u, v) for u in G.nodes() for v in chain(G.neighbours(u), H.neighbours(u)) if u < v) deg = make_deg(G.order(), edges) edges = ((u, v) for u in G.nodes() for v in chain(G.neighbours(u), H.neighbours(u)) if u < v) GC = make(G.order(), G.size() + H.size(), edges, deg) return GC
def difference(G, H): """ Return a new graph that contains the edges that exist in G but not in H. The node sets of H and G must be the same. Parameters ---------- G,H : Two undirected simple staticgraphs Returns ------- D : A new graph with the same type as G. Notes ----- It is mandatory that G and H be both undirected. The node sets of G and H must be same. """ if G.order() != H.order(): msg = "Node sets of the two undirected graphs are not equal!" raise StaticGraphNotEqNodesException(msg) edges = ((u, v) for u in G.nodes() for v in set(G.neighbours(u)) - set(H.neighbours(u)) if u < v) deg = make_deg(G.order(), edges) edges = ((u, v) for u in G.nodes() for v in set(G.neighbours(u)) - set(H.neighbours(u)) if u < v) D = make(G.order(), G.size(), edges, deg) return D
def complement(G): """ Returns the complement of Graph G Parameters ---------- G : An undirected staticgraph Returns ------- H : A new staticgraph graph. Notes ------ It is mandatory that G be undirected. """ nset = set(G.nodes()) n_nodes = G.order() n_edges = n_nodes * (n_nodes - 1) / 2 - G.size() cmp_edges = ((u, v) for u in G.nodes() for v in nset - set(G.neighbours(u)) if u < v) deg = make_deg(n_nodes, cmp_edges) cmp_edges = ((u, v) for u in G.nodes() for v in nset - set(G.neighbours(u)) if u < v) H = make(n_nodes, n_edges, cmp_edges, deg) return H