Get and count node neighbors Python
Question: I am trying to read a file with node pairs and weight. I have find the
neighbours of each pair individual and combined both also count them. Later
find the ratio of the neighbours that each node has. I am stuck with finding
nodes.

infile.txt

    
    
    0_node1 0_node2 0w
    1_node1 1_node2 1w
    2_node1 2_node2 2w
    3_node1 3_node2 3w
    4_node1 4_node2 4w
    

Code:

    
    
    import networkx as nx
    import matplotlib.pyplot as plt
    
    G=nx.Graph()
    G = nx.read_edgelist('infile.txt', data=[("weight", float)])
    
    def get_triangle(G):
      for n1 in G.nodes:
        neighbors1 = set(G[n1])
        for n2 in filter(lambda x: x>n1, nodes):
          neighbors2 = set(G[n2])
          common = neighbors1 & neighbors2
          for n3 in filter(lambda x: x>n2, common):
             print n1
             print n2
             print n3
    

I did check the indentations in program does not seem to be problem. I not
getting the neighbours list.


Answer: If you are going to do anything with the values I suggest yield'ing a 3 node
tuple.

I had to change the data so the import would work (i stripped out the w's) and
there is support for finding triangle nodes in networkx which helps limit how
many objects we are iterating over each time.

infile.txt

    
    
    0_node1 0_node2 0
    1_node1 1_node2 1
    2_node1 2_node2 2
    3_node1 3_node2 3
    4_node1 4_node2 4
    0_node1 1_node2 5
    0_node2 1_node2 6
    0_node2 1_node1 7
    

![enter image description here](http://i.stack.imgur.com/IY2q9.png)

code:

    
    
    import networkx as nx
    import itertools as it
    
    def get_triangles(G):
    
        def found_rotation(x):
            for rotation in it.permutations(x):
                if rotation in triangles_duplicates:
                    return 1
                    return 0
    
        triangles = []
        triangles_duplicates=[]
        triangle_nodes = dict((x,nx.triangles(G,x))  for x in G.nodes() if nx.triangles(G,x) > 0)
    
        for vertex1  in triangle_nodes:
            vertex1_neighbors_that_are_triangles = set(G.neighbors(vertex1)).intersection( \
                                                                                           set(triangle_nodes) )
            for vertex2 in vertex1_neighbors_that_are_triangles:
                if triangle_nodes[vertex2] > 0:
                    vertex2_neighbors_that_are_triangles_and_not_vertex1 = \
                                                                           set(G.neighbors(vertex2)).intersection(\
                                                                                                                  G.neighbors(vertex1))-{vertex1}-{vertex2}
                    for vertex3 in vertex2_neighbors_that_are_triangles_and_not_vertex1:
                        if triangle_nodes[vertex3]>0:
                            for z in G.neighbors(vertex3):
                                if (vertex1 == z) and  not found_rotation((vertex1,vertex2,vertex3)):
                                    triangle_nodes[vertex1] -=1
                                    triangle_nodes[vertex2] -=1
                                    triangle_nodes[vertex3] -=1
                                    triangles.append((vertex1,vertex2,vertex3))
                                    triangles_duplicates.append((vertex1,vertex2,vertex3))
        return triangles
    
    if __name__ == "__main__":
        #G = nx.karate_club_graph()
        G = nx.read_edgelist('this.txt', data=[("weight",int)])
    

With data like this the answer would be 2 triangles (or you could switch it
with karate_club_graph and get 45)

I'd be interested in seeing improvements if you do something with this.

