1
1
( function ( exports ) {
2
- reorder = { version : "0.0.2 " } ; // semver
2
+ reorder = { version : "0.0.3 " } ; // semver
3
3
4
4
reorder . dot = science . lin . dot ;
5
5
reorder . length = science . lin . length ;
@@ -644,6 +644,16 @@ function count_crossings(graph, north, south) {
644
644
firstIndex , treeSize , tree , index , weightSum ,
645
645
invert = false , crosscount ;
646
646
647
+ if ( north == undefined ) {
648
+ var comp = reorder . permutation ( graph . nodes ( ) . length ) ;
649
+ north = comp . filter ( function ( n ) {
650
+ return graph . outEdges ( n ) . length != 0 ;
651
+ } ) ,
652
+ south = comp . filter ( function ( n ) {
653
+ return graph . inEdges ( n ) . length != 0 ;
654
+ } ) ;
655
+ }
656
+
647
657
// Choose the smaller axis
648
658
if ( north . length < south . length ) {
649
659
var tmp = north ;
@@ -693,14 +703,18 @@ function count_crossings(graph, north, south) {
693
703
}
694
704
reorder . count_crossings = count_crossings ;
695
705
reorder . barycenter = function ( graph , iter , comps ) {
696
- var perm = [ ] ;
706
+ var perms = [ [ ] , [ ] , 0 ] ;
697
707
// Compute the barycenter heuristic on each connected component
698
708
if ( ! comps ) {
699
709
comps = graph . components ( ) ;
700
710
}
701
- for ( var i = 0 ; i < comps . length ; i ++ )
702
- perm = perm . concat ( reorder . barycenter1 ( graph , comps [ i ] , iter ) ) ;
703
- return perm ;
711
+ for ( var i = 0 ; i < comps . length ; i ++ ) {
712
+ var p = reorder . barycenter1 ( graph , comps [ i ] , iter ) ;
713
+ perms = [ perms [ 0 ] . concat ( p [ 0 ] ) ,
714
+ perms [ 1 ] . concat ( p [ 1 ] ) ,
715
+ perms [ 2 ] + p [ 2 ] ] ;
716
+ }
717
+ return perms ;
704
718
} ;
705
719
706
720
// Take the list of neighbor indexes and return the median according to
@@ -733,8 +747,16 @@ reorder.barycenter1 = function(graph, comp, max_iter) {
733
747
layer ,
734
748
i , v , neighbors ;
735
749
736
- if ( comp . length < 3 )
737
- return comp ;
750
+ layer1 = comp . filter ( function ( n ) {
751
+ return graph . outEdges ( n ) . length != 0 ;
752
+ } ) ;
753
+ layer2 = comp . filter ( function ( n ) {
754
+ return graph . inEdges ( n ) . length != 0 ;
755
+ } ) ;
756
+ if ( comp . length < 3 ) {
757
+ return [ layer1 , layer2 ,
758
+ count_crossings ( graph , layer1 , layer2 ) ] ;
759
+ }
738
760
739
761
if ( ! max_iter )
740
762
max_iter = 24 ;
@@ -786,7 +808,7 @@ reorder.barycenter1 = function(graph, comp, max_iter) {
786
808
else if ( d > 0 ) return 1 ;
787
809
return 0 ;
788
810
} ) ;
789
- for ( i = 0 ; i < layer2 . length ; i ++ )
811
+ for ( i = 0 ; i < layer . length ; i ++ )
790
812
nodes [ layer [ i ] ] . pos = i ;
791
813
crossings = count_crossings ( graph , layer1 , layer2 ) ;
792
814
if ( crossings < best_crossings ) {
@@ -796,7 +818,7 @@ reorder.barycenter1 = function(graph, comp, max_iter) {
796
818
best_iter = iter ;
797
819
}
798
820
}
799
- console . log ( 'Best iter: ' + best_iter ) ;
821
+ // console.log('Best iter: '+best_iter);
800
822
return [ best_layer1 , best_layer2 , best_crossings ] ;
801
823
} ;
802
824
reorder . dijkstra = function ( graph ) {
0 commit comments