Basic Google Pagerank Algorithm

Program that mimics the basic principles of Google’s PageRank algorithm, which is used to rank websites based on their importance and relevance.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageRank {
    // A map of websites and their outgoing links
    private Map<String, List<String>> websites;

    // A map of websites and their PageRank scores
    private Map<String, Double> scores;

    public PageRank() {
        this.websites = new HashMap<>();
        this.scores = new HashMap<>();
    }

    // Add a website and its outgoing links
    public void addWebsite(String website, List<String> links) {
        this.websites.put(website, links);
    }

    // Compute PageRank scores for all websites
    public void computeScores() {
        // Initialize PageRank scores to 1.0 for all websites
        for (String website : this.websites.keySet()) {
            this.scores.put(website, 1.0);
        }

        // Compute new scores for all websites
        for (int i = 0; i < 10; i++) {
            Map<String, Double> newScores = new HashMap<>();

            for (String website : this.websites.keySet()) {
                double score = 0.15; // Damping factor of 0.15

                // Add contributions from other websites
                for (String other : this.websites.keySet()) {
                    if (this.websites.get(other).contains(website)) {
                        // Website other has a link to website,
                        // so add a contribution to its score
                        score += 0.85 * this.scores.get(other) / this.websites.get(other).size();
                    }
                }

                newScores.put(website, score);
            }

            // Update scores with new scores
            this.scores = newScores;
        }
    }

    // Get the PageRank score for a website
    public double getScore(String website) {
        return this.scores.get(website);
    }

    public static void main(String[] args) {
        // Create a PageRank instance
        PageRank pr = new PageRank();

        // Add some websites and their outgoing links
        pr.addWebsite("A", Arrays.asList("B", "C"));
        pr.addWebsite("B", Arrays.asList("A", "C"));
        pr.addWebsite("C", Arrays.asList("A", "B"));

        // Compute PageRank scores
        pr.computeScores();

        // Print scores
        System.out.println("A: " + pr.getScore("A"));
        System.out.println("B: " + pr.getScore("B"));
        System.out.println("C: " + pr.getScore("C"));
    }
}

Written on December 5, 2022