Search in sources :

Example 1 with ExprWeightedEdge

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

the class GraphFunctions method weightedEdgesToVisjs.

private static void weightedEdgesToVisjs(Map<IExpr, Integer> map, StringBuilder buf, Graph<IExpr, ExprWeightedEdge> graph) {
    Set<ExprWeightedEdge> edgeSet = graph.edgeSet();
    GraphType type = graph.getType();
    boolean first = true;
    if (type.isDirected()) {
        buf.append("var edges = new vis.DataSet([\n");
        for (Object object : edgeSet) {
            if (object instanceof ExprWeightedEdge) {
                ExprWeightedEdge edge = (ExprWeightedEdge) object;
                // {from: 1, to: 3},
                if (first) {
                    buf.append("  {from: ");
                } else {
                    buf.append(", {from: ");
                }
                buf.append(map.get(edge.lhs()));
                buf.append(", to: ");
                buf.append(map.get(edge.rhs()));
                buf.append(", label: '");
                buf.append(edge.weight());
                buf.append("'");
                // , arrows: { to: { enabled: true, type: 'arrow'}}
                buf.append(" , arrows: { to: { enabled: true, type: 'arrow'}}");
                buf.append("}\n");
                first = false;
            }
        }
    } else {
        buf.append("var edges = new vis.DataSet([\n");
        for (Object object : edgeSet) {
            if (object instanceof ExprWeightedEdge) {
                ExprWeightedEdge edge = (ExprWeightedEdge) object;
                // {from: 1, to: 3},
                if (first) {
                    buf.append("  {from: ");
                } else {
                    buf.append(", {from: ");
                }
                buf.append(map.get(edge.lhs()));
                buf.append(", to: ");
                buf.append(map.get(edge.rhs()));
                buf.append(", label: '");
                buf.append(edge.weight());
                buf.append("'");
                buf.append("}\n");
                first = false;
            }
        }
    }
    buf.append("]);\n");
}
Also used : GraphType(org.jgrapht.GraphType) ExprWeightedEdge(org.matheclipse.core.expression.data.ExprWeightedEdge)

Example 2 with ExprWeightedEdge

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

the class GraphFunctions method edgesToVisjs.

private static void edgesToVisjs(Map<IExpr, Integer> map, StringBuilder buf, Graph<IExpr, ExprEdge> g) {
    Set<ExprEdge> edgeSet = g.edgeSet();
    GraphType type = g.getType();
    boolean first = true;
    if (type.isDirected()) {
        buf.append("var edges = new vis.DataSet([\n");
        for (Object object : edgeSet) {
            if (object instanceof ExprEdge) {
                ExprEdge edge = (ExprEdge) object;
                // {from: 1, to: 3},
                if (first) {
                    buf.append("  {from: ");
                } else {
                    buf.append(", {from: ");
                }
                buf.append(map.get(edge.lhs()));
                buf.append(", to: ");
                buf.append(map.get(edge.rhs()));
                // , arrows: { to: { enabled: true, type: 'arrow'}}
                buf.append(" , arrows: { to: { enabled: true, type: 'arrow'}}");
                buf.append("}\n");
                first = false;
            } else if (object instanceof ExprWeightedEdge) {
                ExprWeightedEdge weightedEdge = (ExprWeightedEdge) object;
                // {from: 1, to: 3},
                if (first) {
                    buf.append("  {from: ");
                } else {
                    buf.append(", {from: ");
                }
                buf.append(map.get(weightedEdge.lhs()));
                buf.append(", to: ");
                buf.append(map.get(weightedEdge.rhs()));
                // , arrows: { to: { enabled: true, type: 'arrow'}}
                buf.append(" , arrows: { to: { enabled: true, type: 'arrow'}}");
                buf.append("}\n");
                first = false;
            }
        }
    } else {
        // 
        buf.append("var edges = new vis.DataSet([\n");
        for (Object object : edgeSet) {
            if (object instanceof ExprEdge) {
                ExprEdge edge = (ExprEdge) object;
                // {from: 1, to: 3},
                if (first) {
                    buf.append("  {from: ");
                } else {
                    buf.append(", {from: ");
                }
                buf.append(map.get(edge.lhs()));
                buf.append(", to: ");
                buf.append(map.get(edge.rhs()));
                buf.append("}\n");
                first = false;
            } else if (object instanceof ExprWeightedEdge) {
                ExprWeightedEdge weightedEdge = (ExprWeightedEdge) object;
                // {from: 1, to: 3},
                if (first) {
                    buf.append("  {from: ");
                } else {
                    buf.append(", {from: ");
                }
                buf.append(map.get(weightedEdge.lhs()));
                buf.append(", to: ");
                buf.append(map.get(weightedEdge.rhs()));
                buf.append("}\n");
                first = false;
            }
        }
    }
    buf.append("]);\n");
}
Also used : ExprEdge(org.matheclipse.core.expression.data.ExprEdge) GraphType(org.jgrapht.GraphType) ExprWeightedEdge(org.matheclipse.core.expression.data.ExprWeightedEdge)

Example 3 with ExprWeightedEdge

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

the class GraphFunctions method edgesToIExpr.

private static IASTAppendable[] edgesToIExpr(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;
            if (type.isDirected()) {
                edges.append(F.DirectedEdge(weightedEdge.lhs(), weightedEdge.rhs()));
            } else {
                edges.append(F.UndirectedEdge(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;
            if (type.isDirected()) {
                edges.append(F.DirectedEdge(exprEdge.lhs(), exprEdge.rhs()));
            } else {
                edges.append(F.UndirectedEdge(exprEdge.lhs(), exprEdge.rhs()));
            }
        }
    }
    return new IASTAppendable[] { edges, weights };
}
Also used : ExprEdge(org.matheclipse.core.expression.data.ExprEdge) Set(java.util.Set) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) GraphType(org.jgrapht.GraphType) ExprWeightedEdge(org.matheclipse.core.expression.data.ExprWeightedEdge)

Example 4 with ExprWeightedEdge

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

the class GraphFunctions method weightedGraphToAdjacencyMatrix.

public static IExpr weightedGraphToAdjacencyMatrix(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.C1);
        if (g.containsEdge(rhs, lhs)) {
            trie.put(new int[] { to, from }, F.C1);
        }
    }
    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 5 with ExprWeightedEdge

use of org.matheclipse.core.expression.data.ExprWeightedEdge 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 };
}
Also used : ExprEdge(org.matheclipse.core.expression.data.ExprEdge) Set(java.util.Set) 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