Day 5 9/28/2003
Trace method:
Retrospective recursive is a problem that often appears in the programmer exam. Everyone must master!
Related concepts of backtracking
1) Solution Tree: The leaf node may be solution, and the node is traversed.
2) Search and backtrack
Optional in the five numbers, three solutions (solutions must have three layers, to the junction of the leaves):
Root virtual roots
/ / | / /
1 2 3 4 5
/ | | / / | / / / |
2 3 4 5 3 4 5 4 5 5
/ | / / / | / / | | 345 4 5 5 4 5 5 5
Tips in the realization of returning algorithm: Stack
To engage in the retracting algorithm, first, first (the non-intensive algorithm of the seconduous binary tree), the stack has played in non-recurable.
A Process: Push () -> Push () -> Push () -> Push () Stack Results: Abde (e is the leaves, ending the stack)
/ / POP () ABD (E no child, out of the stack)
B C Pop () ab (D No right child, out of the stack)
// POP () a (B has a right child, right child in the stack)
D f.
///.
E g h.
/.
I final results: edbgfihac
Simple algorithm:
...
IF (r! = null) // tree is not available
{While (r! = null)
{Push (S, R);
R = r-> lch; // has been forwarded to the left child
}
While (! Empty (s)) / / Stack of non-empty, out of the stack
{P = POP (s);
Printf (P-> DATA);
P = P-> rch; / / to the right child
While (p! = null)
{push (s, p);
P = P-> lch; // Right child in the stack
}
}
} // This is the legendary backtrack, 嘻嘻 ... I didn't scare you.
5 selection 3 problem algorithm:
Thought: Put: Search
Find out
Edge tree (in touch) while traversing (out of the stack)
Basic process:
Too complicated, then I don't like to use Word painting (lossy image), and then organize again!
Program: n = 5; r = 3
......
Init (s) // Initialization
Push (s, 1) // Root in the stack
While (S.TOP Push (S, S.Data [S.TOP] 1); // Children Find While (! EMPTY (s)) {IF (S.TOP = R-1) Judging whether the "solution" is explained. X = POP (s); // Reserved x, determine if it is the maximum value n, if it is n, then the stack While (x == n) X = POP (s); Push (S, X 1); While (S.TOP } Backpack problem: TW = 20, w [5] = {6, 10, 7, 5, 8} Solution: 1) The leaf node of this solution tree 2) the largest weight Solution tree is as follows: root / | | | / 6 10 7 5 8 / | | / / | / / / | 10 7 5 8 7 5 8 5 8 8 | | | | | 5 8 8 program: TEMP_W indicates the weight of the stack and ... INIT (S); // Initialization i = 0; While (W [i]> TW) i ; IF (i == n) return -1; // Else { Push (s, i); Temp_w = w [i]; i ; While (i {Push (S, I); TEMP_W = W [I]; i ; } MAX_W = 0; While (! EMPTY (s)) {IF (MAX_W MAX_W = TEMP_W; X = POP (s); Temp_w- = w [x]; X ; While (x X ; WHILE (x {Push (s, x); TEMP_W = TEMP_W W [x]; X ; While (x X ; } Please think: four-color map problem, such as painting China map, four colors, one color, neighboring province can't take the same color. Do not lazy, you can't choose the population of not more than xxxxw "Big country" Oh! If there is a day of Taiwan, it is the case, and a color is sent, but Taiwan's programmers earn, save trouble! Ha ha. Greedy method: Do not ask for the best solution, fast speed (with accurate speed) Example: Hufman tree, minimum spanning tree Packing question: There are N items, the weight is w [N], and the N items are loaded into a container that is heavy as a TW, requires several containers? Thought 1: Sort by N items Take out the first container, from the big to small judgment, can not be put. 2 … 3 ... ... ... Thought 2: Sort N items To determine if a new box is needed, if the weight of the item is less than the load weight left, it is put in, and it will be taken into one new box. program: Count = 1; QW [0] = Tw; For (i = 0; i { K = 0; While (K K ; IF (w [i] <= qw [k]) Qw [k] = QW [k] -w [i]; Code [i] = k; // 第 第 第 物 子 子 Else {count ; // Take a new box Qw [count-1] = TW-W [i]; Code [i] = count-1;} } Use greed to understand the backpack problem: N items, weight: w [n] Value V [I] The backpack is limited to TW, and the design is designed to make the total value. method: 0 1 2 3 ... N-1 W0 W1 W2 W3 ... W (N-1) V0 V1 V2 V3 ... V (N-1) V0 / W0 ... V (N-1) / w (n-1) to find the "cost performance" of each item Sort by high to low according to high cost ratio Known: w [n], v [N], TW program: ... For (i = 1; i D [i] = v [i] / w [i]; // seek performance price For (i = 0; i {MAX = -1; For (j = 0; j {IF (D [J]> max) {MAX = D [j]; x = j;} } e [i] = x; D [x] = 0; } TEMP_W = 0; TEMP_V = 0; For (i = 0; i {IF (TEMP_W W [E [I]] <= T Temp_v = temp_v v [e [v]]; } Ministry of treatment: Thought: Decompose the problem of N, decompose into several small-scale problems. Then, based on the solution to the small scale, it is combined into this problem. Example: There are n points x [n] on the plot, and the minimum distance is minimized. Points: Cut a point, you can divide X [i] this into two parts Small partial point | _._.__.__.____.____.___________.__._._.__________._ | Treatment: Solution = minimum distance minimum of min {small part; Large part of the distance minimum; Big part of the minimum spots and small parts of the largest point difference;}