iterate adding/removing edges and perform calculations networkx - graph

I am working with networkx 2.5. in Jupyter notebook, python version 3.6.5 I am working with an indirect graph. I have a graph from which I would like to perform the following steps iteratively
Remove one edge,
Perform some calculations over the remaining graph (number of connected components, diameter and nodes)
Put back the removed edge
Remove another edge
Repeat the whole procedure until all the edges were deleted once
For instance, I have the following graph:
'target' : [3, 2 ,4]})
nodes2 = pd.DataFrame({'nodeS' : [1, 2, 3 , 4],
'density' : [1, 2, 4, 2],
'indiv' : [2, 4, 1, 5]})
G1 = nx.from_pandas_edgelist(edges2, 'source', 'target')
nx.set_node_attributes(G1, pd.Series(nodes2.density, index=nodes2.nodeS).to_dict(), "density")
nx.set_node_attributes(G1, pd.Series(nodes2.indiv, index=nodes2.nodeS).to_dict(), 'indiv')
I plotted the first graph (G1)
The result looks like
Then I make the calculations over the graph:
# largest connected component (it will be necessary for the first calculation after first edge removal)
G1_LCC = G1.subgraph(max(nx.connected_components(G), key=len))
# number of connected components
G1_cc = nx.number_connected_components(G1)
# diameter of largest connected component
G1_diam = nx.diameter(G1_LCC)
# number of connected nodes
G1_nodes = len(nx.nodes(G1_LCC))
Removing an edge:
I tried the next:
e = (1,2)
G2= nx.remove_edge(e)
But it gives me an error:
nx.edges_iter(G)
AttributeError: module 'networkx' has no attribute 'remove_edge'
This should look like (G2)
Then perform operations on G2
Remove an edge which would look like that:
The third G3, (G3)
and so on, until all the edges have been removed once.
Note: I tried with edges iter
nx.edges_iter(G)
but I also got an error
AttributeError: module 'networkx' has no attribute 'edges_iter'
Is there an iterative/ efficient way to remove the edges, make calculations and put them back? Rather than do it one by one? Thank you

remove_edge is a method of the Graph class, not a top-level function in the library. So G2 = G1.remove_edge(e).copy() would be the correct command (you may not need to make a copy if you are adding the edge back in at a later point). edges_iter was also a method of Graph but has been discontinued for edges. So for e in G1.edges(), not for e in nx.edges_iter(G1).

Related

Checking validity of topological sort

Given the following directed graph:
I determined the topological sort to be 0, 1, 2, 3, 7, 6, 5, 4 with the values for each node being:
d[0] = 1
f[0] = 16
d[1] = 2
f[1] = 15
d[2] = 3
f[2] = 14
d[3] = 4
f[3] = 13
d[4] = 7
f[4] = 8
d[5] = 6
f[5] = 9
d[6] = 5
f[6] = 10
d[7] = 11
f[7] = 12
Where d is discovery-time and f is finishing-time.
How can I check whether the topological sort is valid or not?
With python and networkx, you can check it as follows:
import networkx as nx
G = nx.DiGraph()
G.add_edges_from([(0, 2), (1, 2), (2, 3)])
all_topological_sorts = list(nx.algorithms.dag.all_topological_sorts(G))
print([0, 1, 2, 3] in all_topological_sorts) # True
print([2, 3, 1, 0] in all_topological_sorts) # False
However, note that in order to have a topological ordering, the graph must be a Directed Acyclic Graph (DAG). If G is not directed, NetworkXNotImplemented will be raised. If G is not acyclic (as in your case) NetworkXUnfeasible will be raised.
See documentation here.
If you want a less coding approach to this question (since it looks like your original topological ordering was generated without code), you can go back to the definition of a topological sort. Paraphrased from Emory University:
Topological ordering of nodes = an ordering (label) of the nodes/vertices such that for every edge (u,v) in G, u appears earlier than v in the ordering.
There's two ways that you could approach this question: from an edge perspective of a vertex perspective. I describe a naive (meaning with some additional space complexity and cleverness, you could improve on them) implementation of both below.
Edge approach
Iterate through the edges in G. For each edge, retrieve the index of each of its vertices in the ordering. Compared the indices. If the origin vertex isn't earlier than the destination vertex, return false. If you iterate through all of the edges without returning false, return true.
Complexity: O(E*V)
Vertex approach
Iterate through the vertices in your ordering. For each vertex, retrieve its list of outgoing edges. If any of those edges end in a vertex that precedes the current vertex in the ordering, return false. If you iterate through all the vertices without returning false, return true.
Complexity: O(V^2*E)
First, do a graph traversal to get the incoming degree of each vertex. Then start from the first vertex in your list. Every time, when we look at a vertex, we want to check two things 1) is the incoming degree of this vertex is 0? 2) is this vertex a neighbor of the previous vertex? We also want to decrement all its neighbors' incoming degree, as if we cut all edges. If we got a no from the previous questions at some point, we know that this is not a valid topological order. Otherwise, it is. This takes O(V + E) time.

R igraph: Barabasi-Model start.graph too many vertices

I have a large network, which I want to use as a "start.graph" for my Barabasi-Albert-Model, but unfortunately I get this Error.
sample_pa(100, power = 1, m = 2, start.graph = large_network)
Error in sample_pa(100, power = 1, m = 2, start.graph = igraph_worm_traffic_colored[[1]]) :
At games.c:519 : Starting graph has too many vertices, Invalid value
Is there any way to change the maximal number of vertices?
Your error is because you need to have more vertices in your output graph than in your starting graph in order for the BA-model to work. You can take a subgraph of your large network if you want to use it to produce a 100-vertex graph.
g2<-induced.subgraph(large.network, sample(V(large.network), 20))
Or you can increase the number of vertices in your output graph.

NetworkX: connect nodes of two separate graphs in Python

This question is about attempting to model interdependent networks with NetworkX. There are dedicated packages (such as Pymnet), but they don't seem as flexible as NetworkX. And by the way, I wanted to give NetworkX one last chance.
So, let's say we have 2 separate graphs, G1 and G2, which we plot in the same figure:
import networkx as nx
import matplotlib.pyplot as plt
G1=nx.barabasi_albert_graph(3, 2) #n=3, m=2 (number of initial links)
G2=nx.barabasi_albert_graph(3, 2)
pos1=nx.spring_layout(G1)
pos2=nx.spring_layout(G2)
nx.draw_networkx(G1,pos=pos1,node_color='red') #G1 is red
nx.draw_networkx(G2,pos=pos2,node_color='green') #G2 is green
Now, if we attempt to connect node 0 of G1 with node 1 of G2:
G1.add_edge(G1.nodes()[0], G2.nodes()[1])
we don't get any error, but if you plot the graphs again, the image is exactly as before. And if you check the number of edges you get the same results as before:
In[17]: G1.edges()
Out[17]: [(0, 1), (0, 2), (1, 2)]
In[18]: G2.edges()
Out[18]: [(0, 2), (1, 2)]
meaning that the edge was basically not added, or it was added and is not displayed, or it was added, but because it runs from one graph to another, it doesn't belong to any of them.
How do you suggest to create this interconnection from G1 to G2 in NetworkX, without resorting to other packages?
I think the fundamental issue is that you've got a different concept for how networkx thinks of a graph from what it is. I believe you're thinking that the nodes of the graph are objects of some node class that the nodes themselves intrinsically have some attribute saying what their position is. This is not the case. There is no special node class. A graph can have any hashable object as its nodes because really, a graph is just a fancy dict whose keys are what we call nodes.
The nodes of your graph G1 are the integers 0, 1, and 2. G2 has exactly the same nodes. The new edge you've added is between whatever integer is in G1.nodes()[0] and whatever integer is in G2.nodes()[1]. In your example I believe G1 already has that edge.
Separately, you've created two different dicts pos1 and pos2 (which have the same keys - the integer values that form the nodes of the two graphs). These dicts say where the nodes should be plotted. You've told it to plot G1 using pos1. So it puts the circle for the node that is 0 at pos1[0] and similarly for 1 and 2. Then when you later tell it to plot G using pos1 it's going to do exactly the same thing.
What you probably want to do is create a new graph whose nodes consist of the nodes of G1 and of G2, after renaming so that they aren't the same nodes. This is done by union (see documentation.)
G = union(G1, G2, rename=('G1-', 'G2-'))
Then add the edge, noting that the nodes in G have different names, so the command you used won't quite work.

Find all vertices that can reach a set of other vertices [igraph package in R]

How can I find, in a directed graph, all vertices that can reach a set of other vertices with the igraph package in R?
I'm able to find them for a single vertex using (e.g. for all vertices that can reach vertex 4):
subcomponent(g, 4, mode="in")
However, how can I replace "4" with a set of many vertices with a similar result? If I give many vertices to the default function, it seems to return only vertices that can reach all the given vertices. I would like to find all vertices that can reach any of the given vertices...
Thanks
You could make the function subcomponent take a vector of arguments for the parameter v:
g <- erdos.renyi.game(100, 1/200)
mySet <- c(1,2,3)
modified <- Vectorize(igraph:::subcomponent, vectorize.args = "v")
modified(g, mySet, "in")
Which could also be done using an apply function or a loop
sapply(mySet, subcomponent, graph=g, mode="in")

Draw Network in R (control edge thickness plus non-overlapping edges)

I need to draw a network with 5 nodes and 20 directed edges (an edge connecting each 2 nodes) using R, but I need two features to exist:
To be able to control the thickness of each edge.
The edges not to be overlapping (i.e.,the edge form A to B is not drawn over the edge from B to A)
I've spent hours looking for a solution, and tried many packages, but there's always a problem.
Can anybody suggest a solution please and provide a complete example as possible?
Many Thanks in advance.
If it is ok for the lines to be curved then I know two ways. First I create an edgelist:
Edges <- data.frame(
from = rep(1:5,each=5),
to = rep(1:5,times=5),
thickness = abs(rnorm(25)))
Edges <- subset(Edges,from!=to)
This contains the node of origin at the first column, node of destination at the second and weight at the third. You can use my pacake qgraph to plot a weighted graph using this. By default the edges are curved if there are multiple edges between two nodes:
library("qgraph")
qgraph(Edges,esize=5,gray=TRUE)
However this package is not really intended for this purpose and you can't change the edge colors (yet, working on it:) ). You can only make all edges black with a small trick:
qgraph(Edges,esize=5,gray=TRUE,minimum=0,cut=.Machine$double.xmin)
For more control you can use the igraph package. First we make the graph:
library("igraph")
g <- graph.edgelist(as.matrix(Edges[,-3]))
Note the conversion to matrix and subtracting one because the first node is 0. Next we define the layout:
l <- layout.fruchterman.reingold(g)
Now we can change some of the edge parameters with the E()function:
# Define edge widths:
E(g)$width <- Edges$thickness * 5
# Define arrow widths:
E(g)$arrow.width <- Edges$thickness * 5
# Make edges curved:
E(g)$curved <- 0.2
And finally plot the graph:
plot(g,layout=l)
While not an R answer specifically, I would recommend using Cytoscape to generate the network.
You can automate it using a RCytoscape.
http://bioconductor.org/packages/release/bioc/html/RCytoscape.html
The package informatively named 'network' can draw directed networks fairly well, and handle your issues.
ex.net <- rbind(c(0, 1, 1, 1), c(1, 0, 0, 1), c(0, 0, 0, 1), c(1, 0, 1, 0))
plot(network(ex.net), usecurve = T, edge.curve = 0.00001,
edge.lwd = c(4, rep(1, 7)))
The edge.curve argument, if set very low and combined with usecurve=T, separates the edges, although there might be a more direct way of doing this, and edge.lwd can take a vector as its argument for different sizes.
It's not always the prettiest result, I admit. But it's fairly easy to get decent looking network plots that can be customized in a number of different ways (see ?network.plot).
The 'non overlapping' constraint on edges is the big problem here. First, your network has to be 'planar' otherwise it's impossible in 2-dimensions (you cant connect three houses to gas, electric, phone company buildings without crossovers).
I think an algorithm for planar graph layout essentially solves the 4-colour problem. Have fun with that. Heuristics exist, search for planar graph layout, and force-directed, and read Planar Graph Layouts

Resources