# Lecture 10 ###### tags: `Data Structure` `NYCU` ## Reference [Lec10 資料結構 第六週課程](https://youtu.be/zLuuRC56uvI) ## Rewind * Stack / Queue也可以用link-list實作,所以兩者不能和array畫上等號,只能說兩者都是一種data structure,只是用不同方式implement而已 :::spoiler Implementation Example ![](https://hackmd.io/_uploads/HkkCaFyUn.png) ::: * 之前提到的多項式也可以用link-list實作,同樣也可以解決sparse的問題 :::spoiler Implementation Example ![](https://hackmd.io/_uploads/r15haYyL2.png) ::: * Free pool的概念就是像glibc中那樣的回收場(Fast bin/Small bin/Large bin/Unsorted bin) ### Equivalence Relations * A relation over a set, S, is said to be an equivalence relations over S iff it is symmetric, reflexive, and transitive over S. * reflexive, x=x * symmetric, if x=y, then y=x * transitive, if x=y and y=z, then x=z * Example * Input: pairs of numbers `0=4,3=1,6=10,8=9,7=4,6=8,3=5,2=11,11=0` * Output: equivalent sets three equivalent classes {0, 2, 4, 7, 11};{1, 3, 5};{6, 8, 9, 10} * How to implement? ![](https://hackmd.io/_uploads/BJMpI5yIn.png) * Phase 1: 先看過所有的組合然後用link-list的方式建一個表格,如上圖,就可以知道誰和誰有關係 * Phase 2: 最後印出來,簡單來說就是從頭開始檢查,如果印出來後,專門儲存的array會把相對應的index設定成false,例如:0和11還有4有關係(print 0 and 11 and 4),而11和0,2有關係(print 2 only),接著2又和11有關係,此時因為11已經被設定成false,這樣的話就結束這個round,換下一個數值(4)繼續挖掘,4和7, 0有關係(print 7 only)而7又和4有關係,此時繞回來了,這樣就接著往下看, so on and so on until print all of the value or the final index. ```cpp= void equivalence() // Input the equivalence pairs and output the equivalence classes { ifstream inFile("equiv.in", ios::in); //"equiv.in" is the input file if(!inFile) { cerr << "Cannot open input file" << endl; return ; } int i, j, n; inFile >> n; // read number of objects // initialize seq and out ListNodePtr *seq = new ListNodePtr[n]; Boolean *out = new Boolean[n]; for (i=0; i<n; i++) { seq[i] = 0; out[i] = False; } } // Phase 1: input equivalence pairs inFile >> i >> j; while(inFile.good()) { // check end of file ListNode *x = new ListNode(j); x->link = seq[i]; seq[i] = x; // add j to seq[i] ListNode *y = new ListNode(i); y->link = seq[j]; seq[j] = y; // add j to seq[j] inFile >> i >> j; } // Phase 2: output equivalence classes for (i=0; i<n; i++) { if (out[i] == False) { cout << endl << "A new classes: " << i; // for example, i = 0, Output: 0 out[i] = True; ListNode *x = seq[i]; ListNode *top = o; // init stack while (1) { // find rest of class while(x) { // process the list j = x -> data; // when i=0, j=11 if (out[j] == False) { cout << ", " << j; out[j] = True; ListNode *j = x->link; // when i = 0 in the first round, y = 4 x->link = top; top = x; // in stack, top point to node 11 x = y; } else x = x->link; } // end of while(x) if (!top) break; else { x = seq[top->data]; top = top->link; //unstack } } // end of while(1) } // end of if(out[i] == False) } ``` ## Note * Link-list也可以解決sparse matrix(用環狀的link-list) ![](https://hackmd.io/_uploads/H1BTViJUn.png) 每一個row / column都有自己的Link-list ![](https://hackmd.io/_uploads/rJELBjkI2.png) 黃色的head node看起來有8個但其實只有四個,只是為了表示方便所以畫起來長這樣 ![](https://hackmd.io/_uploads/r1UGHikL3.png) * How to implement? ```cpp= enum Boolean {False, True}; struct Triple {int value, row, col;}; class Matrix; // forward declaration class MatrixNode { friend class Matrix; // for reading in a matrix friend istream& operation >> (istream&, Matrix&); private: MatrixNode *down, *right; Boolean head; union { // anonymous union MatrixNode *next; Triple triple; }; MatrixNode(Boolean, Triple *); // constrctor }; MatrixNode::MatrixNode(Boolean b, Triple *t) // constructor { head = b; if (b){right = next = down = this;} // row/column head node else triple = *t; // head node for list of headnodes OR element // node } typedef MatrixNode * MatrixNodePtr; // to allow subsequent creation of array of pointers ``` ```cpp= class Matrix { friend istream& operator >> (istream&, Matrix&); public: ~Matrix(); // destructor private: MatrixNode *headnode; } ```