Divide and conquer algorithm:
The simple generalization is to divide the big problems that cannot be solved temporarily into many of the sub-problems of entry.
If the child problem is still unresolved, continue to divide into sub-problems until the sub-problem is small enough to
can be resolved so far, the original problem is the merger of sub-problems.
Partone:
To print an array a[l,......, R] by splitting the method.
Analysis:
A loop is available, but the divide-and-conquer algorithm solves the problem.
If the length of the sequence to be printed is 1, it can be printed directly;
If the length of the sequence to be printed is N, it can be divided into two parts;
The first part is 1, after N-1 is another division, and so on, until the array length is 1.
void print (int a[], int L, int R) {if (L > R) return;else if (L = = r) {cout<< "A[l]" << "";} else{cout<<a[l]<< "";p rint (A, L + 1, R);} }
Parttwo:
Suppose there is a function divid (), you can divide an array a[l,..., R] into two parts,
Element A[l] As the dividing line, elements less than a[l] on the left, elements greater than AH a[l] on the right
The function declaration is as follows:
int divid (int a[], int L, int R);
Try to divide and conquer the method, log the group A[l,... R] To quickly sort elements in
Analysis:
If the length is less than 1, the default is ordered, if the sequence length is greater than 1, use the divID () function
Divide it into 3 parts, the left side is less than a[l], the middle is a[l], the right is greater than
A[L] section. A[L] is ordered, only the left and right parts of the branch are processed.
void Qsort () {int mid;if (L >= R) Return;else{mid = divID (A, L, R); Qsort (A, L, mid); Qsort (A, mid + 1, R);} }
Partthree;
A binary tree is stored in a binary linked list, and all the node values are printed by the split method.
The root node is referred to by P.
Analysis:
When the tree is empty, nothing is done and the problem is solved directly.
When the tree node tree is greater than or equal to 1, the whole tree is divided into root, left subtree,
Right subtree three parts. root node direct printing, to Zuozi continue to divide treatment,
The right sub-tree continues to be divided into treatment, the final printing of the whole binary tree.
Partfour:
A connected graph G is stored with an adjacency table, and all of the fixed vertex values in the graph are printed by the divide-and-conquer method, assuming that printing begins at Vertex V,
You can divide the entire graph into n parts, and then the previous example looks like you can divide the graph into a sub-graph reached by the first edge,
The sub-graph reached by the second edge ... The sub-graph reached by the nth edge. V can be printed directly, and then separate the individual sub-map treatment.
To prevent loop phenomena, you can set an array visited[] to mark the path that has been traversed.
(interpretation of depth-first search)
void Printgraph (agraph *g, int v) {Arcnode *p;visited[v] = 1;cout<<v<< "";p = G->adjlist[v].firstarc;while (P! = NULL) {if (Visited[p->adjvex] = = 0) {printgraph (G, P->adjvex);} p = P->nextarc;}}
Partfive:
The first sequence traversal sequence and middle sequence traversal sequence of known binary tree are stored in a[l1,...., R1], respectively.
B[l2,... R2]; in two arrays, a binary tree is constructed from these two sequences using a divide-and-conquer algorithm,
stored in a binary-linked list storage structure.
Analysis:
If it is an empty sequence, nothing is done. If the length is greater than 1, then the A[L1] bit binary tree
A[L] in b[], assuming that the subscript is K, then B[l2,..... K-1] is on its left sub-tree
All nodes, B[k + 1, .... R2] is the reaction of all the nodes on the right subtree in a[]
A[L1 + 1,... L1 + k-l2] and b[l2, ..., k-1] continue to divide and treat. The tree can eventually be created.
btnode* Create (int a[], int b[], int L1, int R1, int L2, int R2) {int k;if (L1 > R1) return null;else{s = (btnode*) malloc (sizeof (Btnode)); s->data = a[l1];for (k = L2; k < R2; ++k) {if (a[l1] = = B[k]) {break;}} S->lchild = Create (A, B, L1 + 1, L1 + k-l2, L2, k-1); s->lchild = Create (A, B, L1 + K-L2 + 1, R1, K + 1, R2);} }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Application of divide-and-conquer algorithm (thought) in data structure