forked from logicchains/LPATHBench
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathml.ml
More file actions
66 lines (58 loc) · 1.96 KB
/
ml.ml
File metadata and controls
66 lines (58 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
open List
open Unix
open Printf
type route = {dest: int; cost: int}
type node = route list
type node2 = route array
let readPlaces () =
let f = open_in "agraph" in
let n = int_of_string (input_line f) in
let nodes = Array.init n (fun a -> []) in
let rec loop () =
let nums = Str.split (Str.regexp "[ \t]+") @@ input_line f in
let len = length nums in
if len = 3 then
let (node, neighbour, cost) = (int_of_string (nth nums 0), int_of_string (nth nums 1), int_of_string (nth nums 2)) in
nodes.(node) <- ({dest= neighbour; cost=cost} :: nodes.(node));
loop ()
else ();
in try
loop();
(nodes, n)
with e ->
(nodes, n)
let rec getLongestPath nodes nodeID visited =
visited.(nodeID) <- true;
let max = ref 0 in
iter (fun neighbour -> if (not (visited.(neighbour.dest)))
then (
let dist = neighbour.cost + getLongestPath nodes neighbour.dest visited in
if dist > !max then max := dist;)
else ();)
nodes.(nodeID);
visited.(nodeID) <- false;
!max
let rec getLongestPath2 (nodes: node2 array) nodeID visited =
visited.(nodeID) <- true;
let rec loop i maxDist =
if i < 0 then maxDist
else
let neighbour = nodes.(nodeID).(i) in
if (not visited.(neighbour.dest))
then
let dist = neighbour.cost + getLongestPath2 nodes neighbour.dest visited in
let newMax = if dist > maxDist then dist else maxDist in
loop (i-1) newMax
else
loop (i-1) maxDist in
let (max: int) = loop (Array.length nodes.(nodeID) - 1) 0 in
visited.(nodeID) <- false;
max;;
let () =
let (nodes, numNodes) = readPlaces() in
let visited = Array.init numNodes (fun x -> false) in
let fstNodes = Array.map (fun n1 -> Array.of_list n1) nodes in
let start = Unix.gettimeofday() in
let len = getLongestPath2 fstNodes 0 visited in
printf "%d LANGUAGE Ocaml %d\n" len (int_of_float @@ 1000. *. (Unix.gettimeofday() -. start))
(* print_int @@ getLongestPath nodes 0 visited;*)