From 630b3856805be44a8ff2423f10734fc65ff0ffce Mon Sep 17 00:00:00 2001 From: sjain1909 Date: Mon, 22 Jul 2024 22:10:58 +0530 Subject: [PATCH] Adding solution of PageRank algorithm. ## Fixing Issue #3773 ## Description This pull request adds the implementation of the PageRank algorithm in C++ to the DSA folder. PageRank is an algorithm used by Google Search to rank web pages in their search engine results. It measures the importance of website pages by counting the number and quality of links to each page, which is then used to determine a relative score of the page's importance. ## Type of PR - [ ] Bug fix - [x] Feature enhancement - [ ] Documentation update - [ ] Security enhancement - [ ] Other (specify): _______________ ## Checklist - [x] I have performed a self-review of my code. - [x] I have read and followed the Contribution Guidelines. - [x] I have tested the changes thoroughly before submitting this pull request. - [x] I have provided relevant issue numbers, screenshots, and videos after making the changes. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have followed the code style guidelines of this project. - [x] I have checked for any existing open issues that my pull request may address. - [x] I have ensured that my changes do not break any existing functionality. - [x] Each contributor is allowed to create a maximum of 4 issues per day. This helps us manage and address issues efficiently. - [x] I have read the resources for guidance listed below. - [x] I have followed security best practices in my code changes. ## Additional Context The PageRank algorithm implementation includes: - Calculation of the PageRank of each page in a graph using an iterative approach. - Consideration of damping factors to simulate the probability of random web surfing. - Clear and well-commented code for easy understanding and maintenance. ## Resources for Guidance Please read the following resources before submitting your contribution: - [x] [Code Harbor Hub Community Features](https://www.codeharborhub.live/community/features) - [x] [Markdown Guide](https://www.markdownguide.org/) --- dsa/Advance/pageRank.md | 153 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 dsa/Advance/pageRank.md diff --git a/dsa/Advance/pageRank.md b/dsa/Advance/pageRank.md new file mode 100644 index 000000000..ce81b91c1 --- /dev/null +++ b/dsa/Advance/pageRank.md @@ -0,0 +1,153 @@ +--- +id: pagerank-algorithm +title: PageRank Algorithm for Web Page Ranking +sidebar_label: 0010 - PageRank Algorithm +tags: [PageRank, Web Page Ranking, Algorithm, C++, Problem Solving] +description: This is a solution for implementing the PageRank Algorithm to rank web pages based on their importance and relevance. +--- + +## Problem Statement + +### Problem Description + +The PageRank Algorithm, developed by Google, ranks web pages in search engine results based on their importance and relevance. It assigns a numerical weighting to each element of a hyperlinked set of documents with the purpose of measuring its relative importance within the set. + +### Examples + +**Example 1:** + +```plaintext +Input: +Number of pages: 3 +Links: +0 -> 1 +1 -> 2 +2 -> 0 + +Output: +PageRank values: +Page 0: 0.33 +Page 1: 0.33 +Page 2: 0.33 + +Explanation: Each page has an equal rank since each links to the next in a circular manner. +``` + +### Constraints + +- The input is a directed graph where nodes represent web pages and edges represent links between them. +- The algorithm should handle graphs with up to 10^5 vertices and edges. + +## Solution of Given Problem + +### Intuition and Approach + +The PageRank Algorithm follows these steps: + +1. Initialize the rank of all pages to a uniform value. +2. For a fixed number of iterations or until convergence, update the rank of each page based on the ranks of pages linking to it. +3. Apply a damping factor to simulate the probability of a random web surfer following links. + +### Approaches + +#### Codes in Different Languages + + + + + ```cpp + #include + using namespace std; + + class PageRank { + int V; + vector> adj; + vector rank; + double damping; + int maxIter; + +public: + PageRank(int V, double damping = 0.85, int maxIter = 100) { + this->V = V; + this->damping = damping; + this->maxIter = maxIter; + adj.resize(V); + rank.resize(V, 1.0 / V); + } + + void addEdge(int v, int w) { + adj[v].push_back(w); + } + + void calculatePageRank() { + vector newRank(V, 0.0); + for (int iter = 0; iter < maxIter; ++iter) { + for (int i = 0; i < V; ++i) { + newRank[i] = (1 - damping) / V; + for (int j = 0; j < V; ++j) { + if (find(adj[j].begin(), adj[j].end(), i) != adj[j].end()) { + newRank[i] += damping * rank[j] / adj[j].size(); + } + } + } + rank = newRank; + } + } + + void printPageRank() { + for (int i = 0; i < V; ++i) { + cout << "Page " << i << ": " << rank[i] << "\n"; + } + } +}; + +int main() { + int V, E; + cout << "Enter the number of pages (vertices): "; + cin >> V; + cout << "Enter the number of links (edges): "; + cin >> E; + + PageRank pr(V); + for (int i = 0; i < E; ++i) { + int v, w; + cout << "Enter link (v -> w): "; + cin >> v >> w; + pr.addEdge(v, w); + } + + pr.calculatePageRank(); + cout << "PageRank values:\n"; + pr.printPageRank(); + + return 0; +} + ``` + + + +### Complexity Analysis + +- **Time Complexity:** $O(V + E)$ per iteration, where `V` is the number of vertices and `E` is the number of edges. +- **Space Complexity:** $O(V + E)$ + +The time complexity is linear with respect to the number of vertices and edges per iteration. The space complexity is linear due to the storage requirements for the adjacency list and rank vectors. + +## Video Explanation of Given Problem + + +--- + +

Authors:

+ +
+{['sjain1909'].map(username => ( + +))} +