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: /// athos@1583: /// This demo program shows how to find a minimum weight spanning tree athos@1583: /// in a graph by using the Kruskal algorithm. alpar@1641: /// alpar@1641: /// \include kruskal_demo.cc 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@1584: //Make the input for the kruskal. athos@1182: typedef ListGraph::EdgeMap ECostMap; zsuzska@1584: ECostMap edge_cost_map(g); zsuzska@1584: zsuzska@1584: // Fill the edge_cost_map. zsuzska@1584: edge_cost_map.set(e1, -10); zsuzska@1584: edge_cost_map.set(e2, -9); zsuzska@1584: edge_cost_map.set(e3, -8); zsuzska@1584: edge_cost_map.set(e4, -7); zsuzska@1584: edge_cost_map.set(e5, -6); zsuzska@1584: edge_cost_map.set(e6, -5); zsuzska@1584: edge_cost_map.set(e7, -4); zsuzska@1584: edge_cost_map.set(e8, -3); zsuzska@1584: edge_cost_map.set(e9, -2); zsuzska@1584: edge_cost_map.set(e10, -1); zsuzska@1584: zsuzska@1584: // Make the map or the vector, which will contain the edges of the minimum zsuzska@1584: // spanning tree. zsuzska@1584: athos@1182: typedef ListGraph::EdgeMap EBoolMap; zsuzska@1578: EBoolMap tree_map(g); athos@1182: athos@1182: vector tree_edge_vec; athos@1182: athos@1182: zsuzska@1584: //Kruskal Algorithm. athos@1182: zsuzska@1584: //Input: a graph (g); a costmap of the graph (edge_cost_map); a zsuzska@1584: //boolmap (tree_map) or a vector (tree_edge_vec) to store the edges zsuzska@1584: //of the output tree; athos@1182: zsuzska@1584: //Output: it gives back the value of the minimum spanning tree, and zsuzska@1584: //set true for the edges of the tree in the edgemap tree_map or zsuzska@1584: //store the edges of the tree in the vector tree_edge_vec; zsuzska@1584: zsuzska@1584: zsuzska@1584: // Kruskal with boolmap; zsuzska@1584: std::cout << "The weight of the minimum spanning tree is " << zsuzska@1584: kruskal(g, edge_cost_map, tree_map)<=0; i--) zsuzska@1584: std::cout << g.id(tree_edge_vec[i]) << ";" ; zsuzska@1584: std::cout << std::endl; alpar@1641: std::cout << "The size of the tree again is: "<< tree_edge_vec.size() alpar@1641: << std::endl; zsuzska@1584: zsuzska@1584: athos@1182: return 0; athos@1182: }