src/work/marci_graph_demo.cc
author marci
Tue, 30 Dec 2003 13:59:08 +0000
changeset 9 a9ed3f1c2c63
child 12 0810e3fc64a4
permissions -rw-r--r--
marci
marci@9
     1
#include <iostream>
marci@9
     2
#include <vector>
marci@9
     3
#include <string>
marci@9
     4
marci@9
     5
#include <marci_list_graph.hh>
marci@9
     6
#include <marci_graph_traits.hh>
marci@9
     7
#include <marci_property_vector.hh>
marci@9
     8
#include <marci_bfs.hh>
marci@9
     9
#include <marci_max_flow.hh>
marci@9
    10
marci@9
    11
using namespace marci;
marci@9
    12
marci@9
    13
int main (int, char*[])
marci@9
    14
{
marci@9
    15
  typedef graph_traits<list_graph>::node_iterator node_iterator;
marci@9
    16
  typedef graph_traits<list_graph>::edge_iterator edge_iterator;
marci@9
    17
  typedef graph_traits<list_graph>::each_node_iterator each_node_iterator;
marci@9
    18
  typedef graph_traits<list_graph>::each_edge_iterator each_edge_iterator;
marci@9
    19
  typedef graph_traits<list_graph>::out_edge_iterator out_edge_iterator;
marci@9
    20
  typedef graph_traits<list_graph>::in_edge_iterator in_edge_iterator;
marci@9
    21
  typedef graph_traits<list_graph>::sym_edge_iterator sym_edge_iterator;
marci@9
    22
marci@9
    23
  list_graph G;
marci@9
    24
  std::vector<node_iterator> vector_of_node_iterators;
marci@9
    25
  for(int i=0; i!=8; ++i) vector_of_node_iterators.push_back(G.add_node());
marci@9
    26
  for(int i=0; i!=8; ++i)
marci@9
    27
    for(int j=0; j!=8; ++j) {
marci@9
    28
      if ((i<j)&&(i+j)%3) G.add_edge(vector_of_node_iterators[i], vector_of_node_iterators[j]);
marci@9
    29
    }  
marci@9
    30
marci@9
    31
  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@9
    32
  std::cout << "number of nodes: " << number_of<each_node_iterator>(G.first_node()) << std::endl;
marci@9
    33
marci@9
    34
  for(each_node_iterator i=G.first_node(); i.is_valid(); ++i) {
marci@9
    35
    std::cout << "node " << G.id(i) << std::endl;
marci@9
    36
    std::cout << " outdegree (out_edge_iterator): " << number_of(G.first_out_edge(i)) << " "; 
marci@9
    37
    for(out_edge_iterator j=G.first_out_edge(i); j.is_valid(); ++j) { 
marci@9
    38
      std::cout << "(" << G.id(G.tail(j)) << "--" << G.id(j) << "->" << G.id(G.head(j)) << ") ";
marci@9
    39
    }
marci@9
    40
    std::cout << std::endl; 
marci@9
    41
    std::cout << " indegree: (in_edge_oterator) " << number_of(G.first_in_edge(i)) << " ";
marci@9
    42
    for(in_edge_iterator j=G.first_in_edge(i); j.is_valid(); ++j) { 
marci@9
    43
      std::cout << j << " "; } 
marci@9
    44
    std::cout << std::endl;
marci@9
    45
    std::cout << " degree: (sym_edge_iterator) " << number_of(G.first_sym_edge(i)) << " ";
marci@9
    46
    for(sym_edge_iterator j=G.first_sym_edge(i); j.is_valid(); ++j) { 
marci@9
    47
      std::cout << j << " "; } 
marci@9
    48
    std::cout<<std::endl;
marci@9
    49
  }
marci@9
    50
marci@9
    51
  std::cout << "all edges: ";
marci@9
    52
  for(each_edge_iterator i=G.first_edge(); i.is_valid(); ++i) {
marci@9
    53
    std::cout << i << " ";
marci@9
    54
  }
marci@9
    55
  std::cout << std::endl;
marci@9
    56
marci@9
    57
  std::cout << "node property array test" << std::endl;
marci@9
    58
  node_property_vector<list_graph, int> my_property_vector(G);
marci@9
    59
  each_node_iterator v;
marci@9
    60
  G.get_first(v);
marci@9
    61
  my_property_vector.put(v, 42);
marci@9
    62
  my_property_vector.put(++G.first_node(), 314);
marci@9
    63
  my_property_vector.put(++++G.first_node(), 1956);
marci@9
    64
  my_property_vector.put(vector_of_node_iterators[3], 1989);
marci@9
    65
  my_property_vector.put(vector_of_node_iterators[4], 2003);
marci@9
    66
  my_property_vector.put(vector_of_node_iterators[7], 1978);
marci@9
    67
  std::cout << "some node property values..." << std::endl;
marci@9
    68
  for(each_node_iterator i=G.first_node(); i.is_valid(); ++i) {
marci@9
    69
    std::cout << my_property_vector.get(i) << std::endl;
marci@9
    70
  }
marci@9
    71
  int _i=1;
marci@9
    72
  int _ii=1;
marci@9
    73
  edge_property_vector<list_graph, int> my_edge_property(G);
marci@9
    74
  for(each_edge_iterator i=G.first_edge(); i.is_valid(); ++i) {
marci@9
    75
    my_edge_property.put(i, _i);
marci@9
    76
    _i*=_ii; ++_ii;
marci@9
    77
  }
marci@9
    78
marci@9
    79
  std::cout << "node and edge property values on the tails and heads of edges..." << std::endl;
marci@9
    80
  for(each_edge_iterator j=G.first_edge(); j.is_valid(); ++j) {
marci@9
    81
    std::cout << my_property_vector.get(G.tail(j)) << "--" << my_edge_property.get(j) << "-->" << my_property_vector.get(G.head(j)) << " ";
marci@9
    82
  }
marci@9
    83
  std::cout << std::endl;
marci@9
    84
marci@9
    85
  //std::cout << "the same for inedges of the nodes..." << std::endl;
marci@9
    86
  //k=0;
marci@9
    87
  //for(each_node_iterator i=G.first_node(); i.is_valid(); ++i) {
marci@9
    88
  //  for(in_edge_iterator j=G.first_in_edge(i); j.is_valid(); ++j) {
marci@9
    89
  //    std::cout << my_property_vector.get(G.tail(j)) << "-->" << my_property_vector.get(G.head(j)) << " ";
marci@9
    90
  //  }
marci@9
    91
  //  std::cout << std::endl;
marci@9
    92
  //}
marci@9
    93
marci@9
    94
  std::cout << "bfs from the first node" << std::endl;
marci@9
    95
  bfs<list_graph> bfs_test(G, G.first_node());
marci@9
    96
  bfs_test.run();
marci@9
    97
  std::cout << "reached: ";
marci@9
    98
  for(each_node_iterator i=G.first_node(); i.is_valid(); ++i) {
marci@9
    99
    std::cout << bfs_test.reached.get(i) << " ";
marci@9
   100
  }
marci@9
   101
  std::cout<<std::endl;
marci@9
   102
  std::cout << "dist: ";
marci@9
   103
  for(each_node_iterator i=G.first_node(); i.is_valid(); ++i) {
marci@9
   104
    std::cout << bfs_test.dist.get(i) << " ";
marci@9
   105
  }
marci@9
   106
  std::cout<<std::endl;
marci@9
   107
marci@9
   108
marci@9
   109
  std::cout << "augmenting path flow algorithm test..." << std::endl;
marci@9
   110
  list_graph flow_test;
marci@9
   111
marci@9
   112
  node_iterator s=flow_test.add_node();
marci@9
   113
  node_iterator v1=flow_test.add_node();
marci@9
   114
  node_iterator v2=flow_test.add_node();
marci@9
   115
  node_iterator v3=flow_test.add_node();
marci@9
   116
  node_iterator v4=flow_test.add_node();
marci@9
   117
  node_iterator t=flow_test.add_node();
marci@9
   118
  
marci@9
   119
  node_property_vector<list_graph, std::string> node_name(flow_test);
marci@9
   120
  node_name.put(s, "s");
marci@9
   121
  node_name.put(v1, "v1");
marci@9
   122
  node_name.put(v2, "v2");
marci@9
   123
  node_name.put(v3, "v3");
marci@9
   124
  node_name.put(v4, "v4");
marci@9
   125
  node_name.put(t, "t");
marci@9
   126
marci@9
   127
  edge_iterator s_v1=flow_test.add_edge(s, v1);
marci@9
   128
  edge_iterator s_v2=flow_test.add_edge(s, v2);
marci@9
   129
  edge_iterator v1_v2=flow_test.add_edge(v1, v2);
marci@9
   130
  edge_iterator v2_v1=flow_test.add_edge(v2, v1);
marci@9
   131
  edge_iterator v1_v3=flow_test.add_edge(v1, v3);
marci@9
   132
  edge_iterator v3_v2=flow_test.add_edge(v3, v2);
marci@9
   133
  edge_iterator v2_v4=flow_test.add_edge(v2, v4);
marci@9
   134
  edge_iterator v4_v3=flow_test.add_edge(v4, v3);
marci@9
   135
  edge_iterator v3_t=flow_test.add_edge(v3, t);
marci@9
   136
  edge_iterator v4_t=flow_test.add_edge(v4, t);
marci@9
   137
marci@9
   138
  edge_property_vector<list_graph, int> cap(flow_test);
marci@9
   139
marci@9
   140
  cap.put(s_v1, 16);
marci@9
   141
  cap.put(s_v2, 13);
marci@9
   142
  cap.put(v1_v2, 10);
marci@9
   143
  cap.put(v2_v1, 4);
marci@9
   144
  cap.put(v1_v3, 12);
marci@9
   145
  cap.put(v3_v2, 9);
marci@9
   146
  cap.put(v2_v4, 14);
marci@9
   147
  cap.put(v4_v3, 7);
marci@9
   148
  cap.put(v3_t, 20);
marci@9
   149
  cap.put(v4_t, 4);
marci@9
   150
marci@9
   151
  std::cout << "on directed graph graph" << std::endl; //<< flow_test;
marci@9
   152
  std::cout << "names and capacity values" << std::endl; 
marci@9
   153
  for(each_node_iterator i=flow_test.first_node(); i.is_valid(); ++i) { 
marci@9
   154
    std::cout << node_name.get(i) << ": ";
marci@9
   155
    std::cout << "out edges: ";
marci@9
   156
    for(out_edge_iterator j=flow_test.first_out_edge(i); j.is_valid(); ++j) 
marci@9
   157
      std::cout << node_name.get(flow_test.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flow_test.head(j)) << " ";
marci@9
   158
    std::cout << "in edges: ";
marci@9
   159
    for(in_edge_iterator j=flow_test.first_in_edge(i); j.is_valid(); ++j) 
marci@9
   160
      std::cout << node_name.get(flow_test.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flow_test.head(j)) << " ";
marci@9
   161
    std::cout << std::endl;
marci@9
   162
  }
marci@9
   163
marci@9
   164
  
marci@9
   165
  //for(each_node_iterator i=flow_test.first_node(); i.is_valid(); ++i) { 
marci@9
   166
  //  std::cout << i << " ";
marci@9
   167
  //}
marci@9
   168
  
marci@9
   169
  max_flow_type<list_graph, int> max_flow_test(flow_test, s, t, cap);
marci@9
   170
  max_flow_test.run();
marci@9
   171
marci@9
   172
  return 0;
marci@9
   173
}