Algorithm--Recursive strategy

Source: Internet
Author: User
This article address: http://www.cnblogs.com/wuyudong/p/algorithm-recursion.html, reprint please indicate source address the concept and basic idea of recursion

A function, procedure, concept, or mathematical structure that, if it appears directly or indirectly within its definition or description, has its own reference, it is said to be recursive or recursively defined. In programming, a procedure or function calls itself directly or indirectly, and is called a recursive call. implementation of Recursive method

Recursion is achieved by means of a recursive work stack; recursion = recursion + regression;

Recursion: The problem advances to a pole, this process is called recursion, this process is equivalent to the stack.

Regression: The problem is solved, and finally back to the original problem, this process is called regression. This process is equivalent to a stack.

For example: Using recursive algorithm to find n!

Definition: function fact (n) =n!

Fact (n-1) = (n-1)!

Then there is fact (n) =n*fact (n-1)

Known fact (1) =1

The following diagram shows the recursive schematic of the call and return:

The cost of recursive implementation is a huge amount of stack space, because the process of each forward recursion, the program will be this layer of the actual variables (value parameters and arguments), local variables constitute a "work record" into the stack top of the work stack, only to exit the layer recursion, only to eject this work record from the top of the stack to release some space. It can be thought that reducing the size of each "work record" can save some space. For example, some arguments can be converted to global variables, some value parameters can be omitted, and the interior of the process is streamlined.

"Examples" to write the results

#include <iostream>
using namespace std;
void Rever ()
{
    char C;
    cin>>c;
    if (c!= '! ') rever ();
    cout<<c;
}
int main ()
{
    rever ();
    System ("PAUSE");
    return 0;
}

"Sample Input" Gnauh. "Sample Output"!huang

The problem-solving program written by recursive method has the advantages of clear structure, strong readability and so on, and the design of recursive algorithm is often easier than that of non-recursive algorithm, so when the problem itself is recursive definition, or the problem involves the data structure is recursive definition, or the solution of the problem is recursive form, Recursive algorithms are often used to solve the problem. types of recursive algorithms

Recursive algorithms can be divided into two types:

Recursive algorithm based on divide-and-conquer strategy;

A recursive algorithm based on backtracking strategy. recursive algorithm based on divide-and-conquer strategy

Divide and Conquer (Divide-and-conquer) algorithm

Design ideas:

1.Divide: Divide the problem into several sub-problems;

2.Conquer: The same way to deal with each sub-problem;

3.Combine: The processing result of each sub-problem is synthesized to form the final processing result.

How to write a recursive program based on the divide-and-conquer strategy.

In the algorithm analysis, we should set up the thinking mode of divide and conquer recursion.

In the programming implementation, to establish recursive confidence (to turst the recursion, Jerry Cain, Stanford).

How to set up the thinking mode of splitting and administering recursion.

Basic principle: target-driven.

Calculate n!:n! = n * (n-1)!, and 1! = 1.

int  Main ()
{
        int   n;
        printf ("Please enter an integer:");
        scanf ("%d",   &n);
        printf ("%d!  =  %d ",   N,   fact (n));
        return 0
}
int   fact (int  N)
{
        if (n  = =  1)    return (1);
        else     return (n * fact (n-1));
}

How to build recursive confidence.

How does a recursive invocation of a function work? When recursive calls are performed, the code is not the same. Access is not the same data. If it is, then we will not interfere with each other and hinder each other.

Example: Finding the maximum value

Problem Description: Given an integer array A, find the maximum value.

How to design the corresponding recursive algorithm.

Target: Max{a[0], a[1], ... a[n-1]}

Can be decomposed into: max{a[0], max{a[1], ... a[n-1]}}

Also known max{x} = X

This is the recursive form of the recursive algorithm and the recursive boundary, according to

This can be written with the corresponding recursive function:

int max (int a[], int first, int n)
{
    int  max;

    if (first = = n-1)  return A[first];
    max = Max (A, first+1, n);
    if (Max < A[first])
          return A[first];
    else  return max;
}

Binary Lookup method

Problem Description:

Find (searching): Based on a given value, in a set of data (especially an array), determine whether there are data elements that have the same value.

Sequential lookups, binary lookups.

int bsearch (int b[], int x, int L, int R)
{
    int mid;
    if (L > R) return ( -1);
    Mid = (L + R)/2;
    if (x = = B[mid]) 
        return mid;
    else if (x < B[mid]) 
        return bsearch (b, X, L, mid-1);
    else 
        return bsearch (b, X, mid+1, R);
}

Hanoi (Hanoi) Tower problem

According to legend, in the ancient Hindu temple of Bramah, a monk poured the gold plate of the three pillars all day, and he was trying to move 64 more than a small gold plate from one pillar to another. The following rules are observed during the move: only one disk is allowed to be moved at a time, and the market must not fall on a small plate (simple. If you move a plate every second, it takes 580 billion years)

Analysis:

There are n plates on column A, from small to large, No. 1th, 2nd, 3rd 、...、 N.

1th Step: 1th, 2nd 、...、 n-1 as a whole, with the help of C, from a to B;

2nd step: Move the n number from a to C;

The 3rd step: the number 1th, 2nd 、...、 n-1 as a whole, with the help of a, from B to C;

These three steps are recorded as:

Move n-1 discs from A to B using C;

Move 1 discs from A to C;

Move n-1 discs from B to C using A; The code is as follows:

#include  <stdio.h>
void   Move (int n, char L, Char M, char R);
int main ()
{
        int   n;
        printf ("Please enter an integer:");
        scanf ("%d",  &n);
        Move (n,  ' A ',  ' B ',  ' C ');
        return 0;
}
//

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.