예제 #1
0
파일: main.c 프로젝트: hudecek/algorithmsHW
/**
 * @brief Graphviz funkce
 * vytvori haldu jako graf ve formatu ".dot"
 *
 * Dodatek k graphvizu:
 * Graphviz je nastroj, ktery vam umozni vizualizaci datovych struktur,
 * coz se hodi predevsim k ladeni.
 * Tento program generuje nekolik souboru neco.dot v mainu
 * Vygenerovane soubory nahrajte do online nastroje pro zobrazeni graphvizu:
 * http://sandbox.kidstrythisathome.com/erdos/
 * Alternativne si muzete nainstalovat prekladac z jazyka dot do obrazku na svuj
 * pocitac.
 **/
void makeGraphviz(MinHeap* heap, int index, FILE* pFile) {
    fprintf(pFile, "\"%i\"\n", heap->array[index]);
    if(leftIndex(index) < heap->size) {
        fprintf(pFile, "\"%i\" -> \"%i\"\n", heap->array[index], getLeft(heap, index));
        makeGraphviz(heap, leftIndex(index), pFile);
    }
    if(rightIndex(index) < heap->size) {
        fprintf(pFile, "\"%i\" -> \"%i\"\n", heap->array[index], getRight(heap, index));
        makeGraphviz(heap, rightIndex(index), pFile);
    }
}
예제 #2
0
파일: main.c 프로젝트: hudecek/algorithmsHW
/**
 * @brief Opravi haldu 'heap' tak aby splnovala vlastnost minimove haldy
 * Kontrola zacina u prvku na pozici 'index'
 */
void heapify(MinHeap* heap, int index) {
    //TODO
    int left = leftIndex(index);
    int right = rightIndex(index);
    int min = index;
    if(left < heap->size && heap!=NULL) {

        if(right<heap->size) {

            if(heap->array[left] < heap->array[right] &&
                    heap->array[left] < heap->array[index]) {
                min = left;
            }
            else if(heap->array[right] < heap->array[index] &&
                    heap->array[right] < heap->array[left]) {
                min = right;
            } else if(heap->array[right] < heap->array[index])
                min = right;
       } else {
            if(heap->array[left]<heap->array[index])
                min = left;
        }
        if(min!=index) {
            swap(heap, min, index);
            heapify(heap, min);
        }
    }

}
예제 #3
0
QVector<int> SixtyOneGameBoardInfo::chainAroundIndex(int index)
{
    if (index < 0 || index >= TOTAL_ITEM_NUMBER)
        return QVector<int>();
    if (_chainAroundIndex61.isEmpty())
        for (int i = 0; i < TOTAL_ITEM_NUMBER; ++i)
            _chainAroundIndex61.push_back(QVector<int>());
    if (_chainAroundIndex61[index].isEmpty())
    {
        QVector<int> result;
        int tmp = leftUpIndex(index);
        if (tmp >= 0)
            result.push_back(tmp);
        tmp = rightUpIndex(index);
        if (tmp >= 0)
            result.push_back(tmp);
        tmp = rightIndex(index);
        if (tmp >= 0)
            result.push_back(tmp);
        tmp = rightDownIndex(index);
        if (tmp >= 0)
            result.push_back(tmp);
        tmp = leftDownIndex(index);
        if (tmp >= 0)
            result.push_back(tmp);
        tmp = leftIndex(index);
        if (tmp >= 0)
            result.push_back(tmp);
        _chainAroundIndex61[index] = result;
    }
    return _chainAroundIndex61[index];
}
예제 #4
0
int SixtyOneGameBoardInfo::nearbyIndex(int index, int direction)
{
    if (direction < 0 || direction > 5)
        return -1;
    switch (direction)
    {
    case 0:
        return leftDownIndex(index);
        break;
    case 1:
        return rightDownIndex(index);
        break;
    case 2:
        return rightIndex(index);
        break;
    case 3:
        return rightUpIndex(index);
        break;
    case 4:
        return leftUpIndex(index);
        break;
    case 5:
        return leftIndex(index);
        break;
    default:
        return -1;
    }
}
void FilledBandedMatrix::print()
{

    for(unsigned long i = 0; i < size()+3; i++)
    {  
        for(unsigned long j = 0; j <= rightIndex(i); j++)
        {
            
            cout << setw(10)<<getEntry(i,j);
        }
//        cout <<"F: ";
        for(unsigned long j =  rightIndex(i) + 1; j <= rightIndex(i) + 4; j++)
        {
            cout <<setw(10) << getEntry(i,j);
        }        
        
        cout << endl;
    }
}
void FilledRow::print()
{
    
    for(unsigned long i = 0; i < rightIndex()+3; i++)
    {
        cout << setw(10)<<(*this)[i];
        

        
        cout << " ";
    }
    cout << endl;
}
예제 #7
0
파일: main.c 프로젝트: hudecek/algorithmsHW
int testIndexes() {
    printf("Test 1. indexovani parent, left, right: ");
    if (parentIndex(2) != 0 || parentIndex(1) != 0 || parentIndex(0) != INT_MAX) {
        printf("NOK - chybny parentIndex\n");
        return 0;
    }
    if (leftIndex(0) != 1 || leftIndex(3) != 7) {
        printf("NOK - chybny leftIndex\n");
        return 0;
    }
    if (rightIndex(0) != 2 || rightIndex(3) != 8) {
        printf("NOK - chybny rightIndex\n");
        return 0;
    }

    MinHeap heap;
    int tmp[] = {1,2,3};
    heap.size = 3;
    for (unsigned int i = 0; i < heap.size; i++) {
        heap.array[i] = tmp[i];
    }

    if (getParent(&heap,0) != INT_MAX || getParent(&heap,1) != 1 ||
            getParent(&heap,2) != 1) {
        printf("NOK - chyba ve funkci getParent\n");
        return 0;
    }
    if (getLeft(&heap,0) != 2 || getLeft(&heap,1) != INT_MAX) {
        printf("NOK - chyba ve funkci getLeft\n");
        return 0;
    }
    if (getRight(&heap,0) != 3 || getRight(&heap,1) != INT_MAX) {
        printf("NOK - chyba ve funkci getRight\n");
        return 0;
    }
    printf("OK\n");
    return 1;
}
void FilledBandedMatrix::applyGivens(unsigned long row1, unsigned long row2, vector<double> *c)
{
    unsigned long col1 = row1;    
    if(leftIndex(row2) < leftIndex(row1))
        throw "Givens should be applied left to right and top to bottom, in order";    
    if (leftIndex(row2) > col1)
        throw "Cannot change elements to the left";
    if (rightIndex(row1) > rightIndex(row2))
        throw "Probably a bug: rightIndex(row1) > rightIndex(row2)";
    if (row2 > size())
        throw "Probably a bug: row2 > size";
    
    increaseSize(max(row1,row2));
    
    
    double a = (*rows[row1])[col1];
    double b = (*rows[row2])[col1];
    double norm = sqrt(a*a + b*b);
    
    a = a/norm;
    b = b/norm;
    
    // @TODO make Givens a static? function    
    // update vector
    
    double en1 = (*c)[row1];
    double en2 = (*c)[row2];
    
    (*c)[row1] = a*en1 + b*en2;
    (*c)[row2] = -b*en1 + a*en2;  
    
    en1 = getEntry(row1, col1);
    en2 = getEntry(row2, col1);
    
    setEntry(row1, col1,  a*en1 + b*en2,true);
    dropFirst(row2);
    
    
    
    for(unsigned long j = col1 + 1; j <= rightIndex(row2); j++)
    {
        en1 = getEntry(row1, j);
        en2 = getEntry(row2, j);
        
        setEntry(row1, j,  a*en1 + b*en2,true);
        setEntry(row2, j, -b*en1 + a*en2,true);  
        
        //        cout<<"j "<<j<<" rowfill " <<rows[row1].getFill()<<endl;
    }
    
    
    for(int i = 0; i < rows[row1]->fillSize(); i++)
    {
        en1 = rows[row1]->getFill(i);
        en2 = rows[row2]->getFill(i);
        
        
        rows[row1]->setFill(i, a*en1 + b*en2);
        rows[row2]->setFill(i, -b*en1 + a*en2);  
        
    }
}