4
$\begingroup$

I have a graph defined as follow:

r = Graph[{1 <-> 2, 1 <-> 4, 2 <-> 3, 3 <-> 4, 3 <-> 5, 5 <-> 6, 5 <-> 7}, VertexLabels -> "Name"]

then I want to get the adjacency matrix of this graph by using the AdjacencyMatrix function. I doubt the output is incorrect, at least, it is not as I expected

AdjacencyMatrix[r]

Adjacency Matrix

Take the first row as example, if it corresponds to vertex 1, this row should be

{0, 1, 0, 1, 0, 0, 0}

Does anyone could help me to explain this output?

$\endgroup$
  • 1
    $\begingroup$ it is correct. The node numbering in the matrix are actually listed as VertexList[r] , which is {1, 2, 4, 3, 5, 6, 7} $\endgroup$ – Nasser Jan 15 '15 at 9:00
2
$\begingroup$

The node numbering in the matrix are actually listed as VertexList[r] , which is {1, 2, 4, 3, 5, 6, 7} I am not sure why Mathematica does it this way, it does not seem natural to me.

r = Graph[{1 <-> 2, 1 <-> 4, 2 <-> 3, 3 <-> 4, 3 <-> 5, 5 <-> 6, 5 <-> 7}, 
          VertexLabels -> "Name"];

Mathematica graphics

To reorder the matrix:

m = AdjacencyMatrix[r];
z = VertexList[r];
m0 = m[[z, z]] // MatrixForm

Mathematica graphics

$\endgroup$
  • $\begingroup$ Probably it does this because you can put as vertices anything you want, also things like images etc. Reordering in such cases would be problematic. $\endgroup$ – Kuba Jan 15 '15 at 9:16
  • $\begingroup$ It's a so nice explanation. Thank you very much! :-) $\endgroup$ – fetag Jan 15 '15 at 9:20
  • $\begingroup$ @fetag: If it fully answers your question, feel free to mark it as such by clicking the accepted answer checkmark below the answer score tag. $\endgroup$ – DumpsterDoofus Jan 15 '15 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.