123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145(**************************************************************************)(* *)(* Ocamlgraph: a generic graph library for OCaml *)(* Copyright (C) 2004-2010 *)(* Sylvain Conchon, Jean-Christophe Filliatre and Julien Signoles *)(* *)(* This software is free software; you can redistribute it and/or *)(* modify it under the terms of the GNU Library General Public *)(* License version 2.1, with the special exception on linking *)(* described in file LICENSE. *)(* *)(* This software is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *)(* *)(**************************************************************************)moduletypeGM=sigvalis_directed:booltypetvalnb_vertex:t->intmoduleV:Sig.COMPARABLEvalout_degree:t->V.t->intvaliter_vertex:(V.t->unit)->t->unitvalfold_vertex:(V.t->'a->'a)->t->'a->'avaliter_succ:(V.t->unit)->t->V.t->unitvalfold_succ:(V.t->'a->'a)->t->V.t->'a->'amoduleMark:sigvalget:V.t->intvalset:V.t->int->unitendendexceptionNoColoring(** Graph coloring with marking.
Only applies to imperative graphs with marks. *)moduleMark(G:GM)=structmoduleBfs=Traverse.Bfs(G)letcoloringgk=ifG.is_directedtheninvalid_arg"coloring: directed graph";(* first step: we eliminate vertices with less than [k] successors *)letstack=Stack.create()inletnb_to_color=ref(G.nb_vertexg)inletcount=ref1inwhile!count>0docount:=0;leterasev=incrcount;G.Mark.setv(k+1);Stack.pushvstackinG.iter_vertex(funv->ifG.Mark.getv=0&&G.out_degreegv<kthenerasev)g;(*Format.printf "eliminating %d nodes@." !count;*)nb_to_color:=!nb_to_color-!countdone;(* second step: we k-color the remaining of the graph *)(* [try_color v i] tries to assign color [i] to vertex [v] *)lettry_colorvi=G.Mark.setvi;G.iter_succ(funw->ifG.Mark.getw=ithenraiseNoColoring)gvinletuncolorv=G.Mark.setv0inif!nb_to_color>0thenbeginletreciterateiter=letv=Bfs.getiterinletm=G.Mark.getvinifm>0theniterate(Bfs.stepiter)elsebeginfori=1tokdotrytry_colorvi;iterate(Bfs.stepiter)withNoColoring->()done;uncolorv;raiseNoColoringendintryiterate(Bfs.startg)withExit->()end;(* third step: we color the eliminated vertices, in reverse order *)Stack.iter(funv->tryfori=1tokdotrytry_colorvi;raiseExitwithNoColoring->()done;raiseNoColoring(* it may still fail on a self edge v->v *)withExit->())stacklettwo_colorg=ifG.is_directedtheninvalid_arg"coloring: directed graph";(* first, set all colors to 0 *)leterasev=G.Mark.setv0inG.iter_vertexeraseg;(* then, use dfs to color the nodes *)letrecdfscv=matchG.Mark.getvwith|1|2ascv->ifcv<>cthenraiseNoColoring(* check for cycles *)|_->G.Mark.setvc;G.iter_succ(dfs(1-c))gvinletstartv=matchG.Mark.getvwith1|2->()|_->dfs1vinG.iter_vertexstartgend(** Graph coloring for graphs without marks: we use an external hash table *)moduletypeG=sigvalis_directed:booltypetvalnb_vertex:t->intmoduleV:Sig.COMPARABLEvalout_degree:t->V.t->intvaliter_vertex:(V.t->unit)->t->unitvalfold_vertex:(V.t->'a->'a)->t->'a->'avaliter_succ:(V.t->unit)->t->V.t->unitvalfold_succ:(V.t->'a->'a)->t->V.t->'a->'aendmoduleMake(G:G)=structmoduleH=Hashtbl.Make(G.V)letadd_marks()=leth=H.create97inh,(modulestructincludeGmoduleMark=structletgetv=tryH.findhvwithNot_found->0letsetvn=H.replacehvnendend:GMwithtypet=G.tandtypeV.t=G.V.t)letcoloringgk=leth,(moduleGM)=add_marks()inletmoduleM=Mark(GM)inM.coloringgk;hlettwo_colorg=leth,(moduleGM)=add_marks()inletmoduleM=Mark(GM)inM.two_colorg;hend