Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Brute-force search
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Implementing the brute-force search== ===Basic algorithm=== In order to apply brute-force search to a specific class of problems, one must implement four [[Subroutine|procedures]], ''first'', ''next'', ''valid'', and ''output''. These procedures should take as a parameter the data ''P'' for the particular instance of the problem that is to be solved, and should do the following: # ''first'' (''P''): generate a first candidate solution for ''P''. # ''next'' (''P'', ''c''): generate the next candidate for ''P'' after the current one ''c''. # ''valid'' (''P'', ''c''): check whether candidate ''c'' is a solution for ''P''. # ''output'' (''P'', ''c''): use the solution ''c'' of ''P'' as appropriate to the application. The ''next'' procedure must also tell when there are no more candidates for the instance ''P'', after the current one ''c''. A convenient way to do that is to return a "null candidate", some conventional data value Ξ that is distinct from any real candidate. Likewise the ''first'' procedure should return Ξ if there are no candidates at all for the instance ''P''. The brute-force method is then expressed by the algorithm ''c'' β ''first''(''P'') '''while''' ''c'' β Ξ '''do''' '''if''' ''valid''(''P'',''c'') '''then''' ''output''(''P'', ''c'') ''c'' β ''next''(''P'', ''c'') '''end while''' For example, when looking for the divisors of an integer ''n'', the instance data ''P'' is the number ''n''. The call ''first''(''n'') should return the integer 1 if ''n'' β₯ 1, or Ξ otherwise; the call ''next''(''n'',''c'') should return ''c'' + 1 if ''c'' < ''n'', and Ξ otherwise; and ''valid''(''n'',''c'') should return '''true''' if and only if ''c'' is a divisor of ''n''. (In fact, if we choose Ξ to be ''n'' + 1, the tests ''n'' β₯ 1 and ''c'' < ''n'' are unnecessary.)The brute-force search algorithm above will call ''output'' for every candidate that is a solution to the given instance ''P''. The algorithm is easily modified to stop after finding the first solution, or a specified number of solutions; or after testing a specified number of candidates, or after spending a given amount of [[central processing unit|CPU]] time.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Brute-force search
(section)
Add topic