邻接矩阵实现无向图有向图基本操作.docx

上传人:b****1 文档编号:1704980 上传时间:2022-10-23 格式:DOCX 页数:8 大小:15.70KB
下载 相关 举报
邻接矩阵实现无向图有向图基本操作.docx_第1页
第1页 / 共8页
邻接矩阵实现无向图有向图基本操作.docx_第2页
第2页 / 共8页
邻接矩阵实现无向图有向图基本操作.docx_第3页
第3页 / 共8页
邻接矩阵实现无向图有向图基本操作.docx_第4页
第4页 / 共8页
邻接矩阵实现无向图有向图基本操作.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

邻接矩阵实现无向图有向图基本操作.docx

《邻接矩阵实现无向图有向图基本操作.docx》由会员分享,可在线阅读,更多相关《邻接矩阵实现无向图有向图基本操作.docx(8页珍藏版)》请在冰豆网上搜索。

邻接矩阵实现无向图有向图基本操作.docx

邻接矩阵实现无向图有向图基本操作

邻接矩阵实现无向图有向图基本操作

/*/

/*以无向图为基类,有向图为派生类,以邻接矩阵为存储方式,实现无向

图,有向

图的建立,插入,删除等操作*//*/

#includeiostream#includestringusingnamespacestd;struct_ArcInfo//边信息

{

char*v1;

char*v2;

};

struct_VexInfo{

intadj;//顶点类型,是否相邻接};

classUnDirectGraph{

public:

intArcNum;//边数

_ArcInfoArcInfo;

//邻接矩阵_VexInfoAdjMax[20][20];

intVexNum;//顶点数

char*Vexs[20];//存放顶点

public:

UnDirectGraph();

~UnDirectGraph();

intLocateVex(char*Vex);//获得节点在图中序号,在邻接矩阵中使用virtualvoidCreateGraph();//构造无向图

voidShowGraph();//输出邻接矩阵

voidInsertVex(char*&newVex);//向无向图中插入一个新节点virtualvoidInsertArc(char*&v1,char*&v2);//向无向图中插入一条边virtualvoidDeleteVex(char*oldVex);//删除一个节点virtualvoidDeleteArc(char*&v1,char*&v2);//删除一条边};

classDirectGraph:

publicUnDirectGraph//有向图继承无向图{

public:

DirectGraph();

~DirectGraph();

重写基类的四个函数,基类中相应函数设为虚函数//

voidCreateGraph();//构造有向图

voidInsertArc(char*&v1,char*&v2);//向有向图中插入一条弧voidDeleteArc(char*&v1,char*&v2);//删除有向图中一条弧voidDeleteVex(char*oldVex);//删除有向图中的一个顶点};

UnDirectGraph:

UnDirectGraph(){

VexNum=0;

ArcNum=0;

inti;

for(i=0;i20;i++)

Vexs[i]=newchar;

ArcInfo.v1=newchar;

ArcInfo.v2=newchar;

}

UnDirectGraph:

~UnDirectGraph(){

}

intUnDirectGraph:

LocateVex(char*Vex)//获取节点vex在图中的位置

{

inti;

for(i=0;iVexNum;i++)

{

if(strcmp(Vex,Vexs[i])==0)returni;

}

return0;

}

voidUnDirectGraph:

CreateGraph()//创建无向图{

cout"输入顶点数和边数:

";

cinVexNumArcNum;

inti,j,k;

cout"输入各个顶点:

"endl;

for(i=0;iVexNum;i++)//输入顶点cinVexs[i];

for(i=0;iVexNum;i++)

{

for(j=0;jVexNum;j++)

AdjMax[i][j].adj=0;//邻接矩阵初始化各点不相邻}

for(k=0;kArcNum;k++)

{

cout"输入边的信息:

";

cinArcInfo.v1ArcInfo.v2;

i=LocateVex(ArcInfo.v1);

j=LocateVex(ArcInfo.v2);

AdjMax[j][i].adj=AdjMax[i][j].adj=1;//无向图中两个顶点相邻

}

}

voidUnDirectGraph:

ShowGraph()//输出邻接矩阵{

inti,j;

for(i=0;iVexNum;i++){

for(j=0;jVexNum;j++)

cout""AdjMax[i][j].adj;

coutendl;

}

}

voidUnDirectGraph:

InsertVex(char*&newVex)//向图中插入一个节点

{

Vexs[VexNum]=newVex;

VexNum++;

for(inti=0;iVexNum;i++){

AdjMax[i][VexNum-1].adj=AdjMax[VexNum-1][i].adj=0;//插入一个孤立

节点

}

}

voidUnDirectGraph:

InsertArc(char*&v1,char*&v2)//在图中增加一条

{

inti,j;

i=LocateVex(v1);

j=LocateVex(v2);

if(i0||j0)

return;

ArcNum++;

AdjMax[i][j].adj=AdjMax[j][i].adj=1;}

voidUnDirectGraph:

DeleteArc(char*&v1,char*&v2)//删除两个节点之

间的边

{

inti,j;

i=LocateVex(v1);

j=LocateVex(v2);

ArcNum--;

AdjMax[i][j].adj=AdjMax[j][i].adj=0;}

voidUnDirectGraph:

DeleteVex(char*oldVex)

{

intk;

intk1,k2;

k=LocateVex(oldVex);//待删除节点的序号for(inti=0;iVexNum;i++)

{

k1=LocateVex(Vexs[i]);

if(AdjMax[k][k1].adj==1)//两节点之间有边DeleteArc(oldVex,Vexs[i]);//删除该节点与其他结点之间的边

}

for(intj=k+1;jVexNum;j++)Vexs[j-1]=Vexs[j];//该节点之后的元素向前移VexNum--;

cout"删除后顶点数:

"VexNumendl;cout"删除后边数:

"ArcNumendl;

}

DirectGraph:

DirectGraph()//派生类的构造函数,首先要实现基类的构

造函数

{

VexNum=0;

ArcNum=0;

inti;

for(i=0;i20;i++)

Vexs[i]=newchar;

ArcInfo.v1=newchar;

ArcInfo.v2=newchar;

}

DirectGraph:

~DirectGraph()

{

}

voidDirectGraph:

CreateGraph()//创建有向图

{

cout"输入有向图顶点数和弧数:

";cinVexNumArcNum;

inti,j,k;

cout"输入有向图的各个顶点:

"endl;for(i=0;iVexNum;i++)cinVexs[i];

for(i=0;iVexNum;i++){

for(j=0;jVexNum;j++)

AdjMax[i][j].adj=0;

}

for(k=0;kArcNum;k++)

{

cout"输入有向图1条弧的弧头和弧尾:

"endl;

cinArcInfo.v1ArcInfo.v2;i=LocateVex(ArcInfo.v1);j=LocateVex(ArcInfo.v2);AdjMax[i][j].adj=1;

}

}

voidDirectGraph:

InsertArc(char*&v1,char*&v2)//向有向图中插入一

条弧

{

inti,j;

i=LocateVex(v1);

j=LocateVex(v2);

if(i0||j0)return;

ArcNum++;

AdjMax[i][j].adj=1;

}

voidDirectGraph:

DeleteArc(char*&v1,char*&v2)//删除有向图中的一

条弧

{

inti,j;

i=LocateVex(v1);

j=LocateVex(v2);

ArcNum--;

AdjMax[i][j].adj=0;

}

voidDirectGraph:

DeleteVex(char*oldVex)//删除有向图中的一个顶点

{

intk,k1;

k=LocateVex(oldVex);

for(inti=0;iVexNum;i++){

k1=LocateVex(Vexs[i]);//if(AdjMax[k][k1].adj==1)//要删除结点为弧头的弧

DeleteArc(oldVex,Vexs[i]);

if(AdjMax[k1][k].adj==1)//要删除结点为弧尾的弧

DeleteArc(Vexs[i],oldVex);}

jVexNum;j++)for(intj=k+1;

Vexs[j-1]=Vexs[j];

VexNum--;

cout"删除后结点数:

"VexNumendl;cout"删除后弧数:

"ArcNumendl;}

intmain()

{

cout"无向图操作"endl;

UnDirectGraphUDG=UnDirectGraph();UDG.CreateGraph();

cout"图的邻接矩阵:

"endl;UDG.ShowGraph();

char*newVex=newchar;

cout"插入一个新节点:

"endl;cinnewVex;

UDG.InsertVex(newVex);

cout"插入新增加的边:

"endl;for(inti=0;iUDG.VexNum-1;i++)

UDG.InsertArc(newVex,UDG.Vexs[i]);cout"新图的邻接矩阵:

"endl;UDG.ShowGraph();

cout"删除一个节点"endl;

UDG.DeleteVex(UDG.Vexs[3]);UDG.ShowGraph();

cout"有向图操作"endl;

DirectGraphDG=DirectGraph();DG.CreateGraph();

cout"有向图的邻接矩阵:

"endl;DG.ShowGraph();

cout"向有向图中插入一个节点:

"endl;char*newVex1=newchar;

cinnewVex1;

DG.InsertVex(newVex1);

DG.InsertArc(newVex1,DG.Vexs[0]);//向有向图中插入几条弧

DG.InsertArc(DG.Vexs[2],newVex1);DG.InsertArc(DG.Vexs[1],newVex1);DG.Sh

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 化学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1