addWeights: Add weights to graph.

Description Usage Arguments Value See Also Examples

Description

addWeights allows to add edge weights to a graph. This is the last step of the graph generation process. Note that adding edges is not possible once addWeights was called once.

Usage

1
2
addWeights(graph, generator = NULL, weights = NULL, symmetric = TRUE,
  to.int = FALSE, ...)

Arguments

graph

[grapherator]
Graph.

generator

[function(graph, ...)]
Function used to generate weights. The functions needs to expect the graph as the first argument graph. Additional control argument are possible.

weights

[matrix]
Square matrix of weights. If some weights are already assigned, pay attention to the correct dimensions. If this is passed all other arguments are ignored. Default is NULL.

symmetric

[logical(1)]
Should the weights be symmetric, i.e., w(i, j) = w(j, i) for each pair i, j of nodes? Default is TRUE.

to.int

[logical(1)]
Should weights be rounded to integer? Default is FALSE.

...

[any]
Additional arguments passed down to generator.

Value

[grapherator] Graph.

See Also

Other graph generators: addEdges, addNodes, graph

Examples

 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
36
37
38
39
40
41
42
43
44
45
46
47
48
# first we define a simple graph
g = graph(0, 100)
g = addNodes(g, n = 5, generator = addNodesLHS)
g = addNodes(g, n = c(3, 10, 20, 10, 40), by.centers = TRUE, generator = addNodesUniform,
  lower = c(0, 0), upper = c(15, 15))
g = addEdges(g, generator = addEdgesDelauney)

# first graph contains two integer random weights per edge
g1 = addWeights(g, generator = addWeightsRandom, method = runif, min = 10, max = 20, to.int = TRUE)
g1 = addWeights(g, generator = addWeightsRandom, method = runif, min = 10, max = 30, to.int = TRUE)
## Not run: 
plot(g1)$pl.weights

## End(Not run)

# next one contains correlated weights. The first weight corresponds to the
# Euclidean distance of the points, the second is generated in a way, that
# a given correlation rho is achieved.
g2 = addWeights(g, generator = addWeightsCorrelated, rho = -0.7)
## Not run: 
plot(g2)$pl.weights

## End(Not run)

# Last example contains two weights per edge: the first one is the Manhattan
# block distance between the nodes in the plane. The second one is the Euclidean
# distance plus a normally distributed jitter. Here we write a custom weight
# generator which returns two weight matrizes.
myWeightGenerator = function(graph, ...) {
  n = getNumberOfNodes(graph)
  adj.mat = getAdjacencyMatrix(graph)
  coords = getNodeCoordinates(graph)

  man.dist = as.matrix(dist(coords), method = "manhattan")
  euc.dist = as.matrix(dist(coords)) + abs(rnorm(n * n, ...))

  # keep in mind non-existent edges
  euc.dist[!adj.mat] = man.dist[!adj.mat] = Inf

  # return the necessary format
  return(list(weights = list(man.dist, euc.dist), generator = "MyWG"))
}

g3 = addWeights(g, generator = myWeightGenerator, mean = 30, sd = 5)
## Not run: 
plot(g3)$pl.weights

## End(Not run)

grapherator documentation built on May 1, 2019, 8:19 p.m.