0

Let's say I have to initialise the bi-directional edges for the following graph between the nodes:

enter image description here

I can easily do this using the following code:

import numpy as np
node_num = 3
graph = np.ones([node_num, node_num]) - np.eye(node_num)

Now I am extending this graph in the following way:

enter image description here

What is the simple and efficient way to make it code for this graph?

1 Answer 1

2

Assuming you're looking for an adjacency matrix, you could use:

out = np.block([
    [1 - np.eye(3), np.eye(3)       ],
    [    np.eye(3), np.zeros((3, 3))]
]).astype(int)

out:

array([[0, 1, 1, 1, 0, 0],   # A
       [1, 0, 1, 0, 1, 0],   # B
       [1, 1, 0, 0, 0, 1],   # C
       [1, 0, 0, 0, 0, 0],   # BC
       [0, 1, 0, 0, 0, 0],   # AB
       [0, 0, 1, 0, 0, 0]])  # AB(red)

but I would suggest you just initialize it as the outputted adjacency matrix. I would only use a short one liner for very simple graphs like your first image, not the second.

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

6 Comments

Thank you so much for the answer. Would you please share a bit more detail about outputted adjacency matrix.
Hi, in 5 nodes cases (A, B, C, D, E, AB, AC, AD, AE, BC, BD, BE, CD, CE, DE) it cannot work.
I believe this solution is just for 3 nodes case, it will not work for n nodes.
waiting for your response.
How would the graph generalize for n nodes? A central graph fully connected graph with n / 2 nodes, each of which has one more node connected to itself but not anywhere else?
|

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.