Skip to content

Instantly share code, notes, and snippets.

@awreece
Created December 8, 2012 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awreece/4238636 to your computer and use it in GitHub Desktop.
Save awreece/4238636 to your computer and use it in GitHub Desktop.
(* compile with ocamlfind ocamlopt dfs.ml -package ocamlgraph -linkpkg -package batteries *)
let filename_ = ref "temp.dot"
let start_ = ref "a"
let () =
Arg.parse
["-f", Arg.Set_string(filename_), "Filename";
"-s", Arg.Set_string(start_), "Starting vertex"]
(fun _ -> ())
"Usage: dfs <options>"
let filename = !filename_
let start = !start_
open Graph
open BatOption
module EDGE = struct
type t = string
let compare : t -> t -> int = Pervasives.compare
let default = "default"
end
module G = Imperative.Graph.AbstractLabeled(struct type t = string end)(EDGE)
module B = Builder.I(G)
module DotParser =
Dot.Parse
(B)
(struct
let node (id,_) _ = match id with
| Dot_ast.Ident s
| Dot_ast.Number s
| Dot_ast.String s
| Dot_ast.Html s -> s
let edge _ = "hello"
end)
module Dfs = Traverse.Dfs(G)
let find_vertex g s =
G.fold_vertex (fun v o -> match o with
| Some vert -> Some vert
| None -> if G.V.label v = s then Some v
else None)
g None
let g = DotParser.parse filename
let () =
Dfs.prefix_component (function v -> Pervasives.print_endline (G.V.label v))
g (get (find_vertex g start))
areece:ocamlgraph areece$ ./a.out -f temp.dot
a
c
b
d
areece:ocamlgraph areece$ ./a.out -f temp2.dot
a
b
d
c
digraph graphname {
a -> b -> c;
b -> d;
}
digraph graphname {
a -> b;
b -> c;
b -> d;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment