Skip to content

Commit 87f3f44

Browse files
author
jin
committed
$mol_graph: publicity
1 parent 170cf3d commit 87f3f44

File tree

1 file changed

+59
-28
lines changed

1 file changed

+59
-28
lines changed

graph/graph.ts

+59-28
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
11
namespace $ {
22

3+
/**
4+
* Generic Graph model.
5+
* - Supports any type of Nodes and Edges.
6+
* - All links are ordered, but this ay be ignored.
7+
* - Multigraph supported using arrays of Edges.
8+
* - Hypergraph supported by reusing same Edge on set of links.
9+
* - Ubergraph supported using Edges as Nodes to.
10+
**/
311
export class $mol_graph< Node , Edge > {
412

13+
/** All registered Nodes */
514
nodes = new Set< Node >()
615

16+
/** Edges for Nodes pairs (from-to-edge) */
717
edges_out = new Map< Node , Map< Node , Edge > >()
18+
/** Edges for Nodes pairs (to-from-edge) */
819
edges_in = new Map< Node , Map< Node , Edge > >()
920

21+
// LINKING NODES
22+
23+
/** Full connect two Nodes */
24+
link( from : Node , to : Node , edge : Edge ) {
25+
this.link_out( from , to , edge )
26+
this.link_in( to , from , edge )
27+
}
28+
29+
/** Full disconnect two Nodes */
30+
unlink( from : Node , to : Node ) {
31+
this.edges_in.get( to )?.delete( from )
32+
this.edges_out.get( from )?.delete( to )
33+
}
34+
35+
/** Forward connect two Nodes */
1036
link_out( from : Node , to : Node , edge : Edge ) {
1137

1238
let pair = this.edges_out.get( from )
@@ -22,6 +48,7 @@ namespace $ {
2248

2349
}
2450

51+
/** Backward connect two Nodes */
2552
link_in( to : Node , from : Node , edge : Edge ) {
2653

2754
let pair = this.edges_in.get( to )
@@ -37,24 +64,26 @@ namespace $ {
3764

3865
}
3966

67+
// GETTING EDGES
68+
69+
/** Return any Edge for two Nodes or null */
70+
edge( from : Node , to : Node ) {
71+
return this.edge_out( from, to ) ?? this.edge_in( to, from )
72+
}
73+
74+
/** Return output Edge for two Nodes or null */
4075
edge_out( from : Node , to : Node ) {
4176
return this.edges_out.get( from )?.get( to ) ?? null
4277
}
4378

79+
/** Return input Edge for two Nodes or null */
4480
edge_in( to : Node , from : Node ) {
4581
return this.edges_in.get( to )?.get( from ) ?? null
4682
}
4783

48-
link( from : Node , to : Node , edge : Edge ) {
49-
this.link_out( from , to , edge )
50-
this.link_in( to , from , edge )
51-
}
52-
53-
unlink( from : Node , to : Node ) {
54-
this.edges_in.get( to )?.delete( from )
55-
this.edges_out.get( from )?.delete( to )
56-
}
84+
// MUTATIONS
5785

86+
/** Cut cycles at lowest priority of Edges */
5887
acyclic( get_weight : ( edge : Edge )=> number ) {
5988

6089
const checked = [] as Node[]
@@ -128,6 +157,9 @@ namespace $ {
128157

129158
}
130159

160+
// NODES SELECTION
161+
162+
/** Topoligical ordered set of all Nodes for acyclic graph */
131163
get sorted() {
132164

133165
const sorted = new Set< Node >()
@@ -152,6 +184,7 @@ namespace $ {
152184
return sorted
153185
}
154186

187+
/** All Nodes which don't have input Edges */
155188
get roots() {
156189

157190
const roots = [] as Node[]
@@ -165,7 +198,15 @@ namespace $ {
165198
return roots
166199
}
167200

168-
depth( select: ( left: number, right: number )=> number ) {
201+
// DEPTH STATS
202+
203+
/**
204+
* Nodes depth statistics for acyclic graph
205+
* @example
206+
* graph.depth_stat( Math.min )
207+
* graph.depth_stat( Math.max )
208+
**/
209+
nodes_depth( select: ( left: number, right: number )=> number ) {
169210

170211
const stat = new Map< Node, number >()
171212
const visit = ( node: Node, depth = 0 )=> {
@@ -181,18 +222,16 @@ namespace $ {
181222
return stat
182223
}
183224

184-
get depth_min() {
185-
return this.depth( Math.min )
186-
}
187-
188-
get depth_max() {
189-
return this.depth( Math.max )
190-
}
191-
192-
group_depth( select: ( left: number, right: number )=> number ) {
225+
/**
226+
* Depth's Nodes statistics for acyclic graph
227+
* @example
228+
* graph.depth_nodes( Math.min )
229+
* graph.depth_nodes( Math.max )
230+
**/
231+
depth_nodes( select: ( left: number, right: number )=> number ) {
193232

194233
const groups = [] as Node[][]
195-
for( const [ node, depth ] of this.depth( select ).entries() ) {
234+
for( const [ node, depth ] of this.nodes_depth( select ).entries() ) {
196235

197236
if( groups[ depth ] ) groups[ depth ].push( node )
198237
else groups[ depth ] = [ node ]
@@ -202,14 +241,6 @@ namespace $ {
202241
return groups
203242
}
204243

205-
get group_depth_min() {
206-
return this.group_depth( Math.min )
207-
}
208-
209-
get proup_depth_max() {
210-
return this.group_depth( Math.max )
211-
}
212-
213244
}
214245

215246
}

0 commit comments

Comments
 (0)