Lecture 10

Lecture 10

tags: Data Structure NYCU

Reference

Lec10 資料結構 第六週課程

Rewind

  • Stack / Queue也可以用link-list實作,所以兩者不能和array畫上等號,只能說兩者都是一種data structure,只是用不同方式implement而已 :::spoiler Implementation Example :::
  • 之前提到的多項式也可以用link-list實作,同樣也可以解決sparse的問題 :::spoiler Implementation Example :::
  • 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?
    • 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.
      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) 每一個row / column都有自己的Link-list

    黃色的head node看起來有8個但其實只有四個,只是為了表示方便所以畫起來長這樣

  • How to implement?

      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
    
      class Matrix
      {
          friend istream& operator >> (istream&, Matrix&);
          public:
              ~Matrix(); // destructor
          private:
              MatrixNode *headnode;
      }