Advertisement
Guest User

Untitled

a guest
Mar 28th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 5.61 KB | None | 0 0
  1. open NUnit.Framework
  2. (*20*)
  3. type IGraph =
  4.     abstract AddEdge : int -> int -> unit
  5.     abstract RemoveEdge : int -> int -> unit
  6.     abstract HasEdge : int -> int -> bool
  7.     abstract OutByEdge : int -> int list
  8.     abstract Size : int
  9.  
  10. (*21*)
  11. type MatrixGraph(n : int) =
  12.     let size = n
  13.     let mutable matrix = Array2D.zeroCreate<bool> n n
  14.  
  15.     interface IGraph with
  16.         member g.AddEdge s t =
  17.             matrix.[s, t] <- true
  18.         member g.RemoveEdge s t =
  19.             matrix.[s, t] <- false
  20.         member g.HasEdge s t =
  21.             matrix.[s, t]
  22.         member g.OutByEdge v =
  23.             List.fold (fun s i ->
  24.                         if matrix.[v, i] then i :: s else s)
  25.                         [] [0 .. size-1]
  26.         member g.Size =
  27.             size
  28.  
  29. (*22*)
  30. type ListGraph(n : int) =
  31.     let size = n
  32.     let mutable l = Array.create<int list> n []
  33.  
  34.     interface IGraph with
  35.         member g.AddEdge s t =
  36.             l.[s] <- t :: l.[s]
  37.         member g.RemoveEdge s t =
  38.             l.[s] <- List.filter (fun x -> x <> t) l.[s]
  39.         member g.HasEdge s t =
  40.             List.exists (fun x -> x = t) l.[s]
  41.         member g.OutByEdge v =
  42.             l.[v]
  43.         member g.Size =
  44.             size
  45.  
  46. let isVertice v x =
  47.     v=x
  48.  
  49. (*23*)
  50. let fromVertice (g : IGraph) v =
  51.     let byEdgeFromSet r =
  52.         List.concat (List.map g.OutByEdge r)
  53.     let exclude exclusionPredicate list =
  54.         List.filter (fun u -> not (exclusionPredicate u)) list
  55.     let excludeAll exclusionList list =
  56.         exclude (fun u -> List.exists (isVertice u) exclusionList) list
  57.     let rec fromVerticeSet r =
  58.         let p = excludeAll r (byEdgeFromSet r)
  59.         if p = [] then r else fromVerticeSet (p @ r)
  60.     fromVerticeSet [v]
  61.  
  62. (*24*)
  63. let toVertice (g : IGraph) v =
  64.     let destReachableFrom x =
  65.         List.exists (isVertice v) (fromVertice g x)
  66.     let accumulateVertice acc x =
  67.         if destReachableFrom x then x :: acc else acc
  68.     List.fold accumulateVertice [] [0 .. g.Size-1]
  69.  
  70. (*25*)
  71. type IMarkedGraph<'T> =
  72.    inherit IGraph
  73.    abstract SetMark : int -> 'T -> unit
  74.     abstract GetMark : int -> 'T
  75.  
  76. (*26*)
  77. type MarkedGraph<'T>(graph : IGraph) =
  78.     let mutable g = graph
  79.     let mutable mx = Array.zeroCreate<'T> g.Size
  80.  
  81.    interface IMarkedGraph<'T> with
  82.         member p.AddEdge s t =
  83.             g.AddEdge s t
  84.         member p.RemoveEdge s t =
  85.             g.RemoveEdge s t
  86.         member p.HasEdge s t =
  87.             g.HasEdge s t
  88.         member p.OutByEdge v =
  89.             g.OutByEdge v
  90.         member p.Size =
  91.             g.Size
  92.         member p.SetMark v m =
  93.             mx.[v] <- m
  94.         member p.GetMark v =
  95.             mx.[v]  
  96.  
  97. let randomNetwork sz =  
  98.     let getName k =
  99.         match k with
  100.         | 0 -> "Windows"
  101.         | 1 -> "MacOS"
  102.         | 2 -> "Linux"
  103.         | _ -> ""
  104.     let g = (MarkedGraph<string*bool>(ListGraph(sz)) :> IMarkedGraph<string*bool>)
  105.     let r = System.Random()
  106.     let k = r.Next(g.Size * (g.Size - 1) / 2)
  107.     List.iter (fun i -> g.AddEdge i (i + 1); g.AddEdge (i + 1) i) [0 .. sz - 2]
  108.     let e = List.init k (fun i -> (r.Next(sz), r.Next(sz)))
  109.     List.iter (fun (s,t) -> if not (g.HasEdge s t) then g.AddEdge s t; g.AddEdge t s) e
  110.     let m = List.init sz (fun i -> (i, getName (r.Next(3))))
  111.     List.iter (fun (v,m) -> g.SetMark v (m, false)) m
  112.     g
  113.  
  114. let simulateNet (g : IMarkedGraph<string * bool>) =
  115.     let r = System.Random()
  116.     let prob s =
  117.         match s with
  118.         | "Windows" -> 0.9
  119.         | "MacOS" -> 0.4
  120.         | "Linux" -> 0.01
  121.         | _ -> 0.0
  122.     let markName m =
  123.         match m with
  124.         | true -> "Infected"
  125.         | false -> "OK"
  126.     let printNet () =
  127.         List.iter (fun i -> let (os, mark) = (g.GetMark i);
  128.                             printfn "Computer %A with %A is %A" i os (markName mark)) [0 .. g.Size-1]
  129.     let someOfInfected l =
  130.         List.fold (fun s i -> (snd (g.GetMark i)) || s) false l
  131.     let allInfected () =
  132.         List.fold (fun s i -> (snd (g.GetMark i)) && s) true [0 .. g.Size-1]
  133.     let infect v =
  134.         let n = g.GetMark v
  135.         g.SetMark v (fst n, true)
  136.     let checkVertice v =
  137.         let x = (prob (fst ( g.GetMark v)))
  138.         let d = r.NextDouble()
  139.         if (someOfInfected (g.OutByEdge v)) && (d < x) then
  140.             infect v
  141.     let rec loop x =
  142.         if allInfected() then ()
  143.         else
  144.             printfn "Step %i" (x + 1)
  145.             List.iter checkVertice [0 .. g.Size-1]
  146.             if x % 10 = 0 then
  147.                 printNet()
  148.             loop (x + 1)
  149.     infect 0
  150.     loop 0
  151.     printNet()
  152.  
  153.  
  154. [<TestFixture>]
  155. type TestGraph () =
  156.     let mutable g = ListGraph (10) :> IGraph
  157.     do
  158.         g.AddEdge 0 1
  159.         g.AddEdge 2 3
  160.         g.AddEdge 1 2
  161.         g.AddEdge 4 5
  162.         g.AddEdge 6 7
  163.         g.AddEdge 7 8
  164.         g.AddEdge 8 6
  165.  
  166.     [<Test>]
  167.     member t.``From vertex`` () =
  168.         let f = fromVertice g 9
  169.         Assert.AreEqual (List.sort f, [9])
  170.         let f = fromVertice g 4
  171.         Assert.AreEqual (List.sort f, [4;5])
  172.         let f = fromVertice g 0
  173.         Assert.AreEqual (List.sort f, [0;1;2;3])
  174.         let f = fromVertice g 6
  175.         Assert.AreEqual (List.sort f, [6;7;8])
  176.  
  177.     [<Test>]
  178.     member t.``To vertex`` () =
  179.         let f = toVertice g 9
  180.         Assert.AreEqual (List.sort f, [9])
  181.         let f = toVertice g 5
  182.         Assert.AreEqual (List.sort f, [4;5])
  183.         let f = toVertice g 3
  184.         Assert.AreEqual (List.sort f, [0;1;2;3])
  185.         let f = toVertice g 6
  186.         Assert.AreEqual (List.sort f, [6;7;8])
  187.              
  188. [<EntryPoint>]
  189. let main argv =
  190.     0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement