0

So this is a code for page rank where i have to populate a matrix and run formula on it. The function for some reason is populating some entries as nan where it should be zero. Here's an input that's causing the nan. 7 is number of line, 100 is the powerIterations. The websites are from and to, so from google.com to gmail.com.

7 100

google.com gmail.com

google.com maps.com

facebook.com ufl.edu

ufl.edu google.com

ufl.edu gmail.com

maps.com facebook.com

maps.com quora.com

The thing has to do with quora.com page not having any pages outgoing, so it should all return a rank of zero, not nan.

void AdjacencyListorMatrix::PopulateMatrix() {
set<string>::iterator it;
int i;

for (i = 1, it = pages.begin(); it != pages.end(); ++it, i++) {
    vertexMaps[*it] = i;
}

vertices = pages.size();
matrix = new double*[vertices];

for (int i = 0; i < vertices; i++) {
    matrix[i] = new double[vertices];
}

for (int i = 0; i < vertices; i++) {
    for (int j = 0; j < vertices; j++) {
        matrix[i][j] = 0.0;
    }
}

multimap<string, string>::iterator mapIt;

for (mapIt = inputMaps.begin(); mapIt != inputMaps.end(); mapIt++) {
    matrix[vertexMaps[mapIt->second] - 1][vertexMaps[mapIt->first] -1] = 1;
}

int columnAdds[vertices];

for (int i = 0; i < vertices; i++) {
    columnAdds[i] = 0.0;
}

for (int i = 0; i < vertices; i++) {
    for (int j = 0; j < vertices; j++) {
        columnAdds[j] += matrix[i][j];
    }
}

for (int i = 0; i < vertices; i++) {
    for (int j = 0; j < vertices; j++) {
        matrix[i][j] /= columnAdds[j];
    }
}

result = new double[vertices];

for (int i = 0; i < vertices; i++) {
    result[i] = 1.0 / (double) vertices;
}
}

This is the class info:

class AdjacencyListorMatrix {
private:
    set<string> pages;
    multimap<string, string> inputMaps;
    map<string, long> vertexMaps;
    double **matrix;
    double *result;
    int vertices;

public:
    void PopulateVertices(string from, string to);
    void PopulateMatrix();
    void PageRank(int n);
    ~AdjacencyListorMatrix();
};

     1    2    3    4    5    6 
1    0  nan    0  0.5  nan    0 
2    0  nan  0.5    0  nan  0.5 
3    0  nan    0    0  nan  0.5 
4    0  nan  0.5    0  nan    0 
5    0  nan    0  0.5  nan    0 
6    1  nan    0    0  nan    0 

How do i make those nan be a 0??

  • `result = new double[vertices];` -- You're using `std::multimap`, so why are you not using `std::vector result(vertices)` here and not risk a memory leak or other issues? And please post a [mcve], not just the class member functions. – PaulMcKenzie Sep 21 '18 at 20:53
  • 3
    `0 / 0` -> `Nan` from `matrix[i][j] /= columnAdds[j];`. – Jarod42 Sep 21 '18 at 21:00
  • [ok that helped...this fixed it, thanks] for (int i = 0; i < vertices; i++) { for (int j = 0; j < vertices; j++) { if (columnAdds[j] == 0) { matrix[i][j] = 0; } else { matrix[i][j] /= columnAdds[j]; } } } And i had to use multi map cause of the repetition of inputs. – David Volkov Sep 21 '18 at 22:10
  • @DavidVolkov My point is not your usage of `multimap`. It is your *non*-usage of `std::vector`. Since you are already using containers such as multimap, why not just go full and use the other containers such as `vector`? It is much easier to simply use `vector` than to fight with handling raw memory. Also, this `int columnAdds[vertices];` is **not** valid C++ code. Arrays in C++ must have a compile-time expression to denote the number of entries. To overcome this issue, use `std::vector`. – PaulMcKenzie Sep 21 '18 at 23:57

0 Answers0