igraph-help
[Top][All Lists]

## Re: [igraph] I am not understanding something.....

 From: Tamas Nepusz Subject: Re: [igraph] I am not understanding something..... Date: Wed, 18 Jun 2008 13:28:39 +0200

```Now, how can i build a cycle like "for every vertex in G"?
```
```Either you can simply loop over all vertex indices in a for loop:

for i in xrange(g.vcount()):
print g.neighbors()    # or do whatever you want

```
Or if the core of your loop is simple, you can use a list comprehension from Python 2.4 up:
```
deg_squared = [g.degree(i) ** 2 for i in xrange(g.vcount())]

```
(this returns the square of the degrees -- okay, pretty dump example, I know)
```
A simple way to build an adjacency list of the graph is:

adj_list = [g.neighbors(i) for i in xrange(g.vcount())]

```
If you want to do something with the vertex attributes, you can also loop over the vertex sequence of the graph this way:
```
for vertex in g.vs:
print vertex["name"], g.degree(vertex.index)

```
g.vs returns a VertexSeq object, which practically behaves like an array, so you can iterate over it, you can index it and so on. See help(VertexSeq) for more details.
```
--
Tamas

```
```

Thanks again!

marco

```
On Wed, Jun 18, 2008 at 1:17 PM, Tamas Nepusz <address@hidden> wrote:
I am not understanding how to cycle on every node of a graph, and once
```"i am looking" at a one node, how to cycle between the neighbors of
that node....
```
```
```
Given a Graph object g, g.neighbors(i) gives you the neighbors of node i. g.successors(i) gives you the successors of that node (following outgoing edges), while g.predecessors(i) gives you the predecessors (following the
```incoming edges). Note that you'll have to supply vertex IDs to
g.neighbors().

E.g.:

```
```g = Graph.Tree(10,3)
g.neighbors(0)
```
```[1, 2, 3]

--
T.

_______________________________________________
igraph-help mailing list
http://lists.nongnu.org/mailman/listinfo/igraph-help

```
```

--
Calvin Coolidge  - "I have never been hurt by what I have not said."

_______________________________________________
igraph-help mailing list
```