zsuzska@1578: /* -*- C++ -*- zsuzska@1578: * demo/kruskal_demo.cc - Part of LEMON, a generic C++ optimization library zsuzska@1578: * zsuzska@1578: * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport zsuzska@1578: * (Egervary Research Group on Combinatorial Optimization, EGRES). zsuzska@1578: * zsuzska@1578: * Permission to use, modify and distribute this software is granted zsuzska@1578: * provided that this copyright notice appears in all copies. For zsuzska@1578: * precise terms see the accompanying LICENSE file. zsuzska@1578: * zsuzska@1578: * This software is provided "AS IS" with no warranty of any kind, zsuzska@1578: * express or implied, and with no claim as to its suitability for any zsuzska@1578: * purpose. zsuzska@1578: * zsuzska@1578: */ zsuzska@1578: zsuzska@1578: ///\ingroup demos zsuzska@1578: ///\file zsuzska@1578: ///\brief Minimum weight spanning tree by Kruskal algorithm (demo). zsuzska@1578: /// zsuzska@1578: ///This demo program shows how to find a minimum weight spannin tree zsuzska@1578: ///of a graph by using the Kruskal algorithm. zsuzska@1578: athos@1182: #include athos@1182: #include athos@1182: athos@1182: #include athos@1182: #include athos@1182: #include athos@1182: athos@1182: athos@1182: using namespace std; athos@1182: using namespace lemon; athos@1182: athos@1182: athos@1182: int main() { athos@1182: athos@1182: typedef ListGraph::Node Node; athos@1182: typedef ListGraph::Edge Edge; athos@1182: typedef ListGraph::NodeIt NodeIt; athos@1182: typedef ListGraph::EdgeIt EdgeIt; athos@1182: zsuzska@1578: ListGraph g; zsuzska@1578: //Make an example graph g. zsuzska@1578: Node s=g.addNode(); zsuzska@1578: Node v1=g.addNode(); zsuzska@1578: Node v2=g.addNode(); zsuzska@1578: Node v3=g.addNode(); zsuzska@1578: Node v4=g.addNode(); zsuzska@1578: Node t=g.addNode(); zsuzska@1578: zsuzska@1578: Edge e1 = g.addEdge(s, v1); zsuzska@1578: Edge e2 = g.addEdge(s, v2); zsuzska@1578: Edge e3 = g.addEdge(v1, v2); zsuzska@1578: Edge e4 = g.addEdge(v2, v1); zsuzska@1578: Edge e5 = g.addEdge(v1, v3); zsuzska@1578: Edge e6 = g.addEdge(v3, v2); zsuzska@1578: Edge e7 = g.addEdge(v2, v4); zsuzska@1578: Edge e8 = g.addEdge(v4, v3); zsuzska@1578: Edge e9 = g.addEdge(v3, t); zsuzska@1578: Edge e10 = g.addEdge(v4, t); athos@1182: zsuzska@1578: //Make the input and output for the kruskal. athos@1182: typedef ListGraph::EdgeMap ECostMap; athos@1182: typedef ListGraph::EdgeMap EBoolMap; athos@1182: zsuzska@1578: ECostMap edge_cost_map(g, 2); zsuzska@1578: EBoolMap tree_map(g); athos@1182: zsuzska@1578: // Kruskal. zsuzska@1578: std::cout << "The weight of the minimum spanning tree by using Kruskal algorithm is " zsuzska@1578: << kruskal(g, ConstMap(2), tree_map)< tree_edge_vec; athos@1182: zsuzska@1578: //Test with non uniform costs and inserter. zsuzska@1578: std::cout << "The weight of the minimum spanning tree with non-uniform costs is " << zsuzska@1578: kruskal(g, edge_cost_map_2, std::back_inserter(tree_edge_vec)) <