# Simple PageRank algorithm

Source: Internet
Author: User
Keywords Algorithm value implementation
PageRank algorithm

PageRank algorithm is Google once Shong "leaning on the Sky Sword", The algorithm by Larry Page and http://www.aliyun.com/zixun/aggregation/16959.html ">sergey Brin was invented at Stanford University, and the paper was clicked to download: the PageRank citation ranking:bringing to the Web.

In this paper, some reference documents are used to elicit the problems, then some realization algorithms of PageRank are given, and finally the PageRank algorithm is extended to the mapreduce frame.

PageRank's core ideas are 2 points:

1. If a Web page is linked to many other Web pages, this page is more important, that is, the PageRank value will be relatively high;

2. If a page with a PageRank value is linked to a different page, the PageRank value of the page being linked to will increase accordingly.

Here is a picture from Wikipedia, each of which represents a Web page, and the size of the ball reflects the size of the PageRank value of the page. Links to page B and Web page e are many, so B and e have a higher PageRank value, and although very few pages point to C, the most important page B points to C, so C's PageRank value is larger than E.

Reference content:

2.Google Secrets-PageRank thoroughly explain Chinese version

3. Numerical analysis and algorithm Page 161 application Example: Google's PageRank algorithm

4.Numeric Methods with matlab or Chinese translation version matlab numerical calculation

5. Calculate PageRank Page PageRank and Markov chains using MapReduce thought

1. Problem background

From reference 3

2. Mathematical modeling

From reference 3, Understanding Web link matrix $g$, Markov process ("web surfing"), transfer matrix $a$, probability $p$ for users to click on a link address in the current page (generally 0.85).

Finally, we get an equation $ax=x$, which is actually a eigenvector that $a$ the eigenvalues of a matrix of 1.

The following section uses the disk theorem to explain that 1 is the main eigenvalue of the matrix $a$, so we can solve it by using the power method.

A detailed introduction to the Power method reference another article numerical Methods Using Matlab: Chapter three matrix eigenvalue and singular value solution

3. Solving PageRank

Suppose the page link model as shown on the right side of the figure above.

(1) Power method

There is a PageRank simple algorithm on the wiki, it does not consider the transfer probability, but uses the iterative method, each time updates each page PageRank value, the update way is to divide each page's PageRank value evenly to it points to all pages, Each Web page accumulates all the pages that point to it as the value of its PageRank value, until the PageRank value of all pages converges or a certain threshold condition is met.

The implementation of the PageRank algorithm in the MapReduce framework is followed by this idea. Considering the transition probability and the algorithm is similar, multiply the last transfer probability plus a random jump probability.

According to the above thought, the following MATLAB code implementation can get the PageRank value of each page.

N=6;i=[2 3 4 4 5 6 1 6 1];j=[1 2 2 3 3 3 4 5-6]; G=sparse (i,j,1,n,n);% power Methodfor j = 1:n L{j} = Find (G (:, j)); C (j) = Length (l{j}); ENDP =. 85;delta = (1-p)/n;x = Ones (n,1)/n;z = zeros (n,1); cnt = 0;while Max (ABS (z)) &gt; 0001 z = x; x = zeros (n,1); For j = 1:n if C (j) = = 0 x = x + z (j)/n;% transfer to any Web page else x (l{j}) = X (l{j}) + Z (j)/C (j);% pagerank the last value to all pointed pages end x = p*x + Del Ta; CNT = Cnt+1;end

The resulting vector $x$ saves the PageRank value of each page, although the number of links is the same, but the page ① is higher than the page ④ and page ⑤, and the ② value of the page PageRank is the second highest, because the page ① link to it above, the equivalent of stained with the page ① light.

x = 0.2675 0.2524 0.1323 0.1698 0.0625 0.1156

This article gives a python version of the algorithm implementation, the blogger uses a Third-party module Python-graph,python-graph module to implement a number of graph algorithms, the use of the module, the first need to install the code as follows:

Easy_install Python-graph-coreeasy_install Python-graph-dot

The python version of the algorithm implementation:

The results obtained after 32 iterations are the same as the previous results:

This is no.32 iteration{' 1 ': 0.2675338708706491, ' 3 ': 0.13227261904986046, ' 2 ': 0.2524037902400518, ' 5 ': 0.062477242064127136, ' 4 ': 0.1697488529161491, ' 6 ': 0.1155828978186352}

(2) using the special structure of Markov matrices

From reference content 4, where $\delta=\frac{1-p}{n}$

That is, to decompose the matrix $a$, we do not need to display the matrix $a$, and then we can solve a linear equation group.

function x = Pagerank1 (G)% PAGERANK1 Google ' s PageRank flushes version 1-hujiawei%if Nargin &lt; 3, p =. 85; endp=0.85;% eliminate any self-referential linksg = G-diag (Diag (g));% c = out-degree, r = in-degree[n,n] = size (g); c = SUM (g,1 %each row ' s sumr = SUM (g,2);%each Col ' sum% Scale column sums to is 1 (or 0 where there are no out links). K = Find (c~=0);D = Spar Se (k,k,1./c (k), n,n);% Solve (i-p*g*d) x = EE = ones (n,1); I = Speye (n,n); x = (i-p*g*d) \e;% normalize so sum (x) = = 1.x = X/su m (x);

(3) Ingenious solution: Inverse iterative algorithm

The ingenious use of the accuracy error in MATLAB results in the $i-a$ of a singular matrix into a nonsingular matrix, while the runtime only has some warning hints, but the results are the same as other algorithms.

function x = pagerank2 (G)% PAGERANK1 Google's PageRank flushes version 2-hujiawei% using inverse according method%if Nargin &lt; 3, p =. 85; endp=0.85;% eliminate any self-referential linksg = G-diag (Diag (g));% c = out-degree, r = in-degree[n,n] = size (g); c = SUM (g,1 %each row ' s sumr = SUM (g,2);%each Col ' sum% Scale column sums to is 1 (or 0 where there are no out links). K = Find (c~=0);D = Spar Se (k,k,1./c (k), n,n);% Solve (i-p*g*d) x = EE = ones (n,1); I = Speye (n,n);% x = (i-p*g*d) \e;delta= (1-p)/n; a=p*g*d+delta;x= (i) \e;% normalize so sum (x) = = 1.x = X/sum (x);

Finally, attach a good code from reference 4 to simulate random surfing generate matrix $g$ code

Using the idea of iteration (or Power method) on the front wiki to implement the PageRank algorithm under the MapReduce framework is simple, you can read the reference content 5 first.

Here's a job for my big data, which requires a simple algorithm on the wiki to implement the PageRank algorithm in the MapReduce framework. The data set is a relationship between Twitter users and can be viewed as a relationship between Web pages, but the TA does not require writing code and running the dataset (there are more than 1G), so the following is an ideal version of the Python version, not validated by the actual large dataset, and Bloggers are not yet able to mapreduce some of the functions in the framework of Python, so a concise and testable PageRank algorithm is implemented.

1. Input output Format

The input of the map function is the < node, the list of edges drawn from the node, where the node is a class, contains its current PageRank value, the output is a < node, the reverse node PageRank value/The total number of reverse node leads to the sum >;

The input of the reduce function is the < node, the total number of reverse node PageRank value/Reverse node extraction edge, and the output is the < node, the edge list from the node, where the node contains the updated PageRank value.

Pseudocode: [Two for a while wrote in English form]

Process the data to the form of {node i:[its adjacent node list],...} While the sum of difference inclusive the NonBlank PageRank values &lt; threshold map ({node i:[its adjacent node list],...}): Map_ output={} for every node J. Adjacent node List:put or sum up {j: (I, PageRank (i)/length (adjacent node list)} into Map_output return map_output reduce (map_output): reduce_output={} for every entry {j: (I, PageRank (i)/length (adjacent node list)} Map_output:put or sum up all values PageRank values for node J and its adjacent node list to Reduce_output return Reduce_ Output2. Sample Demo

Suppose the user 1,2,3,4 is the relationship shown in the following figure:

Suppose there are 2 mapper (A and B) and a reducer (C), the PageRank value of the initial 4 nodes is 0.25

Among them, about the user 1 and 2 of the data is Mappera read and processed, about the user 3 and 4 of the data is Mapperb read and processed [verified, even if a user's data is by different mapper to read, eventually converge to the result is almost]

The input and output of the map are as follows:

Reduce's input output is as follows, the input is 2 mapper output, the result of the output updated the node PageRank value

The reducer is processed and then the result is entered into mapper processing until the iteration exceeds the set value or the sum of the difference of the PageRank values of all nodes obtained after the two iterations (or it can be two norm) less than the set threshold.

3. Experimental results of the example

(1) First of all, using the power method in MATLAB to calculate the results of the example in the case of p=1.0 [its main role is to verify the correctness of the later Python version]

Matlab source code is as follows:

n=4;i=[2, 3 4 3 4 4 1 2];j=[1 1 1 2 2 3 3 4]; G=sparse (I,j,1,n,n); [N,n] = size (g); for j = 1:n L{j} = Find (g (:, j)); C (j) = Length (l{j}); end% power Methodp=1.0;delta = (1-p)/n;x = Ones (n,1)/n;z = zeros (n,1); cnt = 0;while Max (ABS (z)) &gt;. 0001 z = x; x = zeros (n,1); For j = 1:n if C (j) = = 0 x = x + z (j)/N; else X (l{j}) = X (l{j}) + Z (j)/C (j); End end x = p*x + Delta; CNT = cnt+1;endsprintf (' PageRank result: ') x

The results are:

0.10720.35710.21430.3214

(2) The version of page rank in MATLAB did not iterate with the idea of mapreduce, so I wrote another python version of the PageRank algorithm using MapReduce idea (note: Instead of using the Python map and reduce functions to implement it, I use a more easily understood implementation, with a threshold of 0.0001 and a maximum of 100 iterations.

# coding=utf-8__author__ = ' Hujiawei ' __doc__ = ' pagerank MapReduce ' class Node:def __init__ (SELF,ID,PK): Self.id=id Self.pk=pkdef Pk_map (map_input): map_output={} for Node,outlinks in Map_input.items (): For link in Outlinks:size=len ( outlinks) if link in map_output:map_output[link]+= (float) (node.pk) minimal else:map_output[link]= (float) (node.pk) minimal Return map_outputdef pk_reduce (reduce_input): for result in Reduce_input:for Node,value in Result.items (): node.pk+= Valuedef pk_clear (nodes): For node in nodes:node.pk=0def pk_last (nodes): lastnodes=[] for node in Nodes:lastnodes.append ( Node (node.id,node.pk)) return lastnodesdef Pk_diff (nodes,lastnodes): diff=0 to I in range (len (nodes)): Print (' node PK%f , last node PK%f '% (nodes[i].pk, lastnodes[i].pk)) Diff+=abs (nodes[i].pk-lastnodes[i].pk) return diffdef pk_test1 (): Node1 = node (1, 0.25) Node2 = node (2, 0.25) Node3 = node (3, 0.25) NODE4 = node (4, 0.25) nodes = [Node1, Node2, Node3, Node4] Thresho LD = 0.0001 max_iters = for iteR_count in Range (max_iters): Iter_count + + 1 lastnodes=pk_last (nodes) print (' ============ map count%d ================= ' % (iter_count)) in1 = {node1: [Node2, Node3, Node4], node2: [Node3, Node4]} in2 = {node3: [Node1, Node4], node4: [Node2]} MAPOUT1 = Pk_map (in1) mapout2 = Pk_map (in2) for node, value in Mapout1.items (): Print str (node.id) + "' + str (value) for node, Value in Mapout2.items (): Print str (node.id) + ' + str (value) print (' ============ reduce count%d ================= '% (iter _count)) Reducein = [Mapout1, Mapout2] pk_clear (nodes) pk_reduce (Reducein) for node in Nodes:print str (node.id) + ' + str ( node.pk) Diff=pk_diff (nodes,lastnodes) if diff &lt; Threshold:breaki
Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.