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_data_frame does not simplify graph #394

Closed
SamGG opened this issue May 23, 2020 · 4 comments
Closed

graph_from_data_frame does not simplify graph #394

SamGG opened this issue May 23, 2020 · 4 comments

Comments

@SamGG
Copy link

SamGG commented May 23, 2020

Hi,
Thanks for maintaining the important igraph package giving a uniform access to many algorithms.
I think that the graph_from_data_frame function is not explicit concerning the directed argument. When entering a not directed graph with duplicated links, one might expect that the graph will be simplified implicitly. It is not the case, and I think this should be clearly stated in the documentation.
I don't know how an undirected and unsimplified graph is handled by community detection algorithms.
Best.

library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
df = data.frame(from=c(1,2,3,4,1), to=c(2,1,3,5,2), weight=c(1,1,1,1,3))

g <- graph_from_data_frame(df)
g
#> IGRAPH 677ae70 DNW- 5 5 -- 
#> + attr: name (v/c), weight (e/n)
#> + edges from 677ae70 (vertex names):
#> [1] 1->2 2->1 3->3 4->5 1->2
vertex.attributes(g)
#> $name
#> [1] "1" "2" "3" "4" "5"

g <- graph_from_data_frame(df, directed = FALSE)
g
#> IGRAPH 677bf7b UNW- 5 5 -- 
#> + attr: name (v/c), weight (e/n)
#> + edges from 677bf7b (vertex names):
#> [1] 1--2 1--2 3--3 4--5 1--2
edge.attributes(g)
#> $weight
#> [1] 1 1 1 1 3

h <- simplify(g, remove.multiple = FALSE, edge.attr.comb = "sum")
h
#> IGRAPH 677ce17 UNW- 5 4 -- 
#> + attr: name (v/c), weight (e/n)
#> + edges from 677ce17 (vertex names):
#> [1] 1--2 1--2 4--5 1--2
edge.attributes(h)
#> $weight
#> [1] 1 1 1 3

h <- simplify(g, edge.attr.comb = "sum")
h
#> IGRAPH 677d7d4 UNW- 5 2 -- 
#> + attr: name (v/c), weight (e/n)
#> + edges from 677d7d4 (vertex names):
#> [1] 1--2 4--5
edge.attributes(h)
#> $weight
#> [1] 5 1

Created on 2020-05-23 by the reprex package (v0.3.0)

@mbojan
Copy link

mbojan commented May 23, 2020

I don't think graph_from_data_frame() should simplify the graph. There are many settings in which you do not want that and want to make the graph with multiple edges as they are in the edgelist. It is IMHO the right approach in your example that you

graph_from_data_frame(...) %>%
  simplify(...)

if you need to analyze a simple graph instead of a multi-graph.

@gaborcsardi
Copy link
Contributor

gaborcsardi commented May 23, 2020

It is clear that we cannot change this function now:

  • it would break existing code,
  • as @mbojan argues, sometimes you do need those multiple edges, and
  • it is not clear what to do with the edge attributes when simplifying the graph.

@SamGG if you think this is surprising behavior, we can add a note to the manual page. Would you like to submit a pull request?

@SamGG
Copy link
Author

SamGG commented May 24, 2020

Thanks for your feedback. I didn't intend to change the behavior of this function, but just to make the documentation more clear about that. I will do a PR.

@ntamas
Copy link
Member

ntamas commented Oct 8, 2021

The documentation of graph_from_data_frame now includes this sentence:

All edges in the data frame are included in the graph, which may include multiple parallel edges and loops.

I don't know when this was added, but I consider the issue as fixed.

@ntamas ntamas closed this as completed Oct 8, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Mar 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants