use of org.jgrapht.graph.DefaultUndirectedWeightedGraph in project symja_android_library by axkr.
the class GraphFunctions method createWeightedGraph.
/**
* Create an internal DataExpr Graph.
*
* @param arg1
* @return
*/
private static GraphExpr<ExprWeightedEdge> createWeightedGraph(final IAST vertices, final IAST arg1, final IAST edgeWeight) {
if (arg1.size() != edgeWeight.size()) {
return null;
}
Graph<IExpr, ExprWeightedEdge> g;
GraphType t = arg1.isListOfEdges();
if (t != null) {
if (t.isDirected()) {
g = new DefaultDirectedWeightedGraph<IExpr, ExprWeightedEdge>(ExprWeightedEdge.class);
} else {
g = new DefaultUndirectedWeightedGraph<IExpr, ExprWeightedEdge>(ExprWeightedEdge.class);
}
IAST list = arg1;
for (int i = 1; i < list.size(); i++) {
IAST edge = list.getAST(i);
g.addVertex(edge.arg1());
g.addVertex(edge.arg2());
g.addEdge(edge.arg1(), edge.arg2());
}
if (t.isDirected()) {
DefaultDirectedWeightedGraph gw = (DefaultDirectedWeightedGraph<IExpr, ExprWeightedEdge>) g;
for (int i = 1; i < list.size(); i++) {
IAST edge = list.getAST(i);
gw.setEdgeWeight(edge.arg1(), edge.arg2(), edgeWeight.get(i).evalDouble());
}
} else {
DefaultUndirectedWeightedGraph gw = (DefaultUndirectedWeightedGraph<IExpr, ExprWeightedEdge>) g;
for (int i = 1; i < list.size(); i++) {
IAST edge = list.getAST(i);
gw.setEdgeWeight(edge.arg1(), edge.arg2(), edgeWeight.get(i).evalDouble());
}
}
return GraphExpr.newInstance(g);
}
return null;
}
Aggregations