Recursive Algorithm Learning Series 1 (divide and conquer strategy)

Source: Internet
Author: User
    1. Divide and conquer

Instead, we use recursion to solve the problem.AlgorithmThe main technique is to divide a large and complex problem into multiple subproblems. These subproblems can be used as termination conditions or solved in a recursive step, the combination of solutions to all sub-problems constitutes a solution to the original problem.

2. Advantages and Disadvantages of divide and conquer

The divide-and-conquer algorithm usually involves calling one or more recursive methods. When these calls separate data into independent sets to process smaller sets, the divide-and-conquer policy will be very efficient, when data is decomposed, the divide-and-conquer policy may produce a large number of repeated computations, resulting in performance reduction.

3. Draw a rulerProgramAnalysis

A ruler is a simple application of a divide-and-conquer strategy. A ruler is a sequence composed of 1 inch units. Each unit has the longest mark at its end, the mark at 1/2 inch of each inch unit is shorter than the end, the mark at 1/4 is shorter than 1/2, and the mark at 1/8 is shorter than 1/4. Write a program, in an online line, you can use the rule interval to draw a tag, with a specific size mark at a specific position.

Analysis: in a straight line, we can first split this line into two parts, and then split the two parts. Until the accuracy requirements are met. For example, taking the minimum scale as 1/8 inch as an example, drawruler serves as the regression function for the draw scale, and uses the two ends of a line segment (startpos) in the drawruler function ), endpoint (endpos), and variable H are used as parameters. The base height of the tag is baseheight,

The height of the mark should be H * baseheight, and the method of the ruler can be analyzed as follows:

The midpoint of the calculation interval (0.0, 1.0): midpos = (startpost + endpos)/2; draw a mark at the midpoint 1/2, and the height is 3 * baseheight

Separate the midpoint by two straight lines, and then use the nth function drawrule, corresponding start point, end point (0.0, 0.5) and (0.5, 1.0), parameter h-1, in this way, the height can be shorter.

Step 2 (H = 2)

Midpos = (0.0 + 0.5)/2 (1/4), with a height of 2 * baseheight

The height of midpos = (0.5 + 1.0)/2 (3/4) is 2 * baseheight.

Step 1 (H = 1)

Mark at 1/8 and 7/8 respectively. Calculation Method

The height of midpos = (0.0 + 0.25)/2 (1/8) is baseheight.

The height of midpos = (0.75 + 1)/2 (7/8) is baseheight.

As shown in the following figure:

We can regard the mark produced by continuous regression as a node of a binary tree. Root H is the initial value. It is the mark at 1/2, and each parent mark generates two child marks. As shown in

 

4. executable program file

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;

Namespace drawr.pdf
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}

Private void form1_load (Object sender, eventargs E)
{
}

Void drawruler (float startpos, float endpos, int H)
{
Float baseheight = 4;
If (h> 0)
{
Float midpos = (startpos + endpos)/2;
Float Height = H * baseheight;
Drawmark (midpos, height );
Drawruler (startpos, midpos, h-1 );
Drawruler (midpos, endpos, h-1 );
}
}

Void drawmark (float POs, float height)
{
Using (Graphics G = This. creategraphics ())
{
Float xoffset = 100 + Pos;
Float yoffset = 100-height;
Solidbrush brusuh = new solidbrush (color. Black );
Pen P = new pen (brusuh, 1 );
G. drawline (p, xoffset, yoffset, xoffset, 100 );
}
}
Private void form1_paint (Object sender, painteventargs E)
{
# Region draw a straight line first
Using (Graphics G = E. Graphics)
{
Float xoffset = 100;
Float yoffset = 100;
Int Len = 300;
Solidbrush brusuh = new solidbrush (color. Black );
Pen P = new pen (brusuh, 2 );
G. drawline (p, xoffset, yoffset, xoffset + Len, yoffset );
}
# Endregion

Drawruler (0,300, 3 );
}
}
}

/Files/jillzhang/drawruler.rar

5. Learning Summary

This series is a learning series of algorithms. All the content is from textbooks and is not original. Only programs are implemented for individuals. For more details, refer to the data structure C ++ description.

-----------------------------------------------------
A person is old and cannot use his head. He occasionally uses algorithms to train his mind to prevent premature aging. Haha
jillzhang jillzhang@126.com

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.