Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert known connected component to adjacency matrix with tensor manipulations only?

Let's say I have a N elements and I know how there are connected. That means there's a vector [c1, c2, ..., cn] where ci in [0...CC] CC- components count.

I want to get adjacency matrix from it.

Simple example input

[0, 0, 1, 2, 0, 1]

output

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

Is it possible to get it with linear algebra only plus some extra tensor operations from TF?

like image 436
sh1ng Avatar asked Oct 22 '25 12:10

sh1ng


1 Answers

That can be done simply with a broadcasted comparison with itself:

import tensorflow as tf

input = [0, 0, 1, 2, 0, 1]
adjacency_matrix = tf.cast(tf.equal(tf.expand_dims(input, 1), input), tf.int32)
print(sess.run(adjacency_matrix))

Output:

[[1 1 0 0 1 0]
 [1 1 0 0 1 0]
 [0 0 1 0 0 1]
 [0 0 0 1 0 0]
 [1 1 0 0 1 0]
 [0 0 1 0 0 1]]
like image 124
jdehesa Avatar answered Oct 25 '25 04:10

jdehesa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!