4
 g1=Graph[{UndirectedEdge[a,b]}];
 GraphQ[g1]
 (*
   OUT: True
 *)
   (*Needs["Combinatorica`"]*)
 PlanarQ[g1]
 (*
    OUT: PlanarQ[.-.]
 *)
 Combinatorica`PlanarQ[g1]
 (*
   OUT: Combinatorica`PlanarQ[.-.]
 *)

Why doesn't PlanarQ give back "True" or "False" ?

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125

4 Answers4

4

Your graph is not a Combinatorica graph, rather it is a System graph. You'll need to specify the contexts explicitly.

Needs["GraphUtilities`"];
g1 = System`Graph[{UndirectedEdge[a, b]}];
Combinatorica`PlanarQ[
  GraphUtilities`ToCombinatoricaGraph[g1]]

This returns True but, in general, this process is a pain and rather buggy. I believe that Combinatorica is on the way out and I'd recommend trying to get by without.

Mark McClure
  • 4,862
  • 21
  • 34
2

You probably need:

Needs["GraphUtilities`"]
Needs["Combinatorica`"]

cg1 = ToCombinatoricaGraph[g1];

PlanarQ[cg1]

I don't have v8 to check, however.

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
2

Just a note, Combinatorica is on the way out. However, three months ago a lead on the Graph project told me there were no plans to re-implement algorithm specific interfaces like BrelazColoring for System Graph. So, some things may need Combinatorica for a while, and one way to deal with this is to try to use System'Graph for everything, keep Combinatorica out of package path, and get to Combinatorica functions by explicitly converting your Graph objects to Combinatorica'Graph objects, calling Combinatorica`function and then converting back to system graph. Here is some discussion with the details

For posterity, here's the relabelGraph function I use to solve the ordering issue reported by TomD,

relabelGraph[g_Graph,labeling_]:=Module[{vcoords,gstyle,labelMap,adjMat,newOrder,newCoords,verts},
    verts=VertexList@g;
    vcoords=VertexCoordinates/.AbsoluteOptions[g,VertexCoordinates];
    gstyle=GraphStyle/.AbsoluteOptions[g,GraphStyle];
    labelMap=Thread[Range[Length[verts]]->labeling];

    adjMat=Normal@AdjacencyMatrix[g];
    newOrder=Ordering[VertexList[g]/.labelMap];

    newCoords=Thread[(VertexList[g]/.labelMap)->vcoords];

    AdjacencyGraph[adjMat[[newOrder,newOrder]],VertexCoordinates->newCoords,GraphStyle->gstyle]
];

One way of using it is below. This produces result similar to IndexGraph, but with sorted VertexList

g=relabelGraph[g,Range@Length@VertexList@g];

Some other of my "annoyance fixing" functions are described in graphUsage.nb in the package here

Yaroslav Bulatov
  • 57,332
  • 22
  • 139
  • 197
1

This is also just a note.

I want to specifically draw attention to a possible bug in ToCombinatoricaGraph and a possible workaround. It may have no relevance to the original question.

Also, I am using Mma 7, so things may be fixed in v8.

If I define a graph as follows

Needs["Combinatorica`"]

Needs["GraphUtilities`"]

gr1 = {2 -> 4, 4 -> 3, 3 -> 1}

GraphPlot of gr1

enter image description here

Compare the following:

EdgeList@gr1
EdgeList@ToCombinatoricaGraph@gr1
Edges@ToCombinatoricaGraph@gr1

Output

{{2, 4}, {4, 3}, {3, 1}}
{{1, 2}, {2, 3}, {3, 4}}
{{1, 2}, {2, 3}, {3, 4}}

The workaround I use is to ignore, as far as is possible, ToCombinatoricaGraph and instead convert to a Combinatorica graph using FromOrderedPairs.

For example

Edges@FromOrderedPairs@EdgeList@gr1
EdgeList@FromOrderedPairs@EdgeList@gr1

Output

{{2, 4}, {4, 3}, {3, 1}}
{{2, 4}, {3, 1}, {4, 3}}

Another example, Degrees

Compare

Degrees@MakeSimple@ToCombinatoricaGraph[gr1]
VertexList@MakeSimple@ToCombinatoricaGraph[gr1]

Output

{1, 2, 2, 1}
{1, 2, 3, 4}

with

Degrees@MakeSimple@FromOrderedPairs@EdgeList@gr1
VertexList@MakeSimple@FromOrderedPairs@EdgeList@gr1

{1, 1, 2, 2}
{1, 2, 3, 4}

I'll also include an example with Prufer codes, as here I was badly 'caught out' (I did not know about SO then)

LabeledTreeToCode@MakeSimple@ToCombinatoricaGraph@gr1
LabeledTreeToCode@MakeSimple@FromOrderedPairs@EdgeList@gr1

Output

{2, 3}
{3, 4}

(Only the second answer is correct)

I have reported this to Wolfram. Seemingly, it is connected to reordering of vertices on graph creation by ToCombinatoricaGraph. Here is part of the reply (2009)

The reason that Edges and EdgeList don't function on ToCombinatoricaGraph objects because the Combinatorica package was developed prior to these functions, and the structure hasn't yet been adapted to operate with these functions.

Our development team is currently working to update the Combinatorica
package so that these functions will be compatible.  If you happen to

run across any other issues, or have any questions come up, please do let me know.

In my view, ToCombinatoricaGraph needs to be used with care (and avoided whenever possible). However, there are probably many cases (including the uses given in other answers to the original question) where it makes no difference.

Personally, I would not like to see the Combinatorica package go. It contains many useful functions (if very badly documented).

681234
  • 4,214
  • 2
  • 35
  • 42
  • 1
    I've ran into several bugs that were related to ordering of vertices. My workaround was to make sure VertexList@g is sorted. This also gives more natural AdjacencyMatrix, added example of how to do this in my answer – Yaroslav Bulatov Jun 08 '11 at 04:46