-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagerank_opt.php
140 lines (84 loc) · 2.55 KB
/
pagerank_opt.php
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
<?php
// Inicialization
$k = 100;
$precision = 5;
$start_Nodes = array();
$end_Nodes = array();
$nodes = array();
$shares = array();
$ranks = array();
$old_ranks = array();
$sort_ranks = array();
// Path of nodes
$path = array (
array("A","B"),
array("A","D"),
array("B","C"),
array("B","F"),
array("C","D"),
array("C","E"),
array("D","B"),
array("E","A"),
array("E","D"),
array("F","D")
);
// Print Graph of Nodes
echo '<b> Path: </b><br />';
$size = count( $path );
for ( $i=0; $i<$size; $i++ ){
echo ' '. $path[$i][0]. ' => '. $path[$i][1] . '<br />';
}
// get start nodes
$start_Nodes = array_column( $path, 0 );
// get end nodes
$end_Nodes = array_column( $path, 1 );
// get unique nodes
$nodes = array_unique( $start_Nodes );
// Sort nodes, order the nodes
sort( $nodes );
// Number of unique nodes
$number_Nodes = count( $nodes );
// Populate rank arrays (1/n)
$ranks = array_fill_keys( $nodes, round( ( 1 / $number_Nodes ), $precision ) );
// Count number of paths for each nodes
$countOut = array_count_values( $start_Nodes );
// Print Page Rank inicialization
echo '<br \> Page Rank Initialization <br \> <br \> <b>Page Rank: </b> ';
foreach ( $ranks as $key => $value ) {
echo ' <b>['.$key.']</b> => '.( $ranks[$key] );
}
$j = 1;
// Get shares and ranking
do {
echo '<br \><br \>Iteration nº '. $j . '<br \>';
echo '<hr>';
echo '<br \><b>Shares:</b> ';
foreach ($ranks as $key => $value) {
$shares[$key] = round(($ranks[$key] / $countOut[$key]), $precision);
echo ' <b>['.$key.']</b> => '.($shares[$key]);
}
echo '<br \> <br \><b>Page Rank:</b> ';
$size2 = count($end_Nodes);
$old_ranks = $ranks;
foreach ($ranks as $key => $value) {
$pr = 0;
for ($i=0; $i<$size2; $i++){
if ($end_Nodes[$i] == $key){
$pr += $shares[ $start_Nodes[ $i ] ];
}
}
$ranks[$key]=round(((1-0.85)/$size2)+((0.85)*$pr),$precision);
//$ranks[$key] = $pr;
//$ranks[$key]=round(($pr), $precision);
echo ' <b>['.$key.']</b> => '.($pr);
}
$sort_ranks = $ranks;
arsort($sort_ranks);
echo '<br \> <br \> <b>Ranking: </b>';
foreach ($sort_ranks as $key => $value) {
echo '<b>'.$key.'</b> ';
}
$j++;
$result_array = array_intersect_assoc($old_ranks, $ranks);
if ($j==$k) break;
} while (count($result_array)!=$number_Nodes);