Advertisement
Guest User

OCaml 10

a guest
Jan 15th, 2019
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 4.82 KB | None | 0 0
  1. type 'a graph = ('a * 'a ) list;;                
  2.                                                  
  3. let rec dir_neighbours node graph =              
  4.   let rec aux acc l =                            
  5.     match l with                                  
  6.     | [] -> acc                                  
  7.     | (n, next)::rest ->                          
  8.       if n = node then                            
  9.         aux (next::acc) rest                      
  10.       else aux acc rest                          
  11.   in aux [] graph;;                              
  12.                                                  
  13. let rec undir_neighbours node graph =            
  14.   let rec aux acc l =                            
  15.     match l with                                  
  16.     | [] -> acc                                  
  17.     | (x, y)::rest ->                            
  18.       if x = node then                            
  19.         aux (x::acc) rest                        
  20.       else if y = node then                      
  21.         aux (y::acc) rest                        
  22.       else aux acc rest                          
  23.   in aux [] graph;;                              
  24.                                                  
  25. let depth_first_collect graph start =            
  26.     let rec search visited g =                    
  27.       match g with                                
  28.       | [] -> visited                            
  29.       | n::rest ->                                
  30.             if List.mem n visited then            
  31.               search visited rest                
  32.             else search (n::visited)              
  33.                         ((dir_neighbours n graph)
  34.     in search [] [start];;                        
  35.                                                  
  36. let breadth_first_collect graph start =          
  37.     let rec search visited g =                    
  38.       match g with                                
  39.       | [] -> visited                            
  40.       | n::rest ->                                
  41.         if List.mem n visited then                
  42.           search visited rest                    
  43.         else search (n::visited)                  
  44.                     (rest @ (dir_neighbours n grap
  45.     in search [] [start];;                        
  46.                                                  
  47. let search_path graph start p =                  
  48.   let rec aux visited a path=                    
  49.     if List.mem a visited then                    
  50.       raise NotFound                              
  51.     else                                          
  52.       if p a then                                
  53.         [a]                                      
  54.       else a::(mutua (a::visited)                
  55.                      (dir_neighbours a graph))    
  56.   and mutua visited neighs =                      
  57.     match neighs with                            
  58.     | [] -> raise NotFound                        
  59.     | a::rest ->                                  
  60.       try aux visited a                          
  61.       with NotFound -> mutua visited rest        
  62.    in from_node [] start;;                        
  63.                                                  
  64. (* ALGORITMO ALTERNATIVO *)                      
  65. let gpath g start p =                            
  66.   let rec aux visited l =                        
  67.     match l with                                  
  68.     | [] -> raise NotFound                        
  69.     | x::rest ->                                  
  70.       if List.mem x visited then                  
  71.         aux visited rest                          
  72.       else                                        
  73.         if p x then                              
  74.           [x]                                    
  75.         else                                      
  76.           try x::(aux (x::visited)                
  77.                       (dir_neighbours x g))      
  78.           with NotFound -> aux visited rest      
  79.   in aux [] [start];;                            
  80.                                                  
  81.                                                  
  82. let rec ciclo graph start =                      
  83.    let rec aux visited n =                        
  84.       if (List.mem n visited) and (n <> start) the
  85.          raise NotFound                          
  86.       else if n = start then                      
  87.          List.rev (n::visited)                    
  88.       else mutua (n::visited) (dir_neighbours n gr
  89.    and mutua visited neighs =                    
  90.       match neighs with                          
  91.       | [] -> raise NotFound                      
  92.       | n::rest ->                                
  93.         try aux visited n                        
  94.         with NotFound -> mutua visited rest      
  95.    in mutua [start] (dir_neighbours start graph);;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement