topological_ordering.cc File Reference
Detailed Description
#include <iostream>
#include <string>
#include <list>
#include <lemon/list_graph.h>
#include <lemon/maps.h>
#include <lemon/dfs.h>
using namespace lemon;
template< typename GR >
class SerializingWriteMap
{
public:
typedef typename GR::Node Key;
typedef bool Value;
typedef std::list<Key> OrderedList;
OrderedList order;
SerializingWriteMap( const GR& ) {}
void set( const Key& k, const Value& v ) {
if( v ) order.push_front(k);
}
Value operator[] ( const Key& ) {
return false;
}
};
int main()
{
std::cout << "Topological Ordering demo" << std::endl;
ListGraph gr;
ListGraph::NodeMap<std::string> label(gr);
typedef ListGraph::Node Node;
{
Node belt = gr.addNode();
Node trousers = gr.addNode();
Node necktie = gr.addNode();
Node coat = gr.addNode();
Node socks = gr.addNode();
Node shirt = gr.addNode();
Node shoe = gr.addNode();
Node watch = gr.addNode();
Node pants = gr.addNode();
label[belt] = "Belt";
label[trousers] = "Trousers";
label[necktie] = "Necktie";
label[coat] = "Coat";
label[socks] = "Socks";
label[shirt] = "Shirt";
label[shoe] = "Shoe";
label[watch] = "Watch";
label[pants] = "Pants";
gr.addEdge( socks, shoe );
gr.addEdge( pants, shoe );
gr.addEdge( pants, trousers );
gr.addEdge( trousers, shoe );
gr.addEdge( trousers, belt );
gr.addEdge( belt, coat );
gr.addEdge( shirt, belt );
gr.addEdge( shirt, necktie );
gr.addEdge( necktie, coat );
}
Dfs<ListGraph, Dfs<ListGraph>::DefProcessedMapTraits<SerializingWriteMap<ListGraph> > > dfs(gr);
SerializingWriteMap<ListGraph> list(gr);
dfs.processedMap(list);
dfs.run();
std::cout << "One possible order to dress up:";
for( SerializingWriteMap<ListGraph>::OrderedList::iterator it = list.order.begin(); it != list.order.end(); ++it )
std::cout << " " << label[*it];
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <string>
#include <list>
#include <lemon/list_graph.h>
#include <lemon/maps.h>
#include <lemon/dfs.h>