HITS算法是重要的鏈接分析算法
其中
但是為了空間的考慮
經過分析發現
下面是用Java實現的代碼
package cn
import it
import it
import it
import it
import org
/**
*
* @author You Wang
*/
public class HITS {
/**
* 正向圖
*/
private ImmutableGraph g;
/**
* 反向圖
*/
private ImmutableGraph ig;
/**
* 日志
*/
private final Logger logger;
/**
* 結點數目
*/
private int numNodes;
/**
* 權威分數
*/
private double[] authorityScores;
/**
* 中心分數
*/
private double[] hubScores;
/**
* 兩次權威分數之差絕對值的和
*/
private double authorityNorm;
/**
* 兩次中心分數之差絕對值的和
*/
private double hubNorm;
/**
* 迭代次數
*/
private double numIter =
/**
* 獲取中心差值
* @return
*/
public double getHubNorm() {
return hubNorm;
}
/**
* 獲取權威差值
* @return
*/
public double getAuthorityNorm() {
return authorityNorm;
}
/**
* 獲取權威分數
* @return
*/
public double[] getAuthorityScores() {
return authorityScores;
}
/**
* 獲取中心分數
* @return
*/
public double[] getHubScores() {
return hubScores;
}
/**
* 構造函數
* @param g 要計算的Web圖
*/
public HITS(ImmutableGraph g) {
this
ig = Transform
numNodes = g
authorityScores = new double[numNodes];
hubScores = new double[numNodes];
double is =
for (int i =
authorityScores[i] = is;
hubScores[i] = is;
}
logger = Logger
}
/**
* 設定初始權威分數
* @param scores
*/
public void setInitialAuthorityScores(double[] scores) {
if (scores
throw new IllegalArgumentException(
this
}
/**
* 設定初始中心分數
* @param scores
*/
public void setInitialHubScores(double[] scores) {
if (scores
throw new IllegalArgumentException(
this
}
/**
* 迭代中的一步
*/
public void step() {
(
authorityNorm =
hubNorm =
NodeIterator nit = g
NodeIterator init = ig
double[] as = new double[numNodes];
double[] hs = new double[numNodes];
while(nit
int i = nit
int j = init
assert (i == j);
LazyIntIterator it = init
as[i] =
int k;
while ((k = it
as[i] += hubScores[k];
}
hs[i] =
it = nit
while ((k = it
hs[i] += authorityScores[k];
}
}
// 歸一化處理
normalize(as);
normalize(hs);
authorityNorm = computeNorm(authorityScores
hubNorm = computeNorm(hubScores
authorityScores = as;
hubScores = hs;
(
(
}
/**
* 歸一化
* @param a
*/
private void normalize(double[] a) {
double s =
for (double d : a)
s += d;
for (int i =
a[i] /= s;
}
/**
* 計算絕對差和
* @param a
* @param b
* @return
*/
private double computeNorm(double[] a
if (a
throw new IllegalArgumentException(
double norm =
for (int i =
norm += Math
}
return norm;
}
/**
* 一直迭代
* @param iter 最大迭代次數
*/
public void stepUntil(int iter) {
while (iter
step();
}
/**
* 一直迭代
* @param stopNorm 停止基准
*/
public void stepUntil(double stopNorm) {
while (authorityNorm > stopNorm || hubNorm > stopNorm)
step();
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26820.html