use of org.jgrapht.GraphType in project symja_android_library by axkr.
the class GraphFunctions method edgesToRules.
private static IASTAppendable[] edgesToRules(Graph<IExpr, ?> g) {
Set<Object> edgeSet = (Set<Object>) g.edgeSet();
IASTAppendable edges = F.ListAlloc(edgeSet.size());
IASTAppendable weights = null;
GraphType type = g.getType();
for (Object edge : edgeSet) {
if (edge instanceof ExprWeightedEdge) {
ExprWeightedEdge weightedEdge = (ExprWeightedEdge) edge;
edges.append(F.Rule(weightedEdge.lhs(), weightedEdge.rhs()));
if (weights == null) {
weights = F.ListAlloc(edgeSet.size());
}
weights.append(weightedEdge.weight());
} else if (edge instanceof ExprEdge) {
ExprEdge exprEdge = (ExprEdge) edge;
edges.append(F.Rule(exprEdge.lhs(), exprEdge.rhs()));
}
}
return new IASTAppendable[] { edges, weights };
}
use of org.jgrapht.GraphType 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;
}
use of org.jgrapht.GraphType in project symja_android_library by axkr.
the class GraphFunctions method weightedEdgesToIExpr.
/**
* Return an array of 2 lists. At index 0 the list of edges. At index 1 the list of corresponding
* weights.
*
* @param graph
* @return an array of 2 lists. At index 0 the list of edges. At index 1 the list of corresponding
* weights.
*/
private static IASTAppendable[] weightedEdgesToIExpr(Graph<IExpr, ExprWeightedEdge> graph) {
Set<ExprWeightedEdge> edgeSet = graph.edgeSet();
IASTAppendable edges = F.ListAlloc(edgeSet.size());
IASTAppendable weights = F.ListAlloc(edgeSet.size());
GraphType type = graph.getType();
if (type.isDirected()) {
for (ExprWeightedEdge edge : edgeSet) {
edges.append(F.DirectedEdge(edge.lhs(), edge.rhs()));
weights.append(edge.weight());
}
} else {
for (ExprWeightedEdge edge : edgeSet) {
edges.append(F.UndirectedEdge(edge.lhs(), edge.rhs()));
weights.append(edge.weight());
}
}
return new IASTAppendable[] { edges, weights };
}
Aggregations