Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type 'a graph = ('a * 'a ) list;;
- let rec dir_neighbours node graph =
- let rec aux acc l =
- match l with
- | [] -> acc
- | (n, next)::rest ->
- if n = node then
- aux (next::acc) rest
- else aux acc rest
- in aux [] graph;;
- let rec undir_neighbours node graph =
- let rec aux acc l =
- match l with
- | [] -> acc
- | (x, y)::rest ->
- if x = node then
- aux (x::acc) rest
- else if y = node then
- aux (y::acc) rest
- else aux acc rest
- in aux [] graph;;
- let depth_first_collect graph start =
- let rec search visited g =
- match g with
- | [] -> visited
- | n::rest ->
- if List.mem n visited then
- search visited rest
- else search (n::visited)
- ((dir_neighbours n graph)
- in search [] [start];;
- let breadth_first_collect graph start =
- let rec search visited g =
- match g with
- | [] -> visited
- | n::rest ->
- if List.mem n visited then
- search visited rest
- else search (n::visited)
- (rest @ (dir_neighbours n grap
- in search [] [start];;
- let search_path graph start p =
- let rec aux visited a path=
- if List.mem a visited then
- raise NotFound
- else
- if p a then
- [a]
- else a::(mutua (a::visited)
- (dir_neighbours a graph))
- and mutua visited neighs =
- match neighs with
- | [] -> raise NotFound
- | a::rest ->
- try aux visited a
- with NotFound -> mutua visited rest
- in from_node [] start;;
- (* ALGORITMO ALTERNATIVO *)
- let gpath g start p =
- let rec aux visited l =
- match l with
- | [] -> raise NotFound
- | x::rest ->
- if List.mem x visited then
- aux visited rest
- else
- if p x then
- [x]
- else
- try x::(aux (x::visited)
- (dir_neighbours x g))
- with NotFound -> aux visited rest
- in aux [] [start];;
- let rec ciclo graph start =
- let rec aux visited n =
- if (List.mem n visited) and (n <> start) the
- raise NotFound
- else if n = start then
- List.rev (n::visited)
- else mutua (n::visited) (dir_neighbours n gr
- and mutua visited neighs =
- match neighs with
- | [] -> raise NotFound
- | n::rest ->
- try aux visited n
- with NotFound -> mutua visited rest
- in mutua [start] (dir_neighbours start graph);;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement