Skip to content

Commit 18f3756

Browse files
authored
Merge pull request #3795 from sjain1909/m1
Adding solution of PageRank algorithm.
2 parents 63d74a0 + 630b385 commit 18f3756

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed

dsa/Advance/pageRank.md

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
id: pagerank-algorithm
3+
title: PageRank Algorithm for Web Page Ranking
4+
sidebar_label: 0010 - PageRank Algorithm
5+
tags: [PageRank, Web Page Ranking, Algorithm, C++, Problem Solving]
6+
description: This is a solution for implementing the PageRank Algorithm to rank web pages based on their importance and relevance.
7+
---
8+
9+
## Problem Statement
10+
11+
### Problem Description
12+
13+
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.
14+
15+
### Examples
16+
17+
**Example 1:**
18+
19+
```plaintext
20+
Input:
21+
Number of pages: 3
22+
Links:
23+
0 -> 1
24+
1 -> 2
25+
2 -> 0
26+
27+
Output:
28+
PageRank values:
29+
Page 0: 0.33
30+
Page 1: 0.33
31+
Page 2: 0.33
32+
33+
Explanation: Each page has an equal rank since each links to the next in a circular manner.
34+
```
35+
36+
### Constraints
37+
38+
- The input is a directed graph where nodes represent web pages and edges represent links between them.
39+
- The algorithm should handle graphs with up to 10^5 vertices and edges.
40+
41+
## Solution of Given Problem
42+
43+
### Intuition and Approach
44+
45+
The PageRank Algorithm follows these steps:
46+
47+
1. Initialize the rank of all pages to a uniform value.
48+
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.
49+
3. Apply a damping factor to simulate the probability of a random web surfer following links.
50+
51+
### Approaches
52+
53+
#### Codes in Different Languages
54+
55+
<Tabs>
56+
<TabItem value="cpp" label="C++">
57+
<SolutionAuthor name="sjain1909"/>
58+
```cpp
59+
#include <bits/stdc++.h>
60+
using namespace std;
61+
62+
class PageRank {
63+
int V;
64+
vector<vector<int>> adj;
65+
vector<double> rank;
66+
double damping;
67+
int maxIter;
68+
69+
public:
70+
PageRank(int V, double damping = 0.85, int maxIter = 100) {
71+
this->V = V;
72+
this->damping = damping;
73+
this->maxIter = maxIter;
74+
adj.resize(V);
75+
rank.resize(V, 1.0 / V);
76+
}
77+
78+
void addEdge(int v, int w) {
79+
adj[v].push_back(w);
80+
}
81+
82+
void calculatePageRank() {
83+
vector<double> newRank(V, 0.0);
84+
for (int iter = 0; iter < maxIter; ++iter) {
85+
for (int i = 0; i < V; ++i) {
86+
newRank[i] = (1 - damping) / V;
87+
for (int j = 0; j < V; ++j) {
88+
if (find(adj[j].begin(), adj[j].end(), i) != adj[j].end()) {
89+
newRank[i] += damping * rank[j] / adj[j].size();
90+
}
91+
}
92+
}
93+
rank = newRank;
94+
}
95+
}
96+
97+
void printPageRank() {
98+
for (int i = 0; i < V; ++i) {
99+
cout << "Page " << i << ": " << rank[i] << "\n";
100+
}
101+
}
102+
};
103+
104+
int main() {
105+
int V, E;
106+
cout << "Enter the number of pages (vertices): ";
107+
cin >> V;
108+
cout << "Enter the number of links (edges): ";
109+
cin >> E;
110+
111+
PageRank pr(V);
112+
for (int i = 0; i < E; ++i) {
113+
int v, w;
114+
cout << "Enter link (v -> w): ";
115+
cin >> v >> w;
116+
pr.addEdge(v, w);
117+
}
118+
119+
pr.calculatePageRank();
120+
cout << "PageRank values:\n";
121+
pr.printPageRank();
122+
123+
return 0;
124+
}
125+
```
126+
</TabItem>
127+
</Tabs>
128+
129+
### Complexity Analysis
130+
131+
- **Time Complexity:** $O(V + E)$ per iteration, where `V` is the number of vertices and `E` is the number of edges.
132+
- **Space Complexity:** $O(V + E)$
133+
134+
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.
135+
136+
## Video Explanation of Given Problem
137+
138+
<LiteYouTubeEmbed
139+
id="zQEsow1Z9OM"
140+
params="autoplay=1&autohide=1&showinfo=0&rel=0"
141+
title="Problem Explanation | Solution | Approach"
142+
poster="maxresdefault"
143+
webp
144+
/>
145+
---
146+
147+
<h2>Authors:</h2>
148+
149+
<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}>
150+
{['sjain1909'].map(username => (
151+
<Author key={username} username={username} />
152+
))}
153+
</div>

0 commit comments

Comments
 (0)