2

I have created a directed graph and through a program i am finding out all the cycles it contains. After creating the graph i want to change the colour of the edges which contain the vertices involved in the cycle.

I am using python igraph.

Please Help

Fyre
  • 1,180
  • 8
  • 12

1 Answers1

4

Something like this:

vertex_set = set(vertices_in_cycle)
g.es["color"] = "black"
red_edges = g.es.select(_source_in=vertex_set, _target_in=vertex_set)
red_edges["color"] = "red"

Explanation:

  1. g.es represents the set of all edges in the graph. (Similarly, g.vs is the set of all vertices).

  2. g.es["color"] lets you assign a value to the color attribute of all the edges in the graph. This edge attribute is used by the plotter to decide what color an edge should have. Therefore, in line 2, you are setting the color of all the edges to black. (Note: you could also use a list here instead of a simple string, or you could use HTML color notations for custom colors).

  3. You could use g.es as a list, in which case you get a particular edge of the graph; e.g., g.es[2] would give you the edge with id=2. This is not used here, but it's good to know.

  4. g.es.select is a method that selects a subset of the edges based on some criteria. help(EdgeSeq.select) gives you more info about this; the point here is that in line 3, you are selecting all the edges for which both endpoints lie in the vertex set you are interested in. The selected edges are stored in the red_edges variable, which has the same type as g.es (i.e. EdgeSeq).

  5. In the last row, you are setting the color of all the edges in red_edges to red, overriding the black color you have set in line 2.

Note that the above code will paint not only the edges of the cycle to red but also all the chords of the cycle.

Update: if line 3 in the above code does not work for you for some reason, you can replace lines 2 and 3 with the following:

g.es["color"] = ["red" if (edge.source in vertex_set and \
                           edge.target in vertex_set) else "black" \
                 for edge in g.es]
Tamás
  • 47,239
  • 12
  • 105
  • 124
  • when i tried to implement the above code an error came up AttributeError:'Graph' object has no attribute 'source' – Fyre Apr 09 '12 at 13:04
  • Chances are that you wrote `source_in` and `target_in` instead of `_source_in` and `_target_in` (note the leading underscore). However, it would be informative if you told me which of the commands above gave you this error. – Tamás Apr 09 '12 at 18:58
  • yes i have used the leading underscore but i still i am getting the same error – Fyre Apr 09 '12 at 22:04
  • red_edges = g.es.select(_source_in=vertex_set, _target_in=vertex_set) – Fyre Apr 10 '12 at 14:45
  • I don't know why it doesn't work for you, but I've updated my answer with an alternative solution. – Tamás Apr 11 '12 at 09:01
  • tamas is there any way i can create a animated gif of a cycle being flashed in different colours because i need to highlight my cycle – Fyre Apr 11 '12 at 09:14
  • This should be a separate question; anyway, create separate plots corresponding to each frame and then merge them into an animated GIF using some external software which can do this. – Tamás Apr 11 '12 at 13:34
  • so there is no way in which i can create a gif from the python code itself? It would even work if i can get the plot not saved in any version ie i should be able to see the plot when i run my program showing animated lines – Fyre Apr 11 '12 at 18:41
  • There is, but that has nothing to do with igraph. igraph is primarily not for visualization, so the only thing you can do is to save the frames and then convert them to an animated GIF somehow. This question on Stack Overflow may help a bit: http://stackoverflow.com/questions/753190/programmatically-generate-video-or-animated-gif-in-python – Tamás Apr 11 '12 at 18:57
  • I dont want to necessarily save the image i m saying it is even ok if i can get the animated cycle at the time of plotting – Fyre Apr 11 '12 at 20:23
  • Extended discussions should be avoided in comments, but I cannot move this discussion to the chat because you don't have enough rep, so I'm answering here. You cannot get the animated cycle at the time of plotting because the only thing igraph does is that it saves the plot into a temporary file and invokes an image viewer to show that file. As igraph cannot do any plotting into animated GIFs by default, you cannot get any animation by using igraph only. – Tamás Apr 11 '12 at 20:41
  • On the sourceforge screenshots of igraph with R the last example shows a animated image.I want something like that is it possible in python? – Fyre Apr 12 '12 at 00:14
  • 1
    That was created exactly the way I told you: Gábor (the author of igraph) created the images separately and then merged them into an animated GIF using an external tool (probably ImageMagick). The code snippet you see on the website does the plotting only, but not the merging into an animated GIF. – Tamás Apr 12 '12 at 18:28