



免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
import java.util.ArrayList;import java.util.LinkedHashMap;public class shutPath static int cost;static ArrayList visited = new ArrayList();static ArrayList unVisited = new ArrayList();static ArrayList vertexs = new ArrayList();static LinkedHashMap shortPath = new LinkedHashMap();static ArrayList arcs = new ArrayList();static LinkedHashMap shortPathWay = new LinkedHashMap();public static void main(String args) / TODO Auto-generated method stub /init the verges set arcs.add(new arc(A,B,2); arcs.add(new arc(A,C,4); arcs.add(new arc(A,D,15); arcs.add(new arc(B,D,5); arcs.add(new arc(B,C,1); arcs.add(new arc(C,D,7); arcs.add(new arc(D,E,4); /init the nodes set vertexs.add(A); vertexs.add(B); vertexs.add(C); vertexs.add(D); vertexs.add(E); / init the novisited set visited.add(A); /init the visited set unVisited.add(B); unVisited.add(C); unVisited.add(D); unVisited.add(E); /init the shortPath map for(String unvisitNode:unVisited) boolean access = false; for(arc a:arcs) if(a.startNode.equals(A) & a.endNode.equals(unvisitNode) shortPath.put(unvisitNode,a.weight); access = true; break; if(access = false) shortPath.put(unvisitNode, -1); /把第一个临近节点的前驱找到 initFirstShortPathWay(); while(unVisited.size()0) String lastVisitedNode = getLastVisitedNode(); for(String unvisitNode:unVisited) /获得最后一访问节点到未访问节点到距离 int newPath = getWeight(lastVisitedNode,unvisitNode); if(newPath 0) /获得源点到未访问节点的距离 int oldPath = getOldPath(unvisitNode); /如果二者都存在话,改变shortPath 的相应值为最小值 if(oldPath 0) if(oldPath getOldPath(lastVisitedNode)+newPath) resetShortPath(unvisitNode,getOldPath(lastVisitedNode)+newPath); shortPathWay.put(unvisitNode,lastVisitedNode);/后继前驱 /如果原来不可达的话,但是通过中间节点可以到达,那么同样要改变shortPath else resetShortPath(unvisitNode,getOldPath(lastVisitedNode)+newPath); shortPathWay.put(unvisitNode,lastVisitedNode); String minNode = getTheMinPathNode(); removeNode(minNode,unVisited); addNode(minNode,visited); /输出最终结果 printResult(); /初始化第一个 路径的前驱public static void initFirstShortPathWay() int min = 500; String firstNode =; for(String vertex:shortPath.keySet() int tem = shortPath.get(vertex); if(tem 0) min = min tem?tem:min; for(String vertex:shortPath.keySet() if(min = shortPath.get(vertex)firstNode = vertex; shortPathWay.put(firstNode,A); /add a node to the setpublic static void addNode(String node,ArrayList set) set.add(node);/ remove a node of the setpublic static void removeNode(String delNode,ArrayList set) int index = 0; for(int i=0;i0) min = mintem?tem:min; for(String unode:unVisited) if(min = shortPath.get(unode)node=unode; return node;/得到源点到未访问结点的最短距离public static int getOldPath(String node) if(node.equals(A)return 0; return shortPath.get(node);/重新设定 shortPathpublic static void resetShortPath(String node,int path) for(String snode:shortPath.keySet() if(snode.equals(node)shortPath.put(snode, path); /get the last node of the visited setpublic static String getLastVisitedNode() return visited.get(visited.size()-1);/ get the weightpublic static int getWeight(String startNode, String endNode) int weight=-1; for(arc a:arcs) if(a.startNode.equals(startNode) & a.endNode.equals(endNode) weight = a.weight; System.out.println(a.startNode+-+a.endNode+=+weight); return weight;/ get the min numpublic static void printResult() for(String vertex:shortPath.keySet() System.out.print(从源点A到+vertex+的最短路径为); printPath(vertex); System.out.print(vertex); System.out.print(长度为:+shortPath.get(vertex); System.out.println( ); public static void printPath(String vertex) String node = shortPathWay.get(vertex); if(!node.equals(A)printPath(node); System.out.print(node+ ); class arcString startNode = ;String endNode = ;int weight =0;public arc(String startNode,String endNode,int weight) this.startNode = startNode; this.endNode = endNode; this.weight = weight; 实验结果(截图):实验感想:Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 管道工程行业自律与规范完善路径探索与实施策略考核试卷
- 纺织面料鞋制造节能减排技术考核试卷
- 双十一销量增长秘籍
- 四川农业大学《生物质废弃物资源化利用》2023-2024学年第二学期期末试卷
- 宁夏卫生健康职业技术学院《图形图像处理技术》2023-2024学年第一学期期末试卷
- 宁夏职业技术学院《生物工程概论》2023-2024学年第二学期期末试卷
- 天津市东丽区名校2025年初三第二次联考考生物试题理试题含解析
- 昆明学院《生物工程与技术》2023-2024学年第二学期期末试卷
- 吉林省白城市一中2025届高三第二学期调研考试(语文试题)试题含解析
- 2025年网络工程师水平测试试卷及答案
- XXX防校园欺凌工作领导小组及职责
- 乳腺癌中医特色护理
- 沐足楼面服务员礼貌礼节培训
- 水肥一体化施工组织设
- 新疆特岗幼儿园学前教育模拟测试题
- 教育研究方法教育行动研究法
- 药浴婴幼儿计划书
- 医院检验科实验室生物安全管理手册
- 社区便民服务中心建设
- 高二学考动员主题班会课件
- 幼儿园《村居》教案
评论
0/150
提交评论