-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyPageRank.java
More file actions
164 lines (142 loc) · 5 KB
/
MyPageRank.java
File metadata and controls
164 lines (142 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package graph;
import java.util.*;
import support.graph.CS16Edge;
import support.graph.CS16Vertex;
import support.graph.Graph;
import support.graph.PageRank;
/**
* In this class you will implement one of many different versions
* of the PageRank algorithm. This algorithm will only work on
* directed graphs. Keep in mind that there are many different ways
* to handle sinks.
*
* Make sure you review the help slides and handout for details on
* the PageRank algorithm.
*
*/
public class MyPageRank<V> implements PageRank<V> {
private Graph<V> _g;
private List<CS16Vertex<V>> _vertices;
private Map<CS16Vertex<V>, Double> _vertsToRanks;
private List<CS16Vertex<V>> _graphSinks;
private List<Integer> _outgoingEdges;
private List<Double> _previousPageRank;
private List<Double> _currentPageRank;
private double _numRounds = 0;
private static final double _dampingFactor = 0.85;
private static final int _maxIterations = 100;
private static final double _error = 0.01;
/**
* TODO: Feel free to add in anything else necessary to store the information
* needed to calculate the rank. Maybe make something to keep track of sinks,
* your ranks, and your outgoing edges?
*/
/**
* The main method that does the calculations! You'll want to call the methods
* that initialize your variables here. You'll also want to decide on a
* type of loop - for loop, do while, or while loop - for your calculations.
*
* @return A Map of every Vertex to its corresponding rank
*
*/
@Override
public Map<CS16Vertex<V>, Double> calcPageRank(Graph<V> g) {
_g = g;
_vertices = new ArrayList<>();
_vertsToRanks = new HashMap<>();
_previousPageRank = new ArrayList<>();
_currentPageRank = new ArrayList<>();
_graphSinks = new ArrayList<>();
_outgoingEdges = new ArrayList<>();
Iterator<CS16Vertex<V>> graphVertices = g.vertices();
while(graphVertices.hasNext()){
CS16Vertex<V> next = graphVertices.next();
_vertices.add(next);
int vertexOutgoingEdges = g.numOutgoingEdges(next);
_outgoingEdges.add(vertexOutgoingEdges);
if(vertexOutgoingEdges == 0){
_graphSinks.add(next);
}
double dividedRank = 1.0 / g.getNumVertices();
_currentPageRank.add(dividedRank);
}
int numVertices = _vertices.size();
do{
this.currIntoPrev(numVertices);
this.handleSinks(numVertices);
this.rankUpdater(numVertices);
_numRounds ++;
} while(!checkForStoppage(numVertices));
for(int i = 0; i < numVertices; i++){
_vertsToRanks.put(_vertices.get(i), _currentPageRank.get(i));
}
return _vertsToRanks;
}
/**
* Method used to move the current page rank of a vertex to its previous page rank
*/
private void currIntoPrev(int numVertices){
for(int i= 0; i < numVertices; i++){
_previousPageRank.add(i, _currentPageRank.get(i));
}
}
/**
* Method used to account for sink pages (those with no outgoing
* edges). There are multiple ways you can implement this, check
* the lecture and help slides!
*/
private void handleSinks(int numVertices) {
double sinkSum = 0;
for (int i = 0; i < numVertices; i++){
double previousRank = _previousPageRank.get(i);
if(_outgoingEdges.get(i) == 0){
sinkSum += previousRank / numVertices;
}
}
for(int i = 0; i < numVertices; i++){
_currentPageRank.set(i, sinkSum);
}
}
/**
* Checks whether either of the stopping conditions have been met for the algorithm to stop running
*/
private boolean checkForStoppage(int numVertices){
for (int i = 0; i < numVertices; i++){
if(_numRounds > _maxIterations || Math.abs(_currentPageRank.get(i) - _previousPageRank.get(i)) > _error){
return false;
}
}
_numRounds ++;
return true;
}
/**
* Updates the current rank of each vertex based on its opposite vertices
*/
private void rankUpdater(int numVertices){
for(int i = 0; i < numVertices; i++) {
CS16Vertex<V> vertex = _vertices.get(i);
Iterator<CS16Edge<V>> incomingEdges = _g.incomingEdges(vertex);
while(incomingEdges.hasNext()){
CS16Edge<V> edge = incomingEdges.next();
CS16Vertex<V> oppositeVertex = _g.opposite(vertex, edge);
this.rankUpdaterHelper(vertex, oppositeVertex);
}
double dampingDiluted = (1-_dampingFactor) / (numVertices);
double currentPageRank = _currentPageRank.get(_vertices.indexOf(vertex));
double dampingAccounted = (_dampingFactor * currentPageRank);
double updatedRank = dampingDiluted + dampingAccounted;
_currentPageRank.set(i, updatedRank);
}
}
/**
* Handles the majority of the mathematical calculation of updated current PageRank
*/
private void rankUpdaterHelper(CS16Vertex<V> vertex, CS16Vertex<V> oppositeVertex){
double currentRank = _currentPageRank.get(_vertices.indexOf(vertex));
double oppositeIndex = _previousPageRank.get(_vertices.indexOf(oppositeVertex));
double previousOutgoingEdges = _outgoingEdges.get(_vertices.indexOf(oppositeVertex));
double previousRankDiluted = oppositeIndex/previousOutgoingEdges;
double rankMath = currentRank + previousRankDiluted;
_currentPageRank.set(_vertices.indexOf(vertex), rankMath);
}
}