Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

FromAdjacencyMatrix / FromAdjacencyLists - broken? Workaround?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104192] FromAdjacencyMatrix / FromAdjacencyLists - broken? Workaround?
  • From: Andrew <dr.a.graham at googlemail.com>
  • Date: Thu, 22 Oct 2009 22:33:38 -0400 (EDT)

Hello group,

This question seems to have been touched on in the past but I'm not
sure if it has ever been definitively answered. I would be grateful
for some advice!

I have some data that I would like to explore. It concerns a set of
directed graphs, without edge weights. The data comes to me as the
corresponding asymmetric adjacency matrices.

I can visualise the graphs directly using the native function
GraphPlot.
However, I would prefer to visualise them using ShowGraph from the
Combinatorica package - but this requires that the adjacency matrix is
converted to the Combinatorica graph format first.

The trouble is that (for directed graphs at least),
FromAdjacencyMatrix appears to be broken.

Example:
g = Cycle[3, Type -> Directed] (*construct a simple directed graph in
Combinatorica graph format*)

Edges[g] (*List of edges*)

Vertices[g] (*List of vertices*)

GraphOptions[g] (*Shows the graph options*)

ShowGraph[g, VertexNumber -> True] /.
 x : Arrow[__] -> {Arrowheads[{{Automatic, 0.6}}], x} (*Visualises the
graph with ShowGraph, using a workaround to place the arrowheads mid-
edge, as suggested Apr 2 2008 by dh*)
GraphPlot[g, VertexLabeling -> True, DirectedEdges -> True]
(*Visualises the graph with GraphPlot - the same outcome*)

mg = ToAdjacencyMatrix[g, Type -> Directed] (*Convert to adjacency
matrix*)

mg // TableForm (*Inspect - seems appropriate*)

mg // MatrixForm (*Ditto*)

GraphPlot[mg, VertexLabeling -> True, DirectedEdges -> True] (*Cannot
plot directly with ShowGraph, but with GraphPlot the graph is still
preserved at this stage*)

g2 = FromAdjacencyMatrix[mg, Type -> Directed] (*Convert back to a
graph*)

Edges[g2] (*Oh dear. The direction of edge #3 has been reversed*)

Vertices[g2] (*Vertices are preserved*)

GraphOptions[g2] (*Graph options are preserved*)

ShowGraph[g2, VertexNumber -> True] /.
 x : Arrow[__] -> {Arrowheads[{{Automatic, 0.6}}], x}
GraphPlot[g2, VertexLabeling -> True, DirectedEdges -> True] (*And
visualising the graph with both ShowGraph & GraphPlot it is no longer
the same as the graph we started out with*)

The problem seems to be with FromAdjacencyMatrix
FromAdjacencyLists shows the same problem

Is there something wrong with the code above, or are these functions
truly broken? If they are broken, can anyone help with writing a
function that will correctly turn a directed adjacency matrix into
Combinatorica lists of edges & vertices?

Finally, if these functions are broken, can they be fixed? I have
otherwise found Combinatorica very easy to work with & would prefer to
stick with it rather than native functions if possible. The
Combinatorica book has recently been republished in paperback & I have
found it very helpful.

Many thanks

Andrew Graham
Dept of Neurology
Addenbrooke's Hospital


  • Prev by Date: Re: JLink broken on Snow Leopard
  • Next by Date: Re: Howto convert a 2D graphic to a 3D graphic
  • Previous by thread: Re: OpenerView, multiple sub cells and Notebook1Write...
  • Next by thread: Re: FromAdjacencyMatrix / FromAdjacencyLists - broken?