Commit 7e8ad00d authored by Robert Ricci's avatar Robert Ricci
Browse files

Add a funciton to combine two graph nodes, needed for Jon's

O(n) reduction in tournament time.

Currently leaks memory, but that can be fixed easily.
parent f3196634
......@@ -87,6 +87,41 @@ add_edge g n1 n2 0;; *)
(* Should be: Something that can't be printed, because of how it is
* recursively defined *)
(*
* Combine nodes 1 and 2 - node1 inherits all of node2's edges, and
* node2 is destroyed
*)
let combine_nodes (graph : ('a, 'b) t) (node1 : ('a, 'b) node) (node2 : ('a, 'b)
node) : unit =
(*
* Iterate over all edges for node2
*)
let rec combine_helper (l : ('a, 'b) edge list) : unit =
match l with
edge :: xs -> (
let otherend =
(if edge.src == node2 then edge.dst else edge.dst ) in
(* Remove this edge from the other end's edge list, and add
* the new edge in *)
otherend.node_edges <-
(List.filter (fun x -> x != edge) otherend.node_edges);
(* Add the new edge to the graph *)
(* XXX Make sure it's not already in there! *)
let _ = add_edge graph node1 otherend edge.contents in
combine_helper xs)
| [] -> ()
in
combine_helper node2.node_edges;
(* Clear out all edges for node2 *)
node2.node_edges <- [];
(*
* XXX Actually delete node2 and its edges from the nodes and edges lists
*)
()
;;
(* Some simple helper functions - even though they're simple, they hide
* the list and edge representation so that we could change them later
* if we want *)
......@@ -147,7 +182,7 @@ let read_graph_file (filename : string) : ('a,'b) t =
let dst =
if not (is_member g second) then add_node g second
else find_node g second in
let edge = add_edge g src dst 1 in
let _ = add_edge g src dst 1 in
g)
in
make_graph_from_edges edges
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment