[906] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1956] | 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
[2553] | 5 | * Copyright (C) 2003-2008 |
---|
[1956] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[906] | 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 | |
---|
[810] | 19 | #include <iostream> |
---|
| 20 | #include <vector> |
---|
| 21 | |
---|
| 22 | #include "test_tools.h" |
---|
[921] | 23 | #include <lemon/maps.h> |
---|
| 24 | #include <lemon/kruskal.h> |
---|
| 25 | #include <lemon/list_graph.h> |
---|
[2424] | 26 | |
---|
[2260] | 27 | #include <lemon/concepts/maps.h> |
---|
| 28 | #include <lemon/concepts/graph.h> |
---|
[2424] | 29 | #include <lemon/concepts/ugraph.h> |
---|
[810] | 30 | |
---|
| 31 | |
---|
| 32 | using namespace std; |
---|
[921] | 33 | using namespace lemon; |
---|
[810] | 34 | |
---|
| 35 | void checkCompileKruskal() |
---|
| 36 | { |
---|
[2260] | 37 | concepts::WriteMap<concepts::Graph::Edge,bool> w; |
---|
[2424] | 38 | concepts::WriteMap<concepts::UGraph::UEdge,bool> uw; |
---|
| 39 | |
---|
| 40 | concepts::ReadMap<concepts::Graph::Edge,int> r; |
---|
| 41 | concepts::ReadMap<concepts::UGraph::UEdge,int> ur; |
---|
[810] | 42 | |
---|
[2260] | 43 | concepts::Graph g; |
---|
[2424] | 44 | concepts::UGraph ug; |
---|
| 45 | |
---|
| 46 | kruskal(g, r, w); |
---|
| 47 | kruskal(ug, ur, uw); |
---|
| 48 | |
---|
| 49 | std::vector<std::pair<concepts::Graph::Edge, int> > rs; |
---|
| 50 | std::vector<std::pair<concepts::UGraph::UEdge, int> > urs; |
---|
| 51 | |
---|
| 52 | kruskal(g, rs, w); |
---|
| 53 | kruskal(ug, urs, uw); |
---|
| 54 | |
---|
| 55 | std::vector<concepts::Graph::Edge> ws; |
---|
| 56 | std::vector<concepts::UGraph::UEdge> uws; |
---|
| 57 | |
---|
| 58 | kruskal(g, r, ws.begin()); |
---|
| 59 | kruskal(ug, ur, uws.begin()); |
---|
| 60 | |
---|
| 61 | Kruskal<concepts::UGraph,concepts::ReadMap<concepts::UGraph::UEdge,int> > |
---|
| 62 | alg(ug, ur); |
---|
| 63 | |
---|
| 64 | alg.init(); |
---|
| 65 | alg.initPresorted(uws.begin(), uws.end()); |
---|
| 66 | alg.reinit(); |
---|
| 67 | |
---|
| 68 | alg.emptyQueue(); |
---|
| 69 | |
---|
| 70 | alg.nextEdge(); |
---|
| 71 | alg.processNextEdge(); |
---|
| 72 | alg.processEdge(concepts::UGraph::UEdge()); |
---|
| 73 | |
---|
| 74 | alg.run(); |
---|
| 75 | |
---|
| 76 | alg.treeMap(); |
---|
| 77 | alg.tree(concepts::UGraph::UEdge()); |
---|
[810] | 78 | } |
---|
| 79 | |
---|
| 80 | int main() { |
---|
| 81 | |
---|
[2424] | 82 | typedef ListUGraph::Node Node; |
---|
| 83 | typedef ListUGraph::UEdge UEdge; |
---|
| 84 | typedef ListUGraph::NodeIt NodeIt; |
---|
| 85 | typedef ListUGraph::EdgeIt EdgeIt; |
---|
[810] | 86 | |
---|
[2424] | 87 | ListUGraph G; |
---|
[810] | 88 | |
---|
| 89 | Node s=G.addNode(); |
---|
| 90 | Node v1=G.addNode(); |
---|
| 91 | Node v2=G.addNode(); |
---|
| 92 | Node v3=G.addNode(); |
---|
| 93 | Node v4=G.addNode(); |
---|
| 94 | Node t=G.addNode(); |
---|
| 95 | |
---|
[2424] | 96 | UEdge e1 = G.addEdge(s, v1); |
---|
| 97 | UEdge e2 = G.addEdge(s, v2); |
---|
| 98 | UEdge e3 = G.addEdge(v1, v2); |
---|
| 99 | UEdge e4 = G.addEdge(v2, v1); |
---|
| 100 | UEdge e5 = G.addEdge(v1, v3); |
---|
| 101 | UEdge e6 = G.addEdge(v3, v2); |
---|
| 102 | UEdge e7 = G.addEdge(v2, v4); |
---|
| 103 | UEdge e8 = G.addEdge(v4, v3); |
---|
| 104 | UEdge e9 = G.addEdge(v3, t); |
---|
| 105 | UEdge e10 = G.addEdge(v4, t); |
---|
[810] | 106 | |
---|
[2424] | 107 | typedef ListUGraph::UEdgeMap<int> ECostMap; |
---|
| 108 | typedef ListUGraph::UEdgeMap<bool> EBoolMap; |
---|
[810] | 109 | |
---|
| 110 | ECostMap edge_cost_map(G, 2); |
---|
| 111 | EBoolMap tree_map(G); |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | //Test with const map. |
---|
[2424] | 115 | check(kruskal(G, ConstMap<ListUGraph::UEdge,int>(2), tree_map)==10, |
---|
[810] | 116 | "Total cost should be 10"); |
---|
| 117 | //Test with a edge map (filled with uniform costs). |
---|
[1557] | 118 | check(kruskal(G, edge_cost_map, tree_map)==10, |
---|
[810] | 119 | "Total cost should be 10"); |
---|
| 120 | |
---|
| 121 | edge_cost_map.set(e1, -10); |
---|
| 122 | edge_cost_map.set(e2, -9); |
---|
| 123 | edge_cost_map.set(e3, -8); |
---|
| 124 | edge_cost_map.set(e4, -7); |
---|
| 125 | edge_cost_map.set(e5, -6); |
---|
| 126 | edge_cost_map.set(e6, -5); |
---|
| 127 | edge_cost_map.set(e7, -4); |
---|
| 128 | edge_cost_map.set(e8, -3); |
---|
| 129 | edge_cost_map.set(e9, -2); |
---|
| 130 | edge_cost_map.set(e10, -1); |
---|
| 131 | |
---|
[2424] | 132 | vector<UEdge> tree_edge_vec(5); |
---|
[810] | 133 | |
---|
| 134 | //Test with a edge map and inserter. |
---|
[1557] | 135 | check(kruskal(G, edge_cost_map, |
---|
| 136 | tree_edge_vec.begin()) |
---|
[810] | 137 | ==-31, |
---|
| 138 | "Total cost should be -31."); |
---|
[1557] | 139 | |
---|
[885] | 140 | tree_edge_vec.clear(); |
---|
| 141 | |
---|
[1557] | 142 | check(kruskal(G, edge_cost_map, |
---|
| 143 | back_inserter(tree_edge_vec)) |
---|
| 144 | ==-31, |
---|
| 145 | "Total cost should be -31."); |
---|
| 146 | |
---|
[2424] | 147 | // tree_edge_vec.clear(); |
---|
[1557] | 148 | |
---|
[2424] | 149 | // //The above test could also be coded like this: |
---|
| 150 | // check(kruskal(G, |
---|
| 151 | // makeKruskalMapInput(G, edge_cost_map), |
---|
| 152 | // makeKruskalSequenceOutput(back_inserter(tree_edge_vec))) |
---|
| 153 | // ==-31, |
---|
| 154 | // "Total cost should be -31."); |
---|
[885] | 155 | |
---|
[810] | 156 | check(tree_edge_vec.size()==5,"The tree should have 5 edges."); |
---|
| 157 | |
---|
| 158 | check(tree_edge_vec[0]==e1 && |
---|
| 159 | tree_edge_vec[1]==e2 && |
---|
| 160 | tree_edge_vec[2]==e5 && |
---|
| 161 | tree_edge_vec[3]==e7 && |
---|
| 162 | tree_edge_vec[4]==e9, |
---|
| 163 | "Wrong tree."); |
---|
| 164 | |
---|
[2424] | 165 | Kruskal<ListUGraph, ECostMap> ka(G, edge_cost_map); |
---|
| 166 | |
---|
| 167 | ka.run(); |
---|
| 168 | |
---|
| 169 | check(ka.tree(e1) && |
---|
| 170 | ka.tree(e2) && |
---|
| 171 | ka.tree(e5) && |
---|
| 172 | ka.tree(e7) && |
---|
| 173 | ka.tree(e9), |
---|
| 174 | "Wrong tree."); |
---|
| 175 | |
---|
| 176 | check(ka.treeValue() == -31, |
---|
| 177 | "Total cost should be -31."); |
---|
| 178 | |
---|
[810] | 179 | return 0; |
---|
| 180 | } |
---|