关闭

关闭

关闭

封号提示

内容

首页 C语言大作业

C语言大作业.doc

C语言大作业

印藏着爱情的名存
2017-09-30 0人钱柜777手机版登陆 0 0 0 暂无简介 举报

简介:本文档为《C语言大作业doc》,可适用于IT/计算机领域

C语言大作业**File:tspc*Description:求解货郎担问题的分枝限界算法*Branchandboundalgorithmtosolve*thetravellingsalesmanproblem*Created:*Author:*#include<stdioh>#include<stdlibh>#include<mathh>#defineTRUE()#defineFALSE()#defineMAXCITIES()#defineINFINITY()#defineIINFINITYtypedefintbool*定义边结构*typedefstructEDGE{intheadinttail}EDGE*定义路径结构*typedefstructPATH{EDGEedgeMAXCITIESintedgesNumber}PATH*定义花费矩阵结构*typedefstructMATRIX{intdistanceMAXCITIESMAXCITIESintcitiesNumber}MATRIX*定义树结点*typedefstructNODE{intbound*相应于本结点的花费下界*MATRIXmatrix*当前花费矩阵*PATHpath*已经选定的边*structNODE*left*左枝*structNODE*right*右枝*}NODEintSimplify(MATRIX*)EDGESelectBestEdge(MATRIX)MATRIXLeftNode(MATRIX,EDGE)MATRIXRightNode(MATRIX,EDGE,PATH)PATHAddEdge(EDGE,PATH)PATHBABA(NODE)PATHMendPath(PATH,MATRIX)intMatrixSize(MATRIX,PATH)voidShowMatrix(MATRIX)voidShowPath(PATH,MATRIX)main(){PATHpathNODEroot={,*花费下界*{{{I,,,,},*花费矩阵*{,I,,,},{,,I,,},{,,,I,},{,,,,I}},},*城市数目*{{},},*经历过的路径*,*左枝与右枝*}*归约建立根结点*rootbound=Simplify(rootmatrix)*进入搜索循环*path=BABA(root)ShowPath(path,rootmatrix)}**算法主搜索函数BranchAndBoundAlgorithmSearch*root是当前的根结点已归约数据完善*PATHBABA(NODEroot){intistaticintminDist=INFINITYstaticPATHminPathEDGEselectedEdgeNODE*left,*rightputs("CurrentRoot:n")ShowMatrix(rootmatrix)printf("RootBound:dn",rootbound)*如果当前矩阵大小为说明还有两条边没有选而这两条边必定只能有一种组合*才能构成整体回路所以事实上所有路线已经确定。*if(MatrixSize(rootmatrix,rootpath)==){if(rootbound<minDist){minDist=rootboundminPath=MendPath(rootpath,rootmatrix)getch()return(minPath)}}*根据左下界尽量大的原则选分枝边*selectedEdge=SelectBestEdge(rootmatrix)printf("SelectedEdge:(d,d)n",selectedEdgehead,selectedEdgetail)*建立左右分枝结点*left=(NODE*)malloc(sizeof(NODE))right=(NODE*)malloc(sizeof(NODE))if(left==||right==){fprintf(stderr,"Errormallocn")exit()}*初始化左右分枝结点*left>bound=rootbound*继承父结点的下界*left>matrix=LeftNode(rootmatrix,selectedEdge)*删掉分枝边*left>path=rootpath*继承父结点的路径没有增加新边*left>left=left>right=right>bound=rootboundright>matrix=RightNode(rootmatrix,selectedEdge,rootpath)*删除行列和回路边*right>path=AddEdge(selectedEdge,rootpath)*加入所选边*right>left=right>right=*归约左右分枝结点*left>bound=Simplify(left>matrix)right>bound=Simplify(right>matrix)*链接到根*rootleft=leftrootright=right*显示到监视器*puts("RightBranch:n")ShowMatrix(right>matrix)puts("LeftBranch:n")ShowMatrix(left>matrix)*如果右结点下界小于当前最佳答案继续分枝搜索*if(right>bound<minDist){BABA(*right)}*否则不搜索因为这条枝已经不可能产生更佳路线*else{printf("CurrentminDistisd,",minDist)printf("RightBranch'sBound(=d)n",right>bound)printf("Thisbranchisdeadn")}*如果右结点下界小于当前最佳答案继续分枝搜索*if(left>bound<minDist){BABA(*left)}*否则不搜索因为这条枝已经不可能产生更佳路线*else{printf("CurrentminDistisd,",minDist)printf("LeftBranch'sBound(=d)n",left>bound)printf("Thisbranchisdeadn")}printf("Thebestanswernowisdn",minDist)return(minPath)}*修补路径*PATHMendPath(PATHpath,MATRIXc){introw,colEDGEedgeintn=ccitiesNumberfor(row=row<nrow){edgehead=rowfor(col=col<ncol){edgetail=colif(cdistancerowcol==){path=AddEdge(edge,path)}}}returnpath}*归约费用矩阵返回费用矩阵的归约常数*intSimplify(MATRIX*c){introw,col,mindist,hintn=c>citiesNumberh=*行归约*for(row=row<nrow){*找出本行最小的元素*mindist=INFINITYfor(col=col<ncol){if(c>distancerowcol<mindist){mindist=c>distancerowcol}}*如果本行元素都是无穷说明本行已经被删除*if(mindist==INFINITY)continue*本行每元素减去最小元素*for(col=col<ncol){if(c>distancerowcol!=INFINITY){c>distancerowcol=mindist}}*计算归约常数*h=mindist}*列归约*for(col=col<ncol){*找出本列最小的元素*mindist=INFINITYfor(row=row<nrow){if(c>distancerowcol<mindist){mindist=c>distancerowcol}}*如果本列元素都是无穷说明本列已经被删除*if(mindist==INFINITY)continue*本列元素减去最小元素*for(row=row<nrow){if(c>distancerowcol!=INFINITY){c>distancerowcol=mindist}}*计算归约常数*h=mindist}return(h)}*搜索所有花费为零的边中最合适的使左枝下界更大*EDGESelectBestEdge(MATRIXc){introw,colintn=ccitiesNumberintmaxDEDGEbest,edge*所用函数声明*intD(MATRIX,EDGE)maxD=for(row=row<nrow){for(col=col<ncol){edgehead=rowedgetail=colif(!cdistancerowcolmaxD<D(c,edge)){maxD=D(c,edge)best=edge}}}return(best)}*计算如果选edge作为分枝边左枝(不含edge)下界的增量*intD(MATRIXc,EDGEedge){introw,col,dRow,dColintn=ccitiesNumberdRow=INFINITYfor(col=col<ncol){if(dRow<cdistanceedgeheadcolcol!=edgetail){dRow=cdistanceedgeheadcol}}dCol=INFINITYfor(row=row<nrow){if(dCol<cdistancerowedgetailrow!=edgehead){dCol=cdistancerowedgetail}}return(dRowdCol)}*删掉所选分枝边*MATRIXLeftNode(MATRIXc,EDGEedge){cdistanceedgeheadedgetail=INFINITYreturnc}*删除行列和回路边后的矩阵*MATRIXRightNode(MATRIXc,EDGEedge,PATHpath){introw,colintn=ccitiesNumberEDGEloopEdge*声明所需要的求回路边函数*EDGELoopEdge(PATH,EDGE)for(col=col<ncol)cdistanceedgeheadcol=INFINITYfor(row=row<nrow)cdistancerowedgetail=INFINITYloopEdge=LoopEdge(path,edge)cdistanceloopEdgeheadloopEdgetail=INFINITYreturn(c)}*计算回路边的函数*除了加入的新边当前结点路线集合中还可能包含一些已经选定的边这些边构成一条或*几条路径为了不构成回路必须使其中包含新边的路径头尾不能相连本函数返回这个*头尾相连的边以便把这个回路边的长度设成无穷。*EDGELoopEdge(PATHpath,EDGEedge){inti,jEDGEloopEdge*最小的回路边*loopEdgehead=edgetailloopEdgetail=edgehead*寻找回路边的头端点即包含新边的路径的尾端点*for(i=i<pathedgesNumberi){for(j=j<pathedgesNumberj){if(loopEdgehead==pathedgejhead){*扩大回路边*loopEdgehead=pathedgejtailbreak}}}*寻找回路边的尾端点即包含新边的路径的头端点*for(i=i<pathedgesNumberi){for(j=j<pathedgesNumberj){if(loopEdgetail==pathedgejtail){*扩大回路边*loopEdgetail=pathedgejheadbreak}}}return(loopEdge)}*将新边加入到路径中*PATHAddEdge(EDGEedge,PATHpath){pathedgepathedgesNumber=edgereturnpath}*计算花费矩阵当前阶数*intMatrixSize(MATRIXc,PATHpath){return(ccitiesNumberpathedgesNumber)}*显示路径*voidShowPath(PATHpath,MATRIXc){inti,distEDGEedgeintn=pathedgesNumberdist=printf("nThepathis:")for(i=i<ni){edge=pathedgeiprintf("(d,d)",edgehead,edgetail)dist=cdistanceedgeheadedgetail}*printf("TotalCost:dn",dist)*}*显示花费矩阵*voidShowMatrix(MATRIXc){introw,colintn=ccitiesNumberfor(row=row<nrow){for(col=col<ncol){if(cdistancerowcol!=INFINITY){printf("d",cdistancerowcol)}else{printf("")}}putchar('n')}getch()}

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续钱柜777手机版登陆或者下载,敬请购买!

评分:

/13

意见
反馈

立即扫码关注

爱问共享资料微信公众号

返回
顶部

举报
资料

博聚网