How to use graph-tool to create and modify graphs dynamically?

Hi All,

I am new to graph-tool and I am trying to figure out how to create and
display graphs dynamically by which I mean the following.

To simplify the problem Lets say I have the following edges

(1,2)
(3,4)
(3,7)
(3, 8)
(2, 3)

I don't want to construct the graph with all the edges at once. instead I
want to add or delete an edge one by one and render/display that
immediately such that I can show the user how the graph is evolving after
each edge. Think of it more like a stream of edges that can be either added
or deleted.

Please let me know if you need more information on the question. otherwise,
I hope to hear how this can be achieved using graph-tool?

Thanks,
Kant

attachment.html (994 Bytes)

There is an example of how to do this in the documentation:

https://graph-tool.skewed.de/static/doc/demos/animation/animation.html#dynamic-layout

Hi,

I copy pasted the code from the link you sent me and change to the
following code

#! /usr/bin/env python
from graph_tool.allimport *from numpy.randomimport *from
numpy.linalgimport normimport sys, os, os.path

seed(42)
seed_rng(42)
from gi.repositoryimport Gtk, Gdk, GdkPixbuf, GObject, GLib

g = Graph()

step = 0.005# move step
K = 0.5# preferred edge length

pos = sfdp_layout(g, K = K)# initial layout positions

win = GraphWindow(g, pos, geometry = (500, 400))

edges = [(1, 2), (3, 4), (3, 2), (3, 5), (3, 6), (2, 5)]
count = 0
def update_state():
  global count

sfdp_layout(g, pos = pos, K = K, init_step = step, max_iter = 1)
# my codefor each in edges:
  g.add_edge(each[0], each[1])
if count > 0 and count % 1000 == 0:
  win.graph.fit_to_window(ink = True)

count += 1
win.graph.regenerate_surface()
win.graph.queue_draw()
return True

cid = GLib.idle_add(update_state)
win.connect("delete_event", Gtk.main_quit)

win.show_all()
Gtk.main()

This doesn't seem to work I get RuntimeError: invalid matrix (not
invertible)
Any idea? it looks to me this one line g = random_graph(150, lambda: 1 +
poisson(5), directed=False)
makes a difference? But I want to create my own graph like the code above.

Also what does idle_add do? it calls update whenever it is idle? which
means it redraws whenever it is idle?

Thanks!

attachment.html (12.8 KB)

This doesn't seem to work I get RuntimeError: invalid matrix (not
invertible)
Any idea? it looks to me this one
line g=random_graph(150,lambda:1+poisson(5),directed=False)
makes a difference? But I want to create my own graph like the code above.

You are computing the initial layout on an empty graph, before you add
any edges. You need first to add the edges, and then compute the initial
layout.

Also what does idle_add do? it calls update whenever it is idle? which
means it redraws whenever it is idle?

Yes.

Best,
Tiago