Пример #1
0
 /*
  * Matrix mult but with min-plus, and iterative. Each min-plus operation
  * that changes the path inserts it into a new queue
  */
 public SparseMatrix apsp() {
   SparseMatrix shortestPaths = new SparseMatrix(this);
   SparseMatrix currentPairs = new SparseMatrix(this.rowDim, this.colDim);
   SparseMatrix newPairs = new SparseMatrix(this.rowDim, this.colDim);
   newPairs = new SparseMatrix(this);
   for (int d = 0; d < this.rowDim; d++) {
     shortestPaths.set(d, d, 0.0);
   }
   for (int d = 0; d < this.rowDim; d++) {
     newPairs.set(d, d, 0.0);
   }
   while (!newPairs.isEmpty()) {
     currentPairs = new SparseMatrix(newPairs);
     newPairs = new SparseMatrix(this.rowDim, this.colDim);
     for (int r : currentPairs.rows) {
       Counter row = currentPairs.getRow(r);
       for (int c : row.keySet()) {
         Counter oRow = this.getRow(c);
         for (int oc : oRow.keySet()) {
           double pathLength = currentPairs.get(r, c) + oRow.get(oc);
           if (pathLength < shortestPaths.getPath(r, oc)) {
             newPairs.set(r, oc, pathLength);
             shortestPaths.set(r, oc, pathLength);
           }
         }
       }
     }
   }
   return shortestPaths;
 }
Пример #2
0
 public SparseMatrix makeLaplacian() {
   SparseMatrix laplacian = new SparseMatrix(this.rowDim, this.colDim);
   for (int r : this.getRows()) {
     Counter row = this.getRow(r);
     laplacian.set(r, r, row.sum());
     for (int c : row.keySet()) {
       laplacian.set(r, c, -1 * row.get(c));
     }
   }
   return laplacian;
 }
Пример #3
0
 public SparseMatrix transpose() {
   SparseMatrix transp = new SparseMatrix(this.rowDim, this.colDim);
   for (int r : rows) {
     Counter row = this.getRow(r);
     for (int c : row.keySet()) {
       double v = row.get(c);
       transp.set(c, r, v);
     }
   }
   return transp;
 }
Пример #4
0
 public SparseMatrix stochasticizeRows() {
   SparseMatrix stochasticMat = new SparseMatrix(this.rowDim, this.colDim);
   double[] rowSums = new double[this.rowDim];
   for (int r : this.rows) {
     Counter row = this.getRow(r);
     for (int c : row.keySet()) {
       rowSums[r] += row.get(c);
     }
   }
   for (int r : this.rows) {
     Counter row = this.getRow(r);
     for (int c : row.keySet()) {
       double value = 0;
       if (rowSums[r] != 0) {
         //				if(true){
         value = this.get(r, c) / rowSums[r];
       }
       stochasticMat.set(r, c, value);
     }
   }
   return stochasticMat;
 }