示例#1
0
文件: 126.cpp 项目: CC91/LeetCode
 vector<vector<string>> findLadders(string beginWord, string endWord, unordered_set<string> &wordList) {
     wordList.insert(beginWord);
     wordList.insert(endWord);
     unordered_set<string> curL;
     unordered_set<string> nextL; // provide next word options
     curL.insert(beginWord);
     path.push_back(endWord);
     while (1) {
         for (auto it=curL.begin(); it!=curL.end(); it++) wordList.erase(*it);
         for (auto it=curL.begin(); it!=curL.end(); it++) findW(*it, wordList, nextL);
         if (nextL.empty()) return res; // if there is no option for the next word in the wordlist, then return null res
         if (nextL.count(endWord)) {
             getPath(beginWord, endWord);
             return res;
         }
         swap(curL, nextL);
         nextL.clear();
     }
     return res;
 }
示例#2
0
Tridiagonal* toHessenberg(Matrix* A){
  int i, j, k;
  Tridiagonal *H;
  Vector **W;
  Vector **A_n;
  Matrix *Aux, *Aux2;

  P = createMatrix(A->row, A->col);
  Aux = createMatrix(A->row, A->col);

  for(i = 0; i < A->row; i++) P->data[i][i] = 1;

  W = malloc((A->col - 2) * sizeof(*W));
  /*criando/inicializando A_n tq A_n = A*/
  A_n = malloc(A->col * sizeof(*A_n));
  for(i = 0; i < A->col; i++){
    A_n[i] = createVector(A->row);
    for(j = 0; j < A->row; j++){
      A_n[i]->data[j] = A->data[j][i];
    }
  }

  for(k = 0; k < A->col - 2; k++){
    W[k] = findW(A_n[k], k + 1);
    applyingLeftHouseHolder(W[k], A_n, A->col);
    applyingRightHouseHolder(W[k], A_n, A->col);

    /*
    applyingMatrixRightHouseHolder(W[k], P);
    */
    
    makeMatrixHouseHolder(W[k], Aux);
    Aux2 = P;
    P = multMatrix(P, Aux);
    free(Aux2);

    /***DEBUG***/
    if(DEBUG_HESS > 2){
      printf("MATRIX PARCIAL:\n");
      for(i = 0; i < A->col; i++){
        for(j = 0; j < A->col; j++){
          printf("%.2f ", A_n[j]->data[i]);
        }
        printf("\n");
      }
    }
    /**********/
  }
  /***DEBUG***/
  if(DEBUG_HESS > 2){
    for(j = 0; j < A->col - 2; j++){
      printf("W[%d]: ", j);
      for(i = 0; i < A->col; i++){
        printf("%f ", W[j]->data[i]);
      }
      printf("\n");
    }
  }
  /**********/


  H = createTridiagonal(A->row);
  for (i = 0; i < A->row - 1; i++){
    H->sup->data[i] = A_n[i + 1]->data[i];
    H->princ->data[i] = A_n[i]->data[i];
    H->sub->data[i] = A_n[i]->data[i + 1];
  }
  H->princ->data[A->row - 1] = A_n[A->row - 1]->data[A->row - 1];

  for(i = 0; i < A->col; i++){
    freeVector(A_n[i]);
  }
  free(A_n);

  /*só por agora*/
  for(i = 0; i < A->col - 2; i++){
    freeVector(W[i]);
  }
  free(W);


  return H;
}