If we have as inputs the edges of a graph for example as shown below as matrix
(1,2) (2,3) (3,1)
and from these inputs you want to create your adjacency matrix.
My idea was to iterate throught the matrix and push_back in a vector which contains the unique nodes (1,2,3) and then creating a zero matrix with dimensions equals to the nodes_vector, iterating through the matrix again and see which nodes are connected to put 1 in our matrix.
Is there a faster solution than this?
preguntado el 08 de noviembre de 11 a las 17:11
Yes. If elements are numbered 1-N, then on the first run through, all you have to do is find the biggest number in the edge-list, and the width of your adjacency matrix will be that minus one. Finding all unique nodes is slower than you'd immediately think.
Of course, if you actually need to know which nodes actually exist, then the method you list is optimal.