Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open Graph.Pack.Digraph
- let read_graph filename =
- let string_of_char = String.make 1 in
- let read_int in_ch =
- let is_valid ch = ch >= '0' && ch <= '9' in
- let ioc ch = (int_of_char ch)-(int_of_char '0') in
- let res = ref 0 in
- let tmp = ref '0' in
- try
- while true do
- tmp := (input_char in_ch);
- if (is_valid !tmp) then
- begin
- res := !res * 10 + (ioc !tmp);
- end
- else
- raise Exit;
- done;
- !res;
- with
- Exit -> !res; in
- let in_ch = open_in filename in
- let nb_of_vertex = read_int in_ch in
- let arr_of_vertex = Array.make nb_of_vertex (V.create 0) in
- let graph = create () in
- begin
- for i=0 to (nb_of_vertex-1) do
- arr_of_vertex.(i)<-(V.create i);
- add_vertex graph arr_of_vertex.(i);
- done;
- end;
- try
- while true do
- let f1 = read_int in_ch in
- let f2 = read_int in_ch in
- add_edge graph (arr_of_vertex.(f1)) (arr_of_vertex.(f2));
- done;
- graph;
- with
- End_of_file -> graph;
- ;;
- (* graphe, sommet, tableau de booleens *)
- let rec ancentres g x t =
- iter_succ (fun y_->
- let y = (V.label y_) in
- if not t.(y) then
- begin
- t.(y)<-true;
- ancentres g y_ t;
- end;) g x;
- ;;
- let composanteCFC g x_ =
- let rec descendants g x t =
- iter_pred (fun y_ ->
- let y = (V.label y_) in
- if not t.(y) then
- begin
- t.(y)<-true;
- descendants g y_ t;
- end;) g x
- in
- let x = V.label x_ in
- let nb_of_vertex = nb_vertex g in
- let cfc = Queue.create () in
- let td = (Array.create nb_of_vertex false) in (* change 10 to graph len *)
- let ta = (Array.create nb_of_vertex false) in
- td.(x) <- true;
- ta.(x) <- true;
- descendants g x_ ta;
- ancentres g x_ ta;
- for i=1 to nb_of_vertex do
- if (td.(x) & ta.(x)) then
- Queue.add x cfc;
- done;
- cfc
- ;;
- let g = read_graph "blog.txt";;
- let k = composanteCFC g in
- let z = Queue.pop k in
- z;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement