Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graph_from_edgelist automatically infers the existance of unnamed vertices. #1632

Open
TheLaughingDuck opened this issue Dec 11, 2024 · 3 comments

Comments

@TheLaughingDuck
Copy link

TheLaughingDuck commented Dec 11, 2024

The function graph_from_edgelist automatically infers the existance of unmentioned vertices
I found no clarification of this in the function doc. I think it's an unintuitive programming practice.

Example
This code describes a graph with three vertices, and two edges. However, because the vertices just happen to be named 100,101,102, the generated plot is massive and contains vertices named 1-99, not connected to the actual graph described.

edges <- data.frame(V1 = c(100,101), V2 = c(101,102))
edges <- as.matrix(edges)
plot(igraph::graph_from_edgelist(edges))
@maelle
Copy link
Contributor

maelle commented Dec 13, 2024

Thank you! Posting your reproducible example as reprex for easier later browsing

library("igraph")
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
edges <- data.frame(V1 = c(100,101), V2 = c(101,102))
edges <- as.matrix(edges)
edges
#>       V1  V2
#> [1,] 100 101
#> [2,] 101 102
plot(igraph::graph_from_edgelist(edges))

Created on 2024-12-13 with reprex v2.1.1

@maelle
Copy link
Contributor

maelle commented Dec 13, 2024

library("igraph")
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
edges <- data.frame(V1 = c(100,101), V2 = c(101,102))
edges <- as.matrix(edges)
edges
#>       V1  V2
#> [1,] 100 101
#> [2,] 101 102

g <- igraph::graph_from_edgelist(edges)
E(g)
#> + 2/2 edges from d019165:
#> [1] 100->101 101->102
V(g)
#> + 102/102 vertices, from d019165:
#>   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
#>  [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
#>  [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
#>  [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
#>  [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
#>  [91]  91  92  93  94  95  96  97  98  99 100 101 102

Created on 2024-12-13 with reprex v2.1.1

@maelle
Copy link
Contributor

maelle commented Dec 13, 2024

The docs say

If it is a numeric matrix then its elements are interpreted as vertex ids

But maybe we should make this point more prominent in the docs. @TheLaughingDuck where and how would you expect this info? Happy to then update the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants