Introduction to Combination Algorithm (1)

xiaoxiao2021-03-06  73

[Original]

ZENGYI820

2003-06-14 Introduction to Combination Algorithm (A Brief Introduction To Combinator Algorithm)

The combination algorithm is a very important branch in algorithm analysis. I will not say about it in computer science, I will finish the entire material, the algorithm is what I collected, just a simple introduction, then I put me The material has made a finishing, everyone collects, it feels very useful, I have a long time and energy, I am ready to postively study, not too much time to send a lot of classic articles, this piece is big.

About the algorithm of combined problem, the calculation object is a discrete, limited mathematical structure. From the perspective of methodology, the combined algorithm includes two aspects of the algorithm design and algorithm analysis. Regarding the design of algorithms, a number of methods and techniques with universal significance have been summed up, including dynamic planning, backtracking, branch limit, grahe methods, greedy methods. Below we focus on several representative combination algorithms:

Single form:

This is a linear planning algorithm, which is proposed by G.B. Dantzig in 1947, and later he and other scholars have proposed a simple formation and improvement. These have proven to be effective, linear planning research linear target functions in a set of linear equations and linear inequality constraints. This is a continuous problem, and Dantzig discovers the feasible solution of linear planning issues (that is, all of the constraints) is a super polyhedron. If its best solution exists, this optimal solution must be taken at a vertex of the super multifisa. Since the vertex of the super polyhedron is only limited, linear planning is a group and optimization problem. The simple form is to transfer from a vertex from a veins to another according to a certain rule, making the value of the target function constantly improve, and finally reaches optimal. Although the simple method has been used very well, it requires an index run time in worst case, so that the linear planning issue belongs to the Plass, the problem of being concerned. The subsequent ellipsoid algorithm and the projection algorithm have solved this problem.

Sort and search:

These two parts should be more familiar, so-called sorting, the given element sequence is rearranged in an ordered sequence in accordance with some sequential relationship. For example, sequences of n numbers are rearranged in order from small to large sequential; sequence consisting of n English words is rearranged in the order of dictionary. The so-called retrieval is to find a particular element or an element group in a given collection. Sorting and retrieving has become the most basic and frequently used algorithm in computer science technology. Below we specialize in the sorting algorithm.

When this algorithm is discussed, the data is usually referred to by a file consisting of several records, each record contains one or more data items, wherein the data item that can be marked is referred to as a key code. The n record {R1, R2, ..., RN} of a given file and the set {K1, K2, ..., KN} thereof. The so-called sorting algorithm is the algorithm that requires a certain order of records in the file in the file in the data processing. If the file to be sorted can be loaded into the main memory of the computer, the entire sorting process is not required to access the exemption, and it is called the internal sorting; if the number of recorded records is large, the sequence of the entire sequence The process is impossible to complete in memory, some must be placed on the outside, saying that such sorting problems are externally sorted. When the recorded file is included in the file, if the relative order of the records of the same key code remains unchanged after being sorted, the corresponding sorting algorithm is stable, otherwise it is unstable. If the sorting algorithm is designed to be completed, this sorting algorithm is called a serial (or order) sorting algorithm; if the sorting algorithm is designed to be implemented, it is called a parallel sorting algorithm.

First talk

Internal Sort: Internal Sorting Process is a process of gradually expanding recorded ordered sequence length. During the sorting process, there are two regions in the sorted recording sequence: an ordered zone and a disorder zone. Adding one or several operations in the number of ordered zones is called a sort.

Methods to gradually expand the length of the sequence of sequences, there are several categories:

One. Insert order

It is assumed that during the sorting process, the state of the recording sequence R [1..n] is:

Then, the basic idea of ​​direct insertion sorting is: will record R

Inserted into a sequence sequence r [1..i-1], enabling the recorded ordered sequence from R [1..i-1] to R [1..i].

Obviously, complete this "insert" needs to be divided into three steps:

1. Find R's insertion position J 1;

2. Make a position behind the record in R [J 1..i-1];

3. Copy R to the position of R [J 1].

[I] Direct insertion sort

Insert sorting of "Inserting Location in R [1..i-1] is used in sequence lookup.

Pay attention to the three main points of direct insertion algorithm:

1. During the order of order from R [i-1], the monitoring whistle is set in R [0];

R [0] = r; // Set "Sentry"

For (j = i-1; r [0] .Key

Return J 1; // Returns R's Insert Location to j 1

2. For records found in the lookup process, it is possible to move backward while finding.

For (j = i-1; r [0] .Key

R [J 1] = R [J]

3. I = 2, 3, ..., n, realize the sort of the entire sequence.

Template

Void Insertionsort (Elem R [], INT N)

{

// Direct insertion sorting of the recording sequence r [1..n].

For (i = 2; i <= n; i)

{

R [0] = r; // Copy to monitor whistle

For (j = i-1; r [0] .Key

R [j 1] = r [j]; // Record after shift

R [J 1] = R [0]; // Insert to the correct position

}

} // insertsort

Time analysis:

There are two basic operations that achieve sorting:

(1) The size of two keywords in the "comparison" sequence;

(2) "Mobile" record.

Sort by direct insertion:

[II] Folding semiconeration

Because R [1..i-1] is an ordered ordered sequence in keyword, you can use a folder search to implement "insertion of R to find R in R [1..i-1], which is implemented Insert sorting is a folded sequencing. Its algorithm is as follows:

Template

Void Biinsertionsrth (Elem R [], INT N)

{

// Sedimentation of the recording sequence R [1..n] is folded sequentially.

For (i = 2; i <= L.LENGTH; i)

{

R [0] = r; // Temporal q to R [0]

Low = 1; high = i-1;

While (low <= high)

{// Find inserted in the R [low..high]

m = (low high) / 2; // folded

IF (r [0] .Key

High = m-1; // Insert point in low zone

Else

Low = m 1; // Insertion point in a high half zone

}

For (j = i-1; j> = high 1; --j)

R [j 1] = r [j]; // Record after shift

R [high 1] = r [0]; // insert

}

} // binsertsort

Folding sequencing is significantly reduced by direct insertion sorting significantly reduced the "comparison" number of keywords, but the number of times that records "moving" is unchanged.

[III] Table Insert Sort

In order to reduce the operation of "moving" records during the sorting process, the storage structure used during the sorting process must be changed. Use a static linked list to sort and adjust the positions between each record mutually adjusted once, and each record is adjusted to each of them should be in place.

The algorithm is described as follows:

Template

Void Linsertionsrth (Elem SL [], INT N)

{

// Insert the recording sequence SL [1..n] as a table.

SL [0] .Key = maxint;

SL [0] .Next = 1; SL [1] .next = 0;

For (i = 2; i <= n; i)

For (j = 0, k = SL [0] .next;

SL [k] .key <= sl.key; j = k, k = sl [k] .next)

{SL [J] .next = i; sl.next = k;}

/ / Node i is inserted between node J and node K

} // linsertions

Regarding the adjustment record sequence after sorting:

Three poins in the algorithm:

Where: p indicates the current position of the i-th record;

i indicates the position where the i-th record should be;

q Indicates the current location of the i 1 record

Template

Void Arrange (SlinkListType SL [], int N) {

/ / According to the correction value of each node in the static linked list

// Record position, so that SL is recorded by keyword non-decrement

// Order order

P = SL [0] .next; // p Indicates the current location of the first record

For (i = 1; i

// SL [1..i-1] records have been arranged in ordered in keywords,

// The current position of the i-i record in SL should not be less than i

While (P

/ / Find the i-th record and use P to

/ / It is currently in SL

Q = SL [P] .next; // q Indicates the end of the end

IF (p! = i) {

SL [P] ← → SL; / / exchange record, make the i

// Record in place

Sl.next = p; // Point to the removed record,

/ / Make it from the While cycle

}

P = q; // p indicates that the end of the end of the end,

/ / Preparation for finding the i 1 record

}

} // arrange

two. The exchange sort is obtained by the record in the "exchange" disorder sequence to obtain the smallest or maximum record, and add it to the order in which the recording is increased in this manner;

[I] Bubbling Sort

It is assumed that during the sorting process, the status of the recording sequence R [1..n] is: N-I 1

Then, the basic idea of ​​the first bubble insertion sorting is: "exchange" to the keyword record "exchange" to R [N-I 1] in the keyword "exchange" operation in the record of the record in the disorder sequence Position. Algorithm Description:

Template

Void Bubblesort (Elem R [], INT N)

{

// i indicates the location of the last record in the disorder sequence

i = n;

While (i> 1) {

LastexchangeIndex = 1;

For (j = 1; j

IF (a [j 1]

SWAP (A [J], A [J 1]);

LastexchangeIndex = J;

} // if

} // for

i = lastexchangeindex;

} // while

} // bubblesort

The end conditions of the bubble sorting are: the last trip is not "exchange".

From the process of bubble, the bubbling sort is a process of increasing the length of the order sequence, and is also a process reduced sequencing length, each after a foam, and the length of the unordered sequence is only reduced by 1. We can imagine that if you can sort, the length of the disorder sequence is reduced, and the speed of sorting will be accelerated.

[Ii] a quick sort

Objective: Find a record, with its keyword as a "pivot", where its keyword is smaller than the pivot record, before the recording is moved, but in turn, the recording of the keyword is greater than the pivot is moved to the record. After the sorting, the recorded disorder sequence R [S. will be divided into two parts: R [S..i-1] and R [i 1... ", and R [J]. Key ≤ R.Key ≤ r [J] .key

(s≤j ≤ i-1) pivot (i 1≤j ≤ T)

For example: keyword sequence

52, 49, 80, 36, 14, 58, 61, 97, 23, 75

Adjustment to: 23, 49, 14, 36, (52) 58, 61, 97, 80, 75

The (52) is a pivot, during the adjustment process, two pointers are required: Low and HiGH, their initial values ​​are: s and t, respectively, gradually reduce HIGH, increase the Low, and ensure R [high]. Key ≥ 52, and r [low] .Key ≤ 52, otherwise "exchange" of records.

The algorithm is described as follows:

Template

INT Partition (Elem R [], int low, int high) {

// Exchange record subsequences R [Low..high] records, make

/ / The pivot is recorded in place and returns its location, at this time,

// The records before it (after) are not large (small)

PivotKey = r [low] .key;

// The first record of the sub-table is pivotal record

While (low

// San from both ends of the table

While (low = pivotkey)

--h;

R [low] ← → r [high];

// Switch to the low end than the pivot record

While (Low

low;

R [low] ← → r [high];

// Switch to high ends than the pivot record

}

Return Low; // Return to the pivotial location

} // partition

It is easy to see that the pivot position during the adjustment process is not important. Therefore, in order to reduce the number of movements of the record, the pivot record "removal" should be "removed", and the pivot record should be in the position (now Low = HIGH), then record the pivot. Rewrite the algorithm of the above "one division" as follows:

Template

INT Partition (Elem R [], int low, int high) {

// Exchange record subsequences R [Low..high] records, make

/ / The pivot is recorded in place and returns its location, at this time,

// The records before it (after) are not large (small)

R [0] = r [low];

// The first record of the sub-table is pivotal record

PivotKey = r [low] .key; // pivot record keyword

While (low

// San from both ends of the table

WHILE (Low

= PivotKey)

--h;

R [low] = r [high];

// Move the record that is small than the pivot record to the low end

While (Low

low;

R [high] = r [low];

// Moved to the high end than the pivot record

}

R [low] = r [0]; // pivot record in place

Return Low; // Return to the pivot position

} // partition

[III] Quick Sort

After a segmentation is performed in the disorder sequence, the two subsequences of the segmentation result are rapidly sorted, respectively, and are pushed to each subsequence, only one record is contained in each subsequence. Quick sorting algorithm is described below:

Template

Void Qsort (Elem R [], int low, int high) {

// Quickly sort the record sequence r [low..high]

IF (Low

Pivotloc = Partition (L, Low, HIGH);

// Take the l.r [low..high] into two

Qsort (L, Low, Pivotloc-1);

/ / Sort by the low child table, PivotLoc is a pivot position

Qsort (L, Pivotloc 1, High);

// Remissive to high sub-table

}

} // qsort

Template

Void Quicksort (ELEM R [], INT N) {

// Quickly sort the record sequence

QSORT (R, 1, N);

} // quicksort

Rapid sorting time analysis

Assume that the resulting pivot position I = K once, the time required for n records

T (n) = tpass (n) t (k-1) t (n-k)

Where tpass (n) is a division time for N records, the keyword recorded in the sequence is randomly distributed, then k takes the possibility of any value in 1 to n, thereby obtaining The average of the time required for rapid sorting is:

Set TAVG (1) ≤ B

A result

Typically, rapid sorting is considered to be in the sorting method of all equivalence grade O (NLOGN), the average performance is the best. However, if the initial state to be rowed is in order in order in order, rapid sorting will be degraded into bubble sort, and its time complexity is O (N2).

To avoid this situation, you need to "process" before the fast row, namely: compare R (s) .Key, R (t) .Key and R [ (S T) / 2.Key , Then take the keyword "three of them" record as pivot record. three. Select Sort from the smallest or maximum record of the "Select" keyword in the recording, and add it to the order in which the recorded sequence sequence is increased by this method;

[I] Simple selection

Assume that during the ordering process, the state of the row record sequence is:

And the keywords of all records in the ordered sequence are less than the keyword recorded in the disorder sequence, and the second simple selection sort is selected from the N-I 1 record of the unordered sequence R [i..n]. The smallest record is added to the ordered sequence.

Simply selecting the algorithm of sorting is as follows:

Template

Void SelectSort (Elem R [], int N) {

// Simply select the recording sequence R [1..n].

For (i = 1; i

// Select the number I of the record and exchange it in place.

J = SELECTMINKEY (R, I);

/ / Select KEY minimum record in R [i..n]

IF (i! = j) r ← → r [J];

/ / Exchange with the i-th record

}

} // selectsort

Time performance analysis

Simple sequencing of N records, the number of comparison between keywords required is

The number of mobile records, the minimum is 0, the maximum is 3 (N-1)

[II] Pile Sort


New Post(0)