Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. proc(G : Graph)
  2.   name Graph::bipartite;
  3.   local visitedSet, partOne, partTwo, bipart, _color, vertex, v, _adjacentEdges\
  4. , output, _vertices, toSearch, setToDo, setOne, setTwo;
  5. begin
  6.   output := hold(Bool);
  7.   if 1 < args(0) then
  8.     case args(2)
  9.       of hold(Bool) do
  10.         break
  11.       of hold(Lists) do
  12.         output := hold(Lists);
  13.         break
  14.       otherwise
  15.         error(message("symbolic:graph:InvalidArgumentDefinition"))
  16.     end_case
  17.   end_if;
  18.   bipart := TRUE;
  19.   _vertices := Graph::getVertices(G);
  20.   if nops(_vertices) = 0 then
  21.     error(message("symbolic:graph:GraphHasNoVertices"))
  22.   end_if;
  23.   vertex := op(_vertices[1]);
  24.   toSearch := {op(_vertices)} minus {vertex};
  25.   visitedSet := {vertex};
  26.   _color[vertex] := 1;
  27.   setOne := 1;
  28.   setTwo := 0;
  29.   setToDo := {};
  30.   while not nops(visitedSet) = 0 do
  31.     visitedSet := visitedSet minus {vertex};
  32.     _adjacentEdges := {op(Graph::getAdjacentEdgesLeaving(G, [vertex]))};
  33.     if nops(_adjacentEdges) = 0 then
  34.       if contains(_color, vertex) = FALSE then
  35.         setToDo := setToDo union {vertex}
  36.       end_if
  37.     else
  38.       if contains(_color, vertex) = FALSE then
  39.         if setOne < setTwo then
  40.           _color[vertex] := 1;
  41.           setOne := setOne + 1
  42.         else
  43.           _color[vertex] := 2;
  44.           setTwo := setTwo + 1
  45.         end_if
  46.       end_if
  47.     end_if;
  48.     for v in _adjacentEdges do
  49.       if contains(_color, v) = FALSE then
  50.         _color[v] := _color[vertex] + 1;
  51.         if _color[v] mod 2 = 1 then
  52.           setOne := setOne + 1
  53.         else
  54.           setTwo := setTwo + 1
  55.         end_if;
  56.         visitedSet := visitedSet union {v};
  57.         toSearch := toSearch minus {v}
  58.       else
  59.         if _color[v] = _color[vertex] then
  60.           bipart := FALSE
  61.         end_if
  62.       end_if
  63.     end_for;
  64.     if nops(visitedSet) = 0 then
  65.       if 0 < nops(toSearch) then
  66.         vertex := op(toSearch, 1);
  67.         visitedSet := {vertex};
  68.         toSearch := toSearch minus {vertex}
  69.       end_if
  70.     else
  71.       vertex := op(visitedSet, 1)
  72.     end_if
  73.   end_while;
  74.   for v in setToDo do
  75.     if contains(_color, v) = FALSE then
  76.       if setOne < setTwo then
  77.         _color[op(v)] := 1;
  78.         setOne := setOne + 1
  79.       else
  80.         _color[op(v)] := 2;
  81.         setTwo := setTwo + 1
  82.       end_if
  83.     end_if
  84.   end_for;
  85.   if output = hold(Bool) then
  86.     bipart
  87.   else
  88.     if bipart = TRUE then
  89.       partOne := {};
  90.       for v in _vertices do
  91.         if _color[v] mod 2 = 0 then
  92.           partOne := partOne union {v}
  93.         end_if
  94.       end_for;
  95.       partTwo := {op(_vertices)} minus partOne;
  96.       [sort([op(partOne)]), sort([op(partTwo)])]
  97.     else
  98.       FAIL
  99.     end_if
  100.   end_if
  101. end_proc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement