Reputation: 33
I'm trying to test Pagerank algorithm for jung but it seems that I have a problem in doing it. I have created a weighted and indirect graph with this portion of code:
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);
}
then, in the main class, I used this code to test pagerank on my graph:
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();
but eclipse generates this error: Exception in thread "main" java.lang.IllegalArgumentException: edge 4 already exists in this graph with endpoints and cannot be added with endpoints at 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) at com.tweets.helpers.FileHelper.createGraphForPageRank(FileHelper.java:1496) at com.tweets.test.Main.main(Main.java:105)
I know that there is a problem with the graph creation, but I don't know how to solve it!!!! Can someone please help me.
Upvotes: 0
Views: 231
Reputation: 2704
There are a couple of problems that led to your error.
(1) As @amit observed, since your graph is undirected, you don't need to add an edge from x to y and another one from y to x. However, if you have the following code:
g.addEdge(edgeId, x, y);
...
g.addEdge(edgeId, y, x);
The second call to addEdge() will be silently ignored, which is fine.
(2) You can't reuse edge IDs for different sets of incident nodes; that's what that error message is telling you. Edge objects (and node objects) are analogous to map keys: they have to be unique.
Your code suggests that you don't actually care about the edge objects per se, which means that you can just create a Graph<String, Object>
and do this when you add an edge:
g.addEdge(new Object(), x, y);
This will get easier in JUNG in its next version, which I hope will be out in a few months. :)
Upvotes: 0
Reputation: 178471
The problem seems to be you defined an undirected graph, and you add the same node twice to it. One in the form of (x,y)
, and one in the form of (y,x)
- for the same values of x
and y
.
Solve it by iterating in your inner loop only from the nodeID
, and not from 0:
for (int nodeId = 0; nodeId < adjacencyMatrix.length; nodeId++)
for (int neighborId = nodeId; neighborId < adjacencyMatrix[nodeId].length; neighborId++)
^^^
In addition:
g.addEdge(neighborId,getId(nodeId),getId(neighborId));
your edge ID is not unique, which I think it should be, but I am not familiar enough with the API to be sure.
Upvotes: 1