1
$\begingroup$

I have a directed graph with this Edgelist defined as follow:

g = Graph[{1 -> 2, 1 -> 3, 1 -> 4, 1 -> 6, 2 -> 6, 3 -> 8, 3 -> 11, 4 -> 8, 5 -> 2, 5 -> 6, 5 -> 8, 6 -> 8, 6 -> 10, 6 -> 11, 7 -> 2, 7 -> 6, 8 -> 9, 8 -> 11, 9 -> 6, 10 -> 2, 10 -> 9, 11 -> 3,  11 -> 4, 11 -> 9, 11 -> 10}, VertexLabels -> "Name"]

then I want to get the adjacency matrix of this graph by using following:

AdjacencyMatrix[g]//MatrixForm

The first row of that result is:

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

but it should be:

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

Can anyone help me to show this adjacency matrix in true form?

$\endgroup$
  • $\begingroup$ Vertex names can be anything, not just numbers. When they are consecutive integers, you might assume that they are ordered as 1,2,3,..., but this is not generally true. Make no assumptions about the vertex ordering, instead check it (see VertexList) or use VertexIndex. $\endgroup$ – Szabolcs Aug 16 '15 at 18:38
8
$\begingroup$

The problem is that the vertices get numbered by their order of appearance in the graph. This means that what you called vertex "6" is actually considered vertex "5". To see what I mean try the following command:

VertexList[g]

To set the vertices in the desired order, they should be explicitly listed before the edges:

g = Graph[Table[i,{i,1,11}],{1 -> 2, 1 -> 3, 1 -> 4, 1 -> 6, 2 -> 6, 3 -> 8, 3 -> 11, 4 -> 8, 5 -> 2, 5 -> 6, 5 -> 8, 6 -> 8, 6 -> 10, 6 -> 11, 7 -> 2, 7 -> 6, 8 -> 9, 8 -> 11, 9 -> 6, 10 -> 2, 10 -> 9, 11 -> 3,  11 -> 4, 11 -> 9, 11 -> 10}, VertexLabels -> "Name"]
| improve this answer | |
$\endgroup$

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