Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. datatype 'a graph = Vertex of 'a * (((int ref) * ('a graph ref)) list);
  2.  
  3. fun getAll G =
  4.     let
  5.         val visited = ref [];
  6.  
  7.         fun been g = List.exists (fn x => g = x) (!visited)
  8.  
  9.         fun helper (g as ref (Vertex(_, l))) =
  10.             let
  11.                 val nodes = map (fn (lbl, x) => x) l;
  12.             in
  13.                 visited := g :: (!visited);
  14.                 List.app (fn x => if (been x) then () else helper x) nodes
  15.             end
  16.     in
  17.         helper G;
  18.         !visited
  19.     end;
  20.  
  21. fun hasCycles G =
  22.     List.exists
  23.         (fn (g as ref (Vertex(_, l))) =>
  24.             let
  25.                 val nodes = List.map (fn (_, x) => x) l
  26.             in
  27.                 List.exists
  28.                 (fn node =>
  29.                         List.exists
  30.                             (fn x => x = g)
  31.                             (getAll node)
  32.                 )
  33.                 nodes
  34.             end
  35.         )
  36.         (getAll G);
  37.    
  38.  
  39. exception NoPath;
  40.  
  41. fun pathFromToInSteps graph (source, destination, required_steps)=
  42.     let
  43.         fun findNode G src =
  44.             case (List.find (fn (ref (Vertex(lbl, _))) => lbl = src) (getAll G)) of
  45.                     NONE => raise NoPath
  46.                 |   SOME(g) => g;
  47.  
  48.         fun getTo (ref (Vertex(lbl, l))) (dst, steps) =
  49.             let
  50.                 fun helper [] = raise NoPath
  51.                 |   helper ((ref i, g) :: gs) =
  52.                         i + (getTo g (dst, steps - 1))
  53.                             handle NoPath => helper gs;
  54.             in
  55.                 if steps < 0 then
  56.                     raise NoPath
  57.                 else
  58.                     if lbl = dst andalso steps = 0 then
  59.                         0
  60.                     else
  61.                         helper l
  62.             end;
  63.     in
  64.         getTo (findNode graph source) (destination, required_steps)
  65.     end;
  66.  
  67. fun incrementEdges (inc, label : string) G =
  68.     let
  69.         val total = ref 0;
  70.         fun handleNode (ref (Vertex(_, l))) =
  71.             List.app
  72.                 (fn (len, ref (Vertex(lbl, _))) =>
  73.                         if lbl = label then (
  74.                                 len := !len + inc;
  75.                                 total := !total + inc
  76.                             )
  77.                         else
  78.                             ()
  79.                     )
  80.                 l
  81.     in
  82.         List.app handleNode (getAll G);
  83.         !total
  84.     end;
  85.  
  86. fun pathFromToUnder25 (label1, label2) graph =
  87.     let
  88.         fun findNode G src = valOf(List.find (fn (ref (Vertex(lbl, _))) => lbl = src) (getAll G));
  89.  
  90.         fun getTo (ref (Vertex(lbl, l))) dst limit =
  91.             let
  92.                 fun helper [] = false
  93.                 |   helper ((ref i, g) :: gs) = (getTo g dst (limit - i)) orelse (helper gs)
  94.             in
  95.                 if limit <= 0 then
  96.                     false
  97.                 else
  98.                     if lbl = dst then
  99.                         true
  100.                     else
  101.                         helper l
  102.             end;
  103.     in
  104.         (getTo (findNode graph label1) label2 25) orelse (getTo (findNode graph label2) label1 25)
  105.             handle Option => false
  106.     end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement