src/work/marci/oldies/marci_graph_demo.cc
author klao
Wed, 10 Nov 2004 21:42:28 +0000
changeset 978 175cf8c3a994
parent 280 19f3943521ab
child 986 e997802b855c
permissions -rw-r--r--
"make check" pass under gcc-3.4.3
marci@280
     1
#include <iostream>
marci@280
     2
#include <vector>
marci@280
     3
#include <string>
marci@280
     4
marci@280
     5
#include <list_graph.h>
marci@280
     6
#include <bfs_iterator.h>
marci@280
     7
#include <edmonds_karp.h>
marci@280
     8
alpar@921
     9
using namespace lemon;
marci@280
    10
marci@280
    11
int main (int, char*[])
marci@280
    12
{
marci@280
    13
  typedef ListGraph::Node Node;
marci@280
    14
  typedef ListGraph::Edge Edge;
marci@280
    15
  typedef ListGraph::NodeIt NodeIt;
marci@280
    16
  typedef ListGraph::EdgeIt EdgeIt;
marci@280
    17
  typedef ListGraph::OutEdgeIt OutEdgeIt;
marci@280
    18
  typedef ListGraph::InEdgeIt InEdgeIt;
marci@280
    19
  typedef ListGraph::SymEdgeIt SymEdgeIt;
marci@280
    20
  ListGraph G;
marci@280
    21
  std::vector<Node> vector_of_Nodes;
marci@280
    22
  for(int i=0; i!=8; ++i) vector_of_Nodes.push_back(G.addNode());
marci@280
    23
  for(int i=0; i!=8; ++i)
marci@280
    24
    for(int j=0; j!=8; ++j) 
marci@280
    25
      if ((i<j)&&(i+j)%3) G.addEdge(vector_of_Nodes[i], vector_of_Nodes[j]);
marci@280
    26
marci@280
    27
  std::cout << "We construct a directed graph on the node set {0,1,2,...,7}," <<std::endl << "i-->j is arc iff i<j and (i+j)%3." << std::endl;
marci@280
    28
  std::cout << "number of nodes: " << count(G.first<NodeIt>()) << std::endl;
marci@280
    29
marci@280
    30
  for(NodeIt i=G.first<NodeIt>(); G.valid(i); G.next(i)) {
marci@280
    31
    std::cout << "node " << G.id(i) << std::endl;
marci@280
    32
    std::cout << " outdegree (OutEdgeIt): " << count(G.first<OutEdgeIt>(i)) << " "; 
marci@280
    33
    for(OutEdgeIt j=G.first<OutEdgeIt>(i); G.valid(j); G.next(j)) { 
marci@280
    34
      std::cout << "(" << G.id(G.tail(j)) << "--" << G.id(j) << "->" << G.id(G.head(j)) << ") ";
marci@280
    35
    }
marci@280
    36
    std::cout << std::endl; 
marci@280
    37
marci@280
    38
    std::cout<< " ";
marci@280
    39
    for(OutEdgeIt j=G.first<OutEdgeIt>(i); G.valid(j); G.next(j)) { 
marci@280
    40
      std::cout << G.aNode(j) << "->" << G.bNode(j) << " "; } 
marci@280
    41
    std::cout<<std::endl;
marci@280
    42
marci@280
    43
    std::cout << " indegree: (InEdgeIt) " << count(G.first<InEdgeIt>(i)) << " ";
marci@280
    44
    for(InEdgeIt j=G.first<InEdgeIt>(i); G.valid(j); G.next(j)) { 
marci@280
    45
      std::cout << j << " "; } 
marci@280
    46
    std::cout << std::endl;
marci@280
    47
marci@280
    48
    std::cout<< " ";
marci@280
    49
    for(InEdgeIt j=G.first<InEdgeIt>(i); G.valid(j); G.next(j)) { 
marci@280
    50
      std::cout << G.aNode(j) << "->" << G.bNode(j) << " "; } 
marci@280
    51
    std::cout<<std::endl;
marci@280
    52
marci@280
    53
    std::cout << " degree: (SymEdgeIt) " << count(G.first<SymEdgeIt>(i)) << " ";
marci@280
    54
    for(SymEdgeIt j=G.first<SymEdgeIt>(i); G.valid(j); G.next(j)) { 
marci@280
    55
      std::cout << j << " "; } 
marci@280
    56
    std::cout<<std::endl;
marci@280
    57
marci@280
    58
    std::cout<< " ";
marci@280
    59
    for(SymEdgeIt j=G.first<SymEdgeIt>(i); G.valid(j); G.next(j)) { 
marci@280
    60
      std::cout << G.aNode(j) << "->" << G.bNode(j) << " "; } 
marci@280
    61
    std::cout<<std::endl;
marci@280
    62
  }
marci@280
    63
marci@280
    64
  std::cout << "all edges: ";
marci@280
    65
  for(EdgeIt i=G.first<EdgeIt>(); G.valid(i); G.next(i)) {
marci@280
    66
    std::cout << i << " ";
marci@280
    67
  }
marci@280
    68
  std::cout << std::endl;
marci@280
    69
marci@280
    70
  std::cout << "node property array test" << std::endl;
marci@280
    71
  ListGraph::NodeMap<int> my_property_vector(G);
marci@280
    72
  NodeIt v;
marci@280
    73
  G.first(v);
marci@280
    74
  my_property_vector.set(v, 42);
marci@280
    75
  my_property_vector.set(G.next(G.first<NodeIt>()), 314);
marci@280
    76
  my_property_vector.set(G.next(G.next(G.first<NodeIt>())), 1956);
marci@280
    77
  my_property_vector.set(vector_of_Nodes[3], 1989);
marci@280
    78
  my_property_vector.set(vector_of_Nodes[4], 2003);
marci@280
    79
  my_property_vector.set(vector_of_Nodes[7], 1978);
marci@280
    80
  std::cout << "some node property values..." << std::endl;
marci@280
    81
  for(NodeIt i=G.first<NodeIt>(); G.valid(i); G.next(i)) {
marci@280
    82
    std::cout << my_property_vector.get(i) << std::endl;
marci@280
    83
  }
marci@280
    84
  int _i=1;
marci@280
    85
  int _ii=1;
marci@280
    86
  ListGraph::EdgeMap<int> my_edge_property(G);
marci@280
    87
  for(EdgeIt i=G.first<EdgeIt>(); G.valid(i); G.next(i)) {
marci@280
    88
    my_edge_property.set(i, _i);
marci@280
    89
    _i*=_ii; ++_ii;
marci@280
    90
  }
marci@280
    91
marci@280
    92
  std::cout << "node and edge property values on the tails and heads of edges..." << std::endl;
marci@280
    93
  for(EdgeIt j=G.first<EdgeIt>(); G.valid(j); G.next(j)) {
marci@280
    94
    std::cout << my_property_vector.get(G.tail(j)) << "--" << my_edge_property.get(j) << "-->" << my_property_vector.get(G.head(j)) << " ";
marci@280
    95
  }
marci@280
    96
  std::cout << std::endl;
marci@280
    97
/*
marci@280
    98
  std::cout << "bfs from the first node" << std::endl;
marci@280
    99
  bfs<ListGraph> bfs_test(G, G.first<NodeIt>());
marci@280
   100
  bfs_test.run();
marci@280
   101
  std::cout << "reached: ";
marci@280
   102
  for(NodeIt i=G.first<NodeIt>(); G.valid(i); G.next(i)) {
marci@280
   103
    std::cout << bfs_test.reached.get(i) << " ";
marci@280
   104
  }
marci@280
   105
  std::cout<<std::endl;
marci@280
   106
  std::cout << "dist: ";
marci@280
   107
  for(NodeIt i=G.first<NodeIt>(); G.valid(i); G.next(i)) {
marci@280
   108
    std::cout << bfs_test.dist.get(i) << " ";
marci@280
   109
  }
marci@280
   110
  std::cout<<std::endl;
marci@280
   111
*/
marci@280
   112
marci@280
   113
  std::cout << "augmenting path flow algorithm test..." << std::endl;
marci@280
   114
  ListGraph flowG;
marci@280
   115
marci@280
   116
  Node s=flowG.addNode();
marci@280
   117
  Node v1=flowG.addNode();
marci@280
   118
  Node v2=flowG.addNode();
marci@280
   119
  Node v3=flowG.addNode();
marci@280
   120
  Node v4=flowG.addNode();
marci@280
   121
  Node t=flowG.addNode();
marci@280
   122
  
marci@280
   123
  ListGraph::NodeMap<std::string> node_name(flowG);
marci@280
   124
  node_name.set(s, "s");
marci@280
   125
  node_name.set(v1, "v1");
marci@280
   126
  node_name.set(v2, "v2");
marci@280
   127
  node_name.set(v3, "v3");
marci@280
   128
  node_name.set(v4, "v4");
marci@280
   129
  node_name.set(t, "t");
marci@280
   130
marci@280
   131
  Edge s_v1=flowG.addEdge(s, v1);
marci@280
   132
  Edge s_v2=flowG.addEdge(s, v2);
marci@280
   133
  Edge v1_v2=flowG.addEdge(v1, v2);
marci@280
   134
  Edge v2_v1=flowG.addEdge(v2, v1);
marci@280
   135
  Edge v1_v3=flowG.addEdge(v1, v3);
marci@280
   136
  Edge v3_v2=flowG.addEdge(v3, v2);
marci@280
   137
  Edge v2_v4=flowG.addEdge(v2, v4);
marci@280
   138
  Edge v4_v3=flowG.addEdge(v4, v3);
marci@280
   139
  Edge v3_t=flowG.addEdge(v3, t);
marci@280
   140
  Edge v4_t=flowG.addEdge(v4, t);
marci@280
   141
marci@280
   142
  ListGraph::EdgeMap<int> cap(flowG);
marci@280
   143
marci@280
   144
  cap.set(s_v1, 16);
marci@280
   145
  cap.set(s_v2, 13);
marci@280
   146
  cap.set(v1_v2, 10);
marci@280
   147
  cap.set(v2_v1, 4);
marci@280
   148
  cap.set(v1_v3, 12);
marci@280
   149
  cap.set(v3_v2, 9);
marci@280
   150
  cap.set(v2_v4, 14);
marci@280
   151
  cap.set(v4_v3, 7);
marci@280
   152
  cap.set(v3_t, 20);
marci@280
   153
  cap.set(v4_t, 4);
marci@280
   154
marci@280
   155
  std::cout << "on directed graph graph" << std::endl; //<< flowG;
marci@280
   156
  std::cout << "names and capacity values" << std::endl; 
marci@280
   157
  for(NodeIt i=flowG.first<NodeIt>(); flowG.valid(i); flowG.next(i)) { 
marci@280
   158
    std::cout << node_name.get(i) << ": ";
marci@280
   159
    std::cout << "out edges: ";
marci@280
   160
    for(OutEdgeIt j=flowG.first<OutEdgeIt>(i); flowG.valid(j); flowG.next(j)) 
marci@280
   161
      std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
marci@280
   162
    std::cout << "in edges: ";
marci@280
   163
    for(InEdgeIt j=flowG.first<InEdgeIt>(i); flowG.valid(j); flowG.next(j)) 
marci@280
   164
      std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
marci@280
   165
    std::cout << std::endl;
marci@280
   166
  }
marci@280
   167
marci@280
   168
  //flowG.deleteEdge(s_v1);
marci@280
   169
  //flowG.deleteEdge(s_v2);
marci@280
   170
  //flowG.deleteEdge(v1_v2);
marci@280
   171
  //flowG.deleteEdge(v1_v3);
marci@280
   172
  
marci@280
   173
marci@280
   174
  //flowG.setTail(v3_t, v2);
marci@280
   175
  //flowG.setHead(v3_t, s);
marci@280
   176
/*
marci@280
   177
  for(NodeIt i=flowG.first<NodeIt>(); flowG.valid(i); flowG.next(i)) { 
marci@280
   178
    std::cout << node_name.get(i) << ": ";
marci@280
   179
    std::cout << "out edges: ";
marci@280
   180
    for(OutEdgeIt j=flowG.first<OutEdgeIt>(i); flowG.valid(j); flowG.next(j)) 
marci@280
   181
      std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
marci@280
   182
    std::cout << "in edges: ";
marci@280
   183
    for(InEdgeIt j=flowG.first<InEdgeIt>(i); flowG.valid(j); flowG.next(j)) 
marci@280
   184
      std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
marci@280
   185
    std::cout << std::endl;
marci@280
   186
  }
marci@280
   187
  
marci@280
   188
  for(EdgeIt e=flowG.first<EdgeIt>(); flowG.valid(e); flowG.next(e)) {
marci@280
   189
    std::cout << node_name.get(flowG.tail(e)) << "-"<< cap.get(e) << "->" << node_name.get(flowG.head(e)) << " ";
marci@280
   190
  }
marci@280
   191
*/
marci@280
   192
  /*
marci@280
   193
  while (flowG.valid(flowG.first<EdgeIt>())) {
marci@280
   194
    flowG.deleteEdge(flowG.first<EdgeIt>());
marci@280
   195
    for(NodeIt i=flowG.first<NodeIt>(); flowG.valid(i); flowG.next(i)) { 
marci@280
   196
      std::cout << node_name.get(i) << ": ";
marci@280
   197
      std::cout << "out edges: ";
marci@280
   198
      for(OutEdgeIt j=flowG.first<OutEdgeIt>(i); flowG.valid(j); flowG.next(j)) 
marci@280
   199
	std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
marci@280
   200
      std::cout << "in edges: ";
marci@280
   201
      for(InEdgeIt j=flowG.first<InEdgeIt>(i); flowG.valid(j); flowG.next(j)) 
marci@280
   202
	std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
marci@280
   203
      std::cout << std::endl;
marci@280
   204
    }
marci@280
   205
  }
marci@280
   206
  
marci@280
   207
  while (flowG.valid(flowG.first<NodeIt>())) {
marci@280
   208
    flowG.deleteNode(flowG.first<NodeIt>());
marci@280
   209
    for(NodeIt i=flowG.first<NodeIt>(); flowG.valid(i); flowG.next(i)) { 
marci@280
   210
      std::cout << node_name.get(i) << ": ";
marci@280
   211
      std::cout << "out edges: ";
marci@280
   212
      for(OutEdgeIt j=flowG.first<OutEdgeIt>(i); flowG.valid(j); flowG.next(j)) 
marci@280
   213
	std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
marci@280
   214
      std::cout << "in edges: ";
marci@280
   215
      for(InEdgeIt j=flowG.first<InEdgeIt>(i); flowG.valid(j); flowG.next(j)) 
marci@280
   216
	std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " ";
marci@280
   217
      std::cout << std::endl;
marci@280
   218
    }
marci@280
   219
  }
marci@280
   220
  */
marci@280
   221
marci@280
   222
  //std::cout << std::endl;
marci@280
   223
marci@280
   224
marci@280
   225
  {
marci@280
   226
    ListGraph::EdgeMap<int> flow(flowG, 0);
marci@280
   227
    MaxFlow<ListGraph, int, ListGraph::EdgeMap<int>, ListGraph::EdgeMap<int> > max_flow_test(flowG, s, t, flow, cap);
marci@280
   228
    /*
marci@280
   229
    max_flow_test.augmentOnBlockingFlow<ListGraph>();
marci@280
   230
    for(EdgeIt e=flowG.template first<EdgeIt>(); flowG.valid(e); flowG.next(e)) { 
marci@280
   231
      std::cout<<"("<<flowG.tail(e)<< "-"<<flow.get(e)<<"->"<<flowG.head(e)<<") ";
marci@280
   232
    }
marci@280
   233
    std::cout<<std::endl;
marci@280
   234
    max_flow_test.augmentOnBlockingFlow<ListGraph>();
marci@280
   235
    for(EdgeIt e=flowG.template first<EdgeIt>(); flowG.valid(e); flowG.next(e)) { 
marci@280
   236
      std::cout<<"("<<flowG.tail(e)<< "-"<<flow.get(e)<<"->"<<flowG.head(e)<<") ";
marci@280
   237
    }
marci@280
   238
    std::cout<<std::endl;*/
marci@280
   239
    //max_flow_test.run();
marci@280
   240
    
marci@280
   241
    //std::cout << "maximum flow: "<< std::endl;
marci@280
   242
    while (max_flow_test.augmentOnShortestPath()) {
marci@280
   243
      for(EdgeIt e=flowG.template first<EdgeIt>(); flowG.valid(e); flowG.next(e)) { 
marci@280
   244
	std::cout<<"("<<flowG.tail(e)<< "-"<<flow.get(e)<<"->"<<flowG.head(e)<<") ";
marci@280
   245
      }
marci@280
   246
      std::cout<<std::endl;
marci@280
   247
    }
marci@280
   248
    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@280
   249
  }
marci@280
   250
/*
marci@280
   251
  {
marci@280
   252
    std::list<Node> S;
marci@280
   253
    S.push_back(s); S.push_back(v3);
marci@280
   254
    std::list<Node> T;
marci@280
   255
    T.push_back(t);
marci@280
   256
marci@280
   257
    ListGraph::EdgeMap<int> flow(flowG, 0);
marci@280
   258
    MaxFlow2<ListGraph, int, ListGraph::EdgeMap<int>, ListGraph::EdgeMap<int> > max_flow_test(flowG, S, T, flow, cap);
marci@280
   259
    max_flow_test.run();
marci@280
   260
    
marci@280
   261
    std::cout << "maximum flow: "<< std::endl;
marci@280
   262
    for(EdgeIt e=flowG.template first<EdgeIt>(); flowG.valid(e); flowG.next(e)) { 
marci@280
   263
      std::cout<<"("<<flowG.tail(e)<< "-"<<flow.get(e)<<"->"<<flowG.head(e)<<") ";
marci@280
   264
    }
marci@280
   265
    std::cout<<std::endl;
marci@280
   266
    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@280
   267
  }
marci@280
   268
*/
marci@280
   269
  return 0;
marci@280
   270
}