-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagerank.m
108 lines (83 loc) · 2.88 KB
/
pagerank.m
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
% This is the ranking using pagerank method
get_data = fopen('rankingcandidates.dat','r'); % load the data into matlab
M = textscan(get_data,'%s %s %s %s %s','Delimiter',','); % get the name in the data
%get the initial value of the first place
HC = 0;
BS = 0;
JK = 0;
TC = 0;
DT = 0;
%pagerank matrix
PRM = zeros(240,5); % for borda ranking matrix, first row to fifth row represent
% 1st place to 5th place,
% first column to fifth column represent in the order of
% HC, BS, JK, TC, DT
% To get a 250 by 5 PRM matrix, 250 voters for 250 rows, and 5 columns for rank points from 5 to 1
for i = 1:240
for j = 1:5
if M{j}{i} == 'HC'
PRM(i,1) = PRM(i,1) + 6 - j;
elseif M{j}{i} == 'BS'
PRM(i,2) = PRM(i,2) + 6 - j;
elseif M{j}{i} == 'JK'
PRM(i,3) = PRM(i,3) + 6 - j;
elseif M{j}{i} == 'TC'
PRM(i,4) = PRM(i,4) + 6 - j;
elseif M{j}{i} == 'DT'
PRM(i,5) = PRM(i,5) + 6 - j;
end
end
end
% From 5 by 5 PRM2 matrix, PRM2(i,j) means the total number of competitions i defeats j
PRM2 = zeros(5,5);
for i = 1:240
for j = 1:5
for k = j+1:5
if PRM(i,j) > PRM(i,k)
PRM2(j,k) = PRM2(j,k) + 1;
end
end
end
end
% By calculate the whole PRM2 matrix, calculate the total number of competition j defeats i
% is equal to 240 - the total number of competitions i defeats j
for i = 1:5
for j = 1:5
if i < j
PRM2(i,j) = PRM2(i,j);
elseif i == j
PRM2(i,j) = 0;
else
PRM2(i,j) = 240 - PRM2(j,i);
end
end
end
% PRM3 is a 5 by 1 column matrix of the total loss of each candidates
PRM3 = zeros(5,1);
for i = 1:240
for j = 1:5
PRM3(j) = PRM3(j) + 5 - PRM(i,j);
end
end
% PRM4 is the stochastic matrix
PRM4 = zeros(5,5);
for i = 1:5
for j = 1:5
PRM4(i,j) = PRM2(i,j)/PRM3(j);
end
end
fprintf('For pagerank matrix, first row to fifth row represent 1st place to 5th place\n')
fprintf('first column to fifth column represent in the order of HC, BS, JK, TC, DT\n\n')
disp(PRM4)
[V,D] = eig(PRM4);
%eigenvector of the stochastic matrix corresponding to eigenvalue 1
%shows the rank
Eigv = V(:,1);
Rank = [Eigv(1),Eigv(2),Eigv(3),Eigv(4),Eigv(5)];
particpent_name = {'HC','BS','JK','TC','DT'};
[maximum,index] = max(Rank);
[sorted_value,ranked] = sort(Rank, 'descend');
fprintf('The pagerank method rank five candidates in the order of %s %s %s %s %s\n',particpent_name{ranked(1)},particpent_name{ranked(2)},particpent_name{ranked(3)},particpent_name{ranked(4)},particpent_name{ranked(5)})
show_winner_name_text = [particpent_name{index},' wins in the election'];
disp(show_winner_name_text);
show_winner_votes_text = [particpent_name{index},' has won with maximum Borda points: '];