1
$\begingroup$

I've defined the function, drawGraph, to display a graph as follows:

drawGraph[g_] := GraphPlot[g, VertexLabeling -> True, DirectedEdges -> True]

If I specify a graph via its edges, and then ask for a hamiltonian cycle, everything works fine as seen below

enter image description here

However if I specify a graph via its adjacency matrix, and then ask for a hamiltonian cycle, I'm told that the adjacency matrix is not a graph. However I can display the adjacency matrix as a graph using my drawGraph[] function as seen below.

enter image description here

Why does the drawGraph function interpret the adjacency matrix as a graph but not the FindHamiltonianCycle function? The same problem exists with all the other Path functions -- ShortestPath, EulerianPath, etc.

$\endgroup$
  • 2
    $\begingroup$ GraphPlot is an obsolete function from the time when Mathematica did not have the Graph data structure, and there was no alternative but to use explicit but limited representations such as adjacency matrices. Having a data structure specifically for representing graphs is generally advantageous for programming. Why wouldn't you just use Graph? $\endgroup$ – Szabolcs May 16 '18 at 12:09
1
$\begingroup$

After further study of the Mathematica documentation, I now see how to define a graph via an adjacency matrix, namely use the AdjacencyGraph[] function as follows:

enter image description here

When g is defined this way the Hamiltonian cycle function works

enter image description here

as do all the other graph functions.

The only problem with using the AdjacencyGraph[] function to define the graph is that although the graph is rendered properly, the vertices are not labeled. The only way I could find to label the vertices was to use the GraphPlot[] function.

enter image description here

The commenter above claimed that GraphPlot[] was obsolete, but obsolete or not, it's still apparently useful.

$\endgroup$
  • $\begingroup$ AdjacencyGraph is not obsolete; GraphPlot is. You can use ` AdjacencyGraph[A, VertexLabels->"Name"]` to obtain vertex labels. (A is of course the adjacency matrix.) $\endgroup$ – Henrik Schumacher May 21 '18 at 11:27
  • $\begingroup$ I said that GraphPlot was obsolete, which it is. I never said that AdjacencyGraph was obsolete. To show labels, use VertexLabels -> "Name". This is shown on the documentation page of Graph. If you want the same style as GraphPlot, use GraphStyle -> "VintageDiagram" $\endgroup$ – Szabolcs May 21 '18 at 11:29
  • $\begingroup$ @Szabolcs Sorry for writing AdjacencyGraph when I meant to write GraphPlot. It's been corrected it. And Thanks for the GraphStyle hint. $\endgroup$ – Richard Gostanian May 21 '18 at 13:00

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.