#include<stack>
#include<iostream>
#include<queue>
#include<string>
#include<iomanip>
using namespace std;
bool visited[100]; //判断是否被访问过
bool searched[100]; //判断是否被搜索过
bool flag = 1;
struct EBox
{
int mark;
int ivex; //该边关联的两个顶点的位置
int jvex;
EBox *ilink,*jlink; //分别指向关联顶点的下一条边
};
struct VexBox{
string data; //顶点名称
EBox *firstedge; //指向第一条边关联该节点的边
};
/***********************************/
/*创建邻接表*/
struct Arcnode {
int v;
Arcnode* next;
}; //表结点
struct Vnode {
string data;
Arcnode* arc;
}; //头结点
/*创建树 ***********************/
struct tree {
string data;
tree* first = NULL;
tree* sibling = NULL;
};
struct node {
int v;
tree* t;
};
/***********************************/
class AMLGraph{
private:
VexBox *adjmulist; //顶点数组指针
Vnode *Vex;
int vexnum; //点数
int arcnum; //边数
int maxnum; //最大点数
public:
AMLGraph(int num);
~AMLGraph();
int Locate_Vex(string v); //定位顶点在顶点数组中的位置
void CreateUDG_AML(); //邻接多重表,储存无向图
bool Search_Arc(string v1,string v2) ; //搜素对应的边是否存在
void Find_Neighbour(string v); //输出顶点V的邻接顶点
bool Insert_Arc(string v1,string v2); //插入新边,不插入平行边
void DFS_Traverse(); //深度优先
void DFS(int v);
void BFS_Traverse(); //广度优先
void BFS(int v); // 输出邻接多重表
tree* CreatTree(int v,tree *T);
void TreeDFS(int v,tree *T);
void TreeBFS(int v , tree* T, queue <node> qu);
void DFS_NO_Recursive(int v);
void PreOrder(tree *T);
void print_tree(tree* T, int space);
int getvexnum(){
return vexnum;
}
};
AMLGraph::AMLGraph(int num=20){
adjmulist = new VexBox[num];
Vex = new Vnode [num];
maxnum = num;
}
AMLGraph::~AMLGraph(){
delete [] adjmulist;
}
int AMLGraph::Locate_Vex(string v){
for(int i=0;i<vexnum;i++){
if(adjmulist[i].data==v)
return i;
}
return -1;
}
void AMLGraph::CreateUDG_AML(){
string v1,v2;
int i,j,k;
cout<<"输入定点数目和弧的数目: ";
cin>>vexnum>>arcnum;
while(vexnum > maxnum){
cout<<"顶点数目太多,请重新输入顶点数和边数(提示 : 顶点数目不要超过20个): ";
cin>>vexnum>>arcnum;
}
while(arcnum>(vexnum*(vexnum-1)/2)){
cout<<"边数目太多,请重新输入顶点数和边数(提示 :无平行边的连通图的 边数 <= 点数 * (点数-1) / 2 ): ";
cin>>vexnum>>arcnum;
}
while(arcnum < vexnum-1){
cout<<"边数目太少,请重新输入顶点数和边数(提示 :无平行边的连通图的 边数 不少于 点数-1 : ";
cin>>vexnum>>arcnum;
}
cout<<"输入每个顶点的名称: ";
for(int t=0;t<vexnum;t++){
string s;
cin>>s;
adjmulist[t].data = s;
adjmulist[t].firstedge = NULL;
Vex[t].data = s;
Vex[t].arc = NULL;
}
cout<<"请输入各个边 "<<endl;
for(k=0;k<arcnum;k++){
cout<<"输入第"<<k+1<<"条边的两个顶点: ";
cin>> v1 >> v2;
while(Search_Arc(v1,v2)){
cout<<"此边已存在,本图不支持存在平行边"<<endl;
cout<<"请重新输入第"<<k+1<<"条边的两个顶点: ";
cin>>v1>>v2;
}
i = Locate_Vex(v1);
j = Locate_Vex(v2);
while(i==-1||j==-1){
cout<<"两个顶点之间有不符合要求的,请重新输入 : ";
cin>>v1>>v2;
i = Locate_Vex(v1);
j = Locate_Vex(v2);
}
EBox *p = new EBox;
p->ivex = i;
p->jvex = j;
p->ilink = adjmulist[i].firstedge;
p->jlink = adjmulist[j].firstedge;
adjmulist[i].firstedge = adjmulist[j].firstedge = p;
Arcnode* q= new Arcnode;
Arcnode* t= new Arcnode;
q->v = j;
q->next = Vex[i].arc;
Vex[i].arc = q;
t->v = i;
t->next = Vex[j].arc;
Vex[j].arc = t;
}
cout<<"无向图构造完成"<<endl;
}
bool AMLGraph::Search_Arc(string v1,string v2){
int i;
int j;
EBox *p;
i = Locate_Vex(v1);
j = Locate_Vex(v2);
if(i==-1||j==-1){
cout<<"顶点错误,该边不存在"<<endl;
return false;
}
p = adjmulist[i].firstedge;
while(p){
if(p->ivex == i && p->jvex ==j) return true;
else if(p->ivex == j && p->jvex ==i) return true;
else if(p->ivex == i) p = p->ilink;
else if(p->jvex == i) p = p->jlink;
}
return false;
}
void AMLGraph::Find_Neighbour(string v){
int i = Locate_Vex(v);
if(i==-1){
cout<<"该顶点不在此图中"<<endl;
return ;
}
EBox *p = adjmulist[i].firstedge;
if(p){
cout<<"顶点"<<v<<"的邻接顶点为: ";
while(p){
if(p->ivex == i){
cout<<adjmulist[p->jvex].data<<" ";
p = p->ilink;
}
else{
cout<<adjmulist[p->ivex].data<<" ";
p = p->jlink;
}
}
}
else
cout<<"该顶点无相邻的顶点"<<endl;
}
tree* AMLGraph::CreatTree(int v,tree *T){
T = new tree;
T->data = Vex[v].data;
return T;
}
void AMLGraph::print_tree(tree* T, int space) {
if(T) {
for(int i = 0; i < space; i++) cout << " ";
cout << setfill(‘-‘) << setw(70 - space) << left << T->data << endl;
if(T->first) {
tree* t = T->first;
while(t) {
print_tree(t, space + 4);
t = t->sibling;
}
}
}
}
void AMLGraph::TreeDFS(int v , tree* T){
searched[v] = 1;
bool firstSearched = true;
int w;
tree *t, *q;
Arcnode* p;
p = Vex[v].arc; //p是v的下一个点
while(p){
w = p->v; // w 是 P的v(p储存的数据)
if(!searched[w]){
q = new tree;
q->data = Vex[w].data;
if(firstSearched){
firstSearched = false;
T -> first = q;
}
else t->sibling = q;
t = q;
TreeDFS(w,t);
}
p = p->next;
}
}
void AMLGraph::TreeBFS(int v , tree* T, queue <node> qu){
searched[v] = 1;
bool firstSearched = true;
int w;
tree *t, *q;
node root;
Arcnode* p;
p = Vex[v].arc; //p是v的下一个点
while(p){
w = p->v; // w 是 P的v(p储存的数据)
if(!searched[w]){
searched[w] = 1;
q = new tree;
q->data = Vex[w].data;
if(firstSearched){
firstSearched = false;
T -> first = q;
}
else t->sibling = q;
t = q;
if(Vex[w].arc){
root.v = w;
root.t = t;
qu.push(root);
}
}
p = p->next;
}
while(!qu.empty()){
root = qu.front();
qu.pop();
TreeBFS(root.v , root.t , qu);
}
}
void PreOrder(tree *T) // 先序遍历
{
if(T) {
if(flag) {
cout << T->data;
flag = 0;
} else cout << "->" << T->data;
if(T->first) PreOrder(T->first);
if(T->sibling) PreOrder(T->sibling);
}
}
bool AMLGraph::Insert_Arc(string v1,string v2){
if(Search_Arc(v1,v2)){
cout<<"该边已经存在于图中,不重复插入"<<endl;
return false;
}
int i,j;
i = Locate_Vex(v1);
j = Locate_Vex(v2);
if(i==-1||j==-1){
cout<<"两个顶点中,又不符合要求的,插入失败"<<endl;
return false;
}
EBox *p = new EBox;
p->ivex = i;
p->jvex = j;
p->ilink = adjmulist[i].firstedge;
p->jlink = adjmulist[j].firstedge;
adjmulist[i].firstedge=adjmulist[j].firstedge=p;
arcnum ++;
return true;
}
void AMLGraph::DFS_Traverse(){
//for(int i=0;i<vexnum;i++) visited[i] = false;
for(int i=0;i<vexnum;i++){
visited[i] = false;
}
for(int i=0;i<vexnum;i++){
if(!visited[i]){
DFS(i);
}
cout<<endl;
}
}
void AMLGraph::DFS(int v){
//for(int i=0;i<vexnum;i++) visited[i] = false;
bool flag = true;
visited[v] = true;
if(flag) {
cout<<adjmulist[v].data<<" ";
flag = 0;
} else cout << "-> " <<adjmulist[v].data<<" ";
EBox *p = adjmulist[v].firstedge;
while(p){
if(p->ivex == v){
if(!visited[p->jvex])
DFS(p->jvex);
p = p->ilink;
}
else{
if(!visited[p->ivex])
DFS(p->ivex);
p = p->jlink;
}
}
}
void AMLGraph::BFS_Traverse(){
for(int i=0;i<vexnum;i++)
visited[i]=false;
for(int i=0;i<vexnum;i++)
if(!visited[i])
BFS(i);
cout<<endl;
}
void AMLGraph::BFS(int v){
//for(int i=0;i<vexnum;i++) visited[i] = false;
visited[v]=true;
if(flag) {
cout<<adjmulist[v].data<<" ";
flag = 0;
} else cout << "-> " <<adjmulist[v].data<<" ";
EBox *p;
int pos;
queue<int> qu;
qu.push(v);
while(!qu.empty())
{
pos=qu.front();
qu.pop();
p=adjmulist[pos].firstedge;
while(p)
{
if(p->ivex == pos)
{
if(!visited[p->jvex])
{
visited[p->jvex]=true;
if(flag) {
cout<<adjmulist[p->jvex].data<<" ";
flag = 0;
} else cout << "-> " <<adjmulist[p->jvex].data<<" ";
qu.push(p->jvex);
}
p=p->ilink;
}
else
{
if(!visited[p->ivex])
{
visited[p->ivex]=true;
if(flag) {
cout<<adjmulist[p->ivex].data<<" ";
flag = 0;
} else cout << "-> " << adjmulist[p->ivex].data<<" ";
qu.push(p->ivex);
}
p=p->jlink;
}
}
}
}
void AMLGraph::DFS_NO_Recursive(int v){
//for(int i=0;i<vexnum;i++) visited[i] = false;
EBox* p;
stack <int> st;
int pos;
bool flag = 1;
if(!visited[v]) {
visited[v] = true;
if(flag) {
cout<<adjmulist[v].data<<" ";
flag = 0;
} else cout << "-> " <<adjmulist[v].data<<" ";
st.push(v);
while(!st.empty()) {
pos = st.top();
p=adjmulist[pos].firstedge;
while(p) {
if(p->ivex == pos) {
if(!visited[p->jvex]) {
visited[p->jvex] = 1;
if(flag) {
cout<<adjmulist[p->jvex].data<<" ";
flag = 0;
} else cout << "-> " <<adjmulist[p->jvex].data<<" ";
st.push(p->jvex);
break;
}
p = p->ilink;
} else {
if(!visited[p->ivex]) {
visited[p->ivex] = 1;
if(flag) {
cout<<adjmulist[p->ivex].data<<" ";
flag = 0;
} else cout << "-> " << adjmulist[p->ivex].data<<" ";
st.push(p->ivex);
break;
}
p = p->jlink;
}
}
if(!p) st.pop();
}
}
}
void run(){
cout<<"********************************************************************"<<endl;
cout<<" 无向图的遍历 "<<endl;
cout<<" 请根据提示进行必要的输入以确保图的输入 "<<endl;
AMLGraph a;
a.CreateUDG_AML();
int run = 1;
while(run){
cout<<" 输入 1 ---------对图进行邻接多重表存储图的深度优先遍历(递归) "<<endl;
cout<<" 输入 2 ---------对图进行邻接多重表存储图的广度优先遍历 "<<endl;
cout<<" 输入 3 ---------深度优先生成树输出 "<<endl;
cout<<" 输入 4 ---------广度优先生成树输出 "<<endl;
cout<<" 输入 5 ---------对图进行邻接多重表存储图的深度优先遍历(非递归) "<<endl;
cout<<" 输入 0 ---------结束程序 "<<endl;
int c;
cin>>c;
while(c<0||c>5){
cout<<"输入不符合规则选项,请确保输入无误后重新输入>>> ";
cin>>c;
}
switch(c){
case 0:
{ system("cls");
cout<<"谢谢使用!程序结束!"<<endl;
run = 0;
break;
}
case 1:
{
system("cls");
cout<<"请输入一个遍历的起点>>>>>> 输入起点名字 >>>>>>";
string s;
cin>>s;
while(a.Locate_Vex(s)==-1){
cout << "该顶点不存在! 请重新输入起点的名字>>>>> ";
cin >> s;
}
cout<<"对图进行邻接多重表存储图的深度优先遍历(递归)为"<<endl;
for(int i=0;i<a.getvexnum();i++) visited[i] = false;
a.DFS(a.Locate_Vex(s));
break;
}
case 2:
{
system("cls");
cout<<"请输入一个遍历的起点>>>>>> 输入起点名字 >>>>>>";
string s;
cin>>s;
while(a.Locate_Vex(s)==-1){
cout << "该顶点不存在! 请重新输入起点的名字>>>>> ";
cin >> s;
}
cout<<"对图进行邻接多重表存储图的广度优先遍历为"<<endl;
for(int i=0;i<a.getvexnum();i++){
visited[i] = 0;
searched[i] = 0;
}
a.BFS(a.Locate_Vex(s));
break;
}
case 3:
{
system("cls");
cout<<"请输入一个遍历的起点>>>>>> 输入起点名字 >>>>>>";
string s;
cin>>s;
while(a.Locate_Vex(s)==-1){
cout << "该顶点不存在! 请重新输入起点的名字>>>>> ";
cin >> s;
}
cout<<"对图进行邻接多重表存储图的深度优先遍历为"<<endl;
a.DFS(a.Locate_Vex(s));
cout<<endl;
cout<<"深度优先生成树输出为"<<endl;
tree *T;
for(int i=0;i<a.getvexnum();i++) visited[i] = false;
T = a.CreatTree(a.Locate_Vex(s),T);
a.TreeDFS(a.Locate_Vex(s),T);
a.print_tree(T, a.Locate_Vex(s));
cout<<endl;
break;
}
case 4:
{
system("cls");
cout<<"请输入一个遍历的起点>>>>>> 输入起点名字 >>>>>>";
string s;
cin>>s;
while(a.Locate_Vex(s)==-1){
cout << "该顶点不存在! 请重新输入起点的名字>>>>> ";
cin >> s;
}
cout<<"对图进行邻接多重表存储图的广度优先遍历为"<<endl;
for(int i=0;i<a.getvexnum();i++) visited[i] = false;
a.BFS(a.Locate_Vex(s));
cout<<endl;
cout<<"广度优先生成树输出为"<<endl;
tree *T;
for(int i=0;i<a.getvexnum();i++) searched[i] = false;
T = a.CreatTree(a.Locate_Vex(s),T);
queue<node> qu;
a.TreeBFS(a.Locate_Vex(s),T,qu);
a.print_tree(T, a.Locate_Vex(s));
cout<<endl;
break;
}
case 5:
{
system("cls");
cout<<"请输入一个遍历的起点>>>>>> 输入起点名字 >>>>>>";
string s;
cin>>s;
while(a.Locate_Vex(s)==-1){
cout << "该顶点不存在! 请重新输入起点的名字>>>>> ";
cin >> s;
}
cout<<"对图进行邻接多重表存储图的深度优先遍历(非递归)为"<<endl;
for(int i=0;i<a.getvexnum();i++) visited[i] = false;
a.DFS_NO_Recursive(a.Locate_Vex(s));
cout<<endl;
break;
}
}
}
}
int main(){
run();
return 0;
}