src/work/marci_graph_demo.cc
author klao
Tue, 27 Jan 2004 21:23:33 +0000
changeset 39 28b0d751d29f
parent 12 0810e3fc64a4
child 49 f00a4f7e2149
permissions -rw-r--r--
Alap leiras a BinHeap -rol

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