Description Usage Arguments Value See Also Examples
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.
1 2 |
graph |
[ |
generator |
[ |
weights |
[ |
symmetric |
[ |
to.int |
[ |
... |
[any] |
[grapherator
] Graph.
Other graph generators: addEdges
,
addNodes
, graph
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.