1

I am creating a network by specifying points and edges. However, I want to make this code more generic so that it can handle large number of my_nodes (say 100) and generate a connection matrix. This means that I don't want to enter the points and edges manually cause it will be cumbersome. For every node, there should be 4 edges and the numbering is shown below.

How do I do this? I present the output as well.

import networkx as nx
import numpy as np
import pandas as pd

# build the network with relevant edges
G = nx.Graph()
points = {
    0: (1, 1), 1: (2, 1),
    'a':(1, 2), 'b':(2, 2),
    'c':(0, 1), 'd':(3, 1),
    'e':(1, 0), 'f':(2, 0)
}
for key, pos in points.items():
    G.add_node(key, pos=pos)
G.add_edge('a', 0, name=0)
G.add_edge('b', 1, name=1)
G.add_edge('c', 0, name=2)
G.add_edge(0, 1, name=3)
G.add_edge(1, 'd', name=4)
G.add_edge(0, 'e', name=5)
G.add_edge(1, 'f', name=6)

# find connected edges to nodes 0 and 1
my_nodes = [0, 1]  # could be more here
edges = {
    node: [G.get_edge_data(*edge)['name'] for edge in G.edges(node)]
    for node in my_nodes
}
# build matirx
mat = np.zeros((len(my_nodes), 7), dtype=np.uint8)
for i, node in enumerate(my_nodes):
    mat[i, edges[node]] = 1
    mat[i, edges[node]] = 1
A = pd.DataFrame(mat)
print(A)

Numbering is

enter image description here

The output is

   0  1  2  3  4  5  6
0  1  0  1  1  0  1  0
1  0  1  0  1  1  0  1
6
  • How do you specify the points and edges? Should they be randomly created? Commented Feb 1, 2023 at 9:57
  • Adjacency matrix is square by definition. Your output is not an adjacency matrix! Commented Feb 1, 2023 at 10:00
  • Yes they should be random. For every node, there should be 4 edges and the numbering is according to the previous post: stackoverflow.com/questions/75306778/… Commented Feb 1, 2023 at 10:01
  • Please edit the question to provide this information Commented Feb 1, 2023 at 10:02
  • You may better remove the adjacency matrix part from your question and just focus on the creation of you network. Could you further specify the point locations? Do they need to sit on a lattice? Commented Feb 1, 2023 at 10:10

1 Answer 1

1

Use nx.adjacency_matrix:

# Keep only nodes with degree 4
my_nodes = [node for node, deg in G.degree if deg == 4]

A = pd.DataFrame(nx.adjacency_matrix(G).toarray(), index=G.nodes, columns=G.nodes)
B = A.loc[my_nodes]

Output:

>>> A
   0  1  a  b  c  d  e  f
0  0  1  1  0  1  0  1  0
1  1  0  0  1  0  1  0  1
a  1  0  0  0  0  0  0  0
b  0  1  0  0  0  0  0  0
c  1  0  0  0  0  0  0  0
d  0  1  0  0  0  0  0  0
e  1  0  0  0  0  0  0  0
f  0  1  0  0  0  0  0  0

>>> B
   0  1  a  b  c  d  e  f
0  0  1  1  0  1  0  1  0
1  1  0  0  1  0  1  0  1
Sign up to request clarification or add additional context in comments.

5 Comments

The output B doesn't match the output I have shown.
You don't want the node names? df.columns = range(len(df.columns))
I want the node numbers and apart from this, the other values are not the same as mine. Please see above.
Do you guarantee all edges have a name attribute?
Yes the edges should have.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.