Preliminary knowledge of backtracking method and sort sorting

1. Backtracking method

1.1 Preface

Today, I met a problem solved by backtracking. Let's record the simple usage of backtracking.

1.2 introduction

The backtracking method is simply to enumerate all possible algorithms according to the order of depth first, but the better part of the backtracking algorithm than the violent exhaustive method is that the backtracking algorithm can judge whether the current state meets the conditions of the problem at any time. Once the conditions are not met, it will return to the previous state, eliminating the time to continue to explore.
The return of status will be returned only when the current node no longer meets the conditions of the problem or we have found a solution to the problem. Otherwise, it will be traversed in the solution space tree with depth first.
Of course, for some problems, if the solution space is too large, the backtracking method also has high time complexity, because the backtracking method will try to solve all branches in the space tree. Therefore, according to this kind of problem, we have some optimization pruning strategies and heuristic search strategies.
The so-called optimal pruning strategy is to judge whether the current branch tree meets the conditions of the problem. If the current branch tree does not meet the conditions, it will no longer traverse all paths in the branch.
The so-called heuristic search strategy refers to setting a priority for the order of backtracking search sub nodes, and traversing down from the sub node is more likely to find the solution of the problem.

1.3 function composition

1. Go back to the exit and store the solution of a problem when it is found.
2. The backtracking entity is to traverse all child nodes of the current state and judge whether the next state meets the problem conditions. If the problem conditions are met, it enters the next state.
3. Status return. If the current status does not meet the conditions, it will return to the previous status.

def backtrack(current_statement) -> bool:
	if condition is satisfy:
		solution = current_statement
		return True
		for diff_posibility in current_statement:
			next_statement = diff_posibility
			if next_statement is satisfy condition:
				if backtrack(next_statement):
					return True
					back to current_statement
		return False

1.4 title

  1.5 realization

class Solution
    int n;
    string digits1,s;
    vector<string> letter;
    vector<string> letters= { "", "", "abc", "def", "ghi", "jkl",
                                 "mno", "pqrs", "tuv", "wxyz" };
    vector<string> letterCombinations(string digits)
        n = digits.size();
        if (digits.empty())
            return letter;
        digits1 = digits;
        return letter;
    void backtrack(int i)
        if (i == n) letter.push_back(s);
            for (int j = 0; j < letters[digits1[i] - '0'].size(); j++)
                s += letters[digits1[i] - '0'][j];
                backtrack(i + 1);


2.1 header file


2.2 application method

The sorting method used is similar to the fast scheduling method, and the time complexity is o(nlog(n))

The Sort function has three parameters: (the third parameter can not be written)
(1) The first is the starting address of the array to be sorted.
(2) The second is the end address (the last address to sort)
(3) The third parameter is the sorting method, which can be "descending" or "ascending",
The third parameter can be omitted. The default sorting method is ascending.

It has three parameters sort(begin, end, cmp), where begin is the pointer to the first element of the array to be sort(), end is the pointer to the next position of the last element of the array to be sort(), cmp parameter is the sorting criterion, and cmp parameter can not be written. If it is not written, it is sorted from small to large by default.

2.3 customization

If we need to follow other sorting criteria, we need to define a bool type function to pass in.

2.4 simple implementation code

using namespace std;

int main(){
	int num[10] = {6,5,9,1,2,8,7,3,4,0};
	for(int i=0;i<10;i++){
		cout<<num[i]<<" ";
	}//Output result: 9 8 7 6 5 4 3 2 1 0
	return 0;

Tags: C++ Algorithm

Posted on Sun, 05 Sep 2021 17:39:24 -0400 by wizzard81