In the future, when you post both on Wolfram Community and on Mathematica.SE, please cross-reference the two posts. This is to avoid duplication of effort if the question is already answered in one place. Just include a link to the other one in both posts.
Link to M.SE version:
Response on the linked post:
To randomly remove a fraction
$p$ of vertices or edges from a graph g
, you can do the following:
p = 0.1; (* make sure p has a value *)
verticesToRemove = RandomSample[VertexList[g], Round[p VertexCount[g]]]
result = VertexDelete[g, verticesToRemove]
For edges:
edgesToRemove = RandomSample[EdgeList[g], Round[p EdgeCount[g]]]
result = EdgeDelete[g, edgesToRemove]
Is the result still connected? Check it with ConnectedGraphQ
.
If the result graph is not connected, you can look at what is the size of the largest component in the result, relative to the total graph size:
Length@First@ConnectedComponents[result]/VertexCount[result]
Try it on a scale free network created using preferential attachment:
g = RandomGraph[BarabasiAlbertGraphDistribution[300, 2]]
Repeat the calculation 100 times and compute the average fraction of the largest component:
Table[
verticesToRemove = RandomSample[VertexList[g], Round[p VertexCount[g]]];
result = VertexDelete[g, verticesToRemove];
Length@First@ConnectedComponents[result]/VertexCount[result],
{100}
]
N@Mean[%]