-
Notifications
You must be signed in to change notification settings - Fork 7
/
graph.R
35 lines (33 loc) · 899 Bytes
/
graph.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
library(igraph)
# edges - matrix n x 2 with n > 2
delete.cycles <- function(edges) {
find.edge <- function(edges, e) {
which(apply(t(edges) == e, 2, all))
}
# find cycle starting with given vertex
detect.cycle <- function(g, v) {
starts <- tail_of(g, incident(g, v, mode="out"))
ends <- head_of(g, incident(g, v, mode="in"))
for(i in starts) {
for(j in ends) {
p <- vertex.disjoint.paths(g, i, j)
if(p) g[v,i] <<- 0
}
}
}
g <- graph(edges=t(edges), directed=T)
if(is.dag(g)) return(edges)
i <- 1
while(TRUE) {
detect.cycle(g, i)
i = i + 1
if(i == vcount(g)) break
}
as_edgelist(g)
}
tograph <- function(semrel) {
# different types of edges?
e <- rbind(semrel[[2]], semrel[[3]])
g <- graph(edges=t(e), directed=T)
g
}