南邮数据结构实验三图的基本运算及飞机换乘次数最少问题.docx
《南邮数据结构实验三图的基本运算及飞机换乘次数最少问题.docx》由会员分享,可在线阅读,更多相关《南邮数据结构实验三图的基本运算及飞机换乘次数最少问题.docx(28页珍藏版)》请在冰豆网上搜索。
南邮数据结构实验三图的基本运算及飞机换乘次数最少问题
实验报告
(2015/2016学年第2学期)
课程名称
数据结构A
实验名称
图的基本运算及飞行换乘次数最少问题
实验时间
年
月
日
指导单位
计算机科学与技术系
指导教师
学生姓名
班级学号
学院(系)
专业
试验一图的基本运算
一、问题描述
(1)验证教材中关于在邻接矩阵和邻接表两种不同存储结构上实现图的基本运算的算法(见程序9.1~程序9.8);
(2)在邻接矩阵存储结构上实现图的深度和广度优先遍历算法;
(3)设计主函数,测试上述运算;
(4)提示:
扩充MGraph类,在扩充类上增加DFS和BFS函数;
二、概要设计
图如下所示,显示了名为operation_of_map的(默认文件名)工程,实现了Graph,SeqQueue,结点类ENode,邻接矩阵类MGraph,邻接表LGraph类,包括几种为不同传入类型准备的构造函数。
声明所要求的函数,并在后续过程中实现函数功能,最后通过一个main函数求解。
三、详细设计
1.类与类的层次结构
Graph类
public:
virtualResultCodeInsert(intu,intv,T&w)=0;
virtualResultCodeRemove(intu,intv)=0;
virtualboolExist(intu,intv)const=0;
protected:
intn,e;
SeqQueue类
MGraph类
public:
SeqQueue(intmSize);
~SeqQueue(){delete[]q;}
boolIsEmpty()const{returnfront==rear;}
boolIsFull()const{return(rear+1)%maxSize==front;}
boolFront(T&x)const;
boolEnQueue(Tx);
boolDeQueue();
voidClear(){front=rear=0;}
private:
intfront,rear;
intmaxSize;
T*q;
public:
MGraph(intmSize,constT&noedg);
~MGraph();
ResultCodeInsert(intu,intv,T&w);
ResultCodeRemove(intu,intv);
boolExist(intu,intv)const;
voidDFS();
voidBFS();
protected:
T**a;
TnoEdge;
voidDFS(intv,bool*visited);
voidBFS(intv,bool*visited);
ENode类
LGraph类
public:
ENode(){nextArc=NULL;}
ENode(intvertex,Tweight,ENode*next)
{
adjVex=vertex;
w=weight;
nextArc=next;
}
intadjVex;
Tw;
ENode*nextArc;
public:
LGraph(intmSize);
~LGraph();
ResultCodeInsert(intu,intv,T&w);
ResultCodeRemove(intu,intv);
boolExist(intu,intv)const;
protected:
ENode**a;
四、程序代码
#include"stdafx.h"
#include
usingnamespacestd;
constintINFTY=2147483640;
enumResultCode{Underflow,Duplicate,Failure,Success,NotPresent};
template
classGraph
{
public:
virtualResultCodeInsert(intu,intv,T&w)=0;
virtualResultCodeRemove(intu,intv)=0;
virtualboolExist(intu,intv)const=0;
protected:
intn,e;
};
template
classSeqQueue
{
public:
SeqQueue(intmSize);
~SeqQueue(){delete[]q;}
boolIsEmpty()const{returnfront==rear;}
boolIsFull()const{return(rear+1)%maxSize==front;}
boolFront(T&x)const;
boolEnQueue(Tx);
boolDeQueue();
voidClear(){front=rear=0;}
private:
intfront,rear;
intmaxSize;
T*q;
};
template
SeqQueue:
:
SeqQueue(intmSize)
{
maxSize=mSize;
q=newT[maxSize];
front=rear=0;
}
template
boolSeqQueue:
:
Front(T&x)const
{
if(IsEmpty())
{
returnfalse;
}
x=q[(front+1)%maxSize];
returntrue;
}
template
boolSeqQueue:
:
EnQueue(Tx)//在队尾插入x
{
if(IsFull())
{
cout<<"Full"<returnfalse;
}
q[rear=(rear+1)%maxSize]=x;
returntrue;
}
template
boolSeqQueue:
:
DeQueue()//删除队头元素
{
if(IsEmpty())
{
cout<<"Underflow"<returnfalse;
}
front=(front+1)%maxSize;
returntrue;
}
template
classMGraph:
publicGraph//邻接矩阵类
{
public:
MGraph(intmSize,constT&noedg);
~MGraph();
ResultCodeInsert(intu,intv,T&w);
ResultCodeRemove(intu,intv);
boolExist(intu,intv)const;
voidDFS();
voidBFS();
protected:
T**a;
TnoEdge;
voidDFS(intv,bool*visited);
voidBFS(intv,bool*visited);
};
template
MGraph:
:
MGraph(intmSize,constT&noedg)//构造函数
{
n=mSize;
e=0;
noEdge=noedg;
a=newT*[n];
for(inti=0;i{
a[i]=newT[n];
intj=0;
for(j;ja[i][j]=noEdge;
a[i][j]=0;
}
}
template
MGraph:
:
~MGraph()//析构函数
{
for(inti=0;idelete[]a[i];
delete[]a;
}
template
ResultCodeMGraph:
:
Insert(intu,intv,T&w)//插入函数
{
if(u<0||v<0||u>n-1||v>n-1||u==v)
returnFailure;
if(a[u][v]!
=noEdge)
returnDuplicate;
a[u][v]=w;
e++;
returnSuccess;
}
template
ResultCodeMGraph:
:
Remove(intu,intv)//删除函数
{
if(u<0||v<0||u>n-1||v>n-1||u==v)
returnFailure;
if(a[u][v]==noEdge)
returnNotPresent;
a[u][v]=noEdge;
e--;
returnSuccess;
}
template
boolMGraph:
:
Exist(intu,intv)const//判断边是否存在
{
if(u<0||v<0||u>n-1||v>n-1||u==v||a[u][v]==noEdge)
returnfalse;
returntrue;
}
template
voidMGraph:
:
DFS()//深度遍历
{
bool*visited=newbool[n];
for(inti=0;ivisited[i]=false;
for(inti=0;iif(!
visited[i])
DFS(i,visited);
delete[]visited;
}
template
voidMGraph:
:
DFS(intv,bool*visited)
{
visited[v]=true;
cout<<""<for(inti=0;iif(a[v][i]!
=noEdge&&a[v][i]!
=0&&!
visited[i])
DFS(i,visited);
}
template
voidMGraph:
:
BFS()//广度遍历
{
bool*visited=newbool[n];
for(inti=0;ivisited[i]=false;
for(inti=0;iif(!
visited[i])
BFS(i,visited);
delete[]visited;
}
template
voidMGraph:
:
BFS(intv,bool*visited)
{
SeqQueueq(n);
visited[v]=true;
cout<<""<q.EnQueue(v);
while(!
q.IsEmpty())
{
q.Front(v);
q.DeQueue();
for(inti=0;iif(a[v][i]!
=noEdge&&a[v][i]!
=0&&!
visited[i])
{
visited[i]=true;
cout<<""<
q.EnQueue(i);
}
}
}
template//结点类
classENode
{
public:
ENode(){nextArc=NULL;}
ENode(intvertex,Tweight,ENode*next)
{
adjVex=vertex;
w=weight;
nextArc=next;
}
intadjVex;
Tw;
ENode*nextArc;
};
template
classLGraph:
publicGraph//邻接表类
{
public:
LGraph(intmSize);
~LGraph();
ResultCodeInsert(intu,intv,T&w);
ResultCodeRemove(intu,intv);
boolExist(intu,intv)const;
protected:
ENode**a;
};
template
LGraph:
:
LGraph(intmSize)//构造函数
{
n=mSize;
e=0;
a=newENode*[n];
for(inti=0;ia[i]=NULL;
}
template
LGraph:
:
~LGraph()
{
ENode*p,*q;
for(inti=0;i{
p=a[i];
q=p;
while(p)
{
p=p->nextArc;
deleteq;
q=p;
}
}
delete[]a;
}
template
boolLGraph:
:
Exist(intu,intv)const//判断边是否存在
{
if(u<0||v<0||u>n-1||v>n-1||u==v)
returnfalse;
ENode*p=a[u];
while(p&&p->adjVex!
=v)
p=p->nextArc;
if(!
p)
returnfalse;
elsereturntrue;
}
template
ResultCodeLGraph:
:
Insert(intu,intv,T&w)//插入
{
if(u<0||v<0||u>n-1||v>n-1||u==v)
returnFailure;
if(Exist(u,v))
returnDuplicate;
ENode*p=newENode(v,w,a[u]);
a[u]=p;
e++;
returnSuccess;
}
template
ResultCodeLGraph:
:
Remove(intu,intv)//删除
{
if(u<0||v<0||u>n-1||v>n-1||u==v)
returnFailure;
ENode*p=a[u],*q;
q=NULL;
while(p&&p->adjVex!
=v)
{
q=p;
p=p->nextArc;
}
if(!
p)
returnNotPresent;
if(q)
q->nextArc=p->nextArc;
else
a[u]=p->nextArc;
deletep;
e--;
returnSuccess;
}
intmain()//主函数
{
intn,g;
cout<<"请输入元素的个数:
";
cin>>n;
MGraphA(n,INFTY);
LGraphB(n);
cout<<"请输入边的条数:
";
cin>>g;
int*a=newint[g];
int*b=newint[g];
int*w=newint[g];
for(inti=0;i{
cout<<"请输入边及权值:
";
cin>>a[i]>>b[i]>>w[i];
A.Insert(a[i],b[i],w[i]);
B.Insert(a[i],b[i],w[i]);
}
cout<<"该图的深度优先遍历为:
"<A.DFS();
cout<cout<<"该图的广度优先遍历为:
"<A.BFS();
cout<cout<<"请输入要搜索的边:
";
intc,d;
cin>>c>>d;
if(A.Exist(c,d))
cout<<"邻接矩阵中该边存在!
"<else
cout<<"邻接矩阵中该边不存在!
"<if(B.Exist(c,d))
cout<<"邻接表中该边存在!
"<else
cout<<"邻接表中该边不存在!
"<cout<<"请输入要删除的边:
";
inte,f;
cin>>e>>f;
if(A.Remove(e,f)==Success)
cout<<"邻接矩阵中删除该边成功!
"<elseif(A.Remove(e,f)==NotPresent)
cout<<"邻接矩阵中该边不存在!
"<else
cout<<"输入错误!
"<if(B.Remove(e,f)==Success)
cout<<"邻接表中删除该边成功!
"<elseif(B.Remove(e,f)==NotPresent)
cout<<"邻接表中该边不存在!
"<else
cout<<"邻接表中输入错误!
"<cout<<"删除该边后该图的深度优先遍历为:
"<A.DFS();
cout<cout<<"删除该边后该图的广度优先遍历为:
"<A.BFS();
cout<return0;
}
四、测试和调试
试验二飞机最少换乘次数问题
一、问题描述
(1)设有n个城市,编号为0~n-1,m条航线的起点和终点由用户输入提供。
寻找一条换乘次数最少的线路方案。
(2)提示:
可以使用有向图表示城市间的航线;只要两城市间有航班,则图中这两点间存在一条权为1的边;可以使用Dijkstra算法实现。
(3)思考:
如果是城市公交车的最少换乘问题,将如何解决?
假如希望使用类似的算法,则图中的边如何设置?
二、概要设计
首先创建抽象类Graph与邻接表类MGraph,首先创建结点与边也就是城市与航向条数,之后调用邻接表类的迪杰斯特拉算法实现计算
三、详细设计
1.类与类的层次结构:
Graph类
MGraph类
public:
virtualResultCodeInsert(intu,intv,Tw)=0;
virtualResultCodeRemove(intu,intv)=0;
virtualboolExist(intu,intv)const=0;
protected:
intn,e;
public:
MGraph(intmSize,constTnoedg);
~MGraph();
ResultCodeInsert(intu,intv,Tw);
ResultCodeRemove(intu,intv);
boolExist(intu,intv)const;
intChoose(int*d,bool*s);
voidDijkstra(intv,T*d,int*path);
protected:
T**a;
TnoEdge;
};
template
四、程序代码
#include"stdafx.h"
#include
#include
usingnamespacestd;
constintINF=2147483647;
enumResultCode{Underflow,Duplicate,Failure,Success,NotPresent,OutOfBounds};
template
classGraph//抽象类
{
public:
virtualResultCodeInsert(intu,intv,Tw)=0;
virtualResultCodeRemove(intu,intv)=0;
virtualboolExist(intu,intv)const=0;
protected:
intn,e;
};
template
classMGraph:
publicGraph//邻接矩阵类
{
public:
MGraph(intmSize,constTnoedg);
~MGraph();
ResultCodeInsert(intu,intv,Tw);
ResultCodeRemov