/* Topic: connectivity * Problem description: a sequence of given integer pairs. Each integer represents an object of a certain type, we want to describe p-Q to indicate "P link to Q ". * The "connectivity" relationship can be passed, that is, p-Q, Q-R, then P-r. Our goal is to write an irrelevant pair in the filtering set. Program . The * input of the program is p-q. If the number pair at that point is not implicitly connected to Q by P, the * input is output. If the previous Pair does imply * P connected to Q, the program should ignore p-Q and enter the next pair. * Development language: C ++ * Compiler: G ++ * Author: chinazhangjie * mailbox: chinajiezhang@gmail.com * reference books :《 Algorithm : C language implementation (1st ~ 4) "** test data: * objects: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 * sides: 3-4, 4-9, 8-0, 2-3, 5-6, 2-9, 5-9, 7-3, 4-8, 5-6, 0-2, 6-1 */# include <iostream> # include <vector> # include <string> # include <iterator> using namespace STD; const int objectcount = 10; const int linecount = 12; struct line {int left; int right; public: Line (int l, int R): Left (L), right (r) {} line () {}}; void printobject (const vector <int> & object, Const string & prompt = string (""); // quick search algorithm. To use a copy of source data with other algorithms, copy a copy of the data void quickfind (const vector <int> & object, const vector <line> & line) Before calculation) {vector <int> aobject (object); // A for assist vector <line> Aline (line); For (INT I = 0; I <(INT) Aline. size (); ++ I) {If (aobject [Aline [I]. left] = aobject [Aline [I]. right]) continue; int temp = aobject [Aline [I]. left]; for (Int J = 0; j <(INT) aobject. size (); ++ J) {If (aobject [J] = temp) aobject [J] = aobject [Aline [I]. right] ;}} printobject (aobject, "quickfind") ;}// fast merge algorithm void quickmerge (const vector <int> & object, const vector <line> & line) {vector <int> aobject (object); vector <line> Aline (line); For (INT I = 0; I <(INT) Aline. size (); ++ I) {Int J; int K; For (j = aobject [Aline [I]. right]; J! = Aobject [J]; j = aobject [J]); For (k = aobject [Aline [I]. Left]; k! = Aobject [k]; k = aobject [k]); If (j = k) continue; aobject [k] = J;} printobject (aobject, string ("quickmerge");} // weighted merge algorithm. Add an array to record the number of nodes on each tree. During each merge, connect the smaller tree to the larger tree, to prevent the growth of length paths in the tree void weightedquickmerge (const vector <int> & object, const vector <line> & line) {vector <int> aobject (object ); vector <line> Aline (line); vector <int> nodecount (objectcount, 1); For (INT I = 0; I <(INT) Aline. size (); ++ I) {Int J; int K; For (j = aobject [Aline [I]. right]; J! = Aobject [J]; j = aobject [J]); For (k = aobject [Aline [I]. Left]; k! = Aobject [k]; k = aobject [k]); If (j = k) continue; If (nodecount [J] <nodecount [k]) {aobject [J] = K; nodecount [J] + = nodecount [k];} else {aobject [k] = J; nodecount [k] + = nodecount [J] ;}} printobject (aobject, string ("weightedquickmerge "));} // compress void splitpathcompress (const vector <int> & object, const vector <line> & line) {vector <int> aobject (object ); vector <line> Aline (line); For (INT I = 0; I <(INT) Aline. Size (); ++ I) {Int J; int K; For (j = aobject [Aline [I]. Right]; J! = Aobject [J]; j = aobject [J]) {aobject [J] = aobject [aobject [J];} For (k = aobject [Aline [I]. left]; k! = Aobject [k]; k = aobject [k]) {aobject [k] = aobject [aobject [k]; }} printobject (aobject, string ("splitpathcompresse");} // print the data void printobject (const vector <int> & object, const string & prompt) {cout <prompt <":"; copy (object. begin (), object. end (), ostream_iterator <int> (cout, ""); cout <Endl ;}int main () {// init vector <int> Object (objectcount ); for (INT I = 0; I <objectcount; ++ I) {object [I] = I;} vector <line> line; line. push_back (line (3, 4); line. push_back (line (4, 9); line. push_back (line (8, 0); line. push_back (line (2, 3); line. push_back (line (5, 6); line. push_back (line (2, 9); line. push_back (line (5, 9); line. push_back (line (7, 3); line. push_back (line (4, 8); line. push_back (line (5, 6); line. push_back (line (0, 2); line. push_back (line (6, 1); // quickfind (object, line); quickmerge (object, line); weightedquickmerge (object, line); splitpathcompress (object, line ); return 0 ;}
End of this Article
Reprinted to indicate the source. Thank you!
2011-06-22