Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
61
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. let _ = display_with_gv (read_graph "blog.txt");;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement