0

I have a series of digraphs containing the same nodes, but differing edges - a dynamic/temporal graph - and am stuck for ideas about how best to implement this (Matlab or Python preferably).

I would like to construct a digraph'esque structure whereby each node connects to itself over each time-step (A0 -> A1 -> etc), and also to any other edges at that time-step. For example, in a digraph with nodes {A, B}, at time t_0 the digraph is disconnected. At time t_1, there is an edge from A to B. I would like to create something along the lines of: A0 -> {A1, B1}. B0 -> {B1}. My problem is that I can't get the digraph function to store the node and the time-step. I don't want to use different nodes to represent different time-steps because it makes comparisons too difficult. I want to point to the 'same' node, but store an additional time variable that I can access when traversing my digraph using breadth first search, for example.

Here is a picture of what I hope to implement!

Any ideas?

Thanks for your help!

1 Answers1

0

As always, the appropriate data structure depends on the operations you want to perform on the data structure. So without a better understanding of what you plan to do, there is no clear answer.

However, perhaps you could store each state of the graph as an adjacency matrix. Then you could stack the 2D adjacency matrices into a 3D array. That's a nice compact structure which would give you easy access to each graph as a slice of the 3D array, and perhaps allow you to do the across-timestep operations you envisage.

Using networkx, you can convert to and from adjacency matrices using nx.from_numpy_matrix and nx.to_numpy_matrix:

import numpy as np
import networkx as nx

arrs = np.array([[(0,0), (0,0)], 
                [(1,1), (0,1)]])

for arr in arrs:
    G = nx.from_numpy_matrix(arr)
    print(G.edges())
    assert np.allclose(arr, nx.to_numpy_matrix(G))

on the first iteration prints

[]            

since there are no edges. And at the second iteration, it prints

[(0, 0), (0, 1), (1, 1)]  

since there are edges from node 0 to node 0, node 0 to node 1, and node 1 to node 1:

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677