Search code examples
functionwolfram-mathematicaminimum-spanning-tree

Mathematica function turns red, does not work


I'm trying to find the minimum spanned tree using Mathematica and I want to use the MinimumSpanningTree function from Combinatorica. I'm using the following code.

Needs["Combinatorica`"] 
MinimumSpanningTree[GraphPlot[m]]

where m is a matrix. However, MinimumSpanningTree turns red and does not work. The output gives

out = MinimumSpanningTree[<maximum spanned tree>]  //can't show the tree here

How can I make the MinimumSpanningTree work? Why does it turn red?


Solution

  • The functions turn red when you run into the so-called shadowing problem. You can read more about it in the documentation. This problem is discussed in many places, in particular in the book by Roman Maeder "Programming in Mathematica". A very nice and detailed account on shadowing is the article by David Wagner in Mathematica Journal, available here as pdf. To understand this issue, you will need some basic understanding of contexts and pacakges. The following past SO discussions may also be helpful:

    Making Mathematica packages

    Package import problem in mathematica

    Basically, some of the Combinatorica` functions have the same name as the new system graph-related functions of version 8, so Mathematica does not know which ones to call. If you really want to use Combinatorica` functions, then you will first need to "silently" load Combinatorica` without having it on the $ContextPath afterwards, which is probably most easily accomplished as

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

    Then, you will have to refer to the functions of Combinatorica` by their long names, such as Combinatorica`MinimumSpanningTree. One more thing to keep in mind is that a graph representation in Combinatorica` is different from that in the built-in v.8 functionality, so you may need to transform one into another if you want to mix those.