Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let path (g : int array array) =
- let n = Array.length g in
- let ans_arr = Array.make n 1
- and answer = ref 0 in
- for i = 0 to (n - 1) do
- let cur = g.(i) in
- let cur_size = Array.length cur in
- answer := max !answer (ans_arr.(i));
- for j = 0 to (cur_size - 1) do
- if cur.(j) > (i + 1) then
- ans_arr.(cur.(j) - 1) <- max (ans_arr.(cur.(j) - 1) + 1) (ans_arr.(i) + 1)
- done;
- done;
- !answer;
- (*path [| [|3;5;6|]; [|4|]; [|6|]; [|5|]; [|6|]; [|1|] |] = 4;;*)
- type node = {value: int; mutable neighbours: int list}
- type graph = node array
- let g_make n =
- Array.init n (fun i -> {value = i; neighbours = []})
- let neighbours g i =
- g.(i).neighbours
- let size g =
- Array.length g
- let insert_edge g i j =
- g.(i).neighbours <- j :: g.(i).neighbours;
- g.(j).neighbours <- i :: g.(j).neighbours
- let path2 g =
- let n = size g in
- let ans_arr = Array.make n 1
- and answer = ref 0 in
- for i = 0 to (n - 1) do
- let cur = Array.of_list (neighbours g i) in
- let cur_size = Array.length cur in
- answer := max !answer (ans_arr.(i));
- for j = 0 to (cur_size - 1) do
- if cur.(j) > (i + 1) then
- ans_arr.(cur.(j) - 1) <- max (ans_arr.(cur.(j) - 1) + 1) (ans_arr.(i) + 1)
- done;
- done;
- !answer;;
- let test = g_make 6;;
- insert_edge test 0 2;
- insert_edge test 0 4;
- insert_edge test 0 5;
- insert_edge test 1 3;
- insert_edge test 2 5;
- insert_edge test 3 4;
- insert_edge test 4 5;
- path2 test;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement