简体   繁体   English

如何绘制在网络中链接的代理的唯一对?

[英]How do I draw unique pairs of agents who are linked in a network?

I have a number of agents who are linked through a network and now I am trying to draw random pairs of linked agents. 我有许多通过网络链接的座席,现在我试图随机绘制一对链接的座席。 The final outcome should be a binary matrix (ncol = nrow = number of agents). 最终结果应为二进制矩阵(ncol = nrow =代理数量)。 Each agent may show up only once in a linked pair. 每个代理只能在链接对中出现一次。

This is what I have done so far. 到目前为止,这是我所做的。 First I draw random pairs 首先我画随机对

library(sna,dummies)
pairs  = matrix(sample(1:10,10,replace=F), ncol=2)

Then I convert it to a matrix 然后我将其转换为矩阵

meet = cbind(merge(c(1:10), pairs,  by.x = 1, by.y = 1, all.x = TRUE),
merge(c(1:10), pairs,  by.x = 1, by.y = 2, all.x = TRUE))
meet[,2] = ifelse(is.na(meet[,2]), meet[,4], meet[,2])
meet = meet[,-c(3,4)]
meet = data.matrix(dummy.data.frame(meet, names = "V2"))
meet = meet[,-1]
colnames(meet) = c(1:10)

Let's say agents are linked through the following network 假设代理商通过以下网络链接

nw = rgraph(10, mode = "graph", tprob = 0.5)

If I multiply meet and nw I get a matrix of randomly drawn pairs by eliminating pairs of agents who are not linked. 如果我将Meet和nw相乘,则通过消除未链接的代理对,得到随机绘制的对矩阵。 However, in this case I end up with a very small number of pairs... 但是,在这种情况下,我最终只有很少的一对...

Is there a way of drawing pairs conditioned on a link between agents? 有没有一种方法以代理之间的链接为条件绘制对?

What is the purpose of this? 这样做的目的是什么? is it important to find the largest possible number of pairs? 找到尽可能多的对是否重要? Basically, what you describe is related to the concept of matching in graphs ( http://en.wikipedia.org/wiki/Matching_%28graph_theory%29 ). 基本上,您所描述的内容与图形中的匹配概念有关( http://en.wikipedia.org/wiki/Matching_%28graph_theory%29 )。 There is a distinction between "maximal matching" and "maximum matching". 在“最大匹配”和“最大匹配”之间有区别。

The first is easy to find (and is probably what you try to do. The "trick" for doing it is choosing edges rather than nodes (agents) in your graph. start with a random edge (which adds your first pair of agents), then delete all the edges that are connected to those agents, and choose (randomly) an edge from the remaining edges. repeat the process until no edges are left. This is "maximal" in that you can't add to it any more pairs. However, you may still end up with a limited number of pairs. 第一个很容易找到(可能是您尝试做的。这样做的“技巧”是选择图形中的边而不是节点(代理)。从随机边开始(添加您的第一对代理) ,然后删除所有与这些代理连接的边,然后从其余边中随机选择一条边,重复此过程直到没有边为止。这是“最大”的操作,因为您无法再添加它了但是,您可能仍然只能得到有限数量的对。

To get the maximum number of pairs possible for the given graph, you need to work harder (see the wikipedia link for some resources on that), but is it what you really want? 为了获得给定图的最大对数,您需要更加努力(有关更多资源,请参阅Wikipedia链接),但这是您真正想要的吗? (note: the maximum matching is never much bigger compared to any of the easy-to-find maximal matching matching. (请注意:与任何易于找到的最大匹配匹配相比,最大匹配永远不会更大。

You might want to look at the igraph package which has a couple of functions that deal with matchings (especially of bipartite graphs). 您可能想看一下igraph包,它具有几个处理匹配的功能(尤其是二部图)。 But it might be, that for your goals, a much simpler algorithm is required (the research literature is full of algorithms for matching in special cases). 但是可能是,为了您的目标,需要一种更简单的算法(研究文献中充满了特殊情况下匹配的算法)。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM