Search in sources :

Example 1 with DefaultDirectedWeightedGraph

use of org.jgrapht.graph.DefaultDirectedWeightedGraph 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;
}
Also used : GraphType(org.jgrapht.GraphType) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) DefaultDirectedWeightedGraph(org.jgrapht.graph.DefaultDirectedWeightedGraph) DefaultUndirectedWeightedGraph(org.jgrapht.graph.DefaultUndirectedWeightedGraph) ExprWeightedEdge(org.matheclipse.core.expression.data.ExprWeightedEdge)

Aggregations

GraphType (org.jgrapht.GraphType)1 DefaultDirectedWeightedGraph (org.jgrapht.graph.DefaultDirectedWeightedGraph)1 DefaultUndirectedWeightedGraph (org.jgrapht.graph.DefaultUndirectedWeightedGraph)1 ExprWeightedEdge (org.matheclipse.core.expression.data.ExprWeightedEdge)1 IAST (org.matheclipse.core.interfaces.IAST)1 IExpr (org.matheclipse.core.interfaces.IExpr)1