void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { // Verify input char* inputError = "Expected at least 3 parameters\n N - number of nodes\n from - 1xM vector of indices\n to - 1xM vector of indices\n s - (optional) source node in range (1:N)\n Example: [visit] = lemon_bfs(n, from, to, s);"; if (nrhs < 3 || !mxIsNumeric(prhs[0]) || !mxIsNumeric(prhs[1]) || !mxIsNumeric(prhs[2])) mexErrMsgTxt(inputError); mwSize m = mxGetN(prhs[1]); double* x = mxGetPr(prhs[1]); double* y = mxGetPr(prhs[2]); mwSize n = (mwSize)mxGetScalar(prhs[0]); double s = 0; if (mxGetM(prhs[1]) != 1 || mxGetM(prhs[2]) != 1 || mxGetN(prhs[2]) != m) mexErrMsgTxt(inputError); if (nrhs > 3) { if (!mxIsNumeric(prhs[3])) mexErrMsgTxt(inputError); s = mxGetScalar(prhs[3]); if (s < 1 || s > n) mexErrMsgTxt(inputError); } // Read input SmartDigraph g; g.reserveNode(n); g.reserveArc(m); for (mwIndex i = 0; i < n; i++) g.addNode(); for (mwIndex i = 0; i < m; i++) g.addArc(g.nodeFromId(x[i] - 1), g.nodeFromId(y[i] - 1)); // Do stuff Visitor<SmartDigraph> v; BfsVisit<SmartDigraph, Visitor<SmartDigraph>> bfs(g, v); if (s != 0) bfs.run(g.nodeFromId(s - 1)); else bfs.run(); // Create output if (nlhs > 0) { plhs[0] = mxCreateDoubleMatrix(1, v.list.size(), mxREAL); double *visit = mxGetPr(plhs[0]); for (mwIndex i = 0; i < v.list.size(); i++) visit[i] = g.id(v.list[i]) + 1; } return; }
int main() { typedef dim2::Point<int> Point; Palette palette(true, 50); //creates a directed graph DIGRAPH_TYPEDEFS(SmartDigraph); SmartDigraph g; // Create data structures (i.e. maps) associating values to nodes and arcs of the graph IntArcMap lower(g), capacity(g), cost(g),length(g); IntNodeMap supply(g); Node s; SmartDigraph::NodeMap<int> colors(g), shapes(g); SmartDigraph::NodeMap<double> sizes(g); SmartDigraph::NodeMap<Point> coords(g); SmartDigraph::ArcMap<int> acolors (g), widths(g); SmartDigraph::NodeMap<string> label(g); // Read DIMACS input file ifstream input("USA-road-d.NY.gr"); readDimacsSp(input, g, length, s); input.close(); cout << "Cant Nodos: "<<g.maxNodeId(); colors[g.nodeFromId(0)]=4; sizes[g.nodeFromId(0)]= 100; shapes[g.nodeFromId(0)] = 2; label[g.nodeFromId(0)] = 'A'; coords[g.nodeFromId(0)] = Point(0,300); coords[g.nodeFromId(1)] = Point(200,300); coords[g.nodeFromId(2)] = Point(-100,300); coords[g.nodeFromId(3)] = Point(400,300); graphToEps(g, "salida.eps").title("Salida Grafo").arcWidths(widths).nodeColors(composeMap(palette,colors)).arcColors(composeMap(palette,acolors)).nodeScale(1).absoluteNodeSizes().absoluteArcWidths().coords(coords).nodeSizes(sizes).nodeShapes(shapes).nodeTexts(label).nodeTextSize(1).run(); return 0; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { // Verify input char* inputError = "Expected at least 4 parameters\n N - number of nodes\n from - 1xM vector of indices\n to - 1xM vector of indices\n cap - 1xM vector of capacities\n s - (optional) source node in range (1:N)\n Example: [value, cut] = lemon_haoorlin(n, from, to, cap);"; if (nrhs < 4 || !mxIsNumeric(prhs[0]) || !mxIsNumeric(prhs[1]) || !mxIsNumeric(prhs[2]) || !mxIsNumeric(prhs[3])) mexErrMsgTxt(inputError); mwSize m = mxGetN(prhs[1]); double* x = mxGetPr(prhs[1]); double* y = mxGetPr(prhs[2]); double* c = mxGetPr(prhs[3]); mwSize n = (mwSize)mxGetScalar(prhs[0]); double s = 0; if (mxGetM(prhs[1]) != 1 || mxGetM(prhs[2]) != 1 || mxGetM(prhs[3]) != 1 || mxGetN(prhs[2]) != m || mxGetN(prhs[3]) != m) mexErrMsgTxt(inputError); if (nrhs > 4) { if (!mxIsNumeric(prhs[4])) mexErrMsgTxt(inputError); s = mxGetScalar(prhs[4]); if (s < 1 || s > n) mexErrMsgTxt(inputError); } // Read input SmartDigraph g; g.reserveNode(n); g.reserveArc(m); typedef SmartDigraph::ArcMap<double> ArcMap; typedef SmartDigraph::NodeMap<double> NodeMap; ArcMap cap(g); for (mwIndex i = 0; i < n; i++) g.addNode(); for (mwIndex i = 0; i < m; i++) { SmartDigraph::Arc arc = g.addArc(g.nodeFromId(x[i] - 1), g.nodeFromId(y[i] - 1)); cap[arc] = c[i]; } // Do stuff HaoOrlin<SmartDigraph, ArcMap> haoorlin(g, cap); if (s != 0) haoorlin.run(g.nodeFromId(s - 1)); else haoorlin.run(); // Create output if (nlhs > 0) { plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); double *value = mxGetPr(plhs[0]); value[0] = haoorlin.minCutValue(); } if (nlhs > 1) { NodeMap cutMap(g); haoorlin.minCutMap(cutMap); plhs[1] = mxCreateLogicalMatrix(1, n); bool *cut = mxGetLogicals(plhs[1]); for (mwIndex i = 0; i < n; i++) cut[i] = cutMap[g.nodeFromId(i)]; } return; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { // Verify input char* inputError = "Expected at least 3 parameter\n N - number of nodes\n from - 1xN vector of indices\n to - 1xM vector of indices\n cost - (optional) 1xM vector of edge costs (default: 1)\n lower - (optional) 1xM vector of flow lower bound (default: 0)\n upper - (optional) 1xM vector of flow upper bound (default: INF)\n supply - (optional) 1xN vector of node supply (default: 0)\n Example: [value, flow, pot] = lemon_networksimplex(n, from, to, 'cost', cost, 'lower', lower, 'supply', supply);"; if (nrhs < 3 || nrhs % 2 == 0 || !mxIsNumeric(prhs[0]) || !mxIsNumeric(prhs[1]) || !mxIsNumeric(prhs[2])) mexErrMsgTxt(inputError); mwSize m = mxGetN(prhs[1]); double* x = mxGetPr(prhs[1]); double* y = mxGetPr(prhs[2]); mwSize n = (mwSize)mxGetScalar(prhs[0]); if (mxGetM(prhs[1]) != 1 || mxGetM(prhs[2]) != 1 || mxGetN(prhs[2]) != m) mexErrMsgTxt(inputError); double *c = NULL, *l = NULL, *u = NULL, *s = NULL; for (int i = 3; i < nrhs; i += 2) { mwSize len = mxGetN(prhs[i]) + 1; char* str = (char*) mxCalloc(len, sizeof(char)); mxGetString(prhs[i], str, len); if (strcmp(str, "cost") == 0) { if (!mxIsNumeric(prhs[i + 1]) || mxGetM(prhs[i + 1]) != 1 || mxGetN(prhs[i + 1]) != m) mexErrMsgTxt(inputError); c = mxGetPr(prhs[i + 1]); } else if (strcmp(str, "lower") == 0) { if (!mxIsNumeric(prhs[i + 1]) || mxGetM(prhs[i + 1]) != 1 || mxGetN(prhs[i + 1]) != m) mexErrMsgTxt(inputError); l = mxGetPr(prhs[i + 1]); } else if (strcmp(str, "upper") == 0) { if (!mxIsNumeric(prhs[i + 1]) || mxGetM(prhs[i + 1]) != 1 || mxGetN(prhs[i + 1]) != m) mexErrMsgTxt(inputError); u = mxGetPr(prhs[i + 1]); } else if (strcmp(str, "supply") == 0) { if (!mxIsNumeric(prhs[i + 1]) || mxGetM(prhs[i + 1]) != 1 || mxGetN(prhs[i + 1]) != n) mexErrMsgTxt(inputError); s = mxGetPr(prhs[i + 1]); } else mexErrMsgTxt(inputError); } // Read input SmartDigraph g; g.reserveNode(n); g.reserveArc(m); typedef SmartDigraph::ArcMap<double> ArcMap; typedef SmartDigraph::NodeMap<double> NodeMap; ArcMap lower(g); ArcMap upper(g); ArcMap cost(g); NodeMap supply(g); for (mwIndex i = 0; i < n; i++) { SmartDigraph::Node node = g.addNode(); if (s) supply[node] = s[i]; } for (mwIndex i = 0; i < m; i++) { SmartDigraph::Arc arc = g.addArc(g.nodeFromId(x[i] - 1), g.nodeFromId(y[i] - 1)); if (c) cost[arc] = c[i]; if (l) lower[arc] = l[i]; if (u) upper[arc] = u[i]; } // Do stuff NetworkSimplex<SmartDigraph, double> networksimplex(g); if (c) networksimplex.costMap<ArcMap>(cost); if (l) networksimplex.lowerMap<ArcMap>(lower); if (u) networksimplex.upperMap<ArcMap>(upper); if (s) networksimplex.supplyMap<NodeMap>(supply); networksimplex.run(); // Create output if (nlhs > 0) { plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); double *total = mxGetPr(plhs[0]); total[0] = networksimplex.totalCost(); } if (nlhs > 1) { ArcMap flowMap(g); networksimplex.flowMap(flowMap); plhs[1] = mxCreateDoubleMatrix(1, m, mxREAL); double *flow = mxGetPr(plhs[1]); for (mwIndex i = 0; i < m; i++) flow[i] = flowMap[g.arcFromId(i)]; } if (nlhs > 2) { NodeMap potMap(g); networksimplex.potentialMap(potMap); plhs[2] = mxCreateDoubleMatrix(1, n, mxREAL); double *pot = mxGetPr(plhs[2]); for (mwIndex i = 0; i < n; i++) pot[i] = potMap[g.nodeFromId(i)]; } return; }