当前位置:网站首页>Difference between backtracking and recursion
Difference between backtracking and recursion
2022-07-06 06:26:00 【jq_ ninety-eight】
Look at the data structure recently , It is found that many recursion and backtracking problems are used , I really don't know the difference between the two , I checked some information recently , Let's summarize .
In order to describe a certain state of the problem , The previous state of this state must be used , And describe the previous state , You must use the previous state of the previous state …… This method of defining yourself by yourself , Called recursive definition . In the form of f(n) = n*f(n-1), if n=0,f(n)=1.
Starting from one possibility of the problem , Search for all the possibilities that can be achieved from this situation , When this road comes to ” At the end of “ When , Go back to the starting point , From another possibility , Continue to search . This continuous ” to flash back “ The way to find the solution , Referred to as ” backtracking “.
Recursion is an algorithm structure , Recursion will appear in the subroutine, calling itself or indirectly calling itself . The most direct recursive application is to calculate the factorial of continuous numbers , The law of calculation :n!=(n-1)!*n.
Observe the law of factorial calculation , The result of the former number formation can be directly applied to the calculation of the latter number formation .
int fac(int n)
if(n==1)
return n;
else
return n*fac(n-1);
Backtracking is an algorithmic idea , It can be implemented recursively . Generally speaking, backtracking is a kind of Temptation , Similar to exhaustive , But backtracking has “ prune ” function , For example, the summation problem . Given 7 A digital ,1 2 3 4 5 6 7 Sum equals 7 The combination of , Search from small to large , choice 1+2+3+4 =10>7, Already exceeded 7, After that 5 6 7 There's no need to continue , This is an optimization of the search process . If there is anything unclear, you can have a look 8 queens problem .
Link to the original text :https://blog.csdn.net/u014772862/article/details/51789015
边栏推荐
- 模拟卷Leetcode【普通】1061. 按字典序排列最小的等效字符串
- (中)苹果有开源,但又怎样呢?
- Simulation volume leetcode [general] 1143 Longest common subsequence
- Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
- Black cat takes you to learn UFS Protocol Part 8: UFS initialization (boot operation)
- [C language] qsort function
- Database - current read and snapshot read
- B - The Suspects
- Simulation volume leetcode [general] 1218 Longest definite difference subsequence
- Black cat takes you to learn UFS protocol Chapter 4: detailed explanation of UFS protocol stack
猜你喜欢
随机推荐
[postman] the monitors monitoring API can run periodically
B - The Suspects
Simulation volume leetcode [general] 1218 Longest definite difference subsequence
D - How Many Answers Are Wrong
org. activiti. bpmn. exceptions. XMLException: cvc-complex-type. 2.4. a: Invalid content beginning with element 'outgoing' was found
Private cloud disk deployment
Selenium source code read through · 9 | desiredcapabilities class analysis
【MQTT从入门到提高系列 | 01】从0到1快速搭建MQTT测试环境
Simulation volume leetcode [general] 1314 Matrix area and
leetcode 24. Exchange the nodes in the linked list in pairs
F - true liars (category and search set +dp)
通过修改style设置打印页样式
Aike AI frontier promotion (2.13)
University of Manchester | dda3c: collaborative distributed deep reinforcement learning in swarm agent systems
模拟卷Leetcode【普通】1296. 划分数组为连续数字的集合
LeetCode 729. My schedule I
Esp32 esp-idf watchdog twdt
Still worrying about how to write web automation test cases? Senior test engineers teach you selenium test case writing hand in hand
keil MDK中删除添加到watch1中的变量
LeetCode 1200. 最小绝对差