HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Speed up a program to find the minimum Hamming distance

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
theminimumprogramdistancefindspeedhamming

Problem

I have a solution to the puzzle at https://kattis.csc.kth.se/problem?id=codes.
What can I do to make my algorithm much much faster? What the algorithm does is take an N×K matrix and search for the minimum distance — that is, the least number of 1 for all the products of the matrix and a vector that ranges from 00001 to 11111 (where the top is 2K - 1).

#include 

using namespace std;

void DataEntry(int &a, int &b, int** &matrix) {

    cin >> a >> b;

    matrix = new int*[a];

    for (int i = 0; i > matrix[i][j];
        }
    }

}

int* BinaryAdd(int* a, int* b, int tam) {
    int* c;
    int ac = 0;
    c = new int[tam];

    for (int i = tam - 1; i > -1; i--) {
        c[i] = a[i] + b[i] + ac;
        if (c[i] == 2) {
            c[i] = 0;
            ac = 1;
        } else
            ac = 0;
    }

    return c;
}

int* MultiplyMatrices(int** a, int* b, int tamF, int tamC) {
    int * c;
    c = new int[tamF];
    for (int i = 0; i > t;

    for (int p = 0; p < t; p++) {

        DataEntry(n, k, lecc);

        pro = new int[k];
        uno = new int[k];
        sal = new int[n];
        zero = new int[n];

        for (int i = 0; i < k; i++) {
            pro[i] = 0;
        }
        for (int i = 0; i < k - 1; i++) {
            uno[i] = 0;
        }
        uno[k - 1] = 1;

        for (int i = 0; i < n; i++) {
            zero[i] = 0;
        }

        pro1 = BinaryAdd(pro, uno, k);

        pro = pro1;

        max = pot(2, k);

        sal = MultiplyMatrices(lecc, pro, n, k);

        d = MinimumDistance(sal, n);

        for (int i = 0; i < max - 1; i++) {

            sal = MultiplyMatrices(lecc, pro, n, k);

            pro1 = BinaryAdd(pro, uno, k);

            pro = pro1;

            int ad = MinimumDistance(sal, n);

            if (ad < d)
                d = ad;
        }
        cout << d << endl;
    }

}


Edit: I give you a sample input and output

```
Sample input Sample output
2
7 4
1 0 0 0
0 1 0 0
0 0 1 0 3
0 0

Solution

Considering that this is meant to be a puzzle, I'll write my review as a sequence of escalating hints.

  • Everybody who is suggesting the use of threads or std::vector or matrix libraries is on the wrong track! How do you think error correction is implemented in your CD player or hard disk firmware? No threads or matrix libraries there, I assure you.



  • Don't simulate the computer; use the computer.



  • All operations are modulo 2. The puzzle guarantees k ≤ 16. Under these conditions, you can massively parallelize vector dot products. (Think SIMD or AltiVec, except you don't need SIMD or AltiVec.)



  • I've added the bitwise tag to this question.



I'll conclude with a remark that you've basically written C code. The only thing that makes it C++ is your use of iostream. C++ can be much more expressive than that.

Context

StackExchange Code Review Q#26461, answer score: 6

Revisions (0)

No revisions yet.