Group Abstract Group Abstract

Message Boards Message Boards

0
|
9.8K Views
|
8 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Work with Graph object of Combinatorica package?

Posted 10 years ago
8 Replies
POSTED BY: Szabolcs Horvát
POSTED BY: Szabolcs Horvát

Perfect answer, this explains everything very clearly. Thank you Szabolcs!

Maybe it's easier to work with Combinatorica this way:

First we load these package without adding them to the context path.

Block[{$ContextPath}, Needs["Combinatorica`"]; Needs["GraphUtilities`"]]

Now System` context symbols are not shadowed. But we must explicitly write the context to call any function from these packages.

I prefer this because it enforces a clear rule and it avoids shadowing altogether.

The function GraphUtilities`ToCombinatoricaGraph has a quirk: it evaluates Needs["Combinatorica"]` every time it's called, and this re-adds this package to the context path. To prevent this, we define our own special version:

toCombinatoricaGraph[g_?GraphQ] := 
 Block[{$ContextPath}, GraphUtilities`ToCombinatoricaGraph[g]]

We can now do this:

g = Graph[{1 <-> 2}];

Combinatorica`MinimumVertexColoring[toCombinatoricaGraph[g]]

We don't need to type System`Graph any more.


I suggested a few times to Wolfram Support to change ToCombinatoricaGraph so that it wouldn't add Combinatorica to the context path and make all this easier on users. But I guess no more changes will be done to unsupported packages.

POSTED BY: Szabolcs Horvát

Thank you for your answer Bill, this FullForm is truly a wonderful function! Turns out Graph[{1<->2}] and CompleteGraph[2] are two different animals. By imitating the syntax of the CompleteGraph function given to me by FullForm I managed to find a workaround that lets me define arbitrary graphs and calculate their chromatic number. I still have no clue why my initial approach using simply Graph[{1<->2}] did not work, and I am confident that there exists an easier solution than mine (but who cares, this works!). See the last input on the screenshot below for my solution (the number 5 at the very end is the number of vertices in the graph).

workaround for using Graph[] to define arbitrary graphs

Posted 10 years ago

I believe the Combinatorica package was originally written by Pemmaraju and Skiena outside of Wolfram Inc. They wrote "Computational Discrete Mathematics Combinatorics and Graph Theory with Mathematica(r)" and there are used copies of that available at bargain prices. I urge anyone thinking of using Combinatorica to buy a copy of that.

Later Wolfram Inc. incorporated the Combinatorica package into the Mathematica kernel. This has caused some confusion. The original package is still available if you pull that in.

In[1]:= Needs["Combinatorica`"];
g = CompleteGraph[2];
MinimumVertexColoring[g]

During evaluation of In[1]:= General::compat: Combinatorica Graph and Permutations functionality has been
superseded by preloaded functionality. The package now being loaded may conflict with this. Please see the
Compatibility Guide for details.

Out[3]= {1, 2}

In[4]:= ChromaticNumber[g]

Out[4]= 2
POSTED BY: Bill Simpson
Posted 10 years ago
POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard