-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
97 lines (83 loc) · 2.24 KB
/
Copy pathmain.cpp
File metadata and controls
97 lines (83 loc) · 2.24 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
#include <iostream>
#include <map>
#include <set>
#include<iterator>
#include <iomanip>
using namespace std;
class AdjacencyList {
map<string, set<string> > Graph; //(in_degree)
map<string, float > Rank ; //(page_rank)
map<string, float> d; //(out_degree)
public:
void initialPagerank();
AdjacencyList();
void insert (string from, string to);
map<string, float > PageRank(int n);
};
//constructor
AdjacencyList::AdjacencyList(){
}
//inserting new page connections
void AdjacencyList::insert(string from, string to) {
Graph[to].insert(from);
d[from]+=1;
if (Graph.find (from) == Graph.end()){
Graph[from] = {};
}
}
//setting initial page rank which is 1/V aka r(0)
void AdjacencyList::initialPagerank() {
float V = Graph.size();
auto itr = Graph.begin();
for(int i =0; i < Graph.size(); i++){
Rank[itr->first] = (1/V);
itr++;
}
}
//taking iteration power and finding final Rank
map<string, float > AdjacencyList::PageRank(int n){
map<string, float > Multiplie = Rank;
if (n == 1){
return Rank;
}
// if n > 1 execute matrix multiplication
else if(n > 1){
for(auto & itr : Graph){
double rank = 0;
float value = 0;
auto pageSet= Graph[itr.first];
auto pages = pageSet.begin();
while(pages != pageSet.end()){
value = (1/d[*pages]);
rank += Multiplie[*pages] * value;
pages++;
}
Rank[itr.first] = rank;
}
}
//loop matrix multiplication until (n-1)=1
return PageRank((n-1));
}
int main()
{
map<string, float > FinalRank;
AdjacencyList PageGraph;
int no_of_lines, power_iterations;
string from, to;
cin >> no_of_lines;
cin >> power_iterations;
for(int i = 0;i < no_of_lines; i++){
cin >> from;
cin >> to;
PageGraph.insert(from, to);
}
PageGraph.initialPagerank();
FinalRank = PageGraph.PageRank(power_iterations);
auto itr = FinalRank.begin();
while(itr != FinalRank.end()){
cout<<itr->first << " ";
//precision sets to 2 decimal places
cout<< fixed << setprecision(2) << itr->second << endl;
itr++;
}
}