Today I have solved the problem in javascript

 PageRank is an algorithm used by Google to rank the importance of different websites. While there have been changes over the years, the central idea is to assign each site a score based on the importance of other pages that link to that page.


More mathematically, suppose there are N sites, and each site i has a certain count Ci of outgoing links. Then the score for a particular site Sj is defined as :


score(Sj) = (1 - d) / N + d * (score(Sx) / Cx+ score(Sy) / Cy+ ... + score(Sz) / Cz))

Here, Sx, Sy, ..., Sz denote the scores of all the other sites that have outgoing links to Sj, and d is a damping factor, usually set to around 0.85, used to model the probability that a user will stop searching.


Given a directed graph of links between various websites, write a program that calculates each site's page rank.

const graph = {

  'A': ['B', 'C'],

  'B': ['A'],

  'C': ['A', 'B']

};


const result = calculatePageRank(graph);

function calculatePageRank(graph, dampingFactor = 0.85, iterations = 100) {

  const N = Object.keys(graph).length;

  const initialScore = 1 / N;

 let pageRank = {};

  let tempPageRank;

for (const site in graph) {

    pageRank[site] = initialScore;

  }

 for (let i = 0; i < iterations; i++) {

    tempPageRank = {};

 for (const site in graph) {

      let incomingScore = 0;

for (const incomingSite in graph) {

        if (graph[incomingSite].includes(site)) {

          incomingScore += pageRank[incomingSite] / graph[incomingSite].length;

        }

      } tempPageRank[site] = (1 - dampingFactor) / N + dampingFactor * incomingScore;

    }

pageRank = { ...tempPageRank };

  }

 return pageRank;

}

console.log(result);

Output:

{
  A: 0.43274853801169594,
  B: 0.3333333333333333,
  C: 0.23391812865497078
}

Comments

Popular posts from this blog

Building a Full-Stack Student Management System with React.js and NestJS

Today I have solved the problem in javascript

Today I have solved problem in javascript