Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. open Graph.Pack.Digraph
  2.  
  3. let read_graph filename =
  4.  
  5.   let string_of_char = String.make 1 in
  6.  
  7.   let read_int in_ch =
  8.     let is_valid ch = ch >= '0' && ch <= '9' in
  9.     let ioc ch = (int_of_char ch)-(int_of_char '0') in
  10.    
  11.     let res = ref 0 in
  12.     let tmp = ref '0' in
  13.     try
  14.       while true do
  15.     tmp := (input_char in_ch);
  16.     if (is_valid !tmp) then
  17.       begin
  18.         res := !res * 10 + (ioc !tmp);
  19.       end
  20.     else
  21.       raise Exit;
  22.       done;
  23.       !res;
  24.     with
  25.     Exit -> !res; in
  26.  
  27.   let in_ch = open_in filename in
  28.   let nb_of_vertex = read_int in_ch in
  29.   let arr_of_vertex = Array.make nb_of_vertex (V.create 0) in
  30.   let graph = create () in
  31.   begin
  32.     for i=0 to (nb_of_vertex-1) do
  33.       arr_of_vertex.(i)<-(V.create i);
  34.       add_vertex graph arr_of_vertex.(i);
  35.     done;
  36.   end;
  37.   try
  38.     while true do
  39.       let f1 = read_int in_ch in
  40.       let f2 = read_int in_ch in    
  41.       add_edge graph (arr_of_vertex.(f1)) (arr_of_vertex.(f2));
  42.     done;
  43.     graph;
  44.   with
  45.       End_of_file -> graph;
  46. ;;
  47.  
  48. (* graphe, sommet, tableau de booleens *)
  49. let rec ancentres g x t =
  50.   iter_succ (fun y_->
  51.     let y = (V.label y_) in
  52.     if not t.(y) then
  53.       begin
  54.     t.(y)<-true;
  55.     ancentres g y_ t;
  56.       end;) g x;
  57. ;;
  58.  
  59.  
  60. let composanteCFC g x_ =
  61.  
  62.   let rec descendants g x t =  
  63.     iter_pred (fun y_ ->
  64.       let y = (V.label y_) in
  65.       if not t.(y) then
  66.     begin
  67.       t.(y)<-true;
  68.       descendants g y_ t;
  69.     end;) g x
  70.   in
  71.   let x = V.label x_ in
  72.   let nb_of_vertex = nb_vertex g in
  73.   let cfc = Queue.create () in
  74.   let td = (Array.create nb_of_vertex false) in (* change 10 to graph len *)
  75.   let ta = (Array.create nb_of_vertex false) in
  76.   td.(x) <- true;
  77.   ta.(x) <- true;
  78.   descendants g x_ ta;
  79.   ancentres g x_ ta;
  80.   for i=1 to nb_of_vertex do
  81.     if  (td.(x) & ta.(x)) then
  82.       Queue.add x cfc;
  83.   done;
  84.   cfc
  85. ;;
  86.  
  87. let g = read_graph "blog.txt";;
  88. let k = composanteCFC g in
  89. let z = Queue.pop k in
  90. z;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement