demo/topological_ordering.cc
author alpar
Fri, 08 Feb 2008 10:38:08 +0000
changeset 2569 12c2c5c4330b
parent 2391 14a343be7a5a
permissions -rw-r--r--
#include<cmath> -> #include<lemon/math.h>
alpar@2216
     1
/* -*- C++ -*-
alpar@2216
     2
 *
alpar@2216
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@2216
     4
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
alpar@2216
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@2216
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@2216
     8
 *
alpar@2216
     9
 * Permission to use, modify and distribute this software is granted
alpar@2216
    10
 * provided that this copyright notice appears in all copies. For
alpar@2216
    11
 * precise terms see the accompanying LICENSE file.
alpar@2216
    12
 *
alpar@2216
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@2216
    14
 * express or implied, and with no claim as to its suitability for any
alpar@2216
    15
 * purpose.
alpar@2216
    16
 *
alpar@2216
    17
 */
alpar@2216
    18
alpar@2216
    19
///\ingroup demos
alpar@2216
    20
///\file
alpar@2216
    21
///\brief Demonstrating the Dfs class with topological ordering
alpar@2216
    22
///
alpar@2216
    23
///\include topological_ordering.cc
alpar@2216
    24
alpar@2216
    25
#include <iostream>
alpar@2216
    26
#include <string>
alpar@2216
    27
#include <list>
alpar@2216
    28
#include <lemon/list_graph.h>
alpar@2216
    29
#include <lemon/maps.h>
alpar@2216
    30
#include <lemon/dfs.h>
alpar@2216
    31
alpar@2216
    32
using namespace lemon;
alpar@2216
    33
alpar@2216
    34
alpar@2216
    35
template< typename GR >
alpar@2216
    36
class SerializingWriteMap
alpar@2216
    37
{
alpar@2216
    38
public:
alpar@2216
    39
  typedef typename GR::Node  Key;
alpar@2216
    40
  typedef bool  Value;
alpar@2216
    41
  typedef std::list<Key>  OrderedList;
alpar@2216
    42
alpar@2216
    43
  OrderedList  order;
alpar@2216
    44
alpar@2232
    45
  SerializingWriteMap( const GR& ) {}
alpar@2216
    46
alpar@2216
    47
  void  set( const Key& k, const Value& v ) {
alpar@2216
    48
    if( v ) order.push_front(k);
alpar@2216
    49
  }
alpar@2216
    50
alpar@2216
    51
  Value  operator[] ( const Key& ) {
alpar@2216
    52
    return false;
alpar@2216
    53
  }
alpar@2216
    54
};
alpar@2216
    55
alpar@2216
    56
alpar@2232
    57
int main()
alpar@2216
    58
{
alpar@2216
    59
  std::cout << "Topological Ordering demo" << std::endl;
alpar@2216
    60
alpar@2216
    61
  // Create and build the graph
alpar@2216
    62
  ListGraph  gr;
alpar@2216
    63
  ListGraph::NodeMap<std::string>  label(gr);
alpar@2216
    64
  typedef ListGraph::Node  Node;
alpar@2216
    65
alpar@2216
    66
  // This DAG example for topological ordering is from the book New Algorithms
alpar@2216
    67
  // (Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein)
alpar@2216
    68
  {
alpar@2216
    69
    Node  belt = gr.addNode();
alpar@2216
    70
    Node  trousers = gr.addNode();
alpar@2216
    71
    Node  necktie = gr.addNode();
alpar@2216
    72
    Node  coat = gr.addNode();
alpar@2216
    73
    Node  socks = gr.addNode();
alpar@2216
    74
    Node  shirt = gr.addNode();
alpar@2216
    75
    Node  shoe = gr.addNode();
alpar@2216
    76
    Node  watch = gr.addNode();
alpar@2216
    77
    Node  pants = gr.addNode();
alpar@2216
    78
alpar@2216
    79
    label[belt] = "Belt";
alpar@2216
    80
    label[trousers] = "Trousers";
alpar@2216
    81
    label[necktie] = "Necktie";
alpar@2216
    82
    label[coat] = "Coat";
alpar@2216
    83
    label[socks] = "Socks";
alpar@2216
    84
    label[shirt] = "Shirt";
alpar@2216
    85
    label[shoe] = "Shoe";
alpar@2216
    86
    label[watch] = "Watch";
alpar@2216
    87
    label[pants] = "Pants";
alpar@2216
    88
alpar@2216
    89
    // Here we use a directed edge to indicate precedenc.
alpar@2216
    90
    // The source must precede the target.
alpar@2216
    91
    gr.addEdge( socks, shoe );
alpar@2216
    92
    gr.addEdge( pants, shoe );
alpar@2216
    93
    gr.addEdge( pants, trousers );
alpar@2216
    94
    gr.addEdge( trousers, shoe );
alpar@2216
    95
    gr.addEdge( trousers, belt );
alpar@2216
    96
    gr.addEdge( belt, coat );
alpar@2216
    97
    gr.addEdge( shirt, belt );
alpar@2216
    98
    gr.addEdge( shirt, necktie );
alpar@2216
    99
    gr.addEdge( necktie, coat );
alpar@2216
   100
  }
alpar@2216
   101
alpar@2216
   102
  // Calculate topological order
alpar@2216
   103
  Dfs<ListGraph, Dfs<ListGraph>::DefProcessedMapTraits<SerializingWriteMap<ListGraph> > >  dfs(gr);
alpar@2216
   104
  SerializingWriteMap<ListGraph>  list(gr);
alpar@2216
   105
  dfs.processedMap(list);
alpar@2216
   106
  dfs.run();
alpar@2216
   107
alpar@2216
   108
  // Write out the result
alpar@2216
   109
  std::cout << "One possible order to dress up:";
alpar@2216
   110
  for( SerializingWriteMap<ListGraph>::OrderedList::iterator  it = list.order.begin(); it != list.order.end(); ++it )
alpar@2216
   111
    std::cout << "  " << label[*it];
alpar@2216
   112
  std::cout << std::endl;
alpar@2216
   113
alpar@2216
   114
  return 0;
alpar@2216
   115
}