I am trying to make a dynamic graph using networkx. For example when I use:
import networkx as nx
G = nx.Graph()
it makes an empty graph G which is by default static. How can I change it to dynamic? Some nodes/edges may exist in one timestamp but not in the others. How can I incorporate time? As an example, suppose my graph has three nodes 'a', 'b' and 'c'. At time t1, there is only one edge between 'a' and 'b'. At time t2 the configuration of edges changes and in this timestamp all nodes are connected to each other. Say:
G.add_edge('a','b',timestamp='t1')
G.add_node('c',timestamp='t1')
G.add_edge('a','b',timestamp='t2')
G.add_edge('a','c',timestamp='t2')
G.add_edge('b','c',timestamp='t2')
but this does not work!
the reason I need to do this is that I want to save the graph in gexf format to use it in Gephi.
What should I do?
Using nx.Graph() you cannot have more than one edge between 2 nodes.
However if you use nx.MultiGraph() you can.
What you can do is index the node of the graph with a simple counter as set the node ID and timestamp as a property. You would have to filter the node based on their property if you want to select a subset using nx.get_node_attributes(G, 'id').
In a general manner, NetworkX is not made for dynamic graphs. You can use Stinger which is designed exactly for that.
EDIT: For Gephi, it is completely different. You should export the graph in GEXF and pray for Gephi to read it. Basically something like this:
<?xml version="1.0" encoding="UTF-8"?>
<gexf xmlns="http://www.gexf.net/1.2draft" version="1.2" xmlns:viz="http://www.gexf.net/1.2draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd">
<meta lastmodifieddate="2016-01-07">
<creator>Gephi 0.8.1</creator>
<description></description>
</meta>
<graph defaultedgetype="directed" timeformat="double" mode="dynamic">
<attributes class="node" mode="dynamic">
<attribute id="score" title="score" type="integer"></attribute>
</attributes>
<attributes class="edge" mode="dynamic">
<attribute id="weight" title="Weight" type="float"></attribute>
</attributes>
<nodes>
<node id="n0" label="n0" start="2001.0" end="2022.0">
<attvalues>
<attvalue for="score" value="4" start="2001.0" end="2010.0"></attvalue>
<attvalue for="score" value="3" start="2011.0" end="2011.0"></attvalue>
</attvalues>
<viz:size value="10.0"></viz:size>
<viz:position x="46.152466" y="-287.68558" z="0.0"></viz:position>
<viz:color r="25" g="213" b="100"></viz:color>
</node>
<node id="n1" label="n1" start="2001.0" end="2024.0">
<attvalues>
<attvalue for="score" value="2" start="2001.0" end="2007.0"></attvalue>
<attvalue for="score" value="3" start="2008.0" end="2008.0"></attvalue>
</attvalues>
<viz:size value="10.0"></viz:size>
<viz:position x="-293.90674" y="-442.9504" z="0.0"></viz:position>
<viz:color r="25" g="213" b="100"></viz:color>
</node>
</nodes>
<edges>
<edge source="n0" target="n1">
<attvalues>
<attvalue for="weight" value="3.0" start="2010.0" endopen="2012.0"></attvalue>
<attvalue for="weight" value="4.0" start="2012.0" endopen="2014.0"></attvalue>
<attvalue for="weight" value="4.0" start="2014.0" endopen="2016.0"></attvalue>
<attvalue for="weight" value="4.0" start="2016.0" endopen="2018.0"></attvalue>
<attvalue for="weight" value="5.0" start="2018.0" endopen="2020.0"></attvalue>
<attvalue for="weight" value="6.0" start="2020.0" endopen="2022.0"></attvalue>
<attvalue for="weight" value="8.0" start="2022.0" endopen="2024.0"></attvalue>
<attvalue for="weight" value="9.0" start="2024.0" end="2026.0"></attvalue>
</attvalues>
</edge>
</edges>
</graph>
</gexf>
I am not sure you can directly achieve that with Networkx. People have tried. What is important is that an edge should have a start and end property as double. Same for nodes. Then each edge should have a list of triplets:
[{'start': s, 'end' or 'endopen': e, 'weight': 4}, {..}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With