0

I have a network with nodes and vertices and the following numbering scheme. I want to generate an adjacency matrix A for the nodes 0,1 as shown below. I tried to do using networkx. I present the current and expected outputs.

import networkx as nx
N=2
def pos():
    x, y = 1, N + 3 - 1
    for _ in range(N):
        yield (x, y)
        y -= (x + 2) // (N+1 )
        x = (x + 2) % (N+1)

G = nx.Graph()
it_pos = pos()
for u in range(N):
    G.add_node(u+1, pos=next(it_pos))
    if u % (2 * N) < N:
        for v in (u - 2 * N, u - N, u - N):
            if G.has_node(v + 1):
                G.add_edge(u + 2, v + 2)
    elif u % (2 * N) == N:
        G.add_edge(u + 1, u - N + 1)
    elif u % (2 * N + 1) < 2 * N:
        for v in (u - 1, u - N, u - N):
            G.add_edge(u + 1, v + 1)
    else:
        for v in (u - 1, u - N - 1):
            G.add_edge(u + 1, v + 1)

nx.draw(G, nx.get_node_attributes(G, 'pos'), with_labels=True, font_weight='bold')  
Nodes=len(G.nodes)
A=nx.adjacency_matrix(G).todense()

The current output is

enter image description here

A=matrix([[0., 0.],
        [0., 0.]])

The expected output is

enter image description here

2 Answers 2

1

You want the adjacency matrix between node and its edges, but the function you are using looks for neighbouring nodes.

In order to build your network and get your matrix, you could do the following:

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)
A

Edit: generalize the connection search.

Sign up to request clarification or add additional context in comments.

5 Comments

Is it possible to generalise this cause I want to generate the adjacency matrix for large number of nodes, not only 2 nodes?
Sure, just look at the part after the comment # find connected edges to nodes 0 and 1. There you can place a large list of nodes that you want to check. then you initialize your matrix with shape (len(my_nodes), max_edge_name) and fill the matrix with a loop.
It's not clear to me how to implement this since you are manually entering points and edges above.
Regarding the creation of the network, I assume you have a given network. Otherwise, I would kindly ask you to open a new question for it, because to me this is out of scope looking at the question title.
I have opened a new question: stackoverflow.com/questions/75308331/…
0

You can implement the matrix in Python using a nested list:

A = [[1, 0, 1, 1, 0, 1, 0], [0, 1, 0, 1, 1, 0, 1]]

1 Comment

How does this help? I want to be able to tell the code the number of nodes, vertices and it should generate A as shown above.

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.