[PATCH] Fix: igraph graph.adjacency(..) vertex id assignment

  • From: Mitchell Joblin <mitchell.joblin.ext@xxxxxxxxxxx>
  • Date: Sun, 17 Nov 2013 13:54:09 +0100

- we want to maintain a sequencial indexing for all graphs and
  subgraphs as a design decision
- graph.adjacency will use column names to assign vertex ids
  if they are provided, otherwise a sequencial index is the
  default with numeric type
- if indexing from a larger matrix, the global index will be
  used
- it is not possible to reassign vertex ids as an numeric
  type after a string has been used (e.g. V(g)$name <- numeric)
- to avoid a string cast everytime we want to use the vertex
  ids for indexing the only solution is to generate the igraph
  graph object from a matrix with no row/col lables

Signed-off-by: Mitchell Joblin <mitchell.joblin.ext@xxxxxxxxxxx>
---
 codeface/R/cluster/persons.r | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/codeface/R/cluster/persons.r b/codeface/R/cluster/persons.r
index 25e85be..47689db 100755
--- a/codeface/R/cluster/persons.r
+++ b/codeface/R/cluster/persons.r
@@ -647,8 +647,17 @@ plot.group <- function(N, .tags, .iddb, .comm) {
 ## Given a single cluster of persons, construct an igraph object,
 ## compute some attributes for proper visualisation, and export the
 ## result as a graphviz dot format if a filename is provided.
-save.group <- function(conf, .tags, .iddb, idx, .prank, .filename=NULL,
label) {
-  g <- graph.adjacency(.tags[idx,idx], mode="directed")
+save.group <- function(conf, .adj.mat, .iddb, idx, .prank, .filename=NULL,
label) {
+  adj.mat.sub <- .adj.mat[idx,idx]
+  ## remove row and column names otherwise igraph assigns vertices string
+  ## ids, even using V(g)$name <- numeric.vec won't eliminate the string
type id
+  ## which makes it impossible for using the vertex ids to index unless
+  ## we do a string to integer cast everytime we want to use the index
+  rownames(adj.mat.sub) <- NULL
+  colnames(adj.mat.sub) <- NULL
+
+  g <- graph.adjacency(adj.mat.sub, mode="directed")
+
   ## as.character is important. The igraph C export routines bark
   ## otherwise (not sure what the actual issue is)
   ## NOTE: V(g)$name as label index does NOT work because the name
attribute
-- 
1.7.12.4 (Apple Git-37)

--001a113375767a538804eb894162
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hi,<div><br></div><div style>It has recently become appare=
nt that we rely on igraph to automatically index the vertices sequentially,=
 although there are exceptions where this sequential index assumption is si=
mply incorrect. In general, we need to be more careful of these indexing is=
sues and incorporate more sanity checks after graph manipulations. In this =
direction, the following patch can be used to introduce explicit ids for th=
e vertices by setting row and column names for the adjacency matrix. This i=
s not an appropriate solution because now igraph use vertex ids that are st=
rings by casting integer row and column names from the adjacency matrix. We=
 need the vertex ids to be integer type so that we can use it for indexing =
other data structures. Until this moment I see no opportunity to convert th=
e vertex ids to integer type and using the V(g)$name attribute is restricte=
d to character type.</div>
<div style><br></div><div style>While searching for a solution I also came =
across this posting (<a href=3D"http://lists.gnu.org/archive/html/igraph-he=
lp/2007-10/msg00014.html">http://lists.gnu.org/archive/html/igraph-help/200=
7-10/msg00014.html</a>) where the creator of igraph mentions that sometimes=
 the vertex ids can be silently renumbered.=A0</div>
<div style><br></div><div style>I propose that we should handle indexing en=
tirely on our own by assigning a vertex attribute that is not ever manipula=
ted by igraph. We should implement sanity checks after all graph manipulati=
ons to check the vertex attribute has not been dropped or changed.=A0</div>
<div style><br></div><div style>Kind regards,=A0</div><div style><br></div>=
<div style>Mitchell</div><div style><p class=3D""><span style=3D"font-famil=
y:&#39;Courier New&#39;">From
4d24b49cf3cbec631ce033bdf5d661c6cc7af5bd Mon Sep 17 00:00:00 2001<br>
From: Mitchell Joblin &lt;<a href=3D"mailto:mitchell.joblin.ext@xxxxxxxxxxx=
">mitchell.joblin.ext@xxxxxxxxxxx</a>&gt;<br>
Date: Sun, 17 Nov 2013 13:54:09 +0100<br>
Subject: [PATCH] Fix: igraph graph.adjacency(..) vertex id assignment<br>
<br>
- we want to maintain a sequencial indexing for all graphs and<br>
=A0 subgraphs as a design decision<br>
- graph.adjacency will use column names to assign vertex ids<br>
=A0 if they are provided, otherwise a
sequencial index is the<br>
=A0 default with numeric type<br>
- if indexing from a larger matrix, the global index will be<br>
=A0 used<br>
- it is not possible to reassign vertex ids as an numeric<br>
=A0 type after a string has been used (e.g.
V(g)$name &lt;- numeric)<br>
- to avoid a string cast everytime we want to use the vertex<br>
=A0 ids for indexing the only solution is
to generate the igraph<br>
=A0 graph object from a matrix with no
row/col lables<br>
<br>
Signed-off-by: Mitchell Joblin &lt;<a href=3D"mailto:mitchell.joblin.ext@si=
emens.com">mitchell.joblin.ext@xxxxxxxxxxx</a>&gt;<br>
---<br>
=A0codeface/R/cluster/persons.r | 13
+++++++++++--<br>
=A01 file changed, 11 insertions(+), 2
deletions(-)<br>
<br>
diff --git a/codeface/R/cluster/persons.r b/codeface/R/cluster/persons.r<br=
>
index 25e85be..47689db 100755<br>
--- a/codeface/R/cluster/persons.r<br>
+++ b/codeface/R/cluster/persons.r<br>
@@ -647,8 +647,17 @@ plot.group &lt;- function(N, .tags, .iddb, .comm) {<br=
>
=A0## Given a single cluster of persons,
construct an igraph object,<br>
=A0## compute some attributes for proper
visualisation, and export the<br>
=A0## result as a graphviz dot format if a
filename is provided.<br>
-save.group &lt;- function(conf, .tags, .iddb, idx, .prank, .filename=3DNUL=
L,
label) {<br>
-=A0 g &lt;-
graph.adjacency(.tags[idx,idx], mode=3D&quot;directed&quot;)<br>
+save.group &lt;- function(conf, .adj.mat, .iddb, idx, .prank, .filename=3D=
NULL,
label) {<br>
+=A0 adj.mat.sub &lt;-
.adj.mat[idx,idx]<br>
+=A0 ## remove row and column names
otherwise igraph assigns vertices string<br>
+=A0 ## ids, even using V(g)$name &lt;-
numeric.vec won&#39;t eliminate the string type id<br>
+=A0 ## which makes it impossible for using
the vertex ids to index unless <br>
+=A0 ## we do a string to integer cast
everytime we want to use the index<br>
+=A0 rownames(adj.mat.sub) &lt;- NULL<br>
+=A0 colnames(adj.mat.sub) &lt;- NULL<br>
+<br>
+=A0 g &lt;- graph.adjacency(adj.mat.sub,
mode=3D&quot;directed&quot;)<br>
+<br>
=A0=A0 ## as.character is important. The
igraph C export routines bark<br>
=A0=A0 ## otherwise (not sure what the actual
issue is)<br>
=A0=A0 ## NOTE: V(g)$name as label index does
NOT work because the name attribute<br>
-- <br>
1.7.12.4 (Apple Git-37)</span></p></div><div style><br></div><div style><br=
></div></div>

--001a113375767a538804eb894162--

Other related posts:

  • » [PATCH] Fix: igraph graph.adjacency(..) vertex id assignment - Mitchell Joblin