The unpac monorepo manager self-hosting as a monorepo using unpac
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

new example: word graph

+101 -1
+2
vendor/opam/ocamlgraph/CHANGES.md
··· 1 + 2 + - new example `words` to build and explore a word graph 1 3 2 4 # 2.2.0 (April 11, 2025) 3 5
+2 -1
vendor/opam/ocamlgraph/examples/dune
··· 1 1 (executables 2 - (names color compare_prim_kruskal demo_planar demo_prim demo sudoku depend2dot) 2 + (names color compare_prim_kruskal demo_planar demo_prim demo sudoku 3 + depend2dot words) 3 4 (libraries graph unix graphics threads)) 4 5 5 6 (alias
+97
vendor/opam/ocamlgraph/examples/words.ml
··· 1 + 2 + (* Word Graph 3 + 4 + Given a number of letters n and a file containing words (one per line), 5 + this program builds the undirected graph where 6 + - vertices are words of length n; 7 + - two words are connected with an edge if they differ by exactly one letter. 8 + 9 + Then the program computes and prints the components. 10 + 11 + Finally, it repeatedly queries a word from the user and displays 12 + its component. 13 + *) 14 + 15 + open Format 16 + open Graph 17 + module H = Hashtbl.Make(String) 18 + 19 + module G = Imperative.Graph.Abstract(String) 20 + let g = G.create () 21 + 22 + let words : G.V.t H.t = H.create 16 23 + 24 + let add_word w = 25 + let v = G.V.create w in H.add words w v; G.add_vertex g v 26 + 27 + let rec read_words n c = 28 + match input_line c with 29 + | s -> if String.length s = n then add_word s; read_words n c 30 + | exception End_of_file -> () 31 + 32 + let () = 33 + try match Sys.argv with [| _; n; f |] -> 34 + let n = int_of_string n in 35 + let c = open_in f in 36 + read_words n c; 37 + close_in c; 38 + | _ -> raise Exit 39 + with _ -> eprintf "%s <int> <file>@." Sys.argv.(0); exit 1 40 + 41 + let () = printf "%d words@." (G.nb_vertex g) 42 + 43 + let diff1 s1 s2 = 44 + let n = String.length s1 in 45 + assert (String.length s2 = n); 46 + let rec scan d i = 47 + i = n && d = 1 || 48 + i < n && if s1.[i] = s2.[i] then scan d (i+1) else d = 0 && scan 1 (i+1) in 49 + scan 0 0 50 + 51 + let () = 52 + G.iter_vertex (fun v1 -> 53 + G.iter_vertex (fun v2 -> 54 + if diff1 (G.V.label v1) (G.V.label v2) then G.add_edge g v1 v2 55 + ) g) g 56 + 57 + let () = printf "%d edges@." (G.nb_edges g) 58 + 59 + module C = Components.Undirected(G) 60 + let comp = C.components_array g 61 + 62 + let histogram a = 63 + let h = Hashtbl.create (Array.length a) in 64 + let incr v = 65 + Hashtbl.replace h v (1 + try Hashtbl.find h v with Not_found -> 0) in 66 + Array.iter incr a; 67 + let l = Hashtbl.fold (fun v n acc -> (v, n) :: acc) h [] in 68 + List.sort (fun (v1, _) (v2, _) -> compare v1 v2) l 69 + 70 + let () = 71 + printf "%d components@." (Array.length comp); 72 + Array.sort (fun l1 l2 -> Stdlib.compare (List.length l1) (List.length l2)) 73 + comp; 74 + let print1 v = printf "@ %s" (G.V.label v) in 75 + let print c = 76 + printf "@[<hov 2>%d:" (List.length c); List.iter print1 c; printf "@]@." in 77 + Array.iter print comp; 78 + let hist = histogram (Array.map List.length comp) in 79 + List.iter (fun (v, n) -> printf "%d component(s) of size %d@." n v) hist 80 + 81 + module D = Traverse.Dfs(G) 82 + 83 + let () = 84 + while true do 85 + printf "start: @?"; 86 + let s = read_line () in 87 + try 88 + let v = H.find words s in 89 + printf "@[<hov 2>component:"; 90 + let m = ref 0 in 91 + let visit v = printf "@ %s" (G.V.label v); incr m in 92 + D.prefix_component visit g v; 93 + printf "@]@."; 94 + printf "%d word(s)@." !m 95 + with Not_found -> 96 + printf "not a vertex@." 97 + done