std::vector<sets> all_subsets(int n);
subsets = all_subsets(map.size())
best = subsets[0]
for (i = 1; i < subsets.size(); i++)
if ok(map, subsets[i]) and
subsets[i].size() > best.size()
best = subsets[i]
{1 2 3}.
| AllSS { 1 2 3 } | = | AllSS { 1 2 } union (AllSS { 1 2 } + 3) |
| AllSS { 1 2 } | = | AllSS { 1 } union (AllSS { 1 } + 2) |
| AllSS { 1 } | = | AllSS { } union (AllSS { } + 1) |
| AllSS { } | = | { { } } |
| AllSS { 1 } | = | { { } } union ({ { } } + 1) |
| = | { { } } union { { 1 } } | |
| = | { { } { 1 } } | |
| AllSS { 1 2 } | = | AllSS { 1 } union (AllSS { 1 } + 2) |
| AllSS { 1 2 } | = | { { } { 1 } } union ({ { } { 1 } } + 2) |
| AllSS { 1 2 } | = | { { } { 1 } } union { { 2 } { 1 2 } } |
| AllSS { 1 2 } | = | { { } { 1 } { 2 } { 1 2 } } |
| AllSS { 1 2 3 } | = | AllSS { 1 2 } |
| union (AllSS { 1 2 } + 3) | ||
| AllSS { 1 2 3 } | = | { { } { 1 } { 2 } { 1 2 } } |
| union ({ { } { 1 } { 2 } { 1 2 } } + 3) | ||
| AllSS { 1 2 3 } | = | { { } { 1 } { 2 } { 1 2 } } |
| union { { 3 } { 1 3 } { 2 3 } { 1 2 3 } } | ||
| AllSS { 1 2 3 } | = | { { } { 1 } { 2 } { 1 2 } { 3 } { 1 3 } { 2 3 } { 1 2 3 } } |
void place(
uvec current, uvec & best, unsigned ctown)
if ctown < map.size()
place(current, best, ctown + 1)
current.push_back(ctown)
place(current, best, ctown + 1)
else if best.size() < current.size()
and ok(current)
best = current
uvec maximal_placement()
uvec towns
place(uvec(), towns, 0)
return towns
0: 1 2 1: 0 2: 0
|
->
|
0: 2 1: 2 2: 0 1
|
map[index.front()] has the fewest neighbors.
map[index.back()] has the most neighbors.
If i < j, then
map[index[i]].size() < map[index[j]].size().
i, j, and the
map.
struct map_less {
const umat & map;
map_less(const umat & map) : map(map) { }
bool
operator () (unsigned i, unsigned j) {
return map[i].size() < map[j].size();
}
};
place(uvec index, uvec current, uvec & best, unsigned ctown) if ctown < map.size() place(index, current, best, ctown + 1) current.push_back(index[ctown]) if ok(current) if best.size() < current.size() best = current place(index, current, best, ctown + 1) uvec maximal_placement() uvec towns uvec index(map.size()) for (unsigned i = 0; i < map.size(); i++) index[i] = i std::sort(index.begin(), index.end(), map_less(map)) place(index, uvec(), towns, map.size()) return towns
void place( uvec current, uvec & best, unsigned ctown) const unsigned max = current.size() + (map.size() - ctown) if max > best.size() place(current, best, ctown + 1) current.push_back(ctown) if ok(current) if best.size() < current.size() best = current place(current, best, ctown + 1) uvec maximal_placement() uvec towns place(uvec(), towns, 0) return towns
void place(
flag_set valid, uvec current, uvec & best)
const flags_iter
b = valid.begin(),
e = valid.end(),
v = std::find(b, e, true)
if (v == e) return
*v = false
place(valid, current, best)
const unsigned i = v - b
std::for_each(
map.at(i).begin(), map.at(i).end(),
set_false(valid))
current.push_back(i)
place(valid, current, best)
if best.size() < current.size()
best = current
typedef std::vector<bool> flag_set
typedef flag_set::iterator flags_iter
typedef flag_set::const_iterator flags_citer
struct set_false {
flag_set & flags;
set_false(flag_set & flags)
: flags(flags) { }
void operator () (unsigned i) {
flags.at(i) = false;
}
};
uvec maximal_placement(const umat & map)
uvec towns
flag_set valid(map.size(), true)
place(valid, uvec(), towns)
return towns
const flags_iter
b = valid.begin()
e = valid.end()
v = std::find(b, e, true)
if (v != e) and
(current.size() + (e - v) > best.size())
// as before
const unsigned max = current.size() + std::count(v, e, true) if (v != e) and (max > best.size()) // as before
| 000 | 100 | 300 | |
|---|---|---|---|
| 0 | 1 | ||
| 10 | 1 | ||
| 20 | |||
| 30 | 1 | ||
| 40 | 2 | ||
| 50 | 1 | 2 | |
| 60 | |||
| 70 | 1 | ||
| 80 | |||
| 90 | 1 | 1 | |
| Totals | 5 | 5 | 1 |
| 000 | 100 | |
|---|---|---|
| 0 | 1 | |
| 10 | 1 | |
| 20 | 3 | |
| 30 | ||
| 40 | ||
| 50 | ||
| 60 | 4 | |
| 70 | 1 | |
| 80 | ||
| 90 | 1 | |
| Totals | 10 | 1 |
| total | pages per procedure | |||
|---|---|---|---|---|
| procs | 1 | 2 | 3 | 4 |
| 1 | 1 | |||
| 1 | 1 | |||
| 4 | 3 | 1 | ||
| 5 | 5 | |||
| 8 | 8 | |||
| 9 | 9 | |||
std::vector<int> temp_vec for i = 0; i < no_of_towns; i++ temp_vec.push_back(-100)
or
std::vector<int> temp_vec(no_of_towns, -100)
ulist townsLeft; for uint i = 0; i < map.size(); ++i townsLeft.push_back(i)
or
ulist townsLeft(map.size()) for uint i = 0; i < map.size(); ++i townsLeft[i] = i
void init(int *actarrp, int sz)
for (int i = 0; i < sz; i++)
actarrp[i] = ACTIVE
or
std::fill(actarrp, actarrp + sz, ACTIVE)
int findnext(umat map, int * actarrp, int sz)
int *taarrp = new int [sz]
townaffected(map, actarrp, sz, taarrp)
for (i = 0; i < sz; i++)
if actarrp[i] != ACTIVE
continue
if (taarrp[i] < mincnt) or (mincnt == 0)
mincnt = taarrp[i]
minidx = i
return minidx;
uvec maximum_placement(umat map)
int * actarrp = new int[mapsize]
check_map(map)
init(actarrp, mapsize)
while findactivecnt(actarrp, mapsize) > 0
idx = findnext(map, actarrp, mapsize)
addtolist(map, actarrp, idx, townlist)
return townlist
bool in_match(pvec sort,unsigned temp)
unsigned is_size = sort.size()
for unsigned i = 0; i < is_size; i++
if sort[i].second == temp
return true
return false
void match_find(pvec & sort, unsigned n)
unsigned is_size = sort.size()
for unsigned i = 0; i < is_size; i++
if sort[i].second == n
sort.erase(sort.begin() + i)
return
pvec::iterator
find(pvec pv, unsigned n)
return std::find(pv.begin(), pv.end(), n)
bool found(pvec sort, unsigned n)
return find(sort, n) != sort.end()
void remove(pvec & sort, unsigned n)
pvec::iterator i = find(sort, n)
if i != sort.end()
sort.erase(i)
This page last modified on 18 April 2003.