Skip to content

Commit 9d4c092

Browse files
test & fix clarkson
1 parent a7d9306 commit 9d4c092

File tree

5 files changed

+208
-9
lines changed

5 files changed

+208
-9
lines changed

js/dist/algo.js

+122
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,128 @@ var oddkldtto3sum = function ( S , Si , Sj , a , ai , aj , A , Ai , Aj , B , Bi
888888

889889
exports.oddkldtto3sum = oddkldtto3sum;
890890

891+
/* js/src/minima */
892+
/* js/src/minima/clarkson.js */
893+
894+
/**
895+
* Output sensitive inplace algorithm to find the minima set of a set S of
896+
* elements according to some partial order.
897+
*
898+
* Uses at most 2nA comparisons where A is the cardinality of the minima set.
899+
*
900+
* For (1), at most nA comparisons are used since we compare each element of S
901+
* with each elements of the minima set which is of cardinality at most A
902+
* during the execution of the algorithm.
903+
*
904+
* For (2), for each executed loop we
905+
* obtain a new minimum and increase the size of the constructed minima set by
906+
* 1, hence there are at most A loops execution, each of which loops over at
907+
* most n elements. (2) uses thus at most nA comparisons.
908+
*
909+
* The running time is dominated by the comparison time and thus the complexity
910+
* of this algorihtm is O(nA).
911+
*
912+
* Description and context in
913+
* ------------------------------------------
914+
* More Output-Sensitive Geometric Algorithms.
915+
* -------------------- Kenneth L. Clarkson -
916+
*/
917+
918+
var clarkson = function ( prec , a , i , j ) {
919+
920+
//
921+
// This algorithms reorganizes the input array `a` as follows
922+
// - elements that are minima are put at the front of `a`
923+
// - other elements are put at the back of `a`
924+
//
925+
// During the algorithm, `a` looks like this
926+
//
927+
// ------------------------------------------------------
928+
// | minima set | candidate elements | discarded elements |
929+
// ------------------------------------------------------
930+
// i min dis j
931+
932+
var min , dis , k , inc , tmp ;
933+
934+
min = i ;
935+
dis = j - 1 ;
936+
937+
// While there are candidate elements left.
938+
939+
while ( min <= dis ) {
940+
941+
// (1) Determine if the right-most candidate should be discarded because it
942+
// is dominated by one of the minima elements of the minima set in
943+
// construction.
944+
945+
for ( k = i ; k < min && !prec( a[k] , a[dis] ) ; ++k ) ;
946+
947+
// If so, discard it.
948+
949+
if ( k < min ) --dis ;
950+
951+
// (2) Otherwise, scan the candidates for a minimum. If at this point the
952+
// candidate set is not empty, at least one of its elements must be a
953+
// minimum. We scan the candidate list to find such a minimum.
954+
955+
else {
956+
957+
// Store the current minimum as the left-most candidate.
958+
959+
tmp = a[dis] ;
960+
a[dis] = a[min] ;
961+
a[min] = tmp ;
962+
963+
// For each other candidate, right-to-left.
964+
965+
for ( inc = min + 1 ; inc <= dis ; ) {
966+
967+
// If the current minimum precedes the right-most candidate,
968+
// discard the right-most candidate.
969+
970+
if ( prec( a[min] , a[dis] ) ) --dis ;
971+
972+
// Else, if the right-most candidate precedes the current
973+
// minimum, we can discard the current minimum and the
974+
// right-most candidate becomes the current minimum.
975+
976+
else if ( prec( a[dis] , a[min] ) ) {
977+
tmp = a[dis] ;
978+
a[dis] = a[min] ;
979+
a[min] = tmp ;
980+
--dis ;
981+
}
982+
983+
// Otherwise, we save the candidate for the next round.
984+
985+
else {
986+
tmp = a[dis] ;
987+
a[dis] = a[inc] ;
988+
a[inc] = tmp ;
989+
++inc ;
990+
}
991+
992+
}
993+
994+
// The above loop selects a new minimum from the set of candidates
995+
// and places it at position `min`. We now increase the `min`
996+
// counter to move this minimum from the candidate list to the
997+
// minima set.
998+
999+
++min ;
1000+
1001+
}
1002+
1003+
}
1004+
1005+
// The algorithm returns the outer right bound of the minima set a[i:min].
1006+
1007+
return min ;
1008+
1009+
} ;
1010+
1011+
exports.clarkson = clarkson ;
1012+
8911013
return exports ;
8921014
} ;
8931015
if ( typeof exports === "object" ) {

js/dist/algo.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)