Created
December 8, 2012 04:38
-
-
Save awreece/4238636 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
areece:ocamlgraph areece$ ./a.out -f temp.dot | |
a | |
c | |
b | |
d | |
areece:ocamlgraph areece$ ./a.out -f temp2.dot | |
a | |
b | |
d | |
c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
digraph graphname { | |
a -> b -> c; | |
b -> d; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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