forked from hyoo-ru/mam_mol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.ts
148 lines (96 loc) · 2.77 KB
/
graph.ts
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
namespace $ {
export class $mol_graph< Node , Edge > {
nodes = new Set< Node >()
edges_out = new Map< Node , Map< Node , Edge > >()
edges_in = new Map< Node , Map< Node , Edge > >()
link_out( from : Node , to : Node , edge : Edge ) {
let pair = this.edges_out.get( from )
if( !pair ) {
pair = new Map< Node , Edge >()
this.edges_out.set( from , pair )
this.nodes.add( from )
}
pair.set( to , edge )
this.nodes.add( to )
}
link_in( to : Node , from : Node , edge : Edge ) {
let pair = this.edges_in.get( from )
if( !pair ) {
pair = new Map< Node , Edge >()
this.edges_in.set( from , pair )
this.nodes.add( from )
}
pair.set( to , edge )
this.nodes.add( to )
}
edge_out( from : Node , to : Node ) {
return this.edges_out.get( from )?.get( to ) ?? null
}
edge_in( to : Node , from : Node ) {
return this.edges_in.get( to )?.get( from ) ?? null
}
link( from : Node , to : Node , edge : Edge ) {
this.link_out( from , to , edge )
this.link_in( to , from , edge )
}
unlink( from : Node , to : Node ) {
this.edges_in.get( to )?.delete( from )
this.edges_out.get( from )?.delete( to )
}
acyclic( get_weight : ( edge : Edge )=> number ) {
const checked = [] as Node[]
for( const start of this.nodes ) {
const path = [] as Node[]
const visit = ( from : Node ) : number => {
if( checked.includes( from ) ) return Number.MAX_SAFE_INTEGER
const index = path.lastIndexOf( from )
if( index > -1 ) {
const cycle = path.slice( index )
return cycle.reduce(
( weight , node , index )=> Math.min(
weight ,
get_weight( this.edge_out( node , cycle[ ( index + 1 ) % cycle.length ] )! ) ,
) ,
Number.MAX_SAFE_INTEGER ,
)
}
path.push( from )
dive: try {
const deps = this.edges_out.get( from )
if( !deps ) break dive
for( const [ to , edge ] of deps ) {
if( to === from ) {
this.unlink( from , to )
continue
}
const weight_out = get_weight( edge )
const min = visit( to )
if( weight_out > min ) return min
if( weight_out === min ) this.unlink( from , to )
}
} finally {
path.pop()
}
checked.push( from )
return Number.MAX_SAFE_INTEGER
}
visit( start )
}
}
get sorted() {
const sorted = new Set< Node >()
const visit = ( node : Node ) => {
if( sorted.has( node ) ) return
const deps = this.edges_out.get( node )
if( deps ) {
for( const [dep] of deps ) visit( dep )
}
sorted.add( node )
}
for( const node of this.nodes ) {
visit( node )
}
return sorted
}
}
}