From: Andrew on
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

From: Andrew on
I can't see the post in my browser, but Tom Dowling helpfully emailed
to say that on his Mac (non-Intel) running Mathematica 7.0.0 the code
above ran fine!

I'm using 7.0.1 on a windows (32 bit) platform, OS XP Pro SP3. Just to
check I'm not deluding myself, I ran an abbreviated version of the
steps above again. Here's the inputs & outputs shown:

In[1]:= << "Combinatorica`"

In[2]:= Edges[Cycle[3, Type -> Directed]]
Out[2]= {{1, 2}, {2, 3}, {3, 1}}

In[3]:= Edges[FromAdjacencyMatrix[
ToAdjacencyMatrix[Cycle[3, Type -> Directed], Type -> Directed],
Type -> Directed]]
Out[3]= {{1, 2}, {2, 3}, {1, 3}}

Is it just my system or can anyone else reproduce this?

Best regards
Andrew

From: Andrew on
Looks like it's definitely a bug.

Syd Geraghty helpfully emailed to say that he got the same results on
his (Intel) Mac system running 7.0.1

Is it a 7.0.1 problem or could it be Intel chip-related?

Any other contributions welcome, including how to work around this in
the short term...

Best

Andrew

....

Andrew,
I get exactly the same output as you on my system detailed below.
Bug!
Cheers .... Syd
Syd Geraghty B.Sc, M.Sc.
Mathematica 7.0.1 for Mac OS X x86 (64 - bit) (18th February 2009)
MacOS X V 10.6 Snow LeopardMacBook Pro 2.33 GHz Intel Core 2 Duo 2GB
RAM

....

On Oct 24, 6:48 am, Andrew <dr.a.gra...(a)googlemail.com> wrote:
> I can't see the post in my browser, but Tom Dowling helpfully emailed
> to say that on his Mac (non-Intel) running Mathematica 7.0.0 the code
> above ran fine!
>
> I'm using 7.0.1 on a windows (32 bit) platform, OS XP Pro SP3. Just to
> check I'm not deluding myself, I ran an abbreviated version of the
> steps above again. Here's the inputs & outputs shown:
>
> In[1]:= << "Combinatorica`"
>
> In[2]:= Edges[Cycle[3, Type -> Directed]]
> Out[2]= {{1, 2}, {2, 3}, {3, 1}}
>
> In[3]:= Edges[FromAdjacencyMatrix[
> ToAdjacencyMatrix[Cycle[3, Type -> Directed], Type -> Directed],
> Type -> Directed]]
> Out[3]= {{1, 2}, {2, 3}, {1, 3}}
>
> Is it just my system or can anyone else reproduce this?
>
> Best regards
> Andrew