Skip to content

Commit d7371da

Browse files
output from partition clown
1 parent 8128b6c commit d7371da

File tree

8 files changed

+123
-91
lines changed

8 files changed

+123
-91
lines changed

src/maxback.js

Lines changed: 0 additions & 91 deletions
This file was deleted.

src/maxback/_contract.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { head } from '@aureooms/js-itertools' ;
2+
3+
export default function _contract ( G, ordering ) {
4+
5+
const x = ordering[ordering.length-2];
6+
const y = ordering[ordering.length-1];
7+
8+
const H = new Map();
9+
10+
for ( const u of head( ordering , -2 ) ) {
11+
const n = [];
12+
H.set(u, n);
13+
for ( const v of G.get(u) ) n.push(v === y ? x : v);
14+
}
15+
16+
const nx = [];
17+
H.set(x,nx);
18+
for ( const v of G.get(x) ) if ( v !== y ) nx.push(v);
19+
for ( const v of G.get(y) ) if ( v !== x && v !== y ) nx.push(v);
20+
return H;
21+
22+
}

src/maxback/_order.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { attr , decreasing } from '@aureooms/js-compare' ;
2+
import { PairingHeap } from '@aureooms/js-pairing-heap' ;
3+
4+
export default function* _order ( G ) {
5+
6+
const heap = new PairingHeap( attr( decreasing , 'key' ) );
7+
const refs = new Map();
8+
9+
for ( const v of G.keys() ) refs.set(v, heap.push({ key : 0 , value : v })) ;
10+
11+
for ( const _ of G ) {
12+
13+
const max = heap.pop() ;
14+
const u = max.value ;
15+
yield [ u , max.key ] ;
16+
refs.delete(u);
17+
18+
// update keys
19+
for ( const v of G.get(u) ) {
20+
if (!refs.has(v)) continue;
21+
const ref = refs.get(v);
22+
// max heap so decrease-key is used for +
23+
heap.decreasekey(ref, {
24+
key : ref.value.key + 1 ,
25+
value : ref.value.value
26+
} );
27+
}
28+
29+
}
30+
31+
}

src/maxback/_smallcuts.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { list , map , head , filter , chain } from '@aureooms/js-itertools' ;
2+
3+
import _order from './_order' ;
4+
import _contract from './_contract' ;
5+
6+
export default function* _smallcuts ( G ) {
7+
8+
let H = G;
9+
const id = new Map();
10+
for ( const v of G.keys()) id.set(v,[v]);
11+
12+
while ( H.size >= 2 ) {
13+
14+
const ordering = list(_order(H));
15+
const x = ordering[ordering.length-2][0];
16+
const y = ordering[ordering.length-1][0];
17+
18+
const U = new Set(chain(map( ([u,_]) => id.get(u) , head(ordering,-1) ) ));
19+
const V = new Set(id.get(y));
20+
21+
yield { 'partition' : [ U , V ] , 'size' : ordering[ordering.length-1][1] } ;
22+
23+
id.set(x, id.get(x).concat(id.get(y)));
24+
25+
H = _contract(H, list(map(([u,_]) => u , ordering)));
26+
27+
}
28+
29+
}

src/maxback/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import _contract from './_contract' ;
2+
import _order from './_order' ;
3+
import _smallcuts from './_smallcuts' ;
4+
import maxback from './maxback' ;
5+
import mb from './mb' ;
6+
7+
export default maxback ;
8+
9+
export {
10+
_contract ,
11+
_order ,
12+
_smallcuts ,
13+
maxback ,
14+
mb ,
15+
} ;

src/maxback/maxback.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import adj from '../adj';
2+
import mb from './mb';
3+
import outgoingedges from '../outgoingedges';
4+
5+
/**
6+
* Nagamochi-Ibaraki poly-time algorithm.
7+
*
8+
* @param edges List of edges of a undirected unweighted connected loopless multigraph G.
9+
* @returns {Array} A minimum cut of G.
10+
*/
11+
export default function maxback ( edges ) {
12+
const G = adj( edges ) ;
13+
const { partition : [ U , _ ] } = mb( G ) ;
14+
return outgoingedges( G , U ) ;
15+
}

src/maxback/mb.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { min } from '@aureooms/js-itertools' ;
2+
import { attr , increasing } from '@aureooms/js-compare' ;
3+
4+
import _smallcuts from './_smallcuts' ;
5+
6+
export default function mb ( G ) {
7+
return min( attr( increasing , 'size' ) , _smallcuts(G) , undefined ) ;
8+
}

src/outgoingedges.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function* outgoingedges ( G , U ) {
2+
for ( const u of U ) for ( const v of G.get(u) ) if ( !U.has(v) ) yield [ u , v ] ;
3+
}

0 commit comments

Comments
 (0)