Ejemplo n.º 1
0
int main(int argc, const char * argv[])
{
    struct Graph *g = NULL;
    g = Graph(g);
    
    Graph_addNode(g, "a");
    Graph_addNode(g, "b");
    Graph_addNode(g, "c");
    Graph_addNode(g, "d");
    Graph_addNode(g, "e");
    
    
        
    Graph_addEdge(g, 0, 1, 1);
    Graph_addEdge(g, 1, 0, 1);
    
    Graph_addEdge(g, 1, 2, 1);
    Graph_addEdge(g, 2, 1, 1);
    
    Graph_addEdge(g, 2, 3, 1);
    Graph_addEdge(g, 3, 2, 1);
    
    Graph_addEdge(g, 3, 4, 1);
    Graph_addEdge(g, 4, 3, 1);
    
    Graph_addEdge(g, 1, 3, 3);
    Graph_addEdge(g, 3, 1, 3);
    
    
    Graph_printGraph(g);
    
    return 0;
}
Ejemplo n.º 2
0
static bool Graph_read(Graph& graph, std::istream& stream) {
    if (!stream) {
        return false;
    }

    // s.t. con3122 : x_2250 - x_2866 + e_2866_2250 = 190;
    static boost::regex reg("s\\.t\\. con(\\d+)\\s*:\\s*x_(\\d+)\\s*-\\s*x_(\\d+)\\s*\\+\\s*e_(\\d+)_(\\d+)\\s*=\\s*((\\+|-)?\\d+)\\s*;");

    std::string line;
	while (std::getline(stream, line)) {
        boost::algorithm::trim(line);
        boost::smatch what;
        if (boost::regex_match(line, what, reg)) {
            size_t xj = boost::lexical_cast< size_t >(what[2]);
            size_t xi = boost::lexical_cast< size_t >(what[3]);
            int err = boost::lexical_cast< int >(what[6]);

            LOG4CXX_TRACE(logger, boost::format("regex: %d %d %d %s") % xi % xj % err % line);

            Graph_addEdge(graph, xi, xj, err);
		}	
	}

    return true;
}
Ejemplo n.º 3
0
static bool Graph_divide(Graph& graph, size_t loops, PositionList* position_tbl) {
    typedef std::set< size_t > NodeList;
    NodeList nodes;

    // nodes
    for (Graph::const_iterator i = graph.begin(); i != graph.end(); ++i) {
        nodes.insert(i->first);
    }

    while (!nodes.empty()) {
        // BFS
        Graph component;
        std::deque< size_t > Q = boost::assign::list_of(*nodes.begin());
        while (!Q.empty()) {
            size_t xi = Q.front();
            Q.pop_front();

            if (nodes.find(xi) == nodes.end()) {
                continue;
            }

            nodes.erase(xi);

            Graph::const_iterator i = graph.find(xi);
            if (i != graph.end()) {
                for (Children::const_iterator j = i->second.children.begin(); j != i->second.children.end(); ++j) {
                    Graph_addEdge(component, xi, j->first, j->second);
                    Q.push_back(j->first);
                }
                for (Parents::const_iterator j = i->second.parents.begin(); j != i->second.parents.end(); ++j) {
                    Q.push_back(*j);
                }
            }
        }

        LOG4CXX_TRACE(logger, boost::format("component:%d/%d") % component.size() % graph.size());

        if (!Graph_solve(component, loops, position_tbl)) {
            LOG4CXX_ERROR(logger, "solve component failed");
            return false;
        }
    }

    return true;
}
Ejemplo n.º 4
0
static FlowGraph constructGraph(PyObject *edges, int numVertices)
{
    PyObject *item, *iter;
    int numEdges = PyList_Size(edges), from, to;
    FlowGraph g = Graph_new(numVertices, numEdges);
    float capacity;

    iter = PyObject_GetIter(edges);
    while((item = PyIter_Next(iter))) {
        from = PyInt_AsLong(PyList_GetItem(item, 0));
        to = PyInt_AsLong(PyList_GetItem(item, 1));
        capacity = (float) PyFloat_AsDouble(PyList_GetItem(item, 2));
        Graph_addEdge(g, from, to, capacity);
        Py_DECREF(item);
    }
    Py_DECREF(iter);
    return g;
}
Ejemplo n.º 5
0
static void Graph_compress(const Graph& graph, Graph& new_graph) {
    for (Graph::const_iterator i = graph.begin(); i != graph.end(); ++i) {
        if (i->second.indegree() == 1 && i->second.outdegree() == 1) {
            continue;
        }

        for (Children::const_iterator j = i->second.children.begin(); j != i->second.children.end(); ++j) {
            int delta = j->second;
            Graph::const_iterator k = graph.find(j->first);
            while (k != graph.end() && k->second.indegree() == 1 && k->second.outdegree() == 1) {
                Children::const_iterator c = k->second.children.begin();
                if (c->first == i->first) { // ring
                    break;
                }
                delta += c->second;
                k = graph.find(c->first);
            }
            BOOST_ASSERT(k != graph.end());

            //std::cout << boost::format("compressed\t%d\t%d\t%d") % i->first % k->first % delta << std::endl;
            Graph_addEdge(new_graph, i->first, k->first, delta);
        }
    }
}