带权图的最小生成树——Prim算法和Kruskal算法

带权图的最短路径算法——Dijkstra算法

  1. package graph;
  2. // path.java
  3. // demonstrates shortest path with weighted, directed graphs 带权图的最短路径算法
  4. // to run this program: C>java PathApp
  5. ////////////////////////////////////////////////////////////////
  6. class DistPar // distance and parent
  7. { // items stored in sPath array
  8. public int distance; // distance from start to this vertex
  9. public int parentVert; // current parent of this vertex
  10. // -------------------------------------------------------------
  11. public DistPar(int pv, int d) // constructor
  12. {
  13. distance = d;
  14. parentVert = pv;
  15. }
  16. // -------------------------------------------------------------
  17. } // end class DistPar
  18. ///////////////////////////////////////////////////////////////
  19. class Vertex
  20. {
  21. public char label; // label (e.g. 'A')
  22. public boolean isInTree;
  23. // -------------------------------------------------------------
  24. public Vertex(char lab) // constructor
  25. {
  26. label = lab;
  27. isInTree = false;
  28. }
  29. // -------------------------------------------------------------
  30. } // end class Vertex
  31. ////////////////////////////////////////////////////////////////
  32. class Graph
  33. {
  34. private final int MAX_VERTS = 20;
  35. private final int INFINITY = 1000000;
  36. private Vertex vertexList[]; // list of vertices
  37. private int adjMat[][]; // adjacency matrix
  38. private int nVerts; // current number of vertices
  39. private int nTree; // number of verts in tree
  40. private DistPar sPath[]; // array for shortest-path data
  41. private int currentVert; // current vertex
  42. private int startToCurrent; // distance to currentVert
  43. // -------------------------------------------------------------
  44. public Graph() // constructor
  45. {
  46. vertexList = new Vertex[MAX_VERTS];
  47. // adjacency matrix
  48. adjMat = new int[MAX_VERTS][MAX_VERTS];
  49. nVerts = 0;
  50. nTree = 0;
  51. for(int j=0; j<MAX_VERTS; j++) // set adjacency
  52. for(int k=0; k<MAX_VERTS; k++) // matrix
  53. adjMat[j][k] = INFINITY; // to infinity
  54. sPath = new DistPar[MAX_VERTS]; // shortest paths
  55. } // end constructor
  56. // -------------------------------------------------------------
  57. public void addVertex(char lab)
  58. {
  59. vertexList[nVerts++] = new Vertex(lab);
  60. }
  61. // -------------------------------------------------------------
  62. public void addEdge(int start, int end, int weight)
  63. {
  64. adjMat[start][end] = weight; // (directed)
  65. }
  66. // -------------------------------------------------------------
  67. public void path() // find all shortest paths
  68. {
  69. int startTree = 0; // start at vertex 0
  70. vertexList[startTree].isInTree = true;
  71. nTree = 1; // put it in tree
  72.  
  73. // transfer row of distances from adjMat to sPath
  74. for(int j=0; j<nVerts; j++)
  75. {
  76. int tempDist = adjMat[startTree][j];
  77. sPath[j] = new DistPar(startTree, tempDist);
  78. }
  79.  
  80. // until all vertices are in the tree
  81. while(nTree < nVerts)
  82. {
  83. int indexMin = getMin(); // get minimum from sPath
  84. int minDist = sPath[indexMin].distance;
  85.  
  86. if(minDist == INFINITY) // if all infinite
  87. { // or in tree,
  88. System.out.println("There are unreachable vertices");
  89. break; // sPath is complete
  90. }
  91. else
  92. { // reset currentVert
  93. currentVert = indexMin; // to closest vert
  94. startToCurrent = sPath[indexMin].distance;
  95. // minimum distance from startTree is
  96. // to currentVert, and is startToCurrent
  97. }
  98. // put current vertex in tree
  99. vertexList[currentVert].isInTree = true;
  100. nTree++;
  101. adjust_sPath(); // update sPath[] array
  102. } // end while(nTree<nVerts)
  103.  
  104. displayPaths(); // display sPath[] contents
  105.  
  106. nTree = 0; // clear tree
  107. for(int j=0; j<nVerts; j++)
  108. vertexList[j].isInTree = false;
  109. } // end path()
  110. // -------------------------------------------------------------
  111. public int getMin() // get entry from sPath
  112. { // with minimum distance
  113. int minDist = INFINITY; // assume minimum
  114. int indexMin = 0;
  115. for(int j=1; j<nVerts; j++) // for each vertex,
  116. { // if it's in tree and
  117. if( !vertexList[j].isInTree && // smaller than old one
  118. sPath[j].distance < minDist )
  119. {
  120. minDist = sPath[j].distance;
  121. indexMin = j; // update minimum
  122. }
  123. } // end for
  124. return indexMin; // return index of minimum
  125. } // end getMin()
  126. // -------------------------------------------------------------
  127. public void adjust_sPath()
  128. {
  129. // adjust values in shortest-path array sPath
  130. int column = 1; // skip starting vertex
  131. while(column < nVerts) // go across columns
  132. {
  133. // if this column's vertex already in tree, skip it
  134. if( vertexList[column].isInTree )
  135. {
  136. column++;
  137. continue;
  138. }
  139. // calculate distance for one sPath entry
  140. // get edge from currentVert to column
  141. int currentToFringe = adjMat[currentVert][column];
  142. // add distance from start
  143. int startToFringe = startToCurrent + currentToFringe;
  144. // get distance of current sPath entry
  145. int sPathDist = sPath[column].distance;
  146.  
  147. // compare distance from start with sPath entry
  148. if(startToFringe < sPathDist) // if shorter,
  149. { // update sPath
  150. sPath[column].parentVert = currentVert;
  151. sPath[column].distance = startToFringe;
  152. }
  153. column++;
  154. } // end while(column < nVerts)
  155. } // end adjust_sPath()
  156. // -------------------------------------------------------------
  157. public void displayPaths()
  158. {
  159. for(int j=0; j<nVerts; j++) // display contents of sPath[]
  160. {
  161. System.out.print(vertexList[j].label + "="); // B=
  162. if(sPath[j].distance == INFINITY)
  163. System.out.print("inf"); // inf
  164. else
  165. System.out.print(sPath[j].distance); // 50
  166. char parent = vertexList[ sPath[j].parentVert ].label;
  167. System.out.print("(" + parent + ") "); // (A)
  168. }
  169. System.out.println("");
  170. }
  171. // -------------------------------------------------------------
  172. } // end class Graph
  173. ////////////////////////////////////////////////////////////////
  174. class path
  175. {
  176. public static void main(String[] args)
  177. {
  178. Graph theGraph = new Graph();
  179. theGraph.addVertex('A'); // 0 (start)
  180. theGraph.addVertex('B'); // 1
  181. theGraph.addVertex('C'); // 2
  182. theGraph.addVertex('D'); // 3
  183. theGraph.addVertex('E'); // 4
  184.  
  185. theGraph.addEdge(0, 1, 50); // AB 50
  186. theGraph.addEdge(0, 3, 80); // AD 80
  187. theGraph.addEdge(1, 2, 60); // BC 60
  188. theGraph.addEdge(1, 3, 90); // BD 90
  189. theGraph.addEdge(2, 4, 40); // CE 40
  190. theGraph.addEdge(3, 2, 20); // DC 20
  191. theGraph.addEdge(3, 4, 70); // DE 70
  192. theGraph.addEdge(4, 1, 50); // EB 50
  193.  
  194. System.out.println("Shortest paths");
  195. theGraph.path(); // shortest paths
  196. System.out.println();
  197. } // end main()
  198. } // end class PathApp
  199. ////////////////////////////////////////////////////////////////

Java数据结构——带权图的更多相关文章

  1. java数据结构----带权图

    1.带权图:要引入带权图,首先要引入最小生成树,当所有的边拥有相同的权值时.问题变得简单了,算法可以选择任意一条边加入最小生成树.但是当边有不同的权值时,需要用一些算法决策来选择正确的边. 2.带权图 ...

  2. 带权图的最短路径算法(Dijkstra)实现

    一,介绍 本文实现带权图的最短路径算法.给定图中一个顶点,求解该顶点到图中所有其他顶点的最短路径 以及 最短路径的长度.在决定写这篇文章之前,在网上找了很多关于Dijkstra算法实现,但大部分是不带 ...

  3. 无向带权图的最小生成树算法——Prim及Kruskal算法思路

    边赋以权值的图称为网或带权图,带权图的生成树也是带权的,生成树T各边的权值总和称为该树的权. 最小生成树(MST):权值最小的生成树. 生成树和最小生成树的应用:要连通n个城市需要n-1条边线路.可以 ...

  4. C语言——无向带权图邻接矩阵的建立

    #include <stdio.h> #include "Graph.h" #define MAX_INT 32767 /* #define vnum 20 #defi ...

  5. (5)Java数据结构--有继承图,用途分析

    java 中几种常用数据结构 - u010947402的博客 - CSDN博客http://blog.csdn.net/u010947402/article/details/51878166 JAVA ...

  6. java数据结构_笔记(4)_图

    图一.概念.图: 是一种复杂的非线性数据结构.图的二元组定义: 图 G 由两个集合 V 和 E 组成,记为:G=(V, E)  其中: V 是顶点的有穷非空集合,E 是 V 中顶点偶对(称为边)的有穷 ...

  7. java数据结构----图

    1.图:.在计算机程序设计中,图是最常用的数据结构之一.对于存储一般的数据问题,一般用不到图.但对于某些(特别是一些有趣的问题),图是必不可少的.图是一种与树有些相像的数据结构,从数学意义上来讲,树是 ...

  8. java数据结构_笔记(5)_图的算法

    图的算法 1 图的遍历图的遍历就是从图中某个顶点出发,按某种方法对图中所有顶点访问且仅访问一次.遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础. 2 深度优先遍历从图中某个顶点V 出发 ...

  9. Java数据结构和算法(十五)——无权无向图

    前面我们介绍了树这种数据结构,树是由n(n>0)个有限节点通过连接它们的边组成一个具有层次关系的集合,把它叫做“树”是因为它看起来像一棵倒挂的树,包括二叉树.红黑树.2-3-4树.堆等各种不同的 ...

随机推荐

  1. Android入门笔记(一)

    第一部分,Android开发环境的搭建 1.去http://www.oracle.com/technetwork/java/javase/downloads/index.html下载最新版本jdk并安 ...

  2. Ubuntu配置Ruby和Rails

    安装curl sudo apt-get install curl 安装RVM curl -L https://get.rvm.io | bash -s stable 通过RVM来安装Ruby rvm ...

  3. Linux 下系统调用的三种方法

    系统调用(System Call)是操作系统为在用户态运行的进程与硬件设备(如CPU.磁盘.打印机等)进行交互提供的一组接口.当用户进程需要发生系统调用时,CPU 通过软中断切换到内核态开始执行内核系 ...

  4. 天河2号荣膺第41届TOP500榜首

    国际TOP500组织在6月17日公布最新全球超级计算机500强榜单,由中国国防科技大学研制的“天河二号”以每秒33.86千万亿次的浮点运算速度成为全球最快的超级计算机. 天河2号(又称银河2号),将在 ...

  5. uva 140 bandwidth (好题) ——yhx

     Bandwidth  Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an orde ...

  6. bzoj-4514(网络流)

    题目链接: 4514: [Sdoi2016]数字配对 Description 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数 ...

  7. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  8. python文件调用

    如果列表T是a.py中是全局的,则直接调用即可,例如 #a.py T = [1,2,3,4]   #b.py import a def test():     for i in a.T:        ...

  9. perl 调用shell脚本

    perl调用shell命令 perl调用shell shell调用perl Perl执行shell命令的几种方式及其区别

  10. FineUI小技巧(7)多表头表格导出

    前言 之前我们曾写过一篇文章 FineUI小技巧(3)表格导出与文件下载,对于在 FineUI 中导出表格数据进行了详细描述.今天我们要更进一步,介绍下如何导出多表头表格. 多表头表格的标签定义 在 ...