rewove
rewove

Reputation: 79

How to add specific weights to some edges?

In a network I find out some specific nodes, for example 3, 4, 5 and an initial node 9. I want to add weights to those edges and I need to call in future.

More specific: I need to add weights to edge:(3,9), (4,9), (5,9). And lately I need to recall those weights to do some calculation, i.e. I need a="(3,9)'s weights" something like this.

Upvotes: 1

Views: 377

Answers (1)

G5W
G5W

Reputation: 37661

Since you do not provide any data, I will use a simple example that has links like the ones you describe.

## A simple example
library(igraph)
set.seed(1234)
g = make_ring(10)
g = add_edges(g, c(3,9,4,9,5,9))
E(g)$weight = 1
LO = layout_nicely(g)
plot(g, layout=LO)

Example graph

If you have the "Intitial Node" and the "Specific Nodes", you can identify the Special Edges.

## Get the ids of the special edges
InitialNode = 9
ConnectingNodes = c(3,4,5)
ENDS = as.vector(rbind(ConnectingNodes, InitialNode))
SpecialEdges = get.edge.ids(g, ENDS)

With the IDs of the special edges, you can adjust their weights.

## Add weight to the special edges
E(g)$weight[SpecialEdges] = c(2,4,6)

## plot to show the weights
plot(g, edge.width=E(g)$weight)

Graph with edge weights

If you later need to do something with the weights, you can access the weights using:

E(g)$weight[SpecialEdges]
[1] 2 4 6

Upvotes: 1

Related Questions