所以这是页面排名的代码,我必须在其中填充矩阵并在其上运行公式。由于某种原因,该函数将一些条目填充为nan,其中应为零。这是引起nan的输入。 7是行数,100是powerIterations。网站是往返于google.com到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
这与quora.com页面没有任何传出页面有关,因此它应该全部返回零等级,而不是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;
}
}
这是课程信息:
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
我如何使这些nan为0?