corrupt_graph#
- graph_tool.generation.corrupt_graph(g, p, q=0.0, micro=False, self_loops=False)[git][source]#
Corrupt a graph by removing and adding edges at random.
- Parameters:
- g
Graph Graph to be modified.
- p
floatorint Probability of every existing edge to be removed, or, if
micro == True, the number of edges to be removed.- q
floatorint(optional, default:0.) Probability of every absent edge to be added, or, if
micro == True, the number of edges to be added.- micro
bool(optional, default:False) If
True,pandqare the exact numbers of edges removed and added, instead of probabilities.- self_loops
bool(optional, default:False) If
True, self-loops may be added.
- g
- Returns:
None
See also
duplicate_verticesDuplication of vertices
random_rewireRandom rewiring preserving the degrees
add_random_edgesAddition of random edges
remove_random_edgesRemoval of random edges
generate_erdos_renyiErdős–Rényi model
Notes
Every pair of vertices that is not an edge of
g(an ordered pair, for directed graphs, and including the pairs \((v, v)\) ifself_loops == True) becomes an edge independently with probability \(q\), and then every edge of the original graph is removed independently with probability \(p\), so that an edge is present afterwards with probability \(1 - p\) if it was present before, and \(q\) otherwise. Withmicro == True, exactly \(q\) absent pairs and \(p\) original edges, chosen uniformly at random, are added and removed, respectively.If
gis an Erdős–Rényi graph with density \(\rho\), the corrupted graph is a correlated Erdős–Rényi graph [pedarsani-privacy-2011] with density \(\rho\) if \(p = (1-c)(1-\rho)\) and \(q = \rho(1-c)\), where \(c\) is the Pearson correlation between the presence of an edge in both graphs; such pairs of graphs are used as benchmarks for graph matching.The algorithm runs in time \(O(V + E + M)\), where \(M\) is the number of new edges.
References
[pedarsani-privacy-2011]Pedram Pedarsani and Matthias Grossglauser, “On the privacy of anonymized networks”, Proceedings of the 17th ACM SIGKDD International Conference on Knowledge Discovery and Data Mining, 1235-1243 (2011), DOI: 10.1145/2020408.2020596 [sci-hub, @tor]
Parallel implementation.
If enabled during compilation, this algorithm will run in parallel using OpenMP. See the parallel algorithms section for information about how to control several aspects of parallelization.
Examples
>>> g = gt.collection.data["karate"].copy() >>> print(g) <Graph object, undirected, with 34 vertices and 78 edges, 1 internal vertex property, 2 internal graph properties, at 0x...> >>> gt.corrupt_graph(g, .25, 0.01) >>> print(g) <Graph object, undirected, with 34 vertices and 62 edges, 1 internal vertex property, 2 internal graph properties, at 0x...> >>> gt.corrupt_graph(g, 10, 10, micro=True) >>> print(g) <Graph object, undirected, with 34 vertices and 62 edges, 1 internal vertex property, 2 internal graph properties, at 0x...>
A corrupted version of the network is drawn as follows, with the vertex positions of the original network and the edges absent from it highlighted:
>>> g = gt.collection.data["karate"] >>> c = g.copy() >>> gt.corrupt_graph(c, .25, 0.01) >>> color = c.new_ep("string", val="#999999") >>> for e in c.edges(): ... if g.edge(int(e.source()), int(e.target())) is None: ... color[e] = "#e41a1c" >>> pos = gt.sfdp_layout(g) >>> gt.graph_draw(c, pos=c.own_property(pos), edge_color=color, ... output="corrupt_graph-karate.pdf") <...>
Karate club network corrupted with \(p=0.25\) and \(q=0.01\), drawn with the vertex positions of the original network. The red edges are absent from the original network, and the gray ones are present in both.#