1)用邻接矩阵方式进行图的存储。如果一个图有n个节点,则可以用n*n的二维数组来存储图中的各个节点关系。

对上面图中各个节点分别编号,ABCDEF分别设置为012345。那么AB AC AD 关系可以转换为01 02 03, BC BE BF 可以转换为12 14 15, EF可以转换为45。换句话所,我们将各个节点关系存储在一个n*n的二位数组中,数组下标分别对应A-F,有关系的两个节点,在数组中用1表示,否则用0表示。这上图关系可以用6*6数组表示为:

2)深度优先进行图的遍历以及将图转换为最小生成树

深度优先的原则是从根节点开始,依次寻找后继的第一个子节点,直到没有后继为止。然后回到根节点,寻找根的第二个子节点,然后再依次寻找他的后继,直到没有后继为止。以此类推,直到所有节点都被遍历为止。实现深度优先遍历,有一个回朔的过程,所以需要用栈这种数据结构。

3)广度优先进行图的遍历以及将图转换为最小生成树

广度优先原则是从根节点开始,先将根的所有后继找出来,然后依次将所有后继的后继再找出来。以此类推,直到所有元素节点都被遍历。实现广度优先,是一个顺序过程,所以这里需要用到队列或者链表这种数据结构。

下面是代码实现:

  1. package org.lyk.impl;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Queue;
  5. import java.util.Stack;
  6.  
  7. public class Graph
  8. {
  9. private class Node
  10. {
  11. private boolean wasVisited;
  12. private char data;
  13.  
  14. public Node(char data)
  15. {
  16. this.data = data;
  17. this.wasVisited= false;
  18. }
  19. }
  20.  
  21. private Node[] nodes;
  22. private int[][] adjMetrix;
  23. private int maxSize;
  24. private int index;
  25. public Graph()
  26. {
  27. this.maxSize = 6;
  28. this.index = 0;
  29. this.nodes= new Node[this.maxSize];
  30. this.adjMetrix = new int[this.maxSize][this.maxSize];
  31. }
  32.  
  33. /**
  34. * 增加节点
  35. * @param data
  36. * @throws Exception
  37. */
  38. public void add(char data) throws Exception
  39. {
  40. if(this.index >= this.maxSize)
  41. {
  42. throw new Exception("数组已满");
  43. }
  44. else
  45. {
  46. this.nodes[index++]= new Node(data);
  47. }
  48. }
  49.  
  50. /**
  51. * 设置图中各个节点关系
  52. * @param x
  53. * @param y
  54. * @throws Exception
  55. */
  56. public void setRelation(int x,int y) throws Exception
  57. {
  58. if(x < this.maxSize && y < this.maxSize)
  59. {
  60. this.adjMetrix[x][y] = 1;
  61. this.adjMetrix[y][x] = 1;
  62. }
  63. else
  64. {
  65. throw new Exception("下标错误!");
  66. }
  67. }
  68.  
  69. /**
  70. * 广度优先对图进行遍历
  71. * @param x
  72. * @throws Exception
  73. */
  74. public void showBreathFirst(int x) throws Exception
  75. {
  76. if(x >= 0 && x < this.index)
  77. {
  78. java.util.List<Integer> list = new java.util.ArrayList<>();
  79. int current = 0;
  80. list.add(x);
  81. this.nodes[list.get(current)].wasVisited = true;
  82. while(current < list.size())
  83. {
  84. System.out.println(this.nodes[list.get(current)].data);
  85.  
  86. int nextSuccessor = this.getNextSuccessor(list.get(current));
  87. while(nextSuccessor != -1)
  88. {
  89. list.add(nextSuccessor);
  90. this.nodes[nextSuccessor].wasVisited = true;
  91. nextSuccessor = this.getNextSuccessor(list.get(current));
  92. }
  93. current++;
  94. }
  95.  
  96. this.resetNodes();
  97. }
  98. else
  99. {
  100. throw new Exception("下标越界");
  101. }
  102. }
  103.  
  104. /**
  105. * 深度优先对图进行遍历
  106. * @param x
  107. * @throws Exception
  108. */
  109. public void showDeepFirst(int x) throws Exception
  110. {
  111. if(x < this.index && x >= 0)
  112. {
  113. Stack<Integer> stack = new Stack<>();
  114. stack.push(x);
  115. System.out.println(this.nodes[x].data);
  116. this.nodes[x].wasVisited = true;
  117. while(!stack.isEmpty())
  118. {
  119. int temp = stack.peek();
  120. int nextSuccessor = this.getNextSuccessor(temp);
  121. if(nextSuccessor == -1)
  122. {
  123. stack.pop();
  124. }
  125. else
  126. {
  127. stack.push(nextSuccessor);
  128. System.out.println(this.nodes[nextSuccessor].data);
  129. this.nodes[nextSuccessor].wasVisited = true;
  130. }
  131. }
  132. this.resetNodes();
  133. }
  134. else
  135. {
  136. throw new Exception("下标错误");
  137. }
  138.  
  139. }
  140.  
  141. /**
  142. * 广度优先-最小生成树
  143. * @param x
  144. * @throws Exception
  145. */
  146. public void toMSTBreathFirst(int x ) throws Exception
  147. {
  148. if(x >= 0 && x < this.index)
  149. {
  150. java.util.List<Integer> list = new java.util.ArrayList();
  151. int current = 0;
  152. list.add(x);
  153. this.nodes[list.get(current)].wasVisited = true;
  154. while(current < this.index)
  155. {
  156. int successor = this.getNextSuccessor(list.get(current));
  157. while(successor != -1)
  158. {
  159. list.add(successor);
  160. System.out.println(this.nodes[list.get(current)].data + "->" + this.nodes[successor].data);
  161. this.nodes[successor].wasVisited = true;
  162. successor = this.getNextSuccessor(list.get(current));
  163. }
  164. current++;
  165. }
  166. }
  167. else
  168. {
  169. throw new Exception("下标错误");
  170. }
  171. }
  172.  
  173. /**
  174. * 深度优先求最小生成树
  175. * @param x
  176. * @throws Exception
  177. */
  178. public void toMSTDeepFirst(int x) throws Exception
  179. {
  180. if(x < this.index && x >=0)
  181. {
  182. Stack<Integer> stack = new Stack<Integer>();
  183. stack.push(x);
  184. this.nodes[x].wasVisited = true;
  185. while(!stack.isEmpty())
  186. {
  187. int current = stack.peek();
  188. int nextSuccessor = this.getNextSuccessor(current);
  189. if(nextSuccessor == -1)
  190. {
  191. stack.pop();
  192. }
  193. else
  194. {
  195. stack.push(nextSuccessor);
  196. this.nodes[nextSuccessor].wasVisited = true;
  197. System.out.print(this.nodes[current].data);
  198. System.out.print("-");
  199. System.out.print(this.nodes[nextSuccessor].data);
  200. System.out.print(" ");
  201. }
  202. }
  203.  
  204. this.resetNodes();
  205. }
  206. else
  207. {
  208. throw new Exception("下标错误");
  209. }
  210. }
  211.  
  212. private int getNextSuccessor(int x)
  213. {
  214. for(int i = 0; i <this.maxSize; i++)
  215. {
  216. if(this.adjMetrix[x][i] != 0 && this.nodes[i].wasVisited == false)
  217. {
  218. return i;
  219. }
  220. }
  221. return -1;
  222. }
  223.  
  224. private void resetNodes()
  225. {
  226. for(int i = 0; i < this.index; i++)
  227. {
  228. this.nodes[i].wasVisited = false;
  229. }
  230. }
  231. }

Graph

图的存储,搜索,遍历,广度优先算法和深度优先算法,最小生成树-Java实现的更多相关文章

  1. 图的存储及遍历 深度遍历和广度遍历 C++代码实现

    /*图的存储及遍历*/ #include<iostream> using namespace std; //----------------------------------- //邻接 ...

  2. 图的存储与遍历C++实现

    1.图的存储 设点数为n,边数为m 1.1.二维数组 方法:使用一个二维数组 adj 来存边,其中 adj[u][v] 为 1 表示存在 u到 v的边,为 0 表示不存在.如果是带边权的图,可以在 a ...

  3. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  4. 数据结构作业——图的存储及遍历(邻接矩阵、邻接表+DFS递归、非递归+BFS)

    邻接矩阵存图 /* * @Author: WZY * @School: HPU * @Date: 2018-11-02 18:35:27 * @Last Modified by: WZY * @Las ...

  5. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  6. C++编程练习(9)----“图的存储结构以及图的遍历“(邻接矩阵、深度优先遍历、广度优先遍历)

    图的存储结构 1)邻接矩阵 用两个数组来表示图,一个一维数组存储图中顶点信息,一个二维数组(邻接矩阵)存储图中边或弧的信息. 2)邻接表 3)十字链表 4)邻接多重表 5)边集数组 本文只用代码实现用 ...

  7. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

  8. 邻接矩阵实现图的存储,DFS,BFS遍历

    图的遍历一般由两者方式:深度优先搜索(DFS),广度优先搜索(BFS),深度优先就是先访问完最深层次的数据元素,而BFS其实就是层次遍历,每一层每一层的遍历. 1.深度优先搜索(DFS) 我一贯习惯有 ...

  9. js图的数据结构处理----邻链表,广度优先搜索,最小路径,深度优先搜索,探索时间拓扑

    //邻居连表 //先加入各顶点,然后加入边 //队列 var Queue = (function(){ var item = new WeakMap(); class Queue{ construct ...

随机推荐

  1. 通用的web系统数据导出功能设计实现(导出excel2003/2007 word pdf zip等)

    前言 我们在做web系统中,导出也是很常用的一个功能,如果每一个数据列表都要对应写一个导出的方法不太现实.现在就想设计一个共通的功能来实现这个导出. 需求分析 在开始之前我们先要明白我们要实现怎样一个 ...

  2. DataTable 导到Excel

    /// <summary> /// 将DataTalbe导出到Excel中 /// </summary> /// <param name="dt"&g ...

  3. iOS开发UI篇—Kvc简单介绍

    ios开发UI篇—Kvc简单介绍 一.KVC简单介绍 KVC key valued coding 键值编码 KVC通过键值间接编码 补充: 与KVC相对的时KVO,即key valued observ ...

  4. 001 今天开始系统学习C#

    2016-01-16 之前只是大概了解过c#语言,感觉掌握不牢靠.现在开始系统学习C#.现以该博客作为学习笔记,方便后续查看.C# 目标:系统掌握c#知识 时间:30天 范围:C#基础,Winform ...

  5. 4、android之actionbar用法

    转: 上:http://blog.csdn.net/yuzhiboyi/article/details/32709833 下:http://blog.csdn.net/yuzhiboyi/articl ...

  6. css3简单介绍

    关于css3我先介绍几个简单的选择器: 先进行设置: 字符串匹配属性选择器: E[alt^="a"]  选择属性中以a开头的元素: E[alt$="a"]  选 ...

  7. BOM和DOM(精简版)

    一.BOM 1.browser object model的缩写,简称浏览器对象模型 2.提供与浏览器窗口进行交互的对象 3.核心对象:window.除此之外还有:history,localtion,n ...

  8. Sublime Text 3安装与使用

    本文是Sublime Text 全程指引 by Lucida (http://www.cnblogs.com/figure9/p/sublime-text-complete-guide.html)的笔 ...

  9. M5: 使用StorageFile

    本小节介绍UWP中的文件操作,使用到了FileOpenPickerAPI(在Windows.Storage.Pickers中).本例中,单击打开文件按钮,然后在图片库中选择照片,将选择的照片用作贺卡背 ...

  10. USB协议-USB设备的枚举过程

    USB主机在检测到USB设备插入后,就要对设备进行枚举了.为什么要枚举?枚举就是从设备读取各种描述符信息,这样主机就可以根据这些信息来加载合适的驱动程序,从而知道设备是什么样的设备,如何进行通信等. ...