-
Notifications
You must be signed in to change notification settings - Fork 0
/
digraph.ml
75 lines (55 loc) · 1.58 KB
/
digraph.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(*
**
** ocaml module implementation Digraph
**
** Description: Directed graph ADT
** implementing adjacency list representation
**
** Author: Eray Ozkural (exa) <[email protected]>, (C) 2003
**
** Copyright: See COPYING file that comes with this distribution
**
*)
open Printf
type edge = int * int
(* an edge is an ordered pair of vertices *)
type digraph = {
adj : (int list) Dynarray.dynarray;
}
(* adjacency list representation for directed graph *)
let make () = {
adj = Dynarray.make [];
}
(* get neighborhood of vertex u *)
let n g u = Dynarray.get g.adj u
let adj g u = n g u
let set_adj g u a= Dynarray.set g.adj u a
let degree g u = List.length (n g u)
let add g (u,v) =
let n = n g u in
if not (List.exists ((=) v) n) then
Dynarray.set g.adj u (v :: n)
else
()
let remove g (u,v) =
let n = n g u in
Dynarray.set g.adj u (List.filter ((<>) v) n)
let edge_in g (u,v) =
let n = n g u in
List.exists ((=) v) n
(*let vertex_in g u = u < Dynarray.length g.adj*)
let vertex_in g u = degree g u > 0
let num_edges g =
Array.fold_left (+) 0 (Dynarray.mapa (function x -> List.length x) g.adj)
let num_vertices g = Dynarray.length g.adj
let list_to_string el lst = "[" ^ String.concat ";" (List.map el lst)
^ "]"
let edges_to_string el lst = String.concat "," (List.map el lst)
let to_string g =
let prne i u = "(" ^ string_of_int i ^ "," ^
string_of_int u ^ ")" in
"{" ^ String.concat ","
(Array.to_list (Dynarray.mapai (fun i x -> list_to_string (prne i)
x) g.adj))
^ "}"
let dot_graph g = "TODO: dot graph here"