athos@276: // -*- c++ -*- athos@306: #ifndef HUGO_MINLENGTHPATHS_H athos@306: #define HUGO_MINLENGTHPATHS_H athos@276: klao@491: ///\ingroup galgs alpar@294: ///\file athos@306: ///\brief An algorithm for finding k paths of minimal total length. alpar@294: athos@276: #include athos@607: //#include athos@607: //#include athos@607: #include athos@607: #include athos@607: #include athos@607: #include athos@306: athos@276: namespace hugo { athos@276: alpar@430: /// \addtogroup galgs alpar@430: /// @{ athos@322: klao@310: ///\brief Implementation of an algorithm for finding k paths between 2 nodes athos@306: /// of minimal total length klao@310: /// klao@310: /// The class \ref hugo::MinLengthPaths "MinLengthPaths" implements athos@513: /// an algorithm for finding k edge-disjoint paths klao@310: /// from a given source node to a given target node in an klao@310: /// edge-weighted directed graph having minimal total weigth (length). alpar@456: /// athos@607: ///\warning It is assumed that the lengths are positive, since the athos@607: /// general flow-decomposition is not implemented yet. athos@607: /// alpar@456: ///\author Attila Bernath klao@310: template athos@607: class MinLengthPaths{ athos@607: athos@276: klao@310: typedef typename LengthMap::ValueType Length; athos@511: athos@276: typedef typename Graph::Node Node; athos@276: typedef typename Graph::NodeIt NodeIt; athos@276: typedef typename Graph::Edge Edge; athos@276: typedef typename Graph::OutEdgeIt OutEdgeIt; athos@511: typedef typename Graph::template EdgeMap EdgeIntMap; athos@306: athos@306: typedef ConstMap ConstMap; athos@306: athos@607: //Input athos@607: const Graph& G; athos@276: athos@607: //Auxiliary variables athos@607: //This is the capacity map for the mincostflow problem athos@607: ConstMap const1map; athos@607: //This MinCostFlows instance will actually solve the problem athos@607: MinCostFlows mincost_flow; athos@511: athos@322: //Container to store found paths athos@322: std::vector< std::vector > paths; athos@322: athos@276: public : klao@310: athos@276: athos@607: MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G), athos@607: const1map(1), mincost_flow(_G, _length, const1map){} athos@276: alpar@329: ///Runs the algorithm. alpar@329: alpar@329: ///Runs the algorithm. athos@306: ///Returns k if there are at least k edge-disjoint paths from s to t. athos@607: ///Otherwise it returns the number of found edge-disjoint paths from s to t. athos@306: int run(Node s, Node t, int k) { athos@276: athos@607: int i = mincost_flow.run(s,t,k); athos@607: athos@511: athos@306: athos@322: //Let's find the paths athos@511: //We put the paths into stl vectors (as an inner representation). athos@511: //In the meantime we lose the information stored in 'reversed'. athos@322: //We suppose the lengths to be positive now. athos@511: athos@607: //We don't want to change the flow of mincost_flow, so we make a copy athos@607: //The name here suggests that the flow has only 0/1 values. athos@607: EdgeIntMap reversed(G); athos@607: athos@607: FOR_EACH_LOC(typename Graph::EdgeIt, e, G){ athos@607: reversed[e] = mincost_flow.getFlow()[e]; athos@607: } athos@607: athos@322: paths.clear(); athos@607: //total_length=0; athos@322: paths.resize(k); athos@322: for (int j=0; j athos@607: void getPath(DirPath& p, size_t j){ athos@607: athos@511: p.clear(); athos@607: if (j>paths.size()-1){ athos@607: return; athos@607: } athos@511: typename DirPath::Builder B(p); athos@511: for(typename std::vector::iterator i=paths[j].begin(); athos@511: i!=paths[j].end(); ++i ){ athos@520: B.pushBack(*i); athos@511: } athos@511: athos@511: B.commit(); athos@511: } athos@276: klao@310: }; //class MinLengthPaths athos@276: alpar@430: ///@} athos@276: athos@276: } //namespace hugo athos@276: athos@306: #endif //HUGO_MINLENGTHPATHS_H