COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/marci/max_bipartite_matching_demo.cc @ 196:8a9b9360463e

Last change on this file since 196:8a9b9360463e was 196:8a9b9360463e, checked in by marci, 16 years ago

.

File size: 3.7 KB
Line 
1// -*- c++ -*-
2#include <iostream>
3#include <fstream>
4#include <vector>
5#include <cstdlib>
6
7//#include <LEDA/graph.h>
8//#include <leda_graph_wrapper.h>
9#include <list_graph.h>
10#include <dimacs.h>
11#include <time_measure.h>
12#include <edmonds_karp.h>
13
14/**
15 * Inicializalja a veletlenszamgeneratort.
16 * Figyelem, ez nem jo igazi random szamokhoz,
17 * erre ne bizzad a titkaidat!
18 */
19void random_init()
20{
21        unsigned int seed = getpid();
22        seed |= seed << 15;
23        seed ^= time(0);
24
25        srand(seed);
26}
27
28/**
29 * Egy veletlen int-et ad vissza 0 es m-1 kozott.
30 */
31int random(int m)
32{
33        return int( double(m) * rand() / (RAND_MAX + 1.0) );
34}
35
36using namespace hugo;
37
38using std::cout;
39using std::endl;
40
41int main() {
42//   leda::graph g;
43//   typedef LedaGraphWrapper<leda::graph> Graph;
44//   Graph G(g);
45  typedef ListGraph Graph;
46  Graph G;
47
48  typedef Graph::Node Node;
49  typedef Graph::NodeIt NodeIt; 
50  typedef Graph::Edge Edge;
51  typedef Graph::EdgeIt EdgeIt;
52  typedef Graph::OutEdgeIt OutEdgeIt;
53  typedef Graph::InEdgeIt InEdgeIt;
54
55  //Node s, t;
56  //Graph::EdgeMap<int> cap(G);
57  //readDimacsMaxFlow(std::cin, G, s, t, cap);
58  std::vector<Node> s_nodes;
59  std::vector<Node> t_nodes;
60
61  for(int i=0; i<4; ++i) {
62    s_nodes.push_back(G.addNode());
63  }
64  for(int i=0; i<4; ++i) {
65    t_nodes.push_back(G.addNode());
66  }
67  random_init();
68  for(int i=0; i<6; ++i) {
69    G.addEdge(s_nodes[random(4)], t_nodes[random(4)]);
70  }
71 
72//   G.addEdge(s_nodes[2], t_nodes[4-4]);
73//   G.addEdge(s_nodes[2], t_nodes[7-4]);
74//   G.addEdge(s_nodes[2], t_nodes[4-4]);
75//   G.addEdge(s_nodes[3], t_nodes[6-4]);
76//   G.addEdge(s_nodes[3], t_nodes[5-4]);
77//   G.addEdge(s_nodes[3], t_nodes[5-4]);
78
79
80  Graph::NodeMap<bool> s_map(G); //false
81  Graph::NodeMap<bool> t_map(G); //false
82 
83  for(int i=0; i<4; ++i) {
84    s_map.set(s_nodes[i], true);
85    t_map.set(t_nodes[i], true);
86  }
87
88  cout << "bfs and dfs iterator demo on the directed graph" << endl;
89  for(NodeIt n=G.first<NodeIt>(); G.valid(n); G.next(n)) {
90    cout << G.id(n) << ": ";
91    cout << "out edges: ";
92    for(OutEdgeIt e=G.first<OutEdgeIt>(n); G.valid(e); G.next(e))
93      cout << G.id(G.tail(e)) << "->" << G.id(G.head(e)) << " ";
94    cout << "in edges: ";
95    for(InEdgeIt e=G.first<InEdgeIt>(n); G.valid(e); G.next(e))
96      cout << G.id(G.tail(e)) << "->" << G.id(G.head(e)) << " ";
97    cout << endl;
98  }
99
100
101  {
102    std::cout << "on-the-fly max bipartite matching demo on wrapped leda graph..." << std::endl;
103    Graph::EdgeMap<int> flow(G); //0 flow
104    Graph::EdgeMap<int> cap(G, 1);
105
106    Timer ts;
107    ts.reset();
108
109    MaxMatching<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > max_flow_test(G, s_map, t_map, flow, cap);
110    //max_flow_test.augmentWithBlockingFlow<Graph>();
111    int i=0;
112    while (max_flow_test.augmentOnShortestPath()) {
113//     for(EdgeIt e=G.template first<EdgeIt>(); e.valid(); ++e) {
114//       std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") ";
115//     }
116//     std::cout<<std::endl;
117      ++i;
118    }
119
120    std::cout << "maximum matching: "<< std::endl;
121    for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e)) 
122      if (flow.get(e))
123        std::cout << G.id(G.tail(e)) << "-" << flow.get(e) << "->" << G.id(G.head(e)) << " ";
124    std::cout<<std::endl;
125    std::cout << "edges which are not in this maximum matching: "<< std::endl;
126    for(EdgeIt e=G.first<EdgeIt>(); G.valid(e); G.next(e)) 
127      if (!flow.get(e))
128        std::cout << G.id(G.tail(e)) << "-" << flow.get(e) << "->" << G.id(G.head(e)) << " ";
129    std::cout<<std::endl;
130   
131    std::cout << "elapsed time: " << ts << std::endl;
132    std::cout << "number of augmentation phases: " << i << std::endl;
133    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
134  }
135 
136
137  return 0;
138}
Note: See TracBrowser for help on using the repository browser.