数据结构图的实现Word文档下载推荐.docx

上传人:b****1 文档编号:15360388 上传时间:2022-10-29 格式:DOCX 页数:13 大小:252.44KB
下载 相关 举报
数据结构图的实现Word文档下载推荐.docx_第1页
第1页 / 共13页
数据结构图的实现Word文档下载推荐.docx_第2页
第2页 / 共13页
数据结构图的实现Word文档下载推荐.docx_第3页
第3页 / 共13页
数据结构图的实现Word文档下载推荐.docx_第4页
第4页 / 共13页
数据结构图的实现Word文档下载推荐.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

数据结构图的实现Word文档下载推荐.docx

《数据结构图的实现Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《数据结构图的实现Word文档下载推荐.docx(13页珍藏版)》请在冰豆网上搜索。

数据结构图的实现Word文档下载推荐.docx

publicclassGraph{

staticfinalintMaxNum=20;

//最大节点数目

//用long的最大数来表示无穷大

staticfinallongMaxValue=9223372036854775807L;

char[]Vertex=newchar[MaxNum];

//定义数组,保存顶点信息

intGType;

//图的类型0:

无向图,1:

有向图

intVertxNum;

//顶点的数量

intEdgeNum;

//边的数量

long[][]EdgeWeight=newlong[MaxNum][MaxNum];

//矩阵保存顶点

int[]isTrav=newint[MaxNum];

//用来标记顶点是否已深度遍历

//创建图

publicvoidCreateGraph(Graphg){

inti,j,k;

longweight;

//权

charEstartV,EndV;

//边的起始顶点

System.out.println("

输入途中各顶点的信息"

);

Scannerscan=newScanner(System.in);

for(i=0;

i<

g.VertxNum;

i++){

第"

+(i+1)+"

个顶点"

g.Vertex[i]=(scan.next().toCharArray())[0];

}

输入构成各边的顶点和权值"

for(k=0;

k<

g.EdgeNum;

k++){

+(k+1)+"

条边:

"

EstartV=scan.next().charAt(0);

EndV=scan.next().charAt(0);

weight=scan.nextInt();

EstartV!

=g.Vertex[i];

i++);

//查找开始节点

for(j=0;

EndV!

=g.Vertex[j];

j++);

//查找终结点

g.EdgeWeight[i][j]=weight;

//对应边的权重

if(g.GType==0)//无向图,在对角位置保存权重

g.EdgeWeight[j][i]=weight;

//清空图,将所有的矩阵元素设为无穷大

publicvoidClearGraph(Graphg){

inti,j;

i<

i++)

for(j=0;

j<

g.VertxNum;

j++)

//设置矩阵中各元素的值为MaxValue

g.EdgeWeight[i][j]=Graph.MaxValue;

//输出邻接矩阵

publicvoidPrintGraph(Graphg){

for(j=0;

j<

j++)

System.out.print("

\t"

+g.Vertex[j]);

//输入顶点信息

System.out.println();

for(i=0;

i<

System.out.print(g.Vertex[i]);

j<

j++){

if(g.EdgeWeight[i][j]==Graph.MaxValue)//若权值为无穷大

\t&

//&

表示无穷大

else

+g.EdgeWeight[i][j]);

//深度遍历

publicvoidDeepTraOne(Graphg,intn){//从第n个节点开始遍历

inti;

g.isTrav[n]=1;

//标记为1表示该顶点已经被处理过

System.out.println(g.Vertex[n]);

//输出节点

for(i=0;

i++){

if(g.EdgeWeight[n][i]!

=g.MaxValue&

&

g.isTrav[i]==0){

DeepTraOne(g,i);

//递归进行遍历

publicvoidDeepTraGraph(Graphg){

g.isTrav[i]=0;

深度优先遍历:

g.VertxNum;

if(g.isTrav[i]==0)

DeepTraOne(g,i);

//开始广度遍历

privateintFirstVertex(intv){

if(v<

0||v>

(Vertex.length-1))

return-1;

for(inti=0;

Vertex.length;

i++)

if(EdgeWeight[v][i]==1)

returni;

//返回顶点v相对于w的下一个邻接顶点的索引,失败则返回-1

privateintNextVertex(intv,intw){

(Vertex.length-1)||w<

0||w>

for(inti=w+1;

}

publicvoidBFS(){

inthead=0;

intrear=0;

int[]queue=newint[Vertex.length];

//辅组数组

//顶点访问标记

boolean[]visited=newboolean[Vertex.length];

visited[i]=false;

BFS广度优先遍历:

"

i++){

if(!

visited[i]){

visited[i]=true;

+Vertex[i]);

queue[rear++]=i;

while(head!

=rear){

intj=queue[head++];

for(intk=FirstVertex(j);

k>

=0;

k=NextVertex(j,k)){

//k是为访问的邻接顶点

visited[k]){

visited[k]=true;

+Vertex[k]);

queue[rear++]=k;

//图求度

publicvoidGetDegree(Graphg){

//无向图

if(GType==0){

intdegree=0;

for(inti=0;

i<

i++){

System.out.print("

顶点"

+Vertex[i]+"

的度为:

for(intj=0;

j<

j++){

if(g.EdgeWeight[i][j]!

=MaxValue){

degree++;

}

}

System.out.print(degree);

System.out.println();

degree=0;

}

//有向图

else{

intindegree=0;

intoutdegree=0;

的出度为:

indegree++;

}

if(g.EdgeWeight[j][i]!

outdegree++;

}

System.out.print(indegree);

入度为:

+outdegree+"

indegree=0;

outdegree=0;

}

测试编码:

publicclassGraphTest{

publicstaticvoidmain(String[]args){

Scannerscan=newScanner(System.in);

Graphg=newGraph();

输出生成图的类型:

g.GType=scan.nextInt();

//图的种类

输入图的顶点数量:

g.VertxNum=scan.nex

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

当前位置:首页 > 求职职场 > 面试

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

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