示例#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
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;
}
int main( int argc, char** argv ){

    string          pathSourceLGF, pathSourceTMP, pathSourceTMP_W, pathTarget;
    int             initTime, endTime;
    Timer           T;
    
    initTime        = 1000;
    endTime         = 3000;
    pathSourceLGF   = "/Users/sonneundasche/Documents/FLI/DATA/03Pork/porkNEW_.lgf";
    pathSourceTMP   = "/Users/sonneundasche/Documents/FLI/DATA/03Pork/porkNEW__time_tmpArcIDs.txt";
    pathSourceTMP_W = "/Users/sonneundasche/Documents/FLI/DATA/03Pork/porkNEW__time_tmpArcIDs_amountOnArc.txt";
    
    map< int, vector< SmartDigraph::Arc > >               Time_ToArcVec;
    map< int, vector< pair< SmartDigraph::Arc, int > > > Time_ToArcVec_Weight;
    
    SmartDigraph                        g;
    SmartDigraph::NodeMap< bool >       activeNodes( g );
    SmartDigraph::ArcMap< bool>         activeArcs( g );
    SmartDigraph::NodeMap< int >        nodeID( g );

    
    digraphReader( g, pathSourceLGF )
    .nodeMap("origID", nodeID)
    .run();
    cout << T.realTime() << endl; T.restart();
    cout << "LGF read\n";

    
    tempGR::readTemporalArcList( g, Time_ToArcVec, pathSourceTMP);
    cout << T.realTime() << endl; T.restart();
    tempGR::readTemporalArcListWeighted( g, Time_ToArcVec_Weight, pathSourceTMP_W );
    cout << T.realTime() << endl; T.restart();
    tempGR::temporalGraphActivator<SmartDigraph>    mActivator( g, activeNodes, activeArcs, Time_ToArcVec);
    cout << T.realTime() << endl; T.restart();
    cout << "TempArcs read\n";
    
    // Activate the Subgraph

    // Gib mir nicht den Wert, sondern den Iterator -
    // -> first gibt mir die Zeit
    // jetzt muss ich aber ->second aufrufen um den Vector zu erhalten
    for (int time = initTime; time < endTime ;time++){
        mActivator.activate( time );
    }
    cout << T.realTime() << endl; T.restart();
    
    
    // --------------------------------------------------------------------------------------------------
    // --- Copy the new System
    // --------------------------------------------------------------------------------------------------
    // A new graph 'f' has to be created to ensure that the LEMON IDs are correct
    
    SubDigraph<SmartDigraph>            subGraph( g, activeNodes, activeArcs);
    SmartDigraph                        f;
    SmartDigraph::ArcMap< SmartDigraph::Arc >       giveTargetArc( g );
    vector< vector < SmartDigraph::Arc > >               resultArcs;
    vector<   vector< pair< SmartDigraph::Arc, int > > > resultArcs_Weight;
    
    
    digraphCopy(subGraph, f)
    .arcRef(  giveTargetArc )
    .run();
    
    cout  << countNodes( g )    << "\tnew Nodes: " << countNodes( f ) << endl;
    cout  << countArcs( g )     << "\tnew Arcs:" << countArcs( f ) << endl;
    
    
    for (int time = initTime; time < endTime; time++) {
        vector< SmartDigraph::Arc >     mArcVec;
        for ( auto currentArc : Time_ToArcVec[ time ]) {
            // go though vector and copy valid into new one
            if ( f.valid( currentArc ) ) {
                mArcVec.push_back( giveTargetArc[ currentArc ] ); // save new Arc ID!
            }
        }
        resultArcs.push_back( mArcVec );
    }
    
    for (int time = initTime; time < endTime; time++) {
        vector< pair < SmartDigraph::Arc, int > >     mPairVec;
        for ( auto currentPair : Time_ToArcVec_Weight[ time ]) {
            // go though vector and copy valid into new one
            if ( f.valid( currentPair.first ) ) {
                mPairVec.push_back( make_pair( giveTargetArc[ currentPair.first ],  currentPair.second ) ); // new ArcID
            }
        }
        resultArcs_Weight.push_back( mPairVec );
    }

    tempGR::writeTempGraph<SmartDigraph>( f, resultArcs, "/Users/sonneundasche/Desktop//pork_FromTime1000_tempArcs.txt");
    tempGR::writeTempGraph_weighted<SmartDigraph>( f, resultArcs_Weight, "/Users/sonneundasche/Desktop/pork_FromTime1000_tempArcs_weighted.txt");
    
    digraphWriter(f, "/Users/sonneundasche/Desktop/pork_FromTime1000.lgf")
    .nodeMap("origID", nodeID)
    .run();
    
    cout << T.realTime() << endl; T.restart();
    cout << "Done\n";
    
    
}
int main() {

    int runsNr  = 100000;
    int nodesNr = 100000;
    int trash;
    
   Graph  graph   ( nodesNr );
   double a = .0123;
    
    Timer T(true);

    T.restart();
    /*
   for ( int n = 0 ; n < runsNr ; ++n ) {

      VertexIterator   v , v_end;
      boost::tie( v , v_end ) = vertices(graph);

//      for ( int m = 0 ; m < 100000 ; ++m )   graph[*v] = (a *= 4.*(1.-a));

      while ( v != v_end ) {
//         graph[*v] = (a *= 4.*(1.-a));
//         get ( boost::vertex_bundle , graph )[*v] = (a *= 4.*(1.-a));
//         ((Graph::stored_vertex*)*v)->m_property = (a *= 4.*(1.-a));  // worx w/ list only!!
         ++v;
        trash++;
      }

   }
    
    cout << "Boost Zeit: " << T.realTime() << endl;
    T.restart();
     

//   for ( int n = 0 ; n < 100 ; ++n ) {
//      
//      auto
//         v      =  graph.vertex_set().begin(),
//         v_end  =  graph.vertex_set().end();
//      
//      while ( v != v_end ) {
////         graph[*v] = (a *= 4.*(1.-a));
//         ++v;
//      }
//   }



   std::list<double> liste ( nodesNr );


   for ( int n = 0 ; n < runsNr ; ++n ) {
      std::list<double>::iterator
         l      = liste.begin(),
         l_end  = liste.end();

//      for ( int m = 0 ; m < 100000 ; ++m )   *l = (a *= 4.*(1.-a));

      while ( l != l_end ) {
//         *l = (a *= 4.*(1.-a));
         ++l;
          trash++;
      }

   }
    cout << "List Zeit: " << T.realTime() << endl;


    
    T.restart();
    ListGraph myGraph;
    for (int i = 0; i < nodesNr; i++) myGraph.addNode();
    cout << "ListDigraph Generation Zeit: " << T.realTime() << endl;
    
    T.restart();
    for (int i = 0; i < runsNr; i++){
        ListGraph::NodeIt nd( myGraph );
        while ( nd != INVALID ) {
//            trash = myGraph.id( nd );
            trash++;
            ++nd;
        }
    }
        
    cout << "ListDigraph Zeit: " << T.realTime() << endl;    

    */
    
    T.restart();
    SmartDigraph mySmart;
    for (int i = 0; i < nodesNr; i++) mySmart.addNode();
    int trashSmart = 0;
    cout << "SmartDigraph Generation Zeit: " << T.realTime() << endl;
    
    T.restart();
    for (int i = 0; i < runsNr; i++) {
        SmartDigraph::NodeIt ns( mySmart );
        while (ns != INVALID) {
            trash = mySmart.id( ns ) * 4;
            trashSmart ++;
            ++ns;
        }
    }
    cout << "SmartDigraph Zeit: " << T.realTime() << endl;
    cout << "trash: " << trashSmart << " | SmartNodes: " << countNodes( mySmart ) << endl;
    
    
}
示例#6
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;
}