如何将字符串转换为图中的顶点?
how to convert a string to a vertex in a graph?
我正在尝试 运行 命中 jung 库的算法。该算法的输入是一个图,所以我创建了一个图并将其用作该算法的输入。但是当我 运行 它时,Eclipse 生成此错误:
Exception in thread "main" java.lang.ClassCastException: java.lang.String
cannot be cast to edu.uci.ics.jung.graph.Vertex
at com.tweets.algorithms.HITS.initialize(HITS.java:52)
at com.tweets.algorithms.HITS.<init>(HITS.java:41)
at com.tweets.test.Main.main(Main.java:121)
这是我创建图表的方式:
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> createGraph1(String graphId,
boolean[][] adjacencyMatrix){
Graph<String,Integer> g = new DirectedSparseGraph <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])
//g.addEdge(getId(nodeId, neighborId),nodeId , neighborId);
g.addEdge(neighborId,getId(nodeId),getId(neighborId));
return(g);
}
这就是我在主 class 中调用命中算法的方式:
HITS ranker = new HITS(g);
ranker.evaluate();
ranker.printRankings(true,false);
而IDE表示错误在这个bloc in the hits class:
protected void initialize(Graph g) {
super.initialize(g, true, false);
mPreviousAuthorityScores = new HashMap();
mPreviousHubScores = new HashMap();
for (Iterator vIt = g.getVertices().iterator(); vIt.hasNext();) {
Vertex currentVertex = (Vertex) vIt.next();
setRankScore(currentVertex, 1.0, AUTHORITY_KEY);
setRankScore(currentVertex, 1.0, HUB_KEY);
mPreviousAuthorityScores.put(currentVertex, new MutableDouble(0));
mPreviousHubScores.put(currentVertex, new MutableDouble(0));
}
}
我确定这是一个转换错误,但我无法解决它。
有人可以帮我吗
您的类路径中似乎有两个不兼容的 JUNG 版本:一个使用泛型(例如 Graph)使其成为版本 2.x,另一个使用旧式 Vertex 接口,这是版本 1.x.
您需要从类路径中删除版本 1.x。
我正在尝试 运行 命中 jung 库的算法。该算法的输入是一个图,所以我创建了一个图并将其用作该算法的输入。但是当我 运行 它时,Eclipse 生成此错误:
Exception in thread "main" java.lang.ClassCastException: java.lang.String
cannot be cast to edu.uci.ics.jung.graph.Vertex
at com.tweets.algorithms.HITS.initialize(HITS.java:52)
at com.tweets.algorithms.HITS.<init>(HITS.java:41)
at com.tweets.test.Main.main(Main.java:121)
这是我创建图表的方式:
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> createGraph1(String graphId,
boolean[][] adjacencyMatrix){
Graph<String,Integer> g = new DirectedSparseGraph <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])
//g.addEdge(getId(nodeId, neighborId),nodeId , neighborId);
g.addEdge(neighborId,getId(nodeId),getId(neighborId));
return(g);
}
这就是我在主 class 中调用命中算法的方式:
HITS ranker = new HITS(g);
ranker.evaluate();
ranker.printRankings(true,false);
而IDE表示错误在这个bloc in the hits class:
protected void initialize(Graph g) {
super.initialize(g, true, false);
mPreviousAuthorityScores = new HashMap();
mPreviousHubScores = new HashMap();
for (Iterator vIt = g.getVertices().iterator(); vIt.hasNext();) {
Vertex currentVertex = (Vertex) vIt.next();
setRankScore(currentVertex, 1.0, AUTHORITY_KEY);
setRankScore(currentVertex, 1.0, HUB_KEY);
mPreviousAuthorityScores.put(currentVertex, new MutableDouble(0));
mPreviousHubScores.put(currentVertex, new MutableDouble(0));
}
}
我确定这是一个转换错误,但我无法解决它。 有人可以帮我吗
您的类路径中似乎有两个不兼容的 JUNG 版本:一个使用泛型(例如 Graph)使其成为版本 2.x,另一个使用旧式 Vertex 接口,这是版本 1.x.
您需要从类路径中删除版本 1.x。