示例#1
0
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;
}
示例#2
0
int main(){

    SmartDigraph    g;
    
    auto a = g.addNode();
    auto b = g.addNode();
    
    for (int i = 0; i<20; i++) {
        g.addArc(a, b);
    }
    
    cout << countArcs( g );

}
示例#3
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;
}