File: ugraph.sml

package info (click to toggle)
mlton 20210117%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,464 kB
  • sloc: ansic: 27,682; sh: 4,455; asm: 3,569; lisp: 2,879; makefile: 2,347; perl: 1,169; python: 191; pascal: 68; javascript: 7
file content (77 lines) | stat: -rw-r--r-- 2,413 bytes parent folder | download | duplicates (5)
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
76
77
(*
 *  Undirected graph view.  This makes a graph look undirected.
 *
 *  -- Allen
 *)

signature UNDIRECTED_GRAPH_VIEW =
sig

   val undirected_view : ('n,'e,'g) Graph.graph -> ('n,'e,'g) Graph.graph

end

structure UndirectedGraphView : UNDIRECTED_GRAPH_VIEW =
struct
   
   structure G = Graph
   structure Sort = ListMergeSort

   fun undirected_view (G.GRAPH G) =
   let fun adjacent_edges i =
       let val in_edges  = map (fn (i,j,e) => (j,i,e)) (#in_edges G i)
           val out_edges = #out_edges G i
       in
           Sort.uniqueSort (fn ((i,j,_),(i',j',_)) =>
                              if i < i' then LESS 
                              else if i = i' then 
                                   if j < j' then LESS
                                   else if j = j' then EQUAL
                                   else GREATER
                              else GREATER)
                          (in_edges @ out_edges)
       end
       fun adjacent_nodes i =
       let val succ = #succ G i
           val pred = #pred G i
       in
           Sort.uniqueSort Int.compare (succ @ pred)
       end

       fun has_edge (i,j) = #has_edge G (i,j) orelse #has_edge G (j,i)

   in
       G.GRAPH
       { name            = #name G,
         graph_info      = #graph_info G,
         new_id          = #new_id G,
         add_node        = #add_node G,
         add_edge        = #add_edge G,
         remove_node     = #remove_node G,
         set_in_edges    = #set_in_edges G,
         set_out_edges   = #set_out_edges G,
         set_entries     = #set_exits G,
         set_exits       = #set_entries G,
         garbage_collect = #garbage_collect G,
         nodes           = #nodes G,
         edges           = #edges G,
         order           = #order G,
         size            = #size G,
         capacity        = #capacity G,
         out_edges       = adjacent_edges,
         in_edges        = adjacent_edges,
         succ            = adjacent_nodes,
         pred            = adjacent_nodes,
         has_edge        = has_edge,
         has_node        = #has_node G,
         node_info       = #node_info G,
         entries         = #exits G,
         exits           = #entries G,
         entry_edges     = #entry_edges G,
         exit_edges      = #exit_edges G,
         forall_nodes    = #forall_nodes G,
         forall_edges    = #forall_edges G
       }
   end
end