Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.30 KB | None | 0 0
  1. lexicographic.dfs <- function (graph)
  2. {
  3.   nnodes = dim(graph)[2]
  4.   orderSets = rep(1, nnodes) # all nodes are in set ID 1 to begin with
  5.   for (i in 1:nnodes) {
  6.     currentSet       = orderSets[i]                         # set ID to be redistributed
  7.     nodesInSet       = c(1:nnodes)[orderSets == currentSet] # nodes in current set
  8.     nodeNeighbors    = neighbors(graph, i)                  # all current neighbors
  9.     setNeighbors     = intersect(nodesInSet, nodeNeighbors) # neighbors in current set
  10.     setNonNeighbors  = setdiff(nodesInSet, nodeNeighbors)   # nodes in set that are not neighbors
  11.     orderSets[setNeighbors] = max(orderSets) + 1            # move all neighbors to new set
  12.     orderSets[setNonNeighbors] = max(orderSets) + 1         # move all non neighbors to new set
  13.     orderSets[i] = currentSet                               # keep current node in own set
  14.   }
  15.   return (orderSets)
  16. }
  17.  
  18. is.chordal <- function (graph)
  19. {
  20.   ordering = lexicographic.dfs(graph)
  21.   n        = length(ordering)
  22.   cliques  = cliques(as.igraph(graph))
  23.   for (i in 1:(n-1)) {
  24.     currentNeighbors = neighbors(graph, i)
  25.     foundNeighbors = intersect(ordering[c((i+1):n)], currentNeighbors)
  26.     if (!is.element(foundNeighbors, cliques)) { # <- does not work??? :(
  27.       return (FALSE)
  28.     }
  29.   }
  30.   return (TRUE)
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement