首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取有向图的所有组合

获取有向图的所有组合
EN

Stack Overflow用户
提问于 2019-11-16 17:45:47
回答 1查看 285关注 0票数 1

我正在寻找邻接矩阵中所有可能的有向图的组合

代码语言:javascript
复制
adjacency_matrix <- read.table(text = "A B C D
A   0   0   1   0
B   0   0   1   0
C   0   0   0   1
D   0   0   0   0", header = TRUE)

这是创建的图表:

使用n=4可以进行多少种组合?2^4=16

代码语言:javascript
复制
    A    B    C    D
1   1    1    1    1
2   0    1    1    1
3   1    0    1    1
4   0    0    1    1
5   1    1    0    1
6   0    1    0    1
7   1    0    0    1
8   0    0    0    1
9   1    1    1    0
10  0    1    1    0
11  1    0    1    0
12  0    0    1    0
13  1    1    0    0
14  0    1    0    0
15  1    0    0    0
16  0    0    0    0

所有的组合都是可行的解决方案吗?不,作为有向图,所有的前置都必须在解决方案中。

代码语言:javascript
复制
    A    B    C    D
1   1    1    1    1 #GOOD
2   0    1    1    1 #BAD, for choose C, all of his predecessors must be in the solution: A and B
3   1    0    1    1 #BAD, for choose C , must be A and B
4   0    0    1    1 #BAD, for choose C, must be A and B, for choose D must be in the solution: A,B and C
5   1    1    0    1 #BAD, for choose D, must be C and all his predecessors 
6   0    1    0    1 #BAD, for choose D, must be C and all his predecessors 
7   1    0    0    1 #BAD, for choose D, must be C and all his predecessors 
8   0    0    0    1 #BAD, for choose D, must be all his predecessors 
9   1    1    1    0 #GOOD
10  0    1    1    0 #BAD, for choose C, must be all his predecessors 
11  1    0    1    0 #BAD, for choose C, must be all his predecessors 
12  0    0    1    0 #BAD, for choose C, must be all his predecessors 
13  1    1    0    0 #GOOD
14  0    1    0    0 #GOOD
15  1    0    0    0 #GOOD
16  0    0    0    0 #GOOD

因此,从16组合来看,我一直使用6

代码语言:javascript
复制
    A    B    C    D
1   1    1    1    1 #GOOD
9   1    1    1    0 #GOOD
13  1    1    0    0 #GOOD
14  0    1    0    0 #GOOD
15  1    0    0    0 #GOOD
16  0    0    0    0 #GOOD
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-16 18:29:45

我不能重复你的例子,所以让我们假设给出的第二个矩阵叫做solution。也许这能行得通。

它很慢,所以如果它对你有效,你肯定可以让它更快。

代码语言:javascript
复制
## create your solution
nodes_rank <- c(1,1,2,3)
## nodes rank is same order as your position matrix so A,B,C,D

solution <- matrix(
  c(rep(c(1,0),8),rep(c(1,1,0,0),4), rep(c(rep(1,4),rep(0,4)),2), rep(1,8),rep(0,8) ),
  ,nrow = 16,ncol=4,byrow = F)

check <- function(x){
  for(i in 1:length(x)){
    if(x[i]==1){ ## we want to have certain node
      if(nodes_rank[i]==1){
        #pass -- no worries about node rank 1
      }else{
        # all of previous must be also in
        current_node_rank <- nodes_rank[i]
        ## check if you got all the previous
        have_to_be_in <- which(nodes_rank < current_node_rank)
        are_in <- which(x[1:i]==1) ## we take i so we avoid looping errors
        if(!all(have_to_be_in %in% are_in)){
          return(F) ## if we catch something that we shouldnt break
        }
      }
    }
  }
  return(T) ## all works out its ok
}

index <- apply(solution,1,check)

solution[index,]
     [,1] [,2] [,3] [,4]
[1,]    1    1    1    1
[2,]    1    1    1    0
[3,]    1    1    0    0
[4,]    0    1    0    0
[5,]    1    0    0    0
[6,]    0    0    0    0
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58889438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档