Trustrank in Java (using webgraph)

Source: Internet
Author: User

Trustrank is a biased PageRank method, which starts from some good seeds to propagate trust. It is very easy to implement based on PageRank.

I implement it in Java using the webgraph and Law jar package, following is the code:

 
/*
 
* Trustrank implement
 
*/
 
PackageCn.edu. dlut. wisdom;
 
 
ImportIt. unimi. DSI. law. Rank. PageRank;
 
ImportIt. unimi. DSI. law. Rank. pagerankpowermethod;
 
ImportIt. unimi. DSI. law. Rank. PageRank. iterationnumberstoppingcriterion;
ImportIt. unimi. DSI. law. Rank. PageRank. normdeltastoppingcriterion;
 
 
 
ImportIt. unimi. DSI. webgraph .*;
 
ImportIt. unimi. DSI. fastutil. Doubles .*;
ImportIt. unimi. DSI. fastutil. ints. intset;
 
 
 
/**
 
*
 
* @ Author you Wang
*/
 
Public ClassTrustrank {
 
 
 
/**
 
* Graph on which tr will run
*/
 
PrivateImmutablegraph g;
 
/**
 
* PageRank implementation
 
*/
PrivatePagerankpowermethod PR;
 
/**
 
* To be used as stopping Criterion
 
*/
Private DoubleThreshold = PageRank. default_threshold;
 
/**
 
* Number of iteration
 
*/
Private IntNumberofiteration = PageRank. default_max_iter;
 
 
 
Private DoubleAlpha = PageRank. default_alpha;
 
 
/**
 
*
 
* @ Param g graph on which tr will run
 
*/
 
PublicTrustrank (immutablegraph g ){
This. G = g;
 
PR =NewPagerankpowermethod (g );
 
 
 
}
 
 
Public VoidSetthreshold (DoubleT ){
 
Threshold = T;
 
}
 
 
Public VoidSetalpha (DoubleAlpha ){
 
This. Alpha = Alpha;
 
}
 
 
Public VoidSetiteration (IntN ){
 
Numberofiteration = N;
 
}
 
 
Public Double[] Getrank (){
 
ReturnPr. rank;
 
}
 
 
 
/**
* Set the biased good seeds
 
* @ Param Seeds
 
*/
 
Public VoidSetgoodseeds (intset seeds ){
 
IntNumnodes = G. numnodes ();
Double[] Arr =New Double[Numnodes];
 
IntSeedsize = seeds. Size ();
 
For(IntI = 0; I <numnodes; I ++)
 
If(Seeds. Contains (I ))
Arr [I] = 1/seedsize;
 
Pr. Start = doublearraylist. Wrap (ARR );
 
}
 
 
 
/**
* Compute the tr scores
 
* @ Throws exception
 
*/
 
Public VoidCompute ()ThrowsException {
 
Pr. Alpha = Alpha;
Pr. stepuntil (PageRank. Or (NewNormdeltastoppingcriterion (threshold ),
 
NewIterationnumberstoppingcriterion (numberofiteration )));
 
}
 
}
 
 

If you hava any question, please just put a review, or contact me by e-mail.

Contact Us

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.