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;
}
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);
}
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 };
}
Aggregations