Advertisement
Guest User

Untitled

a guest
Jul 8th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.41 KB | None | 0 0
  1. let calcUpperBound problem =
  2.     let N = problem.nodeCount
  3.     let findEdge = getEdge problem.graph
  4.    
  5.     let rec loop tour unvisitedNodes includedEdges =
  6.         match tour with
  7.         | [] -> failwith "Empty list is bad"
  8.         | xs when xs.Length = N -> tour
  9.         | current :: xs ->
  10.             let f = List.tryFind (fun f -> f.node1 = current || f.node2 = current) includedEdges
  11.             match f with
  12.             | Some(edge) ->
  13.                 let next =
  14.                     if edge.node1 = current then edge.node2
  15.                     else edge.node1
  16.                 loop (next :: tour) (removeFirst current unvisitedNodes) (removeFirst edge includedEdges)
  17.             | None ->
  18.                 let next =
  19.                     List.map (fun x -> (x, findEdge current x |> edgeWeight)) unvisitedNodes
  20.                     |> List.minBy (fun (_, weight) -> weight)
  21.                     |> fst
  22.                 loop (next :: tour) (removeFirst current unvisitedNodes) includedEdges
  23.    
  24.     let allNodes = [ 0..problem.nodeCount - 1 ]
  25.    
  26.     let makeTour i =
  27.         let t = loop [ i ] (removeFirst i allNodes) (List.ofSeq problem.includedEdges)
  28.        
  29.         let tour: Graph.Tour =
  30.             { nodes = t
  31.               totalWeight = tourWeight problem.graph t }
  32.         tour
  33.     let results = List.map makeTour allNodes
  34.     List.minBy (fun (f: Graph.Tour) -> f.totalWeight) results
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement