COIN-OR::LEMON - Graph Library

Ticket #81: port_topological_ordering_0c44cf0a18b1.patch

File port_topological_ordering_0c44cf0a18b1.patch, 4.4 KB (added by Peter Kovacs, 16 years ago)

Port demo/topological_ordering.cc + improve implementation

  • demo/CMakeLists.txt

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1215445786 -7200
    # Node ID 0c44cf0a18b154de1ce6e00e15cb3c474bb7f467
    # Parent  65cba1032f90cd157a56b911b4917920b07fc546
    Port demo/topological_ordering.cc from SVN -r3501 + improve implementation
    Use LoggerBoolMap instead of an own map
    
    diff -r 65cba1032f90 -r 0c44cf0a18b1 demo/CMakeLists.txt
    a b  
    55set (DEMOS
    66  arg_parser_demo
    77  graph_to_eps_demo
    8   lgf_demo)
     8  lgf_demo
     9  topological_ordering)
    910
    1011foreach (DEMO_NAME ${DEMOS})
    1112  add_executable (${DEMO_NAME} ${DEMO_NAME}.cc)
  • demo/Makefile.am

    diff -r 65cba1032f90 -r 0c44cf0a18b1 demo/Makefile.am
    a b  
    77noinst_PROGRAMS += \
    88        demo/arg_parser_demo \
    99        demo/graph_to_eps_demo \
    10         demo/lgf_demo
     10        demo/lgf_demo \
     11        topological_ordering
    1112
    1213endif WANT_DEMO
    1314
    1415demo_arg_parser_demo_SOURCES = demo/arg_parser_demo.cc
    1516demo_graph_to_eps_demo_SOURCES = demo/graph_to_eps_demo.cc
    1617demo_lgf_demo_SOURCES = demo/lgf_demo.cc
     18demo_topological_ordering_SOURCES = demo/topological_ordering.cc
  • new file demo/topological_ordering.cc

    diff -r 65cba1032f90 -r 0c44cf0a18b1 demo/topological_ordering.cc
    - +  
     1/* -*- C++ -*-
     2 *
     3 * This file is a part of LEMON, a generic C++ optimization library
     4 *
     5 * Copyright (C) 2003-2008
     6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8 *
     9 * Permission to use, modify and distribute this software is granted
     10 * provided that this copyright notice appears in all copies. For
     11 * precise terms see the accompanying LICENSE file.
     12 *
     13 * This software is provided "AS IS" with no warranty of any kind,
     14 * express or implied, and with no claim as to its suitability for any
     15 * purpose.
     16 *
     17 */
     18
     19///\ingroup demos
     20///\file
     21///\brief Demonstrating the Dfs class with topological ordering
     22///
     23/// This demo file shows an example for topological ordering of a
     24/// directed acyclic graph (DAG) using \ref lemon::Dfs Dfs algorithm.
     25///
     26///\include topological_ordering.cc
     27
     28#include <iostream>
     29#include <string>
     30#include <vector>
     31#include <lemon/list_graph.h>
     32#include <lemon/graph_utils.h>
     33#include <lemon/maps.h>
     34#include <lemon/dfs.h>
     35
     36using namespace lemon;
     37
     38int main()
     39{
     40  std::cout << "Topological Ordering Demo" << std::endl;
     41
     42  // Create and build an acyclic digraph (DAG)
     43  ListDigraph gr;
     44  ListDigraph::NodeMap<std::string> label(gr);
     45  typedef ListDigraph::Node Node;
     46
     47  // This DAG example for topological ordering is from the book
     48  // New Algorithms (T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein)
     49  {
     50    // Create nodes
     51    Node belt = gr.addNode();
     52    Node trousers = gr.addNode();
     53    Node necktie = gr.addNode();
     54    Node coat = gr.addNode();
     55    Node socks = gr.addNode();
     56    Node shirt = gr.addNode();
     57    Node shoe = gr.addNode();
     58    Node watch = gr.addNode();
     59    Node pants = gr.addNode();
     60
     61    // Set node labels
     62    label[belt] = "Belt";
     63    label[trousers] = "Trousers";
     64    label[necktie] = "Necktie";
     65    label[coat] = "Coat";
     66    label[socks] = "Socks";
     67    label[shirt] = "Shirt";
     68    label[shoe] = "Shoe";
     69    label[watch] = "Watch";
     70    label[pants] = "Pants";
     71
     72    // Create directed arcs that indicate precedence
     73    // (the source must precede the target)
     74    gr.addArc(socks, shoe);
     75    gr.addArc(pants, shoe);
     76    gr.addArc(pants, trousers);
     77    gr.addArc(trousers, shoe);
     78    gr.addArc(trousers, belt);
     79    gr.addArc(belt, coat);
     80    gr.addArc(shirt, belt);
     81    gr.addArc(shirt, necktie);
     82    gr.addArc(necktie, coat);
     83  }
     84
     85  // Run Dfs algorithm and store the nodes in the processing order
     86  // using LoggerBoolMap
     87  typedef LoggerBoolMap< std::vector<Node>::iterator > StoreMap;
     88  std::vector<Node> v(countNodes(gr));
     89  StoreMap map(v.begin());
     90  Dfs<ListDigraph>::DefProcessedMap<StoreMap>::Create dfs(gr);
     91  dfs.processedMap(map).run();
     92
     93  // Print the nodes of the vector in reverse order, which gives a
     94  // topological ordering
     95  std::cout << "A possible order to dress up:";
     96  for(int i = v.size()-1; i >= 0; --i)
     97    std::cout << "  " << label[v[i]];
     98  std::cout << std::endl;
     99
     100  return 0;
     101}