Skip to content

Commit 2fce57f

Browse files
Another go at C* by actually counting occurances.
1 parent 08c037f commit 2fce57f

File tree

4 files changed

+81
-24
lines changed

4 files changed

+81
-24
lines changed

include/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ endif
541541
ifdef DEBUGGING
542542
OPT+=-g
543543
else
544-
FLAGS+= -DNDEBUG
544+
OPT+= -DNDEBUG
545545
ifeq ($(OS),OSF1)
546546
OPT+=-O4
547547
else

models/starComplexity.cc

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ void StarComplexityGen::fillStarMap(unsigned maxStars)
299299
if (elemStars.empty()) return;
300300
// insert the single star graph
301301
starMap.emplace(elemStars[0],1);
302+
counts.emplace(elemStars[0],1);
302303

303304
//unsigned numStars=maxStars;
304305
for (unsigned numStars=2; numStars<=maxStars; ++numStars)
@@ -319,7 +320,11 @@ void StarComplexityGen::fillStarMap(unsigned maxStars)
319320
return;
320321
}
321322
for (auto k: j)
322-
starMap.emplace(k, numStars);
323+
{
324+
auto res=starMap.emplace(k, numStars);
325+
if (res.first->second==numStars) // only count least star operations
326+
counts[k]++;
327+
}
323328
}
324329
};
325330

@@ -350,17 +355,17 @@ void StarComplexityGen::fillStarMap(unsigned maxStars)
350355
handler.host_task([&]() {
351356
resultBufferConsumed=0;
352357
populateStarMap();
353-
if (!lastLoop)
354-
{
355-
block->alreadySeenBacked.clear();
356-
for (auto& k: starMap) block->alreadySeenBacked.push_back(k.first);
357-
// push update already seen vector to device
358-
alreadySeenSwapped=syclQ().single_task
359-
(compute,
360-
[=,block=&*block]{
361-
block->alreadySeen.swap(block->alreadySeenBacked);
362-
});
363-
}
358+
// if (!lastLoop)
359+
// {
360+
// block->alreadySeenBacked.clear();
361+
// for (auto& k: starMap) block->alreadySeenBacked.push_back(k.first);
362+
// // push update already seen vector to device
363+
// alreadySeenSwapped=syclQ().single_task
364+
// (compute,
365+
// [=,block=&*block]{
366+
// block->alreadySeen.swap(block->alreadySeenBacked);
367+
// });
368+
// }
364369
populating=false;
365370
});
366371
});
@@ -459,23 +464,45 @@ void StarComplexityGen::canonicaliseStarMap()
459464
if (c!=i->first)
460465
{
461466
auto& starC=starMap[c];
462-
starC=starC>0? min(starC, i->second): i->second;
467+
if (starC==0 || i->second<starC)
468+
{
469+
starC=i->second;
470+
counts[c]=counts[i->first];
471+
}
472+
else if (i->second==starC)
473+
counts[c]+=counts[i->first];
474+
assert(counts[c]);
463475
toErase.push_back(i->first);
464476
}
465477
}
466-
for (auto i: toErase) starMap.erase(i);
478+
for (auto i: toErase)
479+
{
480+
starMap.erase(i);
481+
counts.erase(i);
482+
}
483+
}
484+
485+
linkRep StarComplexityGen::complement(linkRep x) const
486+
{
487+
return toLinkRep(toNautyRep(~x, elemStars.size()).canonicalise());
467488
}
468489

469490
unsigned StarComplexityGen::symmStar(linkRep x) const
470491
{
471492
auto star=starMap.find(x);
472493
if (star==starMap.end()) return 0;
473-
linkRep complement=toLinkRep(toNautyRep(~x, elemStars.size()).canonicalise());
474-
auto starComp=starMap.find(complement);
494+
auto starComp=starMap.find(complement(x));
475495
if (starComp==starMap.end()) return star->second;
476496
return min(star->second, starComp->second);
477497
}
478498

499+
double lnFactorial(unsigned n)
500+
{
501+
double r=0;
502+
for (unsigned i=2; i<n; ++i) r+=log(i);
503+
return r;
504+
}
505+
479506
GraphComplexity StarComplexityGen::complexity(linkRep g) const
480507
{
481508
const double ilog2=1/log(2.0);
@@ -485,10 +512,38 @@ GraphComplexity StarComplexityGen::complexity(linkRep g) const
485512
ecolab_nauty(ng, ng, lnomega, false);
486513
r.complexity=ecolab::baseComplexity(ng.nodes(), ng.links(), true) - ilog2*lnomega;
487514
auto star=symmStar(g);
488-
if (star==0)
489-
r.starComplexity=nan("");
490-
else
491-
r.starComplexity=2*ceil(ilog2*log(ng.nodes()))+ceil(ilog2*log(star))+
492-
(2*star-1)*ceil(ilog2*(ng.nodes()+2)) - ilog2*lnomega;
515+
switch (star)
516+
{
517+
case 0:
518+
r.starComplexity=nan("");
519+
break;
520+
case 1:
521+
r.starComplexity=2*ceil(ilog2*log(ng.nodes()))+1;
522+
break;
523+
default:
524+
{
525+
auto gIter=starMap.find(g);
526+
unsigned count=0;
527+
if (gIter!=starMap.end() && gIter->second==star)
528+
{
529+
auto countIter=counts.find(g);
530+
assert(countIter!=counts.end());
531+
count=countIter->second;
532+
}
533+
else
534+
{
535+
auto countIter=counts.find(complement(g));
536+
assert(countIter!=counts.end());
537+
count=countIter->second;
538+
}
539+
assert(count>0);
540+
r.starComplexity=2*ceil(ilog2*log(ng.nodes()))+ceil(ilog2*log(star))+(star-2)-ilog2*log(count);
541+
if (star<=ng.nodes())
542+
r.starComplexity+=ilog2*(lnFactorial(star));
543+
else
544+
r.starComplexity+=ilog2*(lnFactorial(ng.nodes())+(star-ng.nodes())*log(ng.nodes()));
545+
}
546+
break;
547+
}
493548
return r;
494549
}

models/starComplexity.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ struct StarComplexityGen
1919
size_t blockSize=128;
2020
// star complexity registry
2121
std::map<linkRep,unsigned> starMap;
22+
std::map<linkRep,unsigned> counts; // counts the number of times linkRep is seen
2223
ElemStars elemStars;
2324
void generateElementaryStars(unsigned nodes);
2425
// fills starMap with graphs of \a numStars
2526
void fillStarMap(unsigned maxStars);
2627
void canonicaliseStarMap();
27-
/// return lesser of star and star of complement
28+
/// return complement canonical graph
29+
linkRep complement(linkRep) const;
2830
unsigned symmStar(linkRep) const;
2931
GraphComplexity complexity(linkRep) const;
3032
};

models/starComplexity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424

2525
for i in starC.starMap.keys():
2626
c=starC.complexity(i)
27-
print(i,starC.symmStar(i),c.complexity(),c.starComplexity())
27+
print(i,starC.symmStar(i),c.complexity(),c.starComplexity(),starC.counts[i],starC.counts[starC.complement(i)])
2828

0 commit comments

Comments
 (0)