Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the target or source of an edge in igraph

Tags:

r

igraph

Given a graph fg, my example:

library("igraph")

fg <- feature.graph <- graph.empty()

fg <- fg + vertices("root", "ho", "ha", value=c(1,2,3)) +
  edges(c("root", "ho", "root", "ha"), label=c("en", "an"), prob=c(0.2,0.8))

I extract an edge of interest:

> edge.of.interest <- E(fg)[from("root")][[1]]
> edge.of.interest
Edge sequence:
    e             
e [1] root -> ho  

To now get the source or the target of this edge I can do:

> get.edge(fg, edge.of.interest)
[1] 1 2
> get.edge(fg, edge.of.interest)[1]
[1] 1
> get.edge(fg, edge.of.interest)[2]
[1] 2

I would have expected a function source and target with this effect:

> target(edge.of.interest)
2

I have been unable to find this or equivalent functions. Is the method through get.edge the best there is?

like image 231
kasterma Avatar asked Dec 19 '22 11:12

kasterma


1 Answers

I know it is an old question but it looks like I have an answer from the top of my head. Do you mean tail_of and head_of ?

> head_of(fg, edge.of.interest)$value
[1] 1

> tail_of(fg, edge.of.interest)$value
[1] 2

Also, ends function may be useful here if both ends are needed at the same time.

like image 150
akhmed Avatar answered Jan 04 '23 17:01

akhmed