| 
									
										
										
										
											2014-01-29 08:38:28 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * FindSeparator-inl.h | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *   Created on: Nov 23, 2010 | 
					
						
							| 
									
										
										
										
											2014-02-21 03:36:14 +08:00
										 |  |  |  *   Updated: Feb 20. 2014 | 
					
						
							| 
									
										
										
										
											2014-01-29 08:38:28 +08:00
										 |  |  |  *       Author: nikai | 
					
						
							| 
									
										
										
										
											2014-02-21 03:36:14 +08:00
										 |  |  |  *       Author: Andrew Melim | 
					
						
							| 
									
										
										
										
											2014-01-29 08:38:28 +08:00
										 |  |  |  *  Description: find the separator of bisectioning for a given graph | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdexcept>
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							| 
									
										
										
										
											2019-12-29 06:39:54 +08:00
										 |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2023-01-13 05:51:47 +08:00
										 |  |  | #include <optional>
 | 
					
						
							| 
									
										
										
										
											2024-12-05 00:43:51 +08:00
										 |  |  | #include <cassert>
 | 
					
						
							| 
									
										
										
										
											2014-01-29 08:38:28 +08:00
										 |  |  | #include <boost/shared_array.hpp>
 | 
					
						
							| 
									
										
										
										
											2020-05-10 09:29:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam/base/timing.h>
 | 
					
						
							| 
									
										
										
										
											2014-01-29 08:38:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 06:14:51 +08:00
										 |  |  | #include "FindSeparator.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-22 07:14:49 +08:00
										 |  |  | #include <metis.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-26 14:22:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-29 08:38:28 +08:00
										 |  |  | extern "C" { | 
					
						
							| 
									
										
										
										
											2022-02-22 06:22:39 +08:00
										 |  |  | #include <metislib.h>
 | 
					
						
							| 
									
										
										
										
											2014-01-29 08:38:28 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 06:14:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-29 08:38:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { namespace partition { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |   typedef boost::shared_array<idx_t> sharedInts; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Return the size of the separator and the partiion indices {part} | 
					
						
							|  |  |  |    * Part [j] is 0, 1, or 2, depending on | 
					
						
							|  |  |  |    * whether node j is in the left part of the graph, the right part, or the | 
					
						
							|  |  |  |    * separator, respectively | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2023-09-22 01:56:20 +08:00
										 |  |  |   std::pair<idx_t, sharedInts> separatorMetis(idx_t n, const sharedInts& xadj, | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |     const sharedInts& adjncy, const sharedInts& adjwgt, bool verbose) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // control parameters
 | 
					
						
							| 
									
										
										
										
											2019-12-29 06:39:54 +08:00
										 |  |  |     std::vector<idx_t> vwgt;        // the weights of the vertices
 | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |     idx_t options[METIS_NOPTIONS]; | 
					
						
							|  |  |  |     METIS_SetDefaultOptions(options);  // use defaults
 | 
					
						
							|  |  |  |     idx_t sepsize;                      // the size of the separator, output
 | 
					
						
							|  |  |  |     sharedInts part_(new idx_t[n]);      // the partition of each vertex, output
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // set uniform weights on the vertices
 | 
					
						
							| 
									
										
										
										
											2019-12-29 06:39:54 +08:00
										 |  |  |     vwgt.assign(n, 1); | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // TODO: Fix at later time
 | 
					
						
							|  |  |  |     //boost::timer::cpu_timer TOTALTmr;
 | 
					
						
							|  |  |  |     if (verbose) { | 
					
						
							|  |  |  |       printf("**********************************************************************\n"); | 
					
						
							|  |  |  |       printf("Graph Information ---------------------------------------------------\n"); | 
					
						
							|  |  |  |       printf("  #Vertices: %d, #Edges: %u\n", n, *(xadj.get()+n) / 2); | 
					
						
							|  |  |  |       printf("\nND Partitioning... -------------------------------------------\n"); | 
					
						
							|  |  |  |       //TOTALTmr.start()
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // call metis parition routine
 | 
					
						
							| 
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 |  |  |     METIS_ComputeVertexSeparator(&n, xadj.get(), adjncy.get(), | 
					
						
							| 
									
										
										
										
											2019-12-29 06:39:54 +08:00
										 |  |  |            &vwgt[0], options, &sepsize, part_.get()); | 
					
						
							| 
									
										
										
										
											2014-02-21 03:36:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |     if (verbose) { | 
					
						
							|  |  |  |       //boost::cpu_times const elapsed_times(timer.elapsed());
 | 
					
						
							|  |  |  |       //printf("\nTiming Information --------------------------------------------------\n");
 | 
					
						
							|  |  |  |       //printf("  Total:        \t\t %7.3f\n", elapsed_times);
 | 
					
						
							|  |  |  |       printf("  Sep size:        \t\t %d\n", sepsize); | 
					
						
							|  |  |  |       printf("**********************************************************************\n"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return std::make_pair(sepsize, part_); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   void modefied_EdgeComputeSeparator(idx_t *nvtxs, idx_t *xadj, idx_t *adjncy, idx_t *vwgt, | 
					
						
							|  |  |  |       idx_t *adjwgt, idx_t *options, idx_t *edgecut, idx_t *part) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2014-02-24 06:55:18 +08:00
										 |  |  |     idx_t i, ncon; | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |     graph_t *graph; | 
					
						
							|  |  |  |     real_t *tpwgts2; | 
					
						
							|  |  |  |     ctrl_t *ctrl; | 
					
						
							| 
									
										
										
										
											2020-04-07 05:31:05 +08:00
										 |  |  |     ctrl = SetupCtrl(METIS_OP_OMETIS, options, 1, 3, nullptr, nullptr); | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |     ctrl->iptype = METIS_IPTYPE_GROW; | 
					
						
							| 
									
										
										
										
											2020-04-07 05:31:05 +08:00
										 |  |  |     //if () == nullptr)
 | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |     //  return METIS_ERROR_INPUT;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     InitRandom(ctrl->seed); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 05:31:05 +08:00
										 |  |  |     graph = SetupGraph(ctrl, *nvtxs, 1, xadj, adjncy, vwgt, nullptr, nullptr); | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     AllocateWorkSpace(ctrl, graph); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ncon = graph->ncon; | 
					
						
							|  |  |  |     ctrl->ncuts = 1; | 
					
						
							| 
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |     /* determine the weights of the two partitions as a function of the weight of the
 | 
					
						
							|  |  |  |        target partition weights */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     tpwgts2 = rwspacemalloc(ctrl, 2*ncon); | 
					
						
							|  |  |  |     for (i=0; i<ncon; i++) { | 
					
						
							|  |  |  |       tpwgts2[i]      = rsum((2>>1), ctrl->tpwgts+i, ncon); | 
					
						
							|  |  |  |       tpwgts2[ncon+i] = 1.0 - tpwgts2[i]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     /* perform the bisection */ | 
					
						
							|  |  |  |     *edgecut = MultilevelBisect(ctrl, graph, tpwgts2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //  ConstructMinCoverSeparator(&ctrl, &graph, 1.05);
 | 
					
						
							|  |  |  |   //  *edgecut = graph->mincut;
 | 
					
						
							|  |  |  |   //  *sepsize = graph.pwgts[2];
 | 
					
						
							|  |  |  |     icopy(*nvtxs, graph->where, part); | 
					
						
							|  |  |  |     std::cout << "Finished bisection:" << *edgecut << std::endl; | 
					
						
							|  |  |  |     FreeGraph(&graph); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     FreeCtrl(&ctrl); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Return the number of edge cuts and the partition indices {part} | 
					
						
							|  |  |  |    * Part [j] is 0 or 1, depending on | 
					
						
							|  |  |  |    * whether node j is in the left part of the graph or the right part respectively | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   std::pair<int, sharedInts> edgeMetis(idx_t n, const sharedInts& xadj,  const sharedInts& adjncy, | 
					
						
							|  |  |  |     const sharedInts& adjwgt, bool verbose) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // control parameters
 | 
					
						
							| 
									
										
										
										
											2019-12-29 06:39:54 +08:00
										 |  |  |     std::vector<idx_t> vwgt;                  // the weights of the vertices
 | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |     idx_t options[METIS_NOPTIONS]; | 
					
						
							|  |  |  |     METIS_SetDefaultOptions(options);  // use defaults
 | 
					
						
							|  |  |  |     idx_t edgecut;                      // the number of edge cuts, output
 | 
					
						
							|  |  |  |     sharedInts part_(new idx_t[n]);      // the partition of each vertex, output
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // set uniform weights on the vertices
 | 
					
						
							| 
									
										
										
										
											2019-12-29 06:39:54 +08:00
										 |  |  |     vwgt.assign(n, 1); | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     //TODO: Fix later
 | 
					
						
							|  |  |  |     //boost::timer TOTALTmr;
 | 
					
						
							|  |  |  |     if (verbose) { | 
					
						
							|  |  |  |       printf("**********************************************************************\n"); | 
					
						
							|  |  |  |       printf("Graph Information ---------------------------------------------------\n"); | 
					
						
							|  |  |  |       printf("  #Vertices: %d, #Edges: %u\n", n, *(xadj.get()+n) / 2); | 
					
						
							|  |  |  |       printf("\nND Partitioning... -------------------------------------------\n"); | 
					
						
							|  |  |  |       //cleartimer(TOTALTmr);
 | 
					
						
							|  |  |  |       //starttimer(TOTALTmr);
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //int wgtflag = 1; // only edge weights
 | 
					
						
							|  |  |  |     //int numflag = 0; // c style numbering starting from 0
 | 
					
						
							|  |  |  |     //int nparts = 2; // partition the graph to 2 submaps
 | 
					
						
							| 
									
										
										
										
											2019-12-29 06:39:54 +08:00
										 |  |  |     modefied_EdgeComputeSeparator(&n, xadj.get(), adjncy.get(), &vwgt[0], adjwgt.get(), | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |         options, &edgecut, part_.get()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |     if (verbose) { | 
					
						
							|  |  |  |       //stoptimer(TOTALTmr);
 | 
					
						
							|  |  |  |       printf("\nTiming Information --------------------------------------------------\n"); | 
					
						
							|  |  |  |       //printf("  Total:        \t\t %7.3f\n", gettimer(TOTALTmr));
 | 
					
						
							|  |  |  |       printf("  Edge cuts:    \t\t %d\n", edgecut); | 
					
						
							|  |  |  |       printf("**********************************************************************\n"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return std::make_pair(edgecut, part_); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Prepare the data structure {xadj} and {adjncy} required by metis | 
					
						
							|  |  |  |    * xadj always has the size equal to the no. of the nodes plus 1 | 
					
						
							|  |  |  |    * adjncy always has the size equal to two times of the no. of the edges in the Metis graph | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   template <class GenericGraph> | 
					
						
							|  |  |  |   void prepareMetisGraph(const GenericGraph& graph, const std::vector<size_t>& keys, WorkSpace& workspace, | 
					
						
							|  |  |  |       sharedInts* ptr_xadj, sharedInts* ptr_adjncy, sharedInts* ptr_adjwgt) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     typedef std::vector<int> Weights; | 
					
						
							|  |  |  |     typedef std::vector<int> Neighbors; | 
					
						
							|  |  |  |     typedef std::pair<Neighbors, Weights> NeighborsInfo; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // set up dictionary
 | 
					
						
							|  |  |  |     std::vector<int>& dictionary = workspace.dictionary; | 
					
						
							|  |  |  |     workspace.prepareDictionary(keys); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // prepare for {adjacencyMap}, a pair of neighbor indices and the correponding edge weights
 | 
					
						
							|  |  |  |     int numNodes = keys.size(); | 
					
						
							|  |  |  |     int numEdges = 0; | 
					
						
							|  |  |  |     std::vector<NeighborsInfo> adjacencyMap; | 
					
						
							|  |  |  |     adjacencyMap.resize(numNodes); | 
					
						
							|  |  |  |     std::cout << "Number of nodes: " << adjacencyMap.size() << std::endl; | 
					
						
							|  |  |  |     int index1, index2; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |     for(const typename GenericGraph::value_type& factor: graph){ | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |       index1 = dictionary[factor->key1.index]; | 
					
						
							|  |  |  |       index2 = dictionary[factor->key2.index]; | 
					
						
							|  |  |  |       std::cout << "index1: " << index1 << std::endl; | 
					
						
							|  |  |  |       std::cout << "index2: " << index2 << std::endl; | 
					
						
							|  |  |  |       // if both nodes are in the current graph, i.e. not a joint factor between frontal and separator
 | 
					
						
							| 
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 |  |  |       if (index1 >= 0 && index2 >= 0) { | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |         std::pair<Neighbors, Weights>& adjacencyMap1 = adjacencyMap[index1]; | 
					
						
							|  |  |  |         std::pair<Neighbors, Weights>& adjacencyMap2 = adjacencyMap[index2]; | 
					
						
							|  |  |  |         try{ | 
					
						
							|  |  |  |          adjacencyMap1.first.push_back(index2); | 
					
						
							|  |  |  |          adjacencyMap1.second.push_back(factor->weight); | 
					
						
							|  |  |  |          adjacencyMap2.first.push_back(index1); | 
					
						
							|  |  |  |          adjacencyMap2.second.push_back(factor->weight); | 
					
						
							|  |  |  |         }catch(std::exception& e){ | 
					
						
							|  |  |  |           std::cout << e.what() << std::endl; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         numEdges++; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // prepare for {xadj}, {adjncy}, and {adjwgt}
 | 
					
						
							|  |  |  |     *ptr_xadj   = sharedInts(new idx_t[numNodes+1]); | 
					
						
							|  |  |  |     *ptr_adjncy = sharedInts(new idx_t[numEdges*2]); | 
					
						
							|  |  |  |     *ptr_adjwgt = sharedInts(new idx_t[numEdges*2]); | 
					
						
							|  |  |  |     sharedInts& xadj = *ptr_xadj; | 
					
						
							|  |  |  |     sharedInts& adjncy = *ptr_adjncy; | 
					
						
							|  |  |  |     sharedInts& adjwgt = *ptr_adjwgt; | 
					
						
							|  |  |  |     int ind_xadj = 0, ind_adjncy = 0; | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |     for(const NeighborsInfo& info: adjacencyMap) { | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |       *(xadj.get() + ind_xadj) = ind_adjncy; | 
					
						
							|  |  |  |       std::copy(info.first .begin(), info.first .end(), adjncy.get() + ind_adjncy); | 
					
						
							|  |  |  |       std::copy(info.second.begin(), info.second.end(), adjwgt.get() + ind_adjncy); | 
					
						
							|  |  |  |       assert(info.first.size() == info.second.size()); | 
					
						
							|  |  |  |       ind_adjncy += info.first.size(); | 
					
						
							|  |  |  |       ind_xadj ++; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (ind_xadj != numNodes) throw std::runtime_error("prepareMetisGraph_: ind_xadj != numNodes"); | 
					
						
							|  |  |  |     *(xadj.get() + ind_xadj) = ind_adjncy; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   template<class GenericGraph> | 
					
						
							| 
									
										
										
										
											2023-01-13 05:51:47 +08:00
										 |  |  |   std::optional<MetisResult> separatorPartitionByMetis(const GenericGraph& graph, | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |     const std::vector<size_t>& keys, WorkSpace& workspace, bool verbose) { | 
					
						
							|  |  |  |     // create a metis graph
 | 
					
						
							|  |  |  |     size_t numKeys = keys.size(); | 
					
						
							| 
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 |  |  |     if (verbose) | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |       std::cout << graph.size() << " factors,\t" << numKeys << " nodes;\t" << std::endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sharedInts xadj, adjncy, adjwgt; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     prepareMetisGraph<GenericGraph>(graph, keys, workspace, &xadj, &adjncy, &adjwgt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // run ND on the graph
 | 
					
						
							| 
									
										
										
										
											2023-02-05 01:42:39 +08:00
										 |  |  |     const auto [sepsize, part] = separatorMetis(numKeys, xadj, adjncy, adjwgt, verbose); | 
					
						
							| 
									
										
										
										
											2023-01-13 05:51:47 +08:00
										 |  |  |     if (!sepsize)  return std::optional<MetisResult>(); | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // convert the 0-1-2 from Metis to 1-2-0, so that the separator is 0, as later
 | 
					
						
							|  |  |  |     //  we will have more submaps
 | 
					
						
							|  |  |  |     MetisResult result; | 
					
						
							|  |  |  |     result.C.reserve(sepsize); | 
					
						
							|  |  |  |     result.A.reserve(numKeys - sepsize); | 
					
						
							|  |  |  |     result.B.reserve(numKeys - sepsize); | 
					
						
							|  |  |  |     int* ptr_part = part.get(); | 
					
						
							|  |  |  |     std::vector<size_t>::const_iterator itKey = keys.begin(); | 
					
						
							|  |  |  |     std::vector<size_t>::const_iterator itKeyLast = keys.end(); | 
					
						
							|  |  |  |     while(itKey != itKeyLast) { | 
					
						
							|  |  |  |       switch(*(ptr_part++)) { | 
					
						
							|  |  |  |       case 0: result.A.push_back(*(itKey++)); break; | 
					
						
							|  |  |  |       case 1: result.B.push_back(*(itKey++)); break; | 
					
						
							|  |  |  |       case 2: result.C.push_back(*(itKey++)); break; | 
					
						
							|  |  |  |       default: throw std::runtime_error("separatorPartitionByMetis: invalid results from Metis ND!"); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (verbose) { | 
					
						
							|  |  |  |       std::cout << "total key: " << keys.size() | 
					
						
							|  |  |  |                     << " result(A,B,C) = " << result.A.size() << ", " << result.B.size() << ", " | 
					
						
							|  |  |  |                     << result.C.size() << "; sepsize from Metis = " << sepsize << std::endl; | 
					
						
							|  |  |  |       //throw runtime_error("separatorPartitionByMetis:stop for debug");
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-22 01:56:20 +08:00
										 |  |  |     if(result.C.size() != size_t(sepsize)) { | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |       std::cout << "total key: " << keys.size() | 
					
						
							|  |  |  |           << " result(A,B,C) = " << result.A.size() << ", " << result.B.size() << ", " << result.C.size() | 
					
						
							|  |  |  |           << "; sepsize from Metis = " << sepsize << std::endl; | 
					
						
							|  |  |  |       throw std::runtime_error("separatorPartitionByMetis: invalid sepsize from Metis ND!"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-16 07:16:49 +08:00
										 |  |  |     return result; | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 |  |  |   /* *************************************************************************/ | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |   template<class GenericGraph> | 
					
						
							| 
									
										
										
										
											2023-01-13 05:51:47 +08:00
										 |  |  |   std::optional<MetisResult> edgePartitionByMetis(const GenericGraph& graph, | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |    const std::vector<size_t>& keys, WorkSpace& workspace, bool verbose) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // a small hack for handling the camera1-camera2 case used in the unit tests
 | 
					
						
							|  |  |  |     if (graph.size() == 1 && keys.size() == 2) { | 
					
						
							|  |  |  |       MetisResult result; | 
					
						
							|  |  |  |       result.A.push_back(keys.front()); | 
					
						
							|  |  |  |       result.B.push_back(keys.back()); | 
					
						
							|  |  |  |       return result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // create a metis graph
 | 
					
						
							|  |  |  |     size_t numKeys = keys.size(); | 
					
						
							|  |  |  |     if (verbose) std::cout << graph.size() << " factors,\t" << numKeys << " nodes;\t" << std::endl; | 
					
						
							|  |  |  |     sharedInts xadj, adjncy, adjwgt; | 
					
						
							|  |  |  |     prepareMetisGraph<GenericGraph>(graph, keys, workspace, &xadj, &adjncy, &adjwgt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // run metis on the graph
 | 
					
						
							| 
									
										
										
										
											2023-02-05 01:42:39 +08:00
										 |  |  |     const auto [edgecut, part] = edgeMetis(numKeys, xadj, adjncy, adjwgt, verbose); | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // convert the 0-1-2 from Metis to 1-2-0, so that the separator is 0, as later we will have more submaps
 | 
					
						
							|  |  |  |     MetisResult result; | 
					
						
							|  |  |  |     result.A.reserve(numKeys); | 
					
						
							|  |  |  |     result.B.reserve(numKeys); | 
					
						
							|  |  |  |     int* ptr_part = part.get(); | 
					
						
							|  |  |  |     std::vector<size_t>::const_iterator itKey = keys.begin(); | 
					
						
							|  |  |  |     std::vector<size_t>::const_iterator itKeyLast = keys.end(); | 
					
						
							|  |  |  |     while(itKey != itKeyLast) { | 
					
						
							|  |  |  |       if (*ptr_part != 0 && *ptr_part != 1) | 
					
						
							|  |  |  |         std::cout << *ptr_part << "!!!" << std::endl; | 
					
						
							|  |  |  |       switch(*(ptr_part++)) { | 
					
						
							|  |  |  |       case 0: result.A.push_back(*(itKey++)); break; | 
					
						
							|  |  |  |       case 1: result.B.push_back(*(itKey++)); break; | 
					
						
							|  |  |  |       default: throw std::runtime_error("edgePartitionByMetis: invalid results from Metis ND!"); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (verbose) { | 
					
						
							|  |  |  |       std::cout << "the size of two submaps in the reduced graph: " << result.A.size() | 
					
						
							|  |  |  |         << " " << result.B.size() << std::endl; | 
					
						
							|  |  |  |       int edgeCut = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |       for(const typename GenericGraph::value_type& factor: graph){ | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |         int key1 = factor->key1.index; | 
					
						
							|  |  |  |         int key2 = factor->key2.index; | 
					
						
							|  |  |  |         // print keys and their subgraph assignment
 | 
					
						
							|  |  |  |         std::cout << key1; | 
					
						
							|  |  |  |         if (std::find(result.A.begin(), result.A.end(), key1) != result.A.end()) std::cout <<"A "; | 
					
						
							|  |  |  |         if (std::find(result.B.begin(), result.B.end(), key1) != result.B.end()) std::cout <<"B "; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         std::cout << key2; | 
					
						
							|  |  |  |         if (std::find(result.A.begin(), result.A.end(), key2) != result.A.end()) std::cout <<"A "; | 
					
						
							|  |  |  |         if (std::find(result.B.begin(), result.B.end(), key2) != result.B.end()) std::cout <<"B "; | 
					
						
							| 
									
										
										
										
											2014-05-06 04:35:22 +08:00
										 |  |  |         std::cout << "weight " << factor->weight;; | 
					
						
							| 
									
										
										
										
											2014-01-29 08:38:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |         // find vertices that were assigned to sets A & B. Their edge will be cut
 | 
					
						
							|  |  |  |         if ((std::find(result.A.begin(), result.A.end(), key1) != result.A.end() && | 
					
						
							|  |  |  |              std::find(result.B.begin(), result.B.end(), key2) != result.B.end()) || | 
					
						
							|  |  |  |             (std::find(result.B.begin(), result.B.end(), key1) != result.B.end() && | 
					
						
							|  |  |  |               std::find(result.A.begin(), result.A.end(), key2) != result.A.end())){ | 
					
						
							|  |  |  |           edgeCut ++; | 
					
						
							|  |  |  |           std::cout << " CUT "; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         std::cout << std::endl; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       std::cout << "edgeCut: " << edgeCut << std::endl; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-16 07:16:49 +08:00
										 |  |  |     return result; | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   bool isLargerIsland(const std::vector<size_t>& island1, const std::vector<size_t>& island2) { | 
					
						
							|  |  |  |     return island1.size() > island2.size(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   // debug functions
 | 
					
						
							|  |  |  |   void printIsland(const std::vector<size_t>& island) { | 
					
						
							|  |  |  |     std::cout << "island: "; | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |     for(const size_t key: island) | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |       std::cout << key << " "; | 
					
						
							|  |  |  |     std::cout << std::endl; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void printIslands(const std::list<std::vector<size_t> >& islands) { | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |     for(const std::vector<std::size_t>& island: islands) | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |         printIsland(island); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void printNumCamerasLandmarks(const std::vector<size_t>& keys, const std::vector<Symbol>& int2symbol) { | 
					
						
							|  |  |  |     int numCamera = 0, numLandmark = 0; | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |     for(const size_t key: keys) | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |     if (int2symbol[key].chr() == 'x') | 
					
						
							|  |  |  |       numCamera++; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       numLandmark++; | 
					
						
							|  |  |  |     std::cout << "numCamera: " << numCamera << " numLandmark: " << numLandmark << std::endl; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   template<class GenericGraph> | 
					
						
							|  |  |  |   void addLandmarkToPartitionResult(const GenericGraph& graph, const std::vector<size_t>& landmarkKeys, | 
					
						
							|  |  |  |       MetisResult& partitionResult, WorkSpace& workspace) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // set up cameras in the dictionary
 | 
					
						
							|  |  |  |     std::vector<size_t>& A = partitionResult.A; | 
					
						
							|  |  |  |     std::vector<size_t>& B = partitionResult.B; | 
					
						
							|  |  |  |     std::vector<size_t>& C = partitionResult.C; | 
					
						
							|  |  |  |     std::vector<int>& dictionary = workspace.dictionary; | 
					
						
							|  |  |  |     std::fill(dictionary.begin(), dictionary.end(), -1); | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |     for(const size_t a: A) | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |       dictionary[a] = 1; | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |     for(const size_t b: B) | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |       dictionary[b] = 2; | 
					
						
							|  |  |  |     if (!C.empty()) | 
					
						
							|  |  |  |       throw std::runtime_error("addLandmarkToPartitionResult: C is not empty"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // set up landmarks
 | 
					
						
							|  |  |  |     size_t i,j; | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |     for(const typename GenericGraph::value_type& factor: graph) { | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |       i = factor->key1.index; | 
					
						
							|  |  |  |       j = factor->key2.index; | 
					
						
							|  |  |  |       if (dictionary[j] == 0) // if the landmark is already in the separator, continue
 | 
					
						
							|  |  |  |         continue; | 
					
						
							|  |  |  |       else if (dictionary[j] == -1) | 
					
						
							|  |  |  |         dictionary[j] = dictionary[i]; | 
					
						
							|  |  |  |       else { | 
					
						
							|  |  |  |         if (dictionary[j] != dictionary[i]) | 
					
						
							|  |  |  |           dictionary[j] = 0; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | //      if (j == 67980)
 | 
					
						
							|  |  |  | //        std::cout << "dictionary[67980]" << dictionary[j] << std::endl;
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |     for(const size_t j: landmarkKeys) { | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |       switch(dictionary[j]) { | 
					
						
							|  |  |  |       case 0: C.push_back(j); break; | 
					
						
							|  |  |  |       case 1: A.push_back(j); break; | 
					
						
							|  |  |  |       case 2: B.push_back(j); break; | 
					
						
							|  |  |  |       default: std::cout << j << ": " << dictionary[j] << std::endl; | 
					
						
							|  |  |  |       throw std::runtime_error("addLandmarkToPartitionResult: wrong status for landmark"); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-01-29 08:38:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define REDUCE_CAMERA_GRAPH
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   template<class GenericGraph> | 
					
						
							| 
									
										
										
										
											2023-01-13 05:51:47 +08:00
										 |  |  |   std::optional<MetisResult> findPartitoning(const GenericGraph& graph, const std::vector<size_t>& keys, | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |       WorkSpace& workspace, bool verbose, | 
					
						
							| 
									
										
										
										
											2023-01-13 05:51:47 +08:00
										 |  |  |       const std::optional<std::vector<Symbol> >& int2symbol, const bool reduceGraph) { | 
					
						
							|  |  |  |     std::optional<MetisResult> result; | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |     GenericGraph reducedGraph; | 
					
						
							|  |  |  |     std::vector<size_t> keyToPartition; | 
					
						
							|  |  |  |     std::vector<size_t> cameraKeys, landmarkKeys; | 
					
						
							|  |  |  |     if (reduceGraph) { | 
					
						
							| 
									
										
										
										
											2023-01-13 05:51:47 +08:00
										 |  |  |       if (!int2symbol.has_value()) | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |         throw std::invalid_argument("findSeparator: int2symbol must be valid!"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // find out all the landmark keys, which are to be eliminated
 | 
					
						
							|  |  |  |       cameraKeys.reserve(keys.size()); | 
					
						
							|  |  |  |       landmarkKeys.reserve(keys.size()); | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |       for(const size_t key: keys) { | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |         if((*int2symbol)[key].chr() == 'x') | 
					
						
							|  |  |  |           cameraKeys.push_back(key); | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           landmarkKeys.push_back(key); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       keyToPartition = cameraKeys; | 
					
						
							|  |  |  |       workspace.prepareDictionary(keyToPartition); | 
					
						
							|  |  |  |       const std::vector<int>& dictionary = workspace.dictionary; | 
					
						
							|  |  |  |       reduceGenericGraph(graph, cameraKeys, landmarkKeys, dictionary, reducedGraph); | 
					
						
							|  |  |  |       std::cout << "original graph: V" << keys.size() << ", E" << graph.size() | 
					
						
							|  |  |  |       << " --> reduced graph: V" << cameraKeys.size() << ", E" << reducedGraph.size() << std::endl; | 
					
						
							|  |  |  |       result = edgePartitionByMetis(reducedGraph, keyToPartition, workspace, verbose); | 
					
						
							|  |  |  |     }  else // call Metis to partition the graph to A, B, C
 | 
					
						
							|  |  |  |       result = separatorPartitionByMetis(graph, keys, workspace, verbose); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-13 05:51:47 +08:00
										 |  |  |     if (!result.has_value()) { | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |       std::cout << "metis failed!" << std::endl; | 
					
						
							| 
									
										
										
										
											2023-01-13 05:51:47 +08:00
										 |  |  |       return {}; | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (reduceGraph) { | 
					
						
							|  |  |  |       addLandmarkToPartitionResult(graph, landmarkKeys, *result, workspace); | 
					
						
							|  |  |  |       std::cout << "the separator size: " << result->C.size() << " landmarks" << std::endl; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   template<class GenericGraph> | 
					
						
							|  |  |  |   int findSeparator(const GenericGraph& graph, const std::vector<size_t>& keys, | 
					
						
							|  |  |  |       const int minNodesPerMap, WorkSpace& workspace, bool verbose, | 
					
						
							| 
									
										
										
										
											2023-01-13 05:51:47 +08:00
										 |  |  |       const std::optional<std::vector<Symbol> >& int2symbol, const bool reduceGraph, | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |       const int minNrConstraintsPerCamera, const int minNrConstraintsPerLandmark) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-13 05:51:47 +08:00
										 |  |  |     std::optional<MetisResult> result = findPartitoning(graph, keys, workspace, | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |       verbose, int2symbol, reduceGraph); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // find the island in A and B, and make them separated submaps
 | 
					
						
							|  |  |  |     typedef std::vector<size_t> Island; | 
					
						
							|  |  |  |     std::list<Island> islands; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::list<Island> islands_in_A = findIslands(graph, result->A, workspace, | 
					
						
							|  |  |  |       minNrConstraintsPerCamera, minNrConstraintsPerLandmark); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::list<Island> islands_in_B = findIslands(graph, result->B, workspace, | 
					
						
							|  |  |  |       minNrConstraintsPerCamera, minNrConstraintsPerLandmark); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     islands.insert(islands.end(), islands_in_A.begin(), islands_in_A.end()); | 
					
						
							|  |  |  |     islands.insert(islands.end(), islands_in_B.begin(), islands_in_B.end()); | 
					
						
							|  |  |  |     islands.sort(isLargerIsland); | 
					
						
							|  |  |  |     size_t numIsland0 = islands.size(); | 
					
						
							| 
									
										
										
										
											2014-01-29 08:38:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef NDEBUG
 | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  | //    verbose = true;
 | 
					
						
							|  |  |  | //    if (!int2symbol) throw std::invalid_argument("findSeparator: int2symbol is not set!");
 | 
					
						
							|  |  |  | //    std::cout << "sep size: " << result->C.size() << "; ";
 | 
					
						
							|  |  |  | //    printNumCamerasLandmarks(result->C, *int2symbol);
 | 
					
						
							|  |  |  | //    std::cout << "no. of island: " << islands.size() << "; ";
 | 
					
						
							|  |  |  | //    std::cout << "island size: ";
 | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  | //    for(const Island& island: islands)
 | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  | //      std::cout << island.size() << " ";
 | 
					
						
							|  |  |  | //    std::cout << std::endl;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  | //    for(const Island& island: islands) {
 | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  | //      printNumCamerasLandmarks(island, int2symbol);
 | 
					
						
							|  |  |  | //    }
 | 
					
						
							| 
									
										
										
										
											2014-01-29 08:38:28 +08:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |     // absorb small components into the separator
 | 
					
						
							|  |  |  |     size_t oldSize = islands.size(); | 
					
						
							|  |  |  |     while(true) { | 
					
						
							|  |  |  |       if (islands.size() < 2) { | 
					
						
							|  |  |  |         std::cout << "numIsland: " << numIsland0 << std::endl; | 
					
						
							|  |  |  |         throw std::runtime_error("findSeparator: found fewer than 2 submaps!"); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       std::list<Island>::reference island = islands.back(); | 
					
						
							|  |  |  |       if ((int)island.size() >= minNodesPerMap) break; | 
					
						
							|  |  |  |       result->C.insert(result->C.end(), island.begin(), island.end()); | 
					
						
							|  |  |  |       islands.pop_back(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (islands.size() != oldSize){ | 
					
						
							|  |  |  |       if (verbose) std::cout << oldSize << "-" << oldSize - islands.size() << " submap(s);\t" << std::endl; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else{ | 
					
						
							|  |  |  |       if (verbose) std::cout << oldSize << " submap(s);\t" << std::endl; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // generate the node map
 | 
					
						
							|  |  |  |     std::vector<int>& partitionTable = workspace.partitionTable; | 
					
						
							|  |  |  |     std::fill(partitionTable.begin(), partitionTable.end(), -1); | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |     for(const size_t key: result->C) | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |       partitionTable[key] = 0; | 
					
						
							|  |  |  |     int idx = 0; | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |     for(const Island& island: islands) { | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |       idx++; | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |       for(const size_t key: island) { | 
					
						
							| 
									
										
										
										
											2014-10-31 00:44:46 +08:00
										 |  |  |         partitionTable[key] = idx; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return islands.size(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-01-29 08:38:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | }} //namespace
 |