Exp 10 : Page Rank Algorithm
class PageRank {
constructor(graph, dampingFactor = 0.85, iterations = 100) {
[Link] = graph;
[Link] = dampingFactor;
[Link] = iterations;
[Link] = {};
}
initScores() {
const numPages = [Link]([Link]).length;
const initialRank = 1 / numPages;
for (const page in [Link]) {
[Link][page] = initialRank;
}
}
calculate() {
[Link]();
for (let i = 0; i < [Link]; i++) {
const newScores = {};
for (const page in [Link]) {
newScores[page] = (1 - [Link]) / [Link]([Link]).length;
for (const linkingPage in [Link]) {
if ([Link][linkingPage].includes(page)) {
const linksCount = [Link][linkingPage].length;
newScores[page] += [Link] * ([Link][linkingPage] /
linksCount);
}
}
}
[Link] = newScores;
}
// Normalize scores to ensure they are near 1 but not exceeding it
const maxScore = [Link](...[Link]([Link]));
for (const page in [Link]) {
[Link][page] /= maxScore;
}
return [Link];
}
}
const graph = {
'A': ['B', 'C'],
'B': ['C'],
'C': ['A'],
'D': ['C', 'A'],
};
function getMaxValue(scores) {
let maxValue = -Infinity;
let maxPage = null;
for (const key in scores) {
if ([Link](key)) {
if (scores[key] > maxValue) {
maxValue = scores[key];
maxPage = key;
}
}
}
return {
maxValue: maxValue,
maxPage: maxPage
};
}
const pageRank = new PageRank(graph);
const scores = [Link]();
[Link](scores);
const { maxValue, maxPage } = getMaxValue(scores);
[Link](`Maximum Ranked Page is: ${maxPage} with a score of ${maxValue}`);