Search in sources :

Example 6 with ExprWeightedEdge

use of org.matheclipse.core.expression.data.ExprWeightedEdge 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)

Example 7 with ExprWeightedEdge

use of org.matheclipse.core.expression.data.ExprWeightedEdge in project symja_android_library by axkr.

the class GraphFunctions method weightedGraphToWeightedAdjacencyMatrix.

public static IExpr weightedGraphToWeightedAdjacencyMatrix(Graph<IExpr, ExprWeightedEdge> g) {
    Set<IExpr> vertexSet = g.vertexSet();
    int size = vertexSet.size();
    Map<IExpr, Integer> map = new HashMap<IExpr, Integer>();
    int indx = 1;
    for (IExpr expr : vertexSet) {
        map.put(expr, indx++);
    }
    final Trie<int[], IExpr> trie = Config.TRIE_INT2EXPR_BUILDER.build();
    for (ExprWeightedEdge edge : g.edgeSet()) {
        IExpr lhs = edge.lhs();
        IExpr rhs = edge.rhs();
        int from = map.get(lhs);
        int to = map.get(rhs);
        trie.put(new int[] { from, to }, F.num(edge.weight()));
        if (g.containsEdge(rhs, lhs)) {
            trie.put(new int[] { to, from }, F.num(edge.weight()));
        }
    }
    return new SparseArrayExpr(trie, new int[] { size, size }, F.C0, false);
}
Also used : IInteger(org.matheclipse.core.interfaces.IInteger) SparseArrayExpr(org.matheclipse.core.expression.data.SparseArrayExpr) HashMap(java.util.HashMap) IExpr(org.matheclipse.core.interfaces.IExpr) ExprWeightedEdge(org.matheclipse.core.expression.data.ExprWeightedEdge)

Example 8 with ExprWeightedEdge

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

Aggregations

ExprWeightedEdge (org.matheclipse.core.expression.data.ExprWeightedEdge)8 GraphType (org.jgrapht.GraphType)6 ExprEdge (org.matheclipse.core.expression.data.ExprEdge)3 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)3 IExpr (org.matheclipse.core.interfaces.IExpr)3 HashMap (java.util.HashMap)2 Set (java.util.Set)2 SparseArrayExpr (org.matheclipse.core.expression.data.SparseArrayExpr)2 IInteger (org.matheclipse.core.interfaces.IInteger)2 DefaultDirectedWeightedGraph (org.jgrapht.graph.DefaultDirectedWeightedGraph)1 DefaultUndirectedWeightedGraph (org.jgrapht.graph.DefaultUndirectedWeightedGraph)1 IAST (org.matheclipse.core.interfaces.IAST)1