rigraph icon indicating copy to clipboard operation
rigraph copied to clipboard

Sparse matrix with negative values causing error

Open ronammar opened this issue 10 years ago • 2 comments

I'm uncertain if this is a bug, but I'm posting it as a potential issue:

g1 <- make_graph(c(1,2, 2,3, 2,4, 2,5, 3,4, 3,5, 4,5, 5,6, 6,7, 6,8, 7,8, 7,10, 8,9), directed=FALSE)
g2 <- make_graph(c(1,2, 1,4, 2,3, 2,4, 4,5, 5,6, 6,7, 6,8, 7,8, 7,10, 8,9), directed=FALSE)
ga1 <- get.adjacency(g1)
ga2 <- get.adjacency(g2)
# This throws the error "Error in rep(unname(x[1:2]), x[3]) : invalid 'times' argument"
g3 <- graph_from_adjacency_matrix(drop0(ga1-ga2))
# Yet this works
g3 <- graph_from_adjacency_matrix(as.matrix(drop0(ga1-ga2)))

If I create the graph casting to matrix first, and then extract the adjacency matrix, the negative values are absent, leading me to believe they are the source of the error when using the sparse matrix (with explicit 0's dropped)

all.equal(get.adjacency(g1), ga1)

[1] TRUE

all.equal(get.adjacency(g3), as.matrix(drop0(ga1-ga2)))

[1] "names for target but not for current" "Length mismatch: comparison on first 0 components" [3] "class(target) is dgCMatrix, current is matrix"

ronammar avatar Aug 25 '15 19:08 ronammar

Looks like the same as #269.

szhorvat avatar Apr 01 '22 13:04 szhorvat

This is not a bug; the documentation states that when weighted=NULL (which is the default), igraph will create an unweighted graph and the number of edges is specified by the cells of the adjacency matrix. You cannot repeat an edge -1 times, hence the error. You need to pass weighted=TRUE to graph_from_adjacency_matrix().

Note that the second call (g3 <- graph_from_adjacency_matrix(as.matrix(drop0(ga1-ga2)))) seems to work, but in reality it doesn't because it loses the weights. That seems to be a bug in graph_from_adjacency_matrix() in the sense that it does not repeat the edges the specified number of times when the input is a dense matrix and weighted is set to NULL.

ntamas avatar Apr 01 '22 15:04 ntamas

Now:

library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
library(Matrix)
g1 <- make_graph(c(1, 2, 2, 3, 2, 4, 2, 5, 3, 4, 3, 5, 4, 5, 5, 6, 6, 7, 6, 8, 7, 8, 7, 10, 8, 9), directed = FALSE)
g2 <- make_graph(c(1, 2, 1, 4, 2, 3, 2, 4, 4, 5, 5, 6, 6, 7, 6, 8, 7, 8, 7, 10, 8, 9), directed = FALSE)
ga1 <- get.adjacency(g1)
#> Warning: `get.adjacency()` was deprecated in igraph 2.0.0.
#> ℹ Please use `as_adjacency_matrix()` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> Warning in as_adjacency_matrix(graph = graph, type = type, attr = attr, : The
#> `edges` argument of `as_adjacency_matrix` is deprecated; it will be removed in
#> igraph 1.4.0
ga2 <- get.adjacency(g2)
#> Warning in as_adjacency_matrix(graph = graph, type = type, attr = attr, : The
#> `edges` argument of `as_adjacency_matrix` is deprecated; it will be removed in
#> igraph 1.4.0
g3 <- graph_from_adjacency_matrix(drop0(ga1 - ga2))
#> Error in rep(unname(x[1:2]), x[3]): invalid 'times' argument
g3 <- graph_from_adjacency_matrix(as.matrix(drop0(ga1 - ga2)))
#> Error in graph.adjacency.dense(adjmatrix, mode = mode, weighted = weighted, : At vendor/cigraph/src/constructors/adjacency.c:330 : Edge counts should be non-negative, found -1. Invalid value

Created on 2024-01-02 with reprex v2.0.2

An error is fine. Please open a new issue if you think something's still not right.

krlmlr avatar Jan 02 '24 20:01 krlmlr