I want to animate a process on a graph (preferably in NetworkX). I have already seen this question. However, when I run the code given in the solution, I just see the final output. Also, that doesn't save the animation in some usable format.
Suppose we have the following graph:
import networkx as nx
g = nx.Graph()
g.add_edges_from([(1, 2), (2, 3), (1, 3), (1, 4), (3, 4), (4, 5), (5, 9), (4, 9)])
Also, we have an initial set of nodes which we call active:
active = {1, 3}
Intuitively, what I want to do is to animate how each active node will cause other nodes in the graph to become active in time. So, if we assume a model where each node gets active if at least two of its neighbors become active, in the second iteration the set of active nodes will be:
active = {1, 3, 2, 4}
In the next iteration, the set of active nodes will be:
active = {1, 3, 2, 4, 5}.
In the final iteration, all the nodes in the graph will become active:
active = {1, 3, 2, 4, 5, 9}
This process, which is called the tipping process, is an example of information diffusion in networks. You can see a very simple implementation of the algorithm below.
def tipping(graph, seed_set, thr=2):
active = seed_set
has_changed = False
for n in filter(lambda n: n not in active, graph.nodes()):
if len(filter(lambda nei: nei in active, graph.neighbors(n))) >= thr:
active.add(n)
has_changed = True
if has_changed:
return tipping(graph, active, thr) | active
return active
I want to know if there is any way that I can visualize this process. I know I can draw the network with the nx.draw()
function in networkX. However, I haven't seen any function which produces animations or any other useful output for this scenario.
One possible solution might be to plot the graph in each step of the process with different node colors, save each of them and make a gif animation with all the saved pictures.
How can I animate the diffusion using Networkx? Preferably the animation would run in an IPython notebook.