南邮数据结构实验三图的基本运算及飞机换乘次数最少问题Word下载.docx
《南邮数据结构实验三图的基本运算及飞机换乘次数最少问题Word下载.docx》由会员分享,可在线阅读,更多相关《南邮数据结构实验三图的基本运算及飞机换乘次数最少问题Word下载.docx(25页珍藏版)》请在冰豆网上搜索。
public:
virtualResultCodeInsert(intu,intv,T&
w)=0;
virtualResultCodeRemove(intu,intv)=0;
virtualboolExist(intu,intv)const=0;
protected:
intn,e;
SeqQueue类
MGraph类
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;
MGraph(intmSize,constT&
noedg);
~MGraph();
ResultCodeInsert(intu,intv,T&
w);
ResultCodeRemove(intu,intv);
boolExist(intu,intv)const;
voidDFS();
voidBFS();
T**a;
TnoEdge;
voidDFS(intv,bool*visited);
voidBFS(intv,bool*visited);
ENode类
LGraph类
ENode(){nextArc=NULL;
ENode(intvertex,Tweight,ENode*next)
{
adjVex=vertex;
w=weight;
nextArc=next;
intadjVex;
Tw;
ENode*nextArc;
LGraph(intmSize);
~LGraph();
ENode<
T>
**a;
四、程序代码
#include"
stdafx.h"
#include<
iostream>
usingnamespacestd;
constintINFTY=2147483640;
enumResultCode{Underflow,Duplicate,Failure,Success,NotPresent};
template<
classT>
classGraph
{
};
classSeqQueue
SeqQueue<
:
SeqQueue(intmSize)
maxSize=mSize;
q=newT[maxSize];
front=rear=0;
}
boolSeqQueue<
Front(T&
x)const
if(IsEmpty())
returnfalse;
x=q[(front+1)%maxSize];
returntrue;
EnQueue(Tx)//在队尾插入x
if(IsFull())
cout<
<
"
Full"
<
endl;
q[rear=(rear+1)%maxSize]=x;
DeQueue()//删除队头元素
Underflow"
front=(front+1)%maxSize;
classMGraph:
publicGraph<
//邻接矩阵类
MGraph<
MGraph(intmSize,constT&
noedg)//构造函数
n=mSize;
e=0;
noEdge=noedg;
a=newT*[n];
for(inti=0;
i<
n;
i++)
a[i]=newT[n];
intj=0;
for(j;
j<
j++)
a[i][j]=noEdge;
a[i][j]=0;
~MGraph()//析构函数
delete[]a[i];
delete[]a;
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<
Remove(intu,intv)//删除函数
if(a[u][v]==noEdge)
returnNotPresent;
a[u][v]=noEdge;
e--;
boolMGraph<
Exist(intu,intv)const//判断边是否存在
n-1||u==v||a[u][v]==noEdge)
voidMGraph<
DFS()//深度遍历
bool*visited=newbool[n];
visited[i]=false;
if(!
visited[i])
DFS(i,visited);
delete[]visited;
DFS(intv,bool*visited)
visited[v]=true;
cout<
v;
if(a[v][i]!
=noEdge&
&
a[v][i]!
=0&
!
BFS()//广度遍历
BFS(i,visited);
BFS(intv,bool*visited)
SeqQueue<
int>
q(n);
q.EnQueue(v);
while(!
q.IsEmpty())
q.Front(v);
q.DeQueue();
for(inti=0;
if(a[v][i]!
{
visited[i]=true;
cout<
i;
q.EnQueue(i);
}
//结点类
classENode
classLGraph:
//邻接表类
LGraph<
LGraph(intmSize)//构造函数
a=newENode<
*[n];
a[i]=NULL;
~LGraph()
*p,*q;
p=a[i];
q=p;
while(p)
{
p=p->
nextArc;
deleteq;
q=p;
}
boolLGraph<
*p=a[u];
while(p&
p->
adjVex!
=v)
p=p->
if(!
p)
elsereturntrue;
ResultCodeLGraph<
w)//插入
if(Exist(u,v))
*p=newENode<
(v,w,a[u]);
a[u]=p;
Remove(intu,intv)//删除
*p=a[u],*q;
q=NULL;
if(q)
q->
nextArc=p->
else
a[u]=p->
deletep;
intmain()//主函数
intn,g;
请输入元素的个数:
"
;
cin>
>
n;
MGraph<
A(n,INFTY);
LGraph<
B(n);
请输入边的条数:
g;
int*a=newint[g];
int*b=newint[g];
int*w=newint[g];
g;
请输入边及权值:
cin>
a[i]>
b[i]>
w[i];
A.Insert(a[i],b[i],w[i]);
B.Insert(a[i],b[i],w[i]);
该图的深度优先遍历为:
A.DFS();
该图的广度优先遍历为:
A.BFS();
请输入要搜索的边:
intc,d;
c>
d;
if(A.Exist(c,d))
邻接矩阵中该边存在!
邻接矩阵中该边不存在!
if(B.Exist(c,d))
邻接表中该边存在!
邻接表中该边不存在!
请输入要删除的边:
inte,f;
e>
f;
if(A.Remove(e,f)==Success)
邻接矩阵中删除该边成功!
elseif(A.Remove(e,f)==NotPresent)
输入错误!
if(B.Remove(e,f)==Success)
邻接表中删除该边成功!
elseif(B.Remove(e,f)==NotPresent)
邻接表中输入错误!
删除该边后该图的深度优先遍历为:
删除该边后该图的广度优先遍历为:
return0;
4、测试和调试
试验二飞机最少换乘次数问题
一、问题描述
(1)设有n个城市,编号为0~n-1,m条航线的起点和终点由用户输入提供。
寻找一条换乘次数最少的线路方案。
(2)提示:
可以使用有向图表示城市间的航线;
只要两城市间有航班,则图中这两点间存在一条权为1的边;
可以使用Dijkstra算法实现。
(3)思考:
如果是城市公交车的最少换乘问题,将如何解决?
假如希望使用类似的算法,则图中的边如何设置?
首先创建抽象类Graph与邻接表类MGraph,首先创建结点与边也就是城市与航向条数,之后调用邻接表类的迪杰斯特拉算法实现计算
三、详细设计
1.类与类的层次结构:
MGraph类
virtualResultCodeInsert(intu,intv,Tw)=0;
MGraph(intmSize,constTnoedg);
ResultCodeInsert(intu,intv,Tw);
intChoose(int*d,bool*s);
voidDijkstra(intv,T*d,int*path);
string.h>
constintINF=2147483647;
enumResultCode{Underflow,Duplicate,Failure,Success,NotPresent,OutOfBounds};
classGraph//抽象类
//邻接矩阵类
ResultCodeRemove