I have a network with two types of edges, one with edge$weight=1 and the other edge$weight=2 (to define different relationships e.g. marriage vs. kinship). I want to choose the neighbors of nodes that are linked by edge$weight=2. Then, after getting the vertex names of the neighbors, I will calculate their degree only for edge$weight=1.
It seems simple, but the following code does not give the correct neighbor names:
V(net)[E(net)[E(net)$weight==2]]
Is there another way to get vertex names with respect to the edge type?
I think you are looking for the adj function which will return the vertices adjacent to a given edge set. In your case you can choose all the relevant vertices by using V(net)[ adj(E(net)$weight==2) ]
For example:
# make a sample graph with edge weights 1 or 2
net <- graph.full(20)
E(net)$weight <- sample(1:2,ecount(net),replace=T)
# now select only those vertices connected by an edge with weight == 2
V(net)[ adj(E(net)$weight==2) ]
Related
how can I create a complete list of dyads from a vertex list?
I have a list (1, 2, 3...) and I need to generate a list containing all possible dyads from that list (1-1, 1-2, 1-3, 2-1, 2-2,...).
I've tried with get.edgelist, but it doesn't work, because the graph is not fully connected (all nodes are connected among them).
Thanks
Using igraph, you can grab all edges of a graph using E(g). If you'd want all possible edges, you can apply it on a complete graph (a graph that is fully connected). If the vertices in your graph are indeed in sequence from 1 to n, you can use make_full_graph() to make a Kn - that is to say a fully connected graph. In this example, the graph has 14 vertices.
g <- make_full_graph(14, directed=F)
el <- as_edgelist(g)
edges <- E(g)
edges_list <- split(el, rep(1:nrow(el), each = ncol(el)))
edges_vert <- unlist(list(t(el)))
edges will be the igraph-object, but I think what you're after is a list in R, like edges_list.
As you see, length(edges_list) is 91 since it is an undirected graph, and the number of edges in complete graphs is a function of the number of vertices.
A complete graph with n vertices is commonly written Kn and has these many edges:
Note that in igraph dyads are called edges and nodes are called vertices.
I'm fairly new to IGraph in R.
I'm doing community detection using IGraph and have already built my communities /clusters using the walktrap technique.
Next, within each cluster, I want to count the number of vertices between each two certain vertices. The reason I want to do this is, for each vertex XX, I want to list vertices that are connected to XX via say max 3 vertices, meaning no further than 3 vertices away from XX.
Can anyone help how this can be done in R please?
making a random graph (for demonstration):
g <- erdos.renyi.game(100, 1/25)
plot(g,vertex.size=3)
get walktrap communities and save as vertex attribute:
V(g)$community<-walktrap.community(g, modularity = TRUE, membership = TRUE)$membership
V(g)$community
now make a subgraph containing only edges and vertices of one community, e.g. community 2:
sub<-induced.subgraph(g,v=V(g)$community==2)
plot(sub)
make a matrix containing all shortest paths:
shortestPs<-shortest.paths(sub)
now count the number of shortest paths smaller or equal to 3.
I also exclude shortest paths from each node to itself (shortestPaths!=0).
also divide by two because every node pair appears twice in the matrix for undirected graphs.
Number_of_shortest_paths_smaller_3 <- length(which(shortestPs<=3 & shortestPs!=0))/2
Number_of_shortest_paths_smaller_3
Hope that's close to what you need, good luck!
I would like to reproduce the kind of "community summary" graph like on page 6 of this paper:
http://arxiv.org/pdf/0803.0476v2.pdf
First a community algorithm is employed on the graph, e.g.:
wc <- walktrap.community(subgraph)
mc <- multilevel.community(subgraph)
Then the vertices are grouped according to community. The size of the community node is a function of the membership size and the edge width is a function of the total edges going from any member of community A to community B.
Please note I don't just want to encode community as color or convex hulls like this:
V(inSubGraph)$color <- commObj$membership+1
plot.igraph( inSubGraph, vertex.color = V(inSubGraph)$color)
or with the convex hulls:
plot(commObj, inSubGraph)
Use the contract.vertices function with the membership vector that the community detection method provides, followed by simplify. In particular:
Assign a numeric vertex attribute with a value of 1 to each vertex as follows: V(g)$size = 1
Assign a numeric edge attribute with a value of 1 to each edge as follows: E(g)$count = 1
Contract the communities into vertices as follows: comm.graph <- contract.vertices(g, wc$membership, vertex.attr.comb=list(size="sum", "ignore")); basically this specifies that the size attribute of the vertices being contracted should be summed and every other vertex attribute should be ignored. (See ?attribute.combination in R for more details). This call contracts the vertices but leaves the original edges so you now have as many edges between the vertices as there were in the original graph between the communities.
Collapse the multiple edges as follows: comm.graph <- simplify(comm.graph, remove.loops=FALSE, edge.attr.comb=list(count="sum", "ignore")).
You now have a graph named comm.graph where the vertices represent the communities of the original graph, the size vertex attribute corresponds to the number of vertices in each community in the original graph, and the count edge attribute corresponds to the number of edges between communities in the original graph.
I'm working on a directed graph in igraph for R. I'm trying to convert it to an undirected graph where just reciprocal edges of the former persist. Should be easy but I'm getting strange results.
first I did it like that
library(igraph)
load("dmNet.Rdata")
#http://www.unet.univie.ac.at/~a0406222/dmNet.Rdata
recNet <- as.undirected(net, mode = "mutual",edge.attr.comb="sum")
when I check E(recNet)$weight there are a lot of edges with a weight of 1, which should not be possible since the sum of two reciprocal edges has to be at least 2. Then I did it like that
recNet <- as.undirected(net, mode = "mutual",edge.attr.comb="c")
now I can see that there are actually some edges containing just one value. My new graph recNet seems to contain nonreciprocal edges of net. What am I doing wrong, or am I missunderstandig the "mutual option"?
This happens because some edges are self-loops in your graph. All the edges that end up with a weight of 1 are self-loops:
all(which(E(recNet)$weight == 1) %in% which(is.loop(recNet)))
# [1] TRUE
Apparently, a self-loop is considered as a mutual edge in a directed graph. If you want to consider self-loops as non-mutual, then you can just remove them from the graph. Be careful, though, because some vertices have multiple self-loops, and you might not want to remove these.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
What is the maximum number of edges in a directed graph with n nodes? Is there any upper bound?
If you have N nodes, there are N - 1 directed edges than can lead from it (going to every other node). Therefore, the maximum number of edges is N * (N - 1).
Directed graph:
Question: What's the maximum number of edges in a directed graph with n vertices?
Assume there are no self-loops.
Assume there there is at most one edge from a given start vertex to a given end vertex.
Each edge is specified by its start vertex and end vertex. There are n
choices for the start vertex. Since there are no self-loops, there are
n-1 choices for the end vertex. Multiplying these together counts all
possible choices.
Answer: n(n−1)
Undirected graph
Question: What's the maximum number of edges in an undirected graph with n vertices?
Assume there are no self-loops.
Assume there there is at most one edge from a given start vertex to a given end vertex.
In an undirected graph, each edge is specified by its two endpoints
and order doesn't matter. The number of edges is therefore the number
of subsets of size 2 chosen from the set of vertices. Since the set of
vertices has size n, the number of such subsets is given by the
binomial coefficient C(n,2) (also known as "n choose 2"). Using the
formula for binomial coefficients, C(n,2) = n(n-1)/2.
Answer: (n*(n-1))/2
In an undirected graph (excluding multigraphs), the answer is n*(n-1)/2. In a directed graph an edge may occur in both directions between two nodes, then the answer is n*(n-1).
In addition to the intuitive explanation Chris Smith has provided, we can consider why this is the case from a different perspective: considering undirected graphs.
To see why in a DIRECTED graph the answer is n*(n-1), consider an undirected graph (which simply means that if there is a link between two nodes (A and B) then you can go in both ways: from A to B and from B to A). The maximum number of edges in an undirected graph is n(n-1)/2 and obviously in a directed graph there are twice as many.
Good, you might ask, but why are there a maximum of n(n-1)/2 edges in an undirected graph?
For that, Consider n points (nodes) and ask how many edges can one make from the first point. Obviously, n-1 edges. Now how many edges can one draw from the second point, given that you connected the first point? Since the first and the second point are already connected, there are n-2 edges that can be done. And so on. So the sum of all edges is:
Sum = (n-1)+(n-2)+(n-3)+...+3+2+1
Since there are (n-1) terms in the Sum, and the average of Sum in such a series is ((n-1)+0)/2 {(last + first)/2}, Sum = n(n-1)/2
If the graph is not a multi graph then it is clearly n * (n - 1), as each node can at most have edges to every other node. If this is a multigraph, then there is no max limit.
Putting it another way:
A complete graph is an undirected graph where each distinct pair of vertices has an unique edge connecting them. This is intuitive in the sense that, you are basically choosing 2 vertices from a collection of n vertices.
nC2 = n!/(n-2)!*2! = n(n-1)/2
This is the maximum number of edges an undirected graph can have. Now, for directed graph, each edge converts into two directed edges. So just multiply the previous result with two. That gives you the result: n(n-1)
In a directed graph having N vertices, each vertex can connect to N-1 other vertices in the graph(Assuming, no self loop). Hence, the total number of edges can be are N(N-1).
In the graph with self loop
max edges= n*n
such as we have 4 nodes(vertex)
4 nodes = 16 edges= 4*4
There can be as many as n(n-1)/2 edges in the graph if not multi-edge is allowed.
And this is achievable if we label the vertices 1,2,...,n and there's an edge from i to j iff i>j.
See here.
The correct answer is n*(n-1)/2. Each edge has been counted twice, hence the division by 2. A complete graph has the maximum number of edges, which is given by n choose 2 = n*(n-1)/2.
Can also be thought of as the number of ways of choosing pairs of nodes n choose 2 = n(n-1)/2. True if only any pair can have only one edge. Multiply by 2 otherwise
Undirected is N^2. Simple - every node has N options of edges (himself included), total of N nodes thus N*N