R/seedQuery_GS.R

seedQuery_GS <- function (G, seed, search_r = 2, r = 0.1, maxsize = NULL)
{
  #if (!require(igraph)) {
   # stop("igraph must be pre-installed!\n")
  #}
  net <- G
  d <- search_r
  if (!is.element("name", list.vertex.attributes(net))) {
    stop("Graph must have 'name' attribute")
  }
  if (!is.element("weight", list.vertex.attributes(net))) {
    stop("Graph must have 'weight' attribute")
  }
  subG <- induced.subgraph(net, seed)
  if (!is.connected(subG)) {
    stop("Input seeds are disjoint")
  }
  in.nodes <- V(subG)$name
  while (TRUE) {
    subx <- V(subG)$name
    for (rad in 1:d) {
      subsum <- sum(V(subG)$weight)/sqrt(length(subx))
      tmp.neigh <- unlist(neighborhood(net, order = rad,
                                       nodes = V(subG)$name))
      pot.nodes <- V(net)[tmp.neigh]$name
      pot.nodes <- setdiff(pot.nodes, in.nodes)
      if (length(pot.nodes) == 0)
        break
      sub.weg <- V(net)[pot.nodes]$weight
      best.nodes <- pot.nodes[which(sub.weg == max(sub.weg))]
      subsum.u <- (sum(V(subG)$weight) + V(net)[best.nodes[1]]$weight)/sqrt(length(subx) +
                                                                              1)
      if (subsum.u > subsum * (1 + r)) {
        tmp <- unlist(lapply(best.nodes[1], function(x) node2treePath(net,
                                                                   V(subG)$name, x)))
        in.nodes <- c(tmp, V(subG)$name)
        subG <- induced.subgraph(net, in.nodes)
        break
      }
    }

    if (is.null(maxsize)){
      if (length(subx) == vcount(subG))#length(subx) == vcount(subG)
              break
    }
    if (!is.null(maxsize)) {
      if (length(subx) == vcount(subG)){
        break
      }
      if (vcount(subG) >= maxsize){
        break
      }
    }
  }
  return(subG)
}
leiming8886/fRNC documentation built on Feb. 21, 2023, 4:12 p.m.