# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#ifndef COMMONDATASTRUCTURE_H
#define COMMONDATASTRUCTURE_H
const int MAXVERTEX = 40;
typedef std::set<int> ADJPOINTSSET;
typedef std::pair<int, ADJPOINTSSET> ADJPOINTSPAIR;
typedef std::map<int, ADJPOINTSPAIR> VERTEXMAP;
typedef VERTEXMAP GRAPH_ADJLIST_MAP;
typedef std::vectorbool> > GRAPH_ADJARRAY_VECTOR;
typedef std::vector<int> OFFSET_VECTOR;
typedef ADJPOINTSSET VERTEXCOVER;
bool SupportsHighResCounter();
static bool sm_bPerformanceCounter;
static __int64 sm_i64Freq;
inline bool CPreciseTimer::SupportsHighResCounter()
return sm_bPerformanceCounter;
inline void CPreciseTimer::UpdateElapsed()
if(true == sm_bPerformanceCounter)
QueryPerformanceCounter(&m_liCount);
m_i64Counts = ((__int64)m_liCount.HighPart << 32) + (__int64)m_liCount.LowPart;
(m_i64Counts *= 1000000) /= sm_i64Freq;
m_i64Counts = (__int64)GetTickCount() * 1000;
if(m_i64Counts > m_i64Start)
m_i64Elapsed = m_i64Counts - m_i64Start;
m_i64Elapsed = (0x7fffffffffffffff - m_i64Start) + m_i64Counts;
#include "cprecisetimer.h"
bool CPreciseTimer::sm_bInit = false;
bool CPreciseTimer::sm_bPerformanceCounter;
__int64 CPreciseTimer::sm_i64Freq;
CPreciseTimer::CPreciseTimer() : m_i64Start(0), m_i64Elapsed(0), m_bRunning(false)
if(TRUE == QueryPerformanceFrequency(&liFreq))
sm_i64Freq = ((__int64)liFreq.HighPart << 32) + (__int64)liFreq.LowPart;
sm_bPerformanceCounter = true;
sm_bPerformanceCounter = false;
void CPreciseTimer::StartTimer()
if(true == sm_bPerformanceCounter)
QueryPerformanceCounter(&m_liCount);
m_i64Start = ((__int64)m_liCount.HighPart << 32) + (__int64)m_liCount.LowPart;
(m_i64Start *= 1000000) /= sm_i64Freq;
m_i64Start = (__int64)GetTickCount() * 1000;
void CPreciseTimer::StopTimer()
__int64 CPreciseTimer::GetTime()
#include "commondatastructure.h"
SimpleGraph(const GRAPH_ADJARRAY_VECTOR adjgraph);
VERTEXCOVER getVertexCover();
void inc(int keyVertex, int targetVertex);
void dec(int keyVertex, int targetVertex);
bool checkIsCover(std::vector<int> &vertexSet);
static void printVertexSet(VERTEXCOVER s);
static std::vectorint> > getCombineNK(int n, int k);
static void showCombinationvector( std::vectorint> > va );
static OFFSET_VECTOR globalOffset;
GRAPH_ADJLIST_MAP m_dstAdjList;
GRAPH_ADJARRAY_VECTOR m_srcAdjArray;
void adjArray2adjList(const GRAPH_ADJARRAY_VECTOR &srcAdjArray, GRAPH_ADJLIST_MAP &dstAdjList);
OFFSET_VECTOR SimpleGraph::globalOffset = \
,10,11,12,13,14,15,16,17,18,19 \
,20,21,22,23,24,25,26,27,28,29 \
,30,31,32,33,34,35,36,37,38,39 \
SimpleGraph::SimpleGraph(const GRAPH_ADJARRAY_VECTOR adjgraph)
adjArray2adjList(m_srcAdjArray,m_dstAdjList);
void SimpleGraph::adjArray2adjList(const GRAPH_ADJARRAY_VECTOR &srcAdjArray, GRAPH_ADJLIST_MAP &dstAdjList)
assert( srcAdjArray.size() < MAXVERTEX);
for (int i = 0; i < srcAdjArray.size(); ++i){
for(int j= 0; j< srcAdjArray[i].size(); ++j)
if(srcAdjArray[i][j] != 0)
s.insert(j + globalOffset[i]);
ADJPOINTSPAIR adjPointsPair(ncount,s);
dstAdjList.insert(make_pair(i,adjPointsPair));
GRAPH_ADJLIST_MAP::iterator it;
for(it = m_dstAdjList.begin(); it != m_dstAdjList.end(); it++)
ADJPOINTSSET s = it->second.second;
ADJPOINTSSET::iterator adjset_it;
for(adjset_it = s.begin(); adjset_it != s.end(); adjset_it++) {
if(*adjset_it > it->first)
inc(*adjset_it, it->first);
VERTEXCOVER SimpleGraph::getVertexCover()
ADJPOINTSSET::iterator adjset_it;
std::cout << "====================================================" << std::endl;
std::cout << "v d s" << std::endl;
std::cout << "-----" << std::endl;
GRAPH_ADJLIST_MAP::iterator it, it_target;
std::cout << "step1.put the highest degree vertex in the cover set" << std::endl;
it_target = m_dstAdjList.begin();
for(it = m_dstAdjList.begin(); it != m_dstAdjList.end(); it++)
if(nMaxDegree < it->second.first)
nMaxDegree = it->second.first;
std::cout << "it_target is " << it_target->first << " " << it_target->second.first << std::endl;
if(it_target->second.first == 0) break;
s.insert(it_target->first);
std::cout << "one of highest degree vertex is vertex: " << it_target->first << std::endl;
SimpleGraph::printVertexSet(s);
std::cout << "step2.update the degree of vertexes in the list.if 0, remove it." << std::endl;
tmps= it_target->second.second;
for(adjset_it = tmps.begin(); adjset_it != tmps.end(); adjset_it++) {
dec(*adjset_it, it_target->first);
it_target->second.first = 0;
it_target->second.second.clear();
adjArray2adjList(m_srcAdjArray,m_dstAdjList);
std::cout << "step3.Exit the loop,get the one of cover set" << std::endl;
bool SimpleGraph::checkIsCover(std::vector<int> &vertexSet)
ADJPOINTSSET::iterator adjset_it;
GRAPH_ADJLIST_MAP::iterator it, it_target;
std::vector<int>::iterator vit;
it_target = m_dstAdjList.begin();
for (vit = vertexSet.begin(); vit != vertexSet.end(); vit++)
for(it = m_dstAdjList.begin(); it != m_dstAdjList.end(); it++)
if(*vit == it->first + 1)
tmps= it_target->second.second;
for(adjset_it = tmps.begin(); adjset_it != tmps.end(); adjset_it++) {
dec(*adjset_it, it_target->first);
it_target->second.first = 0;
it_target->second.second.clear();
for(it = m_dstAdjList.begin(); it != m_dstAdjList.end(); it++)
if(it->second.first != 0)
adjArray2adjList(m_srcAdjArray,m_dstAdjList);
if(m_srcAdjArray.size() == 0)
std::cout << "Adjacency Array vector is null\n";
for (auto& line : m_srcAdjArray) {
for (int j=0; j< i; j++) {
for (const auto& v : line) {
void SimpleGraph::printVertexSet(VERTEXCOVER s)
ADJPOINTSSET::iterator adjset_it;
for(adjset_it = s.begin(); adjset_it != s.end(); adjset_it++) {
std::cout << *adjset_it << " ";
std::cout << "}" << std::endl;
void SimpleGraph::print()
GRAPH_ADJLIST_MAP::iterator it;
for(it = m_dstAdjList.begin(); it != m_dstAdjList.end(); it++)
std::cout << it->first << " " << it->second.first << " ";
SimpleGraph::printVertexSet(it->second.second);
void SimpleGraph::inc(int keyVertex, int targetVertex)
m_dstAdjList.at(keyVertex).first += 1;
m_dstAdjList.at(keyVertex).second.insert(targetVertex);
void SimpleGraph::dec(int keyVertex, int targetVertex)
m_dstAdjList.at(keyVertex).first -= 1;
m_dstAdjList.at(keyVertex).second.erase(targetVertex);
std::vectorint> > SimpleGraph::getCombineNK(int n, int k)
std::vector<int> nums = std::vector<int>();
for(int i = 1; i < k + 1 ; ++i)
std::vectorint>> retvv = std::vectorint>>();
retvv.push_back(std::vector<int>(nums.begin(), nums.begin() + k));
while ((j < k) && (nums[j + 1] == nums[j] + 1))
void SimpleGraph::showCombinationvector( std::vectorint> > va )
std::cout << "C(N,K) = " << va.size() << "\n";
for (int var = 0; var < va.size(); ++var) {
for(int j=0; j< va[var].size();j++)
std::cout << va[var][j] - 1 << " ";

#include "cprecisetimer.h"
int main(int argc, char *argv[])
GRAPH_ADJARRAY_VECTOR graph = {
{0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0},
{0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0},
{0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0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seTimer preciseTimer;
std::cout << "G Upper Triangle Matrix" << std::endl;
std::cout << "Computing Min Vertex Cover..." << std::endl;
VERTEXCOVER coverSet = G.getVertexCover();
std::cout << "Print Min Vertex Cover..." << std::endl;
SimpleGraph::printVertexSet(coverSet);
std::cout << "cover vertex number should be:" << k << std::endl;
std::vectorint>> va = SimpleGraph::getCombineNK(N,k);
SimpleGraph::showCombinationvector(va);
std::vectorint>> allCover;
preciseTimer.StartTimer();
for (int index = 0; index < va.size(); ++index) {
bool bIsCover = G.checkIsCover(va[index]);
allCover.push_back(va[index]);
preciseTimer.StopTimer();
__int64 timeElasps = preciseTimer.GetTime();
std::cout << "time elapse is " << timeElasps << " microseonds" << std::endl;
std::cout << "all cover set is as follows!!!" << std::endl;
SimpleGraph::showCombinationvector(allCover);
std::cout << "Finish!!!" << std::endl;
