calculate the fraction of nodes belonging to the giant component(python , networkx)

900 Views Asked by At

I have a file that contains information about airports. how do i find the fraction of the giant component in a graph? the data looks like

1 43
1 52
43 1
53 146

and so on, it just has source-target , i.e from one airport to another.

Q. Randomly choose a fraction p 0.01 of the nodes and remove them from the network. Calculates the fraction of nodes belonging to the giant component (gee) of the attacked network. tried this

> listofnodes=A.nodes()
print('Number of nodes  before reducing ' ,len(listofnodes))
numofnodes=A.number_of_nodes()
sample=int(numofnodes*0.1)
>output : Number of nodes  before reducing  940
RandomSample = random.sample(listofnodes, sample)
G.remove_nodes_from(RandomSample)
print('Number of nodes  before reducing ' ,len(G.nodes))
>Number of nodes  before reducing  534

https://mathematica.stackexchange.com/questions/130525/how-can-i-remove-a-fraction-p-of-nodes-or-edges-from-a-graph-randomly tried to do something like this, the problem statement here is what i wish to accomplish ,how fraction survives the attack, but the function used there, gives an error saying theres no such function. sorry about earlier :)

1

There are 1 best solutions below

1
On

I don't pretend to give you the entire answer, but I will give you some some code to help you get by as well as redirect you to the networkX documentation.

You have managed to do this:
"Randomly choose a fraction p 0.01 of the nodes and remove them from the network."
(actually you are doing it for p=0.1)

Next you need to calculate the percentage of your nodes that belong to the giant component:

Here's an example on how to extract the giant component of a "random" graph:

G = nx.extended_barabasi_albert_graph(1000, 1, 0.1, 0.2, seed=1)

giantC = max(nx.connected_component_subgraphs(G), key=len) 
# connected_component_subgraphs has been removed. Please read EDIT

Now giantC represents all the nodes in the giant component, how do you obtain the fraction of nodes of G that belong to the giant component giantC?

Then, you just have to calculate the same value for before and after the attack.

EDIT: As NetworkX version 2.4 connected_component_subgraphs has been removed, so for the most recent versions use: connected_components. However this function returns a set of nodes (instead of a graph), so use g.subgraph to create the induced subgraph of the component:

giantC = g.subgraph(max(nx.connected_components(g), key=len))

(Based on this documentation)