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??