![](/img/trans.png)
[英]Java: Class not found for PageRank algorithm in Apache Hadoop
[英]how to test PageRank algorithm for jung?
我正在嘗試測試jung的Pagerank算法,但似乎這樣做有問題。 我用這部分代碼創建了一個加權和間接圖:
private static String getId(int nodeId)
{
return "Node " + nodeId;
}
private static String getId(int nodeId, int neighborId)
{
return "Edge " + nodeId + " -> " + neighborId;
}
public static Graph<String, Integer> createGraphForPageRank(String graphId, double[][] adjacencyMatrix)
{
Graph<String,Integer> g = new UndirectedSparseGraph <String,Integer>();
for (int nodeId = 0; nodeId < adjacencyMatrix.length; nodeId++)
g.addVertex(getId(nodeId));
for (int nodeId = 0; nodeId < adjacencyMatrix.length; nodeId++)
for (int neighborId = 0; neighborId < adjacencyMatrix[nodeId].length; neighborId++)
if (adjacencyMatrix[nodeId][neighborId]>0)
g.addEdge(neighborId,getId(nodeId),getId(neighborId));
return(g);
}
然后,在主類中,我使用以下代碼來測試圖形上的pagerank:
double[][] adjacencyMatrixForPageRank =FileHelper.calculateSimilaritySentences("E:\\my workspace\\TweetsAnalyser2\\outputFiles\\splittedStemmeredFile-1.txt","");
Graph<String,Integer> g2=FileHelper.createGraphForPageRank("MyGraphForPageRank",adjacencyMatrixForPageRank);
PageRank<String,Integer> pagerank= new PageRank<String,Integer>(g2,alpha1);
pagerank.initialize();
pagerank.setTolerance(0.000001);
pagerank.setMaxIterations(200);
pagerank.evaluate();
但是eclipse會產生此錯誤:線程“ main”中的異常java.lang.IllegalArgumentException:該圖中已經存在帶端點的邊緣4,並且不能與edu.uci.ics.jung.graph.AbstractGraph.getValidatedEndpoints(AbstractGraph。 java:93)at edu.uci.ics.jung.graph.UndirectedSparseGraph.addEdge(UndirectedSparseGraph.java:64)at edu.uci.ics.jung.graph.AbstractGraph.addEdge(AbstractGraph.java:60)at edu.uci .ics.jung.graph.AbstractGraph.addEdge(AbstractGraph.java:55)位於com.tweets.helpers.FileHelper.createGraphForPageRank(FileHelper.java:1496)位於com.tweets.test.Main.main(Main.java:105 )
我知道圖形創建存在問題,但是我不知道如何解決! 有人可以幫幫我嗎。
問題似乎是您定義了一個無向圖 ,並且向該圖添加了兩次相同的節點。 一個(x,y)
形式,另一個(y,x)
-對於x
和y
的相同值。
通過僅從nodeID
而不是從0迭代內部循環來解決此問題:
for (int nodeId = 0; nodeId < adjacencyMatrix.length; nodeId++)
for (int neighborId = nodeId; neighborId < adjacencyMatrix[nodeId].length; neighborId++)
^^^
此外:
g.addEdge(neighborId,getId(nodeId),getId(neighborId));
您的邊緣ID不是唯一的,我認為應該是唯一的,但是我對API不夠熟悉,無法確定。
有幾個問題導致您出錯。
(1)正如@amit所觀察到的,由於圖形是無向的,因此您無需在x到y之間添加一條邊,而在y到x之間添加另一邊。 但是,如果您具有以下代碼:
g.addEdge(edgeId, x, y);
...
g.addEdge(edgeId, y, x);
對addEdge()的第二次調用將被忽略,這很好。
(2)您不能為不同的事件節點集重用邊緣ID; 這就是該錯誤消息告訴您的內容。 邊緣對象(和節點對象)類似於映射鍵:它們必須是唯一的。
您的代碼表明您實際上並不關心邊緣對象本身,這意味着您可以只創建Graph<String, Object>
並在添加邊緣時執行此操作:
g.addEdge(new Object(), x, y);
在JUNG的下一個版本中,這將變得更加容易,我希望它將在幾個月后發布。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.