如何形成三重连接的组件以形成无向图?

时间:2019-05-20 16:37:58

标签: python-3.x pandas networkx

我想从给定的数据集(.tsv文件)创建大小为3的无向图。 我已经在R中完成了此操作,但我无法在python中完成此操作,并且我的指南坚持要在python中执行此操作,因为它是一种更加流行的语言。 一点帮助将不胜感激。

试图使用此代码,我在stackoverflow中找到了此代码,但根本没有用。 代码:

    import networkx as nx
    import pandas as pd
    from itertools import chain

    adj_matrix = [
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
    [0, 0, 1, 0, 0, 1, 0, 0, 0, 0],
    [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    ]

    df = pd.DataFrame(adj_matrix)

    G = nx.from_pandas_adjacency(df)

    # filter components of size 3
    triplets = [c for c in nx.connected_components(G) if len(c) == 3]

到目前为止,我的代码

    a_col=length(adj[1,])
    a_row=length(adj[,1])

    trip=NULL  #NULL matrix to store triplet values

    for(i in 1:a_row)
    {
      for(j in 1:a_col)
      {
        if(adj[i,j]==1)
        {
          for(k in (j+1):a_col)
          {
            if(adj[i,k]==1)
           {
            cat("\nTriplets: ",i,j,k)
            trip<-rbind(trip,c(i,j,k))
           }
        }  
        for(l in (i+1):a_row)
        {
          if(adj[l,j]==1)
          {
           cat("\nTriplets: ",i,j,l)
           trip<-rbind(trip,c(i,j,l))
          }
         }
       }
     }
    }

我使用的数据集:

0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0
0   0   0   0   1   0   0   1   0   0
0   0   0   0   0   0   0   0   1   1
0   0   1   0   0   1   0   0   1   0
0   0   0   0   1   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0
0   0   1   0   0   0   0   0   0   0
0   0   0   1   1   0   0   0   0   0
0   0   0   1   0   0   0   0   0   0

预期的三胞胎是:

3   5   8
3   5   6
3   5   9
4   9   10
4   9   5 

1 个答案:

答案 0 :(得分:0)

import networkx as nx
import pandas as pd

# 1. Transform your data to python list of lists:
data = [
    [0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,1,0,0,1,0,0],
    [0,0,0,0,0,0,0,0,1,1],
    [0,0,1,0,0,1,0,0,1,0],
    [0,0,0,0,1,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0],
    [0,0,1,0,0,0,0,0,0,0],
    [0,0,0,1,1,0,0,0,0,0],
    [0,0,0,1,0,0,0,0,0,0]
]

# 2. Create Pandas dataframe from the data
df = pd.DataFrame(data)

# 3. Create graph from the adjacency matrix
G = nx.from_pandas_adjacency(df)

# 4. Print all connected components
for c in nx.connected_components(G):
    print(c)

# END

将打印:

{0}
{1}
{2, 3, 4, 5, 7, 8, 9}
{6}

让我们绘制这张图:

nx.draw(G)

enter image description here

在这里,您可以使用networkx处理图形。您的图没有连接组件,其长度= 3,没有分类,因此我不知道您期望得到什么。但是,如果要查找各种类型的三角形,可以运行此函数:

nx.triangles(G)