Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The following code shows the GraphUtilities package makes a problem for Graph object. I want to use a TwoColoring function without using the GraphUtilities package. Is there a TwoColoring function of Graph object ? Mathematica version is

g1 = {1 -> 2, 2 -> 3};

GraphPlot[g1, VertexLabeling -> True, ImageSize -> 200]    

Graph[g1, VertexLabels -> "Name", ImagePadding -> 20]    

gc = ToCombinatoricaGraph[g1]    
color = TwoColoring[gc]    

         VertexRenderingFunction -> ({Text[Framed[#2, Background -> Hue[color[[#3]]/4], 
         FrameStyle -> RGBColor[0.94, 0.85, 0.36]], #1]} &)]    

color2 = Position[color, 2][[1]] 

HighlightGraph[Graph[g1, VertexLabels -> "Name"], {2}, ImagePadding -> 20]    
HighlightGraph[Graph[{1 -> 2, 2 -> 3}, VertexLabels -> "Name"], {2}, 
                                         ImagePadding -> 20]

No graphics.

share|improve this question

If you load GraphUtilities, context of Graph will be changed to Combinatorica (from System). If you want to use system Graph (when GraphUtilities or Combinatorica loaded), you have to specify System` context. For example,

HighlightGraph[System`Graph[g1, VertexLabels -> "Name"], {2}, 
 ImagePadding -> 20] 

will work.

There is no TwoColoring function for Graph, but you can easily write one using BreadthFirstScan:

        "DiscoverVertex" -> (Sow[{#1, Mod[#3, 2, 1]}] &)]][[2, 1]], 
  Last][[All, All, 1]]

twoColoring[g_] := {}

I make output as two sets but you could modify it whatever you want.

share|improve this answer
Thanks. Your twoColoring function is what I am looking for. Y.Sato – user25076 Sep 19 '13 at 0:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.