Serving the Quantitative Finance Community

 
User avatar
chewwy
Topic Author
Posts: 0
Joined: October 7th, 2010, 2:48 pm

Efficient choosing algorithm

September 29th, 2013, 3:17 pm

This seems like it should be very easy, but my mind is temporarily drawing a blank...I have some code that in its basic form chooses m (unique) items out of a collection of n items, following the algorithm described in pseudocode below.This algorithm seems very inefficient though, particularly when n is large and/or m is close to n. how could it be done better?FUNCTION CHOOSE(n, m) For i =1 To m TryAgain: Generate random integer x between 1 and n If x Not in AnswerCollection Then AnswerCollection.add x Else GOTO TryAgain End if Next i RETURN AnswerCollectionEND FUNCTION
 
User avatar
chewwy
Topic Author
Posts: 0
Joined: October 7th, 2010, 2:48 pm

Efficient choosing algorithm

September 30th, 2013, 9:35 am

Thanks!
 
User avatar
acastaldo
Posts: 14
Joined: October 11th, 2002, 11:24 pm

Efficient choosing algorithm

September 30th, 2013, 12:14 pm

My favorite was Algorithm S from Section 4.3.2 of Knuth, but I have to admit Floyd's algorithm looks even more clever. Thanks for mentioning it!
 
User avatar
Cuchulainn
Posts: 22924
Joined: July 16th, 2004, 7:38 am

Efficient choosing algorithm

October 2nd, 2013, 9:29 am

QuoteOriginally posted by: acastaldoMy favorite was Algorithm S from Section 4.3.2 of Knuth, but I have to admit Floyd's algorithm looks even more clever. Thanks for mentioning it!Do you mean Knuth 3.4.2?STL C++ has the random_shuffle() algorithm.
Last edited by Cuchulainn on October 1st, 2013, 10:00 pm, edited 1 time in total.
 
User avatar
acastaldo
Posts: 14
Joined: October 11th, 2002, 11:24 pm

Efficient choosing algorithm

October 2nd, 2013, 11:55 am

Terribly sorry, I meant Knuth 3.4.2 Algorithm S
 
User avatar
Cuchulainn
Posts: 22924
Joined: July 16th, 2004, 7:38 am

Efficient choosing algorithm

October 2nd, 2013, 11:58 am

QuoteOriginally posted by: acastaldoTerribly sorry, I meant Knuth 3.4.2 Algorithm SThanks for the link. Knuth's book just came to life for me.
 
User avatar
Cuchulainn
Posts: 22924
Joined: July 16th, 2004, 7:38 am

Efficient choosing algorithm

October 5th, 2013, 9:15 am

QuoteOriginally posted by: outrun [..] I love these 3 line algorithms!These are by definition recursive?
 
User avatar
Cuchulainn
Posts: 22924
Joined: July 16th, 2004, 7:38 am

Efficient choosing algorithm

October 5th, 2013, 1:55 pm

Same as?
 
User avatar
Cuchulainn
Posts: 22924
Joined: July 16th, 2004, 7:38 am

Efficient choosing algorithm

October 5th, 2013, 3:18 pm

QuoteOriginally posted by: outrunYes on a functional level, but I can't look inside those calls. If these are precisely documented, what else do you need to know? O(n) Analogy: in the olde mainframe days you had to pre-estimate:a. the number of operationsb. the number of words in memory.If you know a. and b. for shuffle() then finito, yes?
Last edited by Cuchulainn on October 4th, 2013, 10:00 pm, edited 1 time in total.
 
User avatar
Traden4Alpha
Posts: 3300
Joined: September 20th, 2002, 8:30 pm

Efficient choosing algorithm

October 5th, 2013, 3:48 pm

QuoteOriginally posted by: CuchulainnQuoteOriginally posted by: outrunYes on a functional level, but I can't look inside those calls. If these are precisely documented, what else do you need to know? O(n) Analogy: in the olde mainframe days you had to pre-estimate:a. the number of operationsb. the number of words in memory.If you know a. and b. for shuffle() then finito, yes?Assuming one trusts the documentation!