25、邻接表:有向无环图(DAG)的判断
作者:互联网
问题描述 :
目的:使用C++模板设计并逐步完善图的邻接表抽象数据类型(ADT)。
内容:
(1)请参照图的邻接矩阵模板类原型,设计并逐步完善图的邻接表ADT。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。)
(2)设计并实现一个算法,对于给定的有向图(网),判断其是否是有向无环图(DAG)。如是(不存在回路),返回true;否则返回false。图的存储结构采用邻接表。将其加入到ADT中。
注意:DG(有向图), DN(有向网), UDG(无向图), UDN(无向网)
提示:检测回路(有向环)的一种方法是对有向图构造它的拓扑有序序列。如果通过拓扑排序能将该图的所有顶点都排入一个拓扑有序的序列中, 则该图中必定不会出现回路(有向环)。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <sstream>
#include <stack>
#include <map>
#include <ctime>
#include <array>
#include <set>
#include <list>
using namespace std;
//边的定义
template<class TypeOfEdge>
struct Edge_pair
{
int point = 0;
TypeOfEdge length = 0;
//=================
};
//顶点的定义
template<class TypeOfVer, class TypeOfEdge>
struct verNode
{
TypeOfVer ver_data;
list<Edge_pair<TypeOfEdge> > group;
//构造函数,默认会讲头指针设为空.
verNode()
{
group.clear();
//ver_data = 0;
}
//取得结点值(顶点) 估计是为了安全吧???
TypeOfVer getVer()
{
return ver_data;
}
//取得对应的边表
list<Edge_pair<TypeOfEdge> > getHead()
{
return group;
}
//设置结点值(顶点集) 估计是为了安全吧???
void setdata(TypeOfVer value)
{
ver_data = value;
return;
}
//=====================================================
void creat_Point(int new_point, TypeOfEdge new_length)
{
Edge_pair<TypeOfEdge> Next_p;
Next_p.point = new_point;
Next_p.length = new_length;
group.insert(group.begin(), Next_p);
return;
}
//删除指定位置顶点
void del_Point(int n)
{
return;
}
};
template <class TypeOfVer, class TypeOfEdge>//顶点元素类型,边权值类型
class adjlist_graph {
private:
int Vers;//顶点数
int Edges;//边数
vector<verNode<TypeOfVer, TypeOfEdge> >ver;//顶点存储
string GraphKind;//图的种类标志
bool have_dir = false, have_w = false;//图类型参数
vector<TypeOfVer> dfs_map;
vector<bool> dfs_vis;
//======================================================================
bool Delete_Edge(int u, int v)//删除一条边
{
return false;
}
bool DFS(int t)//DFS遍历(递归部分)
{
dfs_map.push_back(ver[t].ver_data);
dfs_vis[t] = true;
for(auto i= ver[t].group.begin();i!= ver[t].group.end();i++)
if (dfs_vis[i->point] == false)
DFS(i->point);
return true;
}
public:
//一个空的构造函数
adjlist_graph()
{
Edges = 0;
Vers = 0;
}
//假的析构函数
~adjlist_graph()
{
;//你电脑内存就640K吗?
}
//判断图空否
bool GraphisEmpty()
{
return Vers == 0;
}
//获取图的类型
string GetGraphKind()
{
return GraphKind;
}
//取得当前顶点数
int GetVerNum()
{
return Vers;
}
//取得当前边数
int GetEdgeNum()
{
return Edges;
}
//自动建立临接表
bool Auto_build(void)
{
//DG(有向图), DN(有向网), UDG(无向图), UDN(无向网)
/*第一行:图的类型 DN UDN
第二行:结点数
第三行:结点集
第四行:无边标记
第五行:边数
第六行:边集
第七行:权集*/
/*第一行:图的类型 DG UDG
第二行:结点数
第三行:结点集
第四行:边数
第五行:边集*/
cin >> GraphKind;//图的类型
cin >> Vers;//结点数
ver.resize(Vers);//开辟节点空间
for (int i = 0; i < Vers; i++)//结点集
{
TypeOfVer now;
cin >> now;
ver[i].setdata(now);
}
cin >> Edges;//边数
vector<int> x_p, y_p, w_p;
for (int i = 0; i < Edges; i++)
{
int c_x, c_y;
cin >> c_x >> c_y;
x_p.push_back(c_x);
y_p.push_back(c_y);
}
//图的类型识别
if (GraphKind == "DG")//DG(有向图)
have_dir = true, have_w = false;
if (GraphKind == "DN")//DN(有向网)
have_dir = true, have_w = true;
if (GraphKind == "UDG")//UDG(无向图)
have_dir = false, have_w = false;
if (GraphKind == "UDN")//UDN(无向网)
have_dir = false, have_w = true;
if (have_w)
for (int i = 0; i < Edges; i++)
{
int c_w;
cin >> c_w;
w_p.push_back(c_w);
}
for (int i = 0; i < Edges; i++)
{
if (have_dir)
if (have_w)
ver[x_p[i]].creat_Point(y_p[i], w_p[i]);
else
ver[x_p[i]].creat_Point(y_p[i], 0);
else
if (have_w)
ver[x_p[i]].creat_Point(y_p[i], w_p[i]), ver[y_p[i]].creat_Point(x_p[i], w_p[i]);
else
ver[x_p[i]].creat_Point(y_p[i], 0), ver[y_p[i]].creat_Point(x_p[i], 0);
}
return 1;
}
//取得G顶点的组
vector<TypeOfVer> GetVer(void)
{
vector<TypeOfVer> head_group;
for (int i = 0; i < Vers; i++)
{
head_group.push_back(ver[i].getVer());
}
return head_group;
}
//输出邻接表
bool Print_photo()
{
int i;
for (i = 0; i < Vers; i++)
{
cout << ver[i].getVer();
if (ver[i].group.size() != 0)
cout << "->";
else
{
cout << endl;
continue;
}
vector<Edge_pair<TypeOfEdge> > out_lis;
out_lis.clear();
for (auto j = ver[i].group.begin(); j != ver[i].group.end(); j++)
{
out_lis.push_back(*j);
}
int j;
for (j = 0; j < out_lis.size() - 1; j++)
if (have_w)
cout << out_lis[j].point << "(" << out_lis[j].length << ")" << "->";
else
cout << out_lis[j].point << "->";
if (have_w)
cout << out_lis[j].point << "(" << out_lis[j].length << ")" << endl;
else
cout << out_lis[j].point << endl;
}
return 1;
}
//往G中添加一个顶点
bool InsertVer(const TypeOfVer& data)
{
verNode<TypeOfVer, TypeOfEdge> new_e;
new_e.setdata(data);
ver.push_back(new_e);
Vers++;
return true;
}
//寻找顶点位置
int Look_Ver(const TypeOfVer& data)
{
int i;
for (i = 0; i < Vers; i++)
if (ver[i].ver_data == data)
return i;
return -1;
}
//删除一个顶点
bool del_Point(int place)
{
int need_del = 0;
if (!(0 <= place && place < Vers))
return false;
int i;
for (i = 0; i < Vers; i++)
{
for (auto j = ver[i].group.begin(); j != ver[i].group.end(); j++)
{
if (j->point == place)
{
need_del++;
ver[i].group.erase(j);
break;
}
}
for (auto j = ver[i].group.begin(); j != ver[i].group.end(); j++)
{
if (j->point > place)
j->point--;
}
}
need_del += ver[place].group.size();
ver.erase(ver.begin() + place);
Vers--;
if (have_dir)
Edges -= need_del;
else
Edges -= (need_del / 2);
return true;
}
//无权图插入一条边
bool Insert_Edge(int u, int v)
{
if (!(0 <= u && u < Vers))
return false;
if (!(0 <= v && v < Vers))
return false;
for (auto i = ver[u].group.begin(); i != ver[u].group.end(); i++)
{
if (i->point == v)
return false;
}
for (auto i = ver[v].group.begin(); i != ver[v].group.end(); i++)
{
if (i->point == u)
return false;
}
if (u == v)
return false;
if (have_dir)
{
Edges++;
ver[u].creat_Point(v, 1);
return true;
}
else
{
Edges++;
ver[u].creat_Point(v, 1);
ver[v].creat_Point(u, 1);
return true;
}
return true;
}
//有权图插入一条边
bool Insert_Edge(int u, int v, TypeOfEdge w)
{
if (!(0 <= u && u < Vers))
return false;
if (!(0 <= v && v < Vers))
return false;
for (auto i = ver[u].group.begin(); i != ver[u].group.end(); i++)
{
if (i->point == v)
return false;
}
for (auto i = ver[v].group.begin(); i != ver[v].group.end(); i++)
{
if (i->point == u)
return false;
}
if (u == v)
return false;
if (have_dir)
{
Edges++;
ver[u].creat_Point(v, w);
return true;
}
else
{
Edges++;
ver[u].creat_Point(v, w);
ver[v].creat_Point(u, w);
return true;
}
return true;
}
//删除边 (外壳:有向(删除1条边), 无向(删除2条边))
bool del_Edge(int u, int v)
{
if (!(0 <= u && u < Vers))
return false;
if (!(0 <= v && v < Vers))
return false;
if (u == v)
return false;
bool ok = true;
if(have_dir)
for (auto i = ver[u].group.begin(); i != ver[u].group.end(); i++)
{
if (i->point == v)
{
ver[u].group.erase(i);
Edges--;
return true;
}
}
else
{
for (auto i = ver[u].group.begin(); i != ver[u].group.end(); i++)
{
if (i->point == v)
{
ver[u].group.erase(i);
break;
}
}
for (auto i = ver[v].group.begin(); i != ver[v].group.end(); i++)
{
if (i->point == u)
{
ver[v].group.erase(i);
Edges--;
return true;
}
}
}
return false;
}
//返回G中指定顶点u的第一个邻接顶点的位序(顶点集)。若顶点在G中没有邻接顶点,则返回-1
int GetFirst_AdjVex(int u)
{
if(ver[u].group.empty())
return -1;
return ver[u].group.begin()->point;
}
//返回G中指定顶点u的下一个邻接顶点(相对于v)的位序(顶点集)。若顶点在G中没有邻接顶点,则返回false
int GetNext_AdjVex(int u, int v)
{
if (ver[u].group.size() == 1)
return -1;
for (auto i = ver[u].group.begin(); i != ver[u].group.end(); i++)
{
if (i->point == v)
{
if ((++i) == ver[u].group.end())
return -1;
return i->point;
}
}
return -1;
}
//是否存在边
bool look_Edge(int u, int v)
{
if (!(0 <= u && u < Vers))
return false;
if (!(0 <= v && v < Vers))
return false;
if (u == v)
return false;
for (auto i = ver[u].group.begin(); i != ver[u].group.end(); i++)
{
if (i->point == v)
return true;
}
for (auto i = ver[v].group.begin(); i != ver[v].group.end(); i++)
{
if (i->point == u)
return true;
}
return false;
}
//两个顶点之间的边的权值 失败返回-1
int Get_legthOfEdge(int u, int v)
{
if (!have_w)
return -1;
if (!(0 <= u && u < Vers))
return -1;
if (!(0 <= v && v < Vers))
return -1;
if (u == v)
return -1;
for (auto i = ver[u].group.begin(); i != ver[u].group.end(); i++)
{
if (i->point == v)
return i->length;
}
return -1;
}
//获取一个顶点的入度
int Search_enterDegree(int p)
{
if (!(0 <= p && p < Vers))
return -1;
if (!have_dir)//is 无向图
return ver[p].group.size();
int cnt = 0;
for (int i = 0; i < Vers; i++)
{
if(i!=p)
for (auto j = ver[i].group.begin(); j != ver[i].group.end(); j++)
{
if (j->point == p)
cnt++;
}
}
//cnt += ver[p].group.size();
return cnt;
}
//获取一个顶点的出度
int Search_outDegree(int p)
{
if (!(0 <= p && p < Vers))
return -1;
return ver[p].group.size();
}
bool look_haveDir(void)
{
return have_dir;
}
//DFS遍历(外壳部分)
void DFS_Traverse(int star)
{
dfs_map.clear();
dfs_vis.clear();
for (int i = 0; i < Vers; i++)
dfs_vis.push_back(false);
DFS(star);
int i;
for (i = 0; i < dfs_map.size()-1; i++)
cout << dfs_map[i] << "->";
cout << dfs_map[i] << endl;
return;
}
//BFS遍历
void BFS_Traverse(int star)
{
vector<TypeOfVer> bfs_lis;
vector<bool>bfs_vis;
bfs_lis.clear();
bfs_vis.clear();
for (int i = 0; i < Vers; i++)
bfs_vis.push_back(false);
queue<int> Q;
Q.push(star);
bfs_vis[star] = true;
bfs_lis.push_back(ver[star].ver_data);
int now;
while (!Q.empty())
{
now = Q.front();
Q.pop();
for (auto i = ver[now].group.begin(); i != ver[now].group.end(); i++)
if (bfs_vis[i->point] == false)
{
bfs_lis.push_back(ver[i->point].ver_data);
bfs_vis[i->point] = true;
Q.push(i->point);
}
}
int i;
for (i = 0; i < bfs_lis.size() - 1; i++)
cout << bfs_lis[i] << "->";
cout << bfs_lis[i] << endl;
return;
}
//是否有路径
bool Search_way(int star, int v)
{
vector<bool>bfs_vis;
bfs_vis.clear();
for (int i = 0; i < Vers; i++)
bfs_vis.push_back(false);
queue<int> Q;
Q.push(star);
bfs_vis[star] = true;
int now;
while (!Q.empty())
{
now = Q.front();
if (now == v)
return true;
Q.pop();
for (auto i = ver[now].group.begin(); i != ver[now].group.end(); i++)
if (bfs_vis[i->point] == false)
{
bfs_vis[i->point] = true;
Q.push(i->point);
}
}
return false;
}
//拓扑排序并判断有无回路 0 有 1 无
bool TopologicalSort(void)
{
if (Edges == 0)
return 0;
int i, j;
//将各个顶点的入度存在indegree
vector<int> indegree;
indegree.clear();
//------
for (i = 0; i < Vers; i++)
indegree.push_back(Search_enterDegree(i));
//------
//入度为0,进栈。
queue<int> S;
//------
for (i = 0; i < indegree.size(); i++)
if (indegree[i] == 0)
S.push(i);
//------
vector<int> sort_list;//the way of sort
//BFS
while (!S.empty())
{
int now;
now = S.front();
S.pop();
sort_list.push_back(now);
int next_p;
next_p = GetFirst_AdjVex(now);//获取now的第一个相邻的点
while (next_p != -1)
{
--indegree[next_p];
if (indegree[next_p] == 0)
S.push(next_p);
/*if (indegree[next_p] == -1)
S.push(next_p);*/
next_p = GetNext_AdjVex(now, next_p);
}
}
if (sort_list.size() == 0)
return 0;
//------
for (i = 0; i < sort_list.size() - 1; i++)
cout << ver[sort_list[i]].ver_data << "->";
cout << ver[sort_list[i]].ver_data << endl;
//------
if (sort_list.size() < Vers)
return 0;
return 1;
}
//判断是否有回路* 1 有 2 无
bool Check_circle()
{
if (Edges == 0)
return 0;
int i, j;
//将各个顶点的入度存在indegree
vector<int> indegree;
indegree.clear();
//------
for (i = 0; i < Vers; i++)
indegree.push_back(Search_enterDegree(i));
//------
//入度为0,进栈。
queue<int> S;
//------
for (i = 0; i < indegree.size(); i++)
if (indegree[i] == 0)
S.push(i);
//------
vector<int> sort_list;//the way of sort
//BFS
while (!S.empty())
{
int now;
now = S.front();
S.pop();
sort_list.push_back(now);
int next_p;
next_p = GetFirst_AdjVex(now);//获取now的第一个相邻的点
while (next_p != -1)
{
--indegree[next_p];
if (indegree[next_p] == 0)
S.push(next_p);
/*if (indegree[next_p] == -1)
S.push(next_p);*/
next_p = GetNext_AdjVex(now, next_p);
}
}
if (sort_list.size() < Vers)
return 1;
return 0;
}
//判断是否有DAG* 1 有 2 无
bool Check_DAG()
{
if (Edges == 0)
return 0;
int i, j;
//将各个顶点的入度存在indegree
vector<int> indegree;
indegree.clear();
//------
for (i = 0; i < Vers; i++)
indegree.push_back(Search_enterDegree(i));
//------
//入度为0,进栈。
queue<int> S;
//------
for (i = 0; i < indegree.size(); i++)
if (indegree[i] == 0)
S.push(i);
//------
vector<int> sort_list;//the way of sort
//BFS
while (!S.empty())
{
int now;
now = S.front();
S.pop();
sort_list.push_back(now);
int next_p;
next_p = GetFirst_AdjVex(now);//获取now的第一个相邻的点
while (next_p != -1)
{
--indegree[next_p];
if (indegree[next_p] == 0)
S.push(next_p);
/*if (indegree[next_p] == -1)
S.push(next_p);*/
next_p = GetNext_AdjVex(now, next_p);
}
}
if (sort_list.size() < Vers)
return 0;
return 1;
}
};
int main()
{
int i;
adjlist_graph<string, int> a;
a.Auto_build();/*
int u, v;
cin >> u >> v;*/
/*int p;
cin >> p;*/
//cout << a.GetGraphKind() << endl;
vector <string> ans;
ans = a.GetVer();
for (i = 0; i < ans.size() - 1; i++)
cout << ans[i] << " ";
cout << ans[i] << endl;
//cout << a.GetVerNum() << endl;
//cout << a.GetEdgeNum() << endl;
a.Print_photo();
cout << endl;
if (a.Check_DAG())
cout << "true" << endl;
else
cout << "false" << endl;
return 0;
}
标签:25,DAG,ver,int,++,group,环图,return,now 来源: https://blog.csdn.net/u014377763/article/details/117262913