views:

1143

answers:

8

So I've had at least two professors mention that backtracking makes an algorithm non-deterministic without giving too much explanation into why that is. I think I understand how this happens, but I have trouble putting it into words. Could somebody give me a concise explanation of the reason for this?

+7  A: 

I'll just quote wikipedia:

A nondeterministic programming language is a language which can specify, at certain points in the program (called "choice points"), various alternatives for program flow. Unlike an if-then statement, the method of choice between these alternatives is not directly specified by the programmer; the program must decide at runtime between the alternatives, via some general method applied to all choice points. A programmer specifies a limited number of alternatives, but the program must later choose between them. ("Choose" is, in fact, a typical name for the nondeterministic operator.) A hierarchy of choice points may be formed, with higher-level choices leading to branches that contain lower-level choices within them.

One method of choice is embodied in backtracking systems, in which some alternatives may "fail", causing the program to backtrack and try other alternatives. If all alternatives fail at a particular choice point, then an entire branch fails, and the program will backtrack further, to an older choice point. One complication is that, because any choice is tentative and may be remade, the system must be able to restore old program states by undoing side-effects caused by partially executing a branch that eventually failed.

Out of the Nondeterministic Programming article.

Johannes Schaub - litb
Congrats. A "read wikipedia" answer that didn't make me feel totally dumb. :-)
Jason Baker
that doesn't make the system undeterministic, I'd go with Mark Harrison's answer: it's a way to _simulate_ (a theoretical) non determenistic machine.
hasen j
hasen j, what system do you mean? backtracking of course can only simulate non-determinism, as long as it runs on a deterministic computer. a prime example is a regular expression library. they take non-deterministic description, and have to transform those into deterministic state machines.
Johannes Schaub - litb
but the transformation will do the exact same thing as the non-deterministic description of the regular expression (take "a|b" as an example).
Johannes Schaub - litb
well, you said it, the computer is deterministic (assuming no human input), so any code that runs on it runs in a deterministic fashion.
hasen j
hasen j. yeah. but that's not a restriction of backtracking itself. it's a restriction of the machine it runs on :)
Johannes Schaub - litb
it's more like a restriction of physics :) My point is that your answer (or its wording, at least) implies that the deterministic machine can act in non-deterministic ways simply because it performs backtracking; which is not true.
hasen j
+12  A: 

It's not so much the case that backtracking makes an algorithm non-deterministic.

Rather, you usually need backtracking to process a non-deterministic algorithm, since (by the definition of non-deterministic) you don't know which path to take at a particular time in your processing, but instead you must try several.

Mark Harrison
+3  A: 

Consider an algorithm for coloring a map of the world. No color can be used on adjacent countries. The algorithm arbitrarily starts at a country and colors it an arbitrary color. So it moves along, coloring countries, changing the color on each step until, "uh oh", two adjacent countries have the same color. Well, now we have to backtrack, and make a new color choice. Now we aren't making a choice as a nondeterministic algorithm would, that's not possible for our deterministic computers. Instead, we are simulating the nondeterministic algorithm with backtracking. A nondeterministic algorithm would have made the right choice for every country.

geowa4
see my comments on litb's answer
hasen j
+1  A: 

Thought experiment:

1) Hidden from view there is some distribution of electric charges which you feel a force from and you measure the potential field they create. Tell me exactly the positions of all the charges.

2) Take some charges and arrange them. Tell me exactly the potential field they create.

Only the second question has a unique answer. This is the non-uniqueness of vector fields. This situation may be in analogy with some non-deterministic algorithms you are considering. Further consider in math limits which do not exist because they have different answers depending on which direction you approach a discontinuity from.

Alex
A: 

If you allow backtracking you allow infinite looping in your program which makes it non-deterministic since the actual path taken may always include one more loop.

jeffD
-1, what does infinite looping have to do with determinism??
hasen j
A: 

Non-Deterministic Turing Machines (NDTMs) could take multiple branches in a single step. DTMs on the other hand follow a trial-and-error process.

You can think of DTMs as regular computers. In contrast, quantum computers are alike to NDTMs and can solve non-deterministic problems much easier (e.g. see their application in breaking cryptography). So backtracking would actually be a linear process for them.

Eduard - Gabriel Munteanu
+1  A: 

The running time of backtracking on a deterministic computer is factorial, i.e. it is in O(n!).

Where a non-deterministic computer could instantly guess correctly in each step, a deterministic computer has to try all possible combinations of choices.

Since it is impossible to build a non-deterministic computer, what your professor probably meant is the following:

A provenly hard problem in the complexity class NP (all problems that a non-deterministic computer can solve efficiently by always guessing correctly) cannot be solved more efficiently on real computers than by backtracking.

The above statement is true, if the complexity classes P (all problems that a deterministic computer can solve efficiently) and NP are not the same. This is the famous P vs. NP problem. The Clay Mathematics Institute has offered a $1 Million prize for its solution, but the problem has resisted proof for many years. However, most researchers believe that P is not equal to NP.

A simple way to sum it up would be: Most interesting problems a non-deterministic computer could solve efficiently by always guessing correctly, are so hard that a deterministic computer would probably have to try all possible combinations of choices, i.e. use backtracking.

mdm
Edited it to note that the algorithms are factorial rather than exponential.
Jason Baker
In terms of complexity classes they are exponential (i.e. in EXPTIME). Also, most O(n!) problems can be solved in O(N^2*2^N) using dynamic programming. But if you find it easier to understand, than it's Ok with me.
mdm
A: 

I wrote a maze runner that uses backtracking (of course), which I'll use as an example.

You walk through the maze. When you reach a junction, you flip a coin to decide which route to follow. If you chose a dead end, trace back to the junction and take another route. If you tried them all, return to the previous junction. This algorithm is non-deterministic, non because of the backtracking, but because of the coin flipping.

Now change the algorithm: when you reach a junction, always try the leftmost route you haven't tried yet first. If that leads to a dead end, return to the junction and again try the leftmost route you haven't tried yet. This algorithm is deterministic. There's no chance involved, it's predictable: you'll always follow the same route in the same maze.

stevenvh