File: test_average.path.length.R

package info (click to toggle)
r-cran-igraph 1.0.1-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 18,232 kB
  • sloc: ansic: 173,538; cpp: 19,365; fortran: 4,550; yacc: 1,164; tcl: 931; lex: 484; makefile: 149; sh: 9
file content (34 lines) | stat: -rw-r--r-- 873 bytes parent folder | download | duplicates (4)
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

context("mean_distance")

test_that("mean_distance works", {
  library(igraph)

  apl <- function(graph) {
    sp <- distances(graph, mode="out")
    if (is_directed(graph)) {
      diag(sp) <- NA
    } else {
      sp[lower.tri(sp, diag=TRUE)] <- NA
    }
    sp[sp=="Inf"] <- NA
    mean(sp, na.rm=TRUE)
  }

  giant.component <- function(graph, mode="weak") {
    clu <- components(graph, mode=mode)
    induced_subgraph(graph, which(clu$membership==which.max(clu$csize)))
  }
  
  g <- giant.component(sample_gnp(100, 3/100))
  expect_that(apl(g), equals(mean_distance(g)))

  g <- giant.component(sample_gnp(100, 6/100, dir=TRUE), mode="strong")
  expect_that(apl(g), equals(mean_distance(g)))

  g <- sample_gnp(100, 2/100)
  expect_that(apl(g), equals(mean_distance(g)))
  
  g <- sample_gnp(100, 4/100, dir=TRUE)
  expect_that(apl(g), equals(mean_distance(g)))
})