DELAPARRA
Index
Color
NL
Category: Programming paradigms

Backtracking

Explained by Updated

What is Backtracking?

Backtracking: When a chosen path fails to satisfy every goal, the engine automatically rewinds and tries the next alternative. Backtracking is a search strategy. The program tries an option, and if it leads to a dead end, it goes back to the last choice point and tries the next option. Logic languages like Prolog do this automatically. Regular expression engines use it too: when part of a pattern fails, the engine gives back characters and tries another way to match.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Backtracking

What should you keep in mind with Backtracking?

That second case has a practical risk. Some regex patterns, such as nested repetition like (a+)+$, can backtrack an enormous number of times on certain inputs. A short malicious string can then keep a server busy for seconds or minutes, which is known as ReDoS. Avoid nested quantifiers, test patterns with long non-matching input, and set limits where your language offers them.

At a glance

CategoryProgramming paradigms
Explained by
Updated

More Programming paradigms terms

All terms starting with B

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.