Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 9.0.1.0.

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

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

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

Needs["GraphUtilities`"]    
gc = ToCombinatoricaGraph[g1]    
color = TwoColoring[gc]    

GraphPlot[g1, 
         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
add comment

1 Answer

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:

twoColoring[g_?BipartiteGraphQ]:=
  GatherBy[Reap[
      BreadthFirstScan[g, 
        "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
add comment

Your Answer

 
discard

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.