| | 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 | |
| | 36 | using namespace lemon; |
| | 37 | |
| | 38 | int 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 | } |