Backtracking algorithm for prime sequence

偶尔善良 提交于 2019-12-10 12:05:59

问题


I'm having trouble doing backtracking and not sure if what I'm doing is backtracking exactly.

I have n amount of integers for my example it will be [5,6,7,8].

From those integers I need to find if a prime sequence exists and if it does display it.

The prime sequence for this example is 7,6,5,8 since 7+6=13 6+5=11 5+8=13

To get the answer I can go through each n and then try to see if its a prime sequence.

Starting at 5:

  • 5,6[7,8]
  • 5,6,7[8]

Since 7+8 isn't prime. Go onto next integer.

Since 5+7 isn't prime. Go onto next integer.

  • 5,8,[6,7]

Since 8+6 or 8+7 isn't prime. You're done with 5.

Starting at 6:

  • 6,5[7,8]
  • 6,5,8[7]

Since 7+8 isn't prime. Go onto next integer.

  • 6,7[5,8]

Since 7+5 or 7+8 isn't prime. Go onto next integer.

Since 6+8 isn't prime. You're done with 6.

Starting at 7:

  • 7,6[5,8]
  • 7,6,5[8]
  • 7,6,5,8

End since you found the prime sequence.

So how can I do this problem with backtracking?


回答1:


The idea of backtracking in this context is basically this: Let me find a continuation of a subsequence (or a prefix) of the total thing that works. If I'm successful I'll return that continuation to my caller. If I'm out of luck, I'll ask my caller to try a different prefix.

More precisely:

// call initially as Backtrack(epsilon, all numbers)
Backtrack(Sequence fixedPrefix, Set unbound) {
  if (unbound is empty) {
    return check(fixedPrefix);
  }
  for all (element in unbound) {
    if (Backtrack(concat(fixedPrefix,element), setminus(unbound,element))
      return true;
  }
  return false;
}

Note that this algorithm will only tell you whether a sequence exists (and its straightforward implementation in C++ would be horribly slow) but not what that sequence is, but that's trivial to modify.

Anyway the "back" in backtracking happens at the very last line where the recursive call is out of luck: this will prompt the calling instance to try another prefix, so the control flow is a bit reversed.




回答2:


Your function (pseudo-code or not) does not do anything productive. I am actually unsure of what it is supposed to do. ie. u = 0; immediately followed by if(u == 4); and your isPrime function is always passed (Integers[0]+integers[0]).

I believe that what you are referring to as backtracking is more appropriately referred to as a recursive function (a function that can call itself). Backtracking is a poor (vague) name for a particular behavior which a recursive function can exhibit.

If you want a recursive function as such, you need to scrap this and start over. Write out what the function should do in plain english (or other language). Then code to that once you know what you need to pass to it, the difference between failure and success, and what to return upon failure or success (how the vector needs to be modified upon a failure).

Super Hint: for small selections of integers, such as you present, try out next_permutation() in the stl < algorithm > to quickly go through the possible arrangements of ints in your vector.



来源:https://stackoverflow.com/questions/10098594/backtracking-algorithm-for-prime-sequence

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!