//
DenseGRAPH.cpp : Defines the entry point for the console application.
//
#include " stdafx.h "
#include < iostream >
#include < vector >
#include < queue >
using namespace std;
class DenseGRAPH
... {
private:
vector<vector<bool> > adj;
int Vcnt;
int Ecnt;
bool digraph;
vector<bool>visited;
vector<int>degree;
vector<int>ord;
vector<int>id;
vector<int>st;
int i;
int cnt;
int ccnt;
int index_v;
struct Edge//边
...{
int v;
int w;
Edge(int v = -1, int w = -1):v(v),w(w)...{}
};
public:
DenseGRAPH(int V, bool digraph_ = false)
...{
adj.resize(V);
visited.resize(V, false);
degree.resize(V, 0);
ord.resize(V, -1);
id.resize(V, -1);
st.resize(V, -1);
Vcnt = V;
Ecnt = 0;
digraph = digraph_;
i = 0;
cnt = 0;
ccnt = 0;
Edge(-1, -1);
index_v = 0;
for(int m = 0; m < V; m++)
adj[m].assign(V, false);
}
int V() const ...{ return Vcnt; }
int E() const ...{ return Ecnt; }
bool directed() const ...{ return digraph; }
void insert(Edge e)
...{
int v = e.v, w = e.w;
if(false == adj[v][w])
Ecnt++;
adj[v][w] = true;
if(!digraph)
adj[w][v] = true;
}
void remove(Edge e)
...{
int v = e.v, w = e.w;
if(true == adj[v][w])
Ecnt--;
adj[v][w] = false;
if(!digraph)
adj[w][v] = false;
}
bool edge(int v, int w) const ...{ return adj[v][w]; }
int begin()...{ i = -1; return next(); }
int next()
...{
for(i++; i < V(); i++)
if(adj[index_v][i] == true)
return i;
return -1;
}
bool end()...{ return i >= V(); }
void DEGREE()
...{
for( index_v = 0; index_v < V(); index_v++)
for(int w = begin(); !end(); w = next())
degree[index_v]++;
}
int operator[](int index_v) const...{ return degree[index_v]; }
//简单路径:顶点和边均不重复。回路或环仅为首顶点和末顶点重合的简单路径。
bool searchR(int index, int w)//简单路径搜索
...{
index_v = index;
if(index_v == w) return true;
visited[index_v] = true;
for(int t = begin(); !end(); t = next())
if(!visited[t])
if(searchR(t, w))
return true;
return false;
}
//给定两个顶点,是否有一条将其连接的简单路径,而且该路径访问了图中的每个顶点,
//且仅访问一次?如果此路径由每个顶点出发由回到该顶点,这个问题就称为哈密顿周游路径。
//《c++算法》p47 效率为指数级,极低。。。
bool H_searchR(int index, int w, int d)//哈密顿路径d=V()-1
...{
index_v = index;
if(index_v == w) return (d == 0);
visited[index_v] = true;
for(int t = begin(); !end(); t = next())
if(!visited[t])
if(H_searchR(t, w, d-1)) return true;
visited[index_v] = false;
return false;
}
//是否存在一条路径连接了两个给定顶点,同时用到了图中的每一条边一次且仅用到一次呢?
//这条路径不必是简单路径,而顶点可以被访问多次。
bool E_Path(int index, int w)//欧拉路径的存在性
...{
index_v = index;
bool found;
int t = degree[index_v] + degree[w];
if((t % 2) != 0)...{ found = false; return false; }
for(t = 0; t <V(); t++)
if((t!= index_v) && (t != w))
if((degree[t] % 2) != 0)
...{ found = false; return false;}
found = true;
}
void DFS(Edge e)//深度优先搜索
...{
int w = e.w;
ord[w] = cnt++;
st[e.w] = e.v;
for(int t = begin(); !end(); t = next())
if(-1 == ord[t])DFS(Edge(w, t));
}
void DFS_search()
...{
for(int index_v = 0; index_v < V(); index_v++)
if(-1 == ord[index_v])DFS(Edge(index_v, index_v));
}
void ccR(int w)
...{
id[w] = ccnt;
for(index_v = begin(); !end(); index_v = next())
if(-1 == id[index_v]) ccR(index_v);
}
void count_ccR()
...{
for( index_v = 0; index_v < V(); index_v++)
if(-1 == id[index_v])
...{
ccR(index_v);
ccnt++;
}
}
bool connect(int s, int t) const//看是否联通,首先得调用count_ccR()
...{
return id[s] == id[t];
}
void BFS(Edge e)//广度优先搜索
...{
queue<Edge>Q;
Q.push(e);
while(!Q.empty())
...{
if(-1 == ord[(e = Q.front()).w] )//vc6.0不支持标准库,无top()成员函数
...{
int v = e.v;
int w = e.w;
ord[w] = cnt++;
st[w] = v;
Q.pop();
for(int t = begin(); !end(); t = next())
if(-1 == ord[t])
Q.push(Edge(w, t));
}
}
}
Edge Get_Edge(int x, int y)
...{
Edge m(x, y);
return m;
}
} ;
int main( int argc, char * argv[])
... {
DenseGRAPH ooo(20,false);
ooo.insert(ooo.Get_Edge(2,3));
// ooo.insert(ooo.Get_Edge(5,4));
ooo.insert(ooo.Get_Edge(3,4));
ooo.insert(ooo.Get_Edge(1,2));
ooo.insert(ooo.Get_Edge(2,4));
// ooo.insert(ooo.Get_Edge(10,5));
ooo.insert(ooo.Get_Edge(6,7));
ooo.insert(ooo.Get_Edge(5,7));
ooo.insert(ooo.Get_Edge(5,6));
// cout<<ooo.E()<<endl;
// ooo.remove(ooo.Get_Edge(10,5));
// cout<<ooo.E()<<endl;
// ooo.insert(ooo.Get_Edge(3,7));
// ooo.insert(ooo.Get_Edge(7,8));
// ooo.insert(ooo.Get_Edge(2,8));
// ooo.DEGREE();
// cout<<ooo.operator[](7)<<endl;
// cout<<ooo.operator [](4)<<endl;
// cout<<ooo.searchR(1,4)<<endl;
cout<<ooo.H_searchR(1, 7, 6)<<endl;
ooo.DFS_search();
ooo.count_ccR();
cout<<ooo.connect(1,5)<<endl;
return 0;
}
//
#include " stdafx.h "
#include < iostream >
#include < vector >
#include < queue >
using namespace std;
class DenseGRAPH
... {
private:
vector<vector<bool> > adj;
int Vcnt;
int Ecnt;
bool digraph;
vector<bool>visited;
vector<int>degree;
vector<int>ord;
vector<int>id;
vector<int>st;
int i;
int cnt;
int ccnt;
int index_v;
struct Edge//边
...{
int v;
int w;
Edge(int v = -1, int w = -1):v(v),w(w)...{}
};
public:
DenseGRAPH(int V, bool digraph_ = false)
...{
adj.resize(V);
visited.resize(V, false);
degree.resize(V, 0);
ord.resize(V, -1);
id.resize(V, -1);
st.resize(V, -1);
Vcnt = V;
Ecnt = 0;
digraph = digraph_;
i = 0;
cnt = 0;
ccnt = 0;
Edge(-1, -1);
index_v = 0;
for(int m = 0; m < V; m++)
adj[m].assign(V, false);
}
int V() const ...{ return Vcnt; }
int E() const ...{ return Ecnt; }
bool directed() const ...{ return digraph; }
void insert(Edge e)
...{
int v = e.v, w = e.w;
if(false == adj[v][w])
Ecnt++;
adj[v][w] = true;
if(!digraph)
adj[w][v] = true;
}
void remove(Edge e)
...{
int v = e.v, w = e.w;
if(true == adj[v][w])
Ecnt--;
adj[v][w] = false;
if(!digraph)
adj[w][v] = false;
}
bool edge(int v, int w) const ...{ return adj[v][w]; }
int begin()...{ i = -1; return next(); }
int next()
...{
for(i++; i < V(); i++)
if(adj[index_v][i] == true)
return i;
return -1;
}
bool end()...{ return i >= V(); }
void DEGREE()
...{
for( index_v = 0; index_v < V(); index_v++)
for(int w = begin(); !end(); w = next())
degree[index_v]++;
}
int operator[](int index_v) const...{ return degree[index_v]; }
//简单路径:顶点和边均不重复。回路或环仅为首顶点和末顶点重合的简单路径。
bool searchR(int index, int w)//简单路径搜索
...{
index_v = index;
if(index_v == w) return true;
visited[index_v] = true;
for(int t = begin(); !end(); t = next())
if(!visited[t])
if(searchR(t, w))
return true;
return false;
}
//给定两个顶点,是否有一条将其连接的简单路径,而且该路径访问了图中的每个顶点,
//且仅访问一次?如果此路径由每个顶点出发由回到该顶点,这个问题就称为哈密顿周游路径。
//《c++算法》p47 效率为指数级,极低。。。
bool H_searchR(int index, int w, int d)//哈密顿路径d=V()-1
...{
index_v = index;
if(index_v == w) return (d == 0);
visited[index_v] = true;
for(int t = begin(); !end(); t = next())
if(!visited[t])
if(H_searchR(t, w, d-1)) return true;
visited[index_v] = false;
return false;
}
//是否存在一条路径连接了两个给定顶点,同时用到了图中的每一条边一次且仅用到一次呢?
//这条路径不必是简单路径,而顶点可以被访问多次。
bool E_Path(int index, int w)//欧拉路径的存在性
...{
index_v = index;
bool found;
int t = degree[index_v] + degree[w];
if((t % 2) != 0)...{ found = false; return false; }
for(t = 0; t <V(); t++)
if((t!= index_v) && (t != w))
if((degree[t] % 2) != 0)
...{ found = false; return false;}
found = true;
}
void DFS(Edge e)//深度优先搜索
...{
int w = e.w;
ord[w] = cnt++;
st[e.w] = e.v;
for(int t = begin(); !end(); t = next())
if(-1 == ord[t])DFS(Edge(w, t));
}
void DFS_search()
...{
for(int index_v = 0; index_v < V(); index_v++)
if(-1 == ord[index_v])DFS(Edge(index_v, index_v));
}
void ccR(int w)
...{
id[w] = ccnt;
for(index_v = begin(); !end(); index_v = next())
if(-1 == id[index_v]) ccR(index_v);
}
void count_ccR()
...{
for( index_v = 0; index_v < V(); index_v++)
if(-1 == id[index_v])
...{
ccR(index_v);
ccnt++;
}
}
bool connect(int s, int t) const//看是否联通,首先得调用count_ccR()
...{
return id[s] == id[t];
}
void BFS(Edge e)//广度优先搜索
...{
queue<Edge>Q;
Q.push(e);
while(!Q.empty())
...{
if(-1 == ord[(e = Q.front()).w] )//vc6.0不支持标准库,无top()成员函数
...{
int v = e.v;
int w = e.w;
ord[w] = cnt++;
st[w] = v;
Q.pop();
for(int t = begin(); !end(); t = next())
if(-1 == ord[t])
Q.push(Edge(w, t));
}
}
}
Edge Get_Edge(int x, int y)
...{
Edge m(x, y);
return m;
}
} ;
int main( int argc, char * argv[])
... {
DenseGRAPH ooo(20,false);
ooo.insert(ooo.Get_Edge(2,3));
// ooo.insert(ooo.Get_Edge(5,4));
ooo.insert(ooo.Get_Edge(3,4));
ooo.insert(ooo.Get_Edge(1,2));
ooo.insert(ooo.Get_Edge(2,4));
// ooo.insert(ooo.Get_Edge(10,5));
ooo.insert(ooo.Get_Edge(6,7));
ooo.insert(ooo.Get_Edge(5,7));
ooo.insert(ooo.Get_Edge(5,6));
// cout<<ooo.E()<<endl;
// ooo.remove(ooo.Get_Edge(10,5));
// cout<<ooo.E()<<endl;
// ooo.insert(ooo.Get_Edge(3,7));
// ooo.insert(ooo.Get_Edge(7,8));
// ooo.insert(ooo.Get_Edge(2,8));
// ooo.DEGREE();
// cout<<ooo.operator[](7)<<endl;
// cout<<ooo.operator [](4)<<endl;
// cout<<ooo.searchR(1,4)<<endl;
cout<<ooo.H_searchR(1, 7, 6)<<endl;
ooo.DFS_search();
ooo.count_ccR();
cout<<ooo.connect(1,5)<<endl;
return 0;
}