Search in sources :

Example 6 with ExprEdge

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

the class GraphFunctions method graphToAdjacencyMatrix.

public static IExpr graphToAdjacencyMatrix(Graph<IExpr, ExprEdge> 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 (ExprEdge 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) ExprEdge(org.matheclipse.core.expression.data.ExprEdge) SparseArrayExpr(org.matheclipse.core.expression.data.SparseArrayExpr) HashMap(java.util.HashMap) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

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