图的存储,搜索,遍历,广度优先算法和深度优先算法,最小生成树-Java实现
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)广度优先进行图的遍历以及将图转换为最小生成树
广度优先原则是从根节点开始,先将根的所有后继找出来,然后依次将所有后继的后继再找出来。以此类推,直到所有元素节点都被遍历。实现广度优先,是一个顺序过程,所以这里需要用到队列或者链表这种数据结构。
下面是代码实现:
- package org.lyk.impl;
- import java.util.ArrayList;
- import java.util.Queue;
- import java.util.Stack;
- public class Graph
- {
- private class Node
- {
- private boolean wasVisited;
- private char data;
- public Node(char data)
- {
- this.data = data;
- this.wasVisited= false;
- }
- }
- private Node[] nodes;
- private int[][] adjMetrix;
- private int maxSize;
- private int index;
- public Graph()
- {
- this.maxSize = 6;
- this.index = 0;
- this.nodes= new Node[this.maxSize];
- this.adjMetrix = new int[this.maxSize][this.maxSize];
- }
- /**
- * 增加节点
- * @param data
- * @throws Exception
- */
- public void add(char data) throws Exception
- {
- if(this.index >= this.maxSize)
- {
- throw new Exception("数组已满");
- }
- else
- {
- this.nodes[index++]= new Node(data);
- }
- }
- /**
- * 设置图中各个节点关系
- * @param x
- * @param y
- * @throws Exception
- */
- public void setRelation(int x,int y) throws Exception
- {
- if(x < this.maxSize && y < this.maxSize)
- {
- this.adjMetrix[x][y] = 1;
- this.adjMetrix[y][x] = 1;
- }
- else
- {
- throw new Exception("下标错误!");
- }
- }
- /**
- * 广度优先对图进行遍历
- * @param x
- * @throws Exception
- */
- public void showBreathFirst(int x) throws Exception
- {
- if(x >= 0 && x < this.index)
- {
- java.util.List<Integer> list = new java.util.ArrayList<>();
- int current = 0;
- list.add(x);
- this.nodes[list.get(current)].wasVisited = true;
- while(current < list.size())
- {
- System.out.println(this.nodes[list.get(current)].data);
- int nextSuccessor = this.getNextSuccessor(list.get(current));
- while(nextSuccessor != -1)
- {
- list.add(nextSuccessor);
- this.nodes[nextSuccessor].wasVisited = true;
- nextSuccessor = this.getNextSuccessor(list.get(current));
- }
- current++;
- }
- this.resetNodes();
- }
- else
- {
- throw new Exception("下标越界");
- }
- }
- /**
- * 深度优先对图进行遍历
- * @param x
- * @throws Exception
- */
- public void showDeepFirst(int x) throws Exception
- {
- if(x < this.index && x >= 0)
- {
- Stack<Integer> stack = new Stack<>();
- stack.push(x);
- System.out.println(this.nodes[x].data);
- this.nodes[x].wasVisited = true;
- while(!stack.isEmpty())
- {
- int temp = stack.peek();
- int nextSuccessor = this.getNextSuccessor(temp);
- if(nextSuccessor == -1)
- {
- stack.pop();
- }
- else
- {
- stack.push(nextSuccessor);
- System.out.println(this.nodes[nextSuccessor].data);
- this.nodes[nextSuccessor].wasVisited = true;
- }
- }
- this.resetNodes();
- }
- else
- {
- throw new Exception("下标错误");
- }
- }
- /**
- * 广度优先-最小生成树
- * @param x
- * @throws Exception
- */
- public void toMSTBreathFirst(int x ) throws Exception
- {
- if(x >= 0 && x < this.index)
- {
- java.util.List<Integer> list = new java.util.ArrayList();
- int current = 0;
- list.add(x);
- this.nodes[list.get(current)].wasVisited = true;
- while(current < this.index)
- {
- int successor = this.getNextSuccessor(list.get(current));
- while(successor != -1)
- {
- list.add(successor);
- System.out.println(this.nodes[list.get(current)].data + "->" + this.nodes[successor].data);
- this.nodes[successor].wasVisited = true;
- successor = this.getNextSuccessor(list.get(current));
- }
- current++;
- }
- }
- else
- {
- throw new Exception("下标错误");
- }
- }
- /**
- * 深度优先求最小生成树
- * @param x
- * @throws Exception
- */
- public void toMSTDeepFirst(int x) throws Exception
- {
- if(x < this.index && x >=0)
- {
- Stack<Integer> stack = new Stack<Integer>();
- stack.push(x);
- this.nodes[x].wasVisited = true;
- while(!stack.isEmpty())
- {
- int current = stack.peek();
- int nextSuccessor = this.getNextSuccessor(current);
- if(nextSuccessor == -1)
- {
- stack.pop();
- }
- else
- {
- stack.push(nextSuccessor);
- this.nodes[nextSuccessor].wasVisited = true;
- System.out.print(this.nodes[current].data);
- System.out.print("-");
- System.out.print(this.nodes[nextSuccessor].data);
- System.out.print(" ");
- }
- }
- this.resetNodes();
- }
- else
- {
- throw new Exception("下标错误");
- }
- }
- private int getNextSuccessor(int x)
- {
- for(int i = 0; i <this.maxSize; i++)
- {
- if(this.adjMetrix[x][i] != 0 && this.nodes[i].wasVisited == false)
- {
- return i;
- }
- }
- return -1;
- }
- private void resetNodes()
- {
- for(int i = 0; i < this.index; i++)
- {
- this.nodes[i].wasVisited = false;
- }
- }
- }
Graph
图的存储,搜索,遍历,广度优先算法和深度优先算法,最小生成树-Java实现的更多相关文章
- 图的存储及遍历 深度遍历和广度遍历 C++代码实现
/*图的存储及遍历*/ #include<iostream> using namespace std; //----------------------------------- //邻接 ...
- 图的存储与遍历C++实现
1.图的存储 设点数为n,边数为m 1.1.二维数组 方法:使用一个二维数组 adj 来存边,其中 adj[u][v] 为 1 表示存在 u到 v的边,为 0 表示不存在.如果是带边权的图,可以在 a ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 数据结构作业——图的存储及遍历(邻接矩阵、邻接表+DFS递归、非递归+BFS)
邻接矩阵存图 /* * @Author: WZY * @School: HPU * @Date: 2018-11-02 18:35:27 * @Last Modified by: WZY * @Las ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- C++编程练习(9)----“图的存储结构以及图的遍历“(邻接矩阵、深度优先遍历、广度优先遍历)
图的存储结构 1)邻接矩阵 用两个数组来表示图,一个一维数组存储图中顶点信息,一个二维数组(邻接矩阵)存储图中边或弧的信息. 2)邻接表 3)十字链表 4)邻接多重表 5)边集数组 本文只用代码实现用 ...
- 【算法导论】图的深度优先搜索遍历(DFS)
关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...
- 邻接矩阵实现图的存储,DFS,BFS遍历
图的遍历一般由两者方式:深度优先搜索(DFS),广度优先搜索(BFS),深度优先就是先访问完最深层次的数据元素,而BFS其实就是层次遍历,每一层每一层的遍历. 1.深度优先搜索(DFS) 我一贯习惯有 ...
- js图的数据结构处理----邻链表,广度优先搜索,最小路径,深度优先搜索,探索时间拓扑
//邻居连表 //先加入各顶点,然后加入边 //队列 var Queue = (function(){ var item = new WeakMap(); class Queue{ construct ...
随机推荐
- 通用的web系统数据导出功能设计实现(导出excel2003/2007 word pdf zip等)
前言 我们在做web系统中,导出也是很常用的一个功能,如果每一个数据列表都要对应写一个导出的方法不太现实.现在就想设计一个共通的功能来实现这个导出. 需求分析 在开始之前我们先要明白我们要实现怎样一个 ...
- DataTable 导到Excel
/// <summary> /// 将DataTalbe导出到Excel中 /// </summary> /// <param name="dt"&g ...
- iOS开发UI篇—Kvc简单介绍
ios开发UI篇—Kvc简单介绍 一.KVC简单介绍 KVC key valued coding 键值编码 KVC通过键值间接编码 补充: 与KVC相对的时KVO,即key valued observ ...
- 001 今天开始系统学习C#
2016-01-16 之前只是大概了解过c#语言,感觉掌握不牢靠.现在开始系统学习C#.现以该博客作为学习笔记,方便后续查看.C# 目标:系统掌握c#知识 时间:30天 范围:C#基础,Winform ...
- 4、android之actionbar用法
转: 上:http://blog.csdn.net/yuzhiboyi/article/details/32709833 下:http://blog.csdn.net/yuzhiboyi/articl ...
- css3简单介绍
关于css3我先介绍几个简单的选择器: 先进行设置: 字符串匹配属性选择器: E[alt^="a"] 选择属性中以a开头的元素: E[alt$="a"] 选 ...
- BOM和DOM(精简版)
一.BOM 1.browser object model的缩写,简称浏览器对象模型 2.提供与浏览器窗口进行交互的对象 3.核心对象:window.除此之外还有:history,localtion,n ...
- Sublime Text 3安装与使用
本文是Sublime Text 全程指引 by Lucida (http://www.cnblogs.com/figure9/p/sublime-text-complete-guide.html)的笔 ...
- M5: 使用StorageFile
本小节介绍UWP中的文件操作,使用到了FileOpenPickerAPI(在Windows.Storage.Pickers中).本例中,单击打开文件按钮,然后在图片库中选择照片,将选择的照片用作贺卡背 ...
- USB协议-USB设备的枚举过程
USB主机在检测到USB设备插入后,就要对设备进行枚举了.为什么要枚举?枚举就是从设备读取各种描述符信息,这样主机就可以根据这些信息来加载合适的驱动程序,从而知道设备是什么样的设备,如何进行通信等. ...