Manolito Pérez
Manolito Pérez

Reputation: 103

Subsets of edges

First of all, I have to say that I am new to R, and my first impression is very good, so I am determined to become proficient in it. Well, I have a simple undirected graph in the package 'igraph', and I want to generate all subsets of its edges. I have seen two functions that do that, one being 'combn', from the package 'combinat', and the other one being 'set_combn', from the package 'sets' (if I remember correctly). However, none of them does what I want, and I don't quite understand what is happenning. More precisely, my graph has the following edge list: (1,2), (1,3), (2,3).

Now, I want to generate all the subsets with two edges, and I call the function 'combn' as follows: combn(mylist, 2). The result is an array that contains pairs of vertices (it should be pairs of edges, I think), and moreover, there are repeated pairs.

On the other hand, if I call set_combn(mylist,2), it gives the following result: {{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}}, which is also incorrect, right?

Any help, please?

Upvotes: 0

Views: 157

Answers (1)

G5W
G5W

Reputation: 37661

I think that I can show what you are looking for. If this is not what you want, please let me know and I will delete the answer.

First, your test graph. You seem to have three nodes with all possible connections. There is more than one way to get that, but a simple way is this:

library(igraph)

g = make_full_graph(3)
E(g)
+ 3/3 edges from a1c6ff4:
[1] 1--2 1--3 2--3

Now that we have a graph with the edgelist you want, let's get the combinations of edges. You may have already answered your question, but did not know it. You can get the subsets with two edges using combn.

(CB2 = combn(E(g), 2))
     [,1] [,2] [,3]
[1,]    1    1    2
[2,]    2    3    3

This is not showing anything about the vertices. Instead, The columns are showing you all possible pairs of edge ids. You can use the columns of edge ids, to get the edges more explicitly. For example, the first column is telling you that one pair of edges is edges 1 and 2. You can use the column to refer to those edges.

E(g)[CB2[,1]]
+ 2/3 edges from a1c6ff4:
[1] 1--2 1--3 

Looking at the columns this way, you can see that for the example, we have gotten all possible pairs.

Upvotes: 1

Related Questions