1、顺序表用于图的深度优先遍历

public class SeqList {

    public final int MaxSize = 10;

    public Object list[];
public int size; /**
* 初始化
*/
public SeqList() {
list = new Object[MaxSize];
this.size = 0;
} /* public SeqList initSeqList(SeqList seqList) {
seqList.list = new Object[MaxSize];
seqList.size = 0;
}*/ public boolean isFull(SeqList list) {
if (list.size >= MaxSize) {
return true;
}
return false;
} public boolean isEmpty(SeqList list) {
if (list.size <= 0) {
return true;
}
return false;
} public void insertList(SeqList seqList, int i, Object data) {
if (isFull(seqList)) {
System.out.println("已满无法插入");
return;
} else if (i < 0 || i > seqList.size) {
System.out.println("您输入的位置有问题");
return;
} for (int j = seqList.size; j > i; j--) {
seqList.list[j] = seqList.list[j - 1];
} seqList.list[i] = data;
seqList.size++;
} public void deleteList(SeqList seqList, int i) {
if (isEmpty(seqList)) {
System.out.println("已空,没有元素可删除");
return;
} else if (i < 0 || i >= seqList.size) {
System.out.println("您输入的位置参数有问题");
return;
} for (int j = i+1; j <= seqList.size - 1; j++) {
seqList.list[j-1] = seqList.list[j];
}
seqList.size--;
} public int getSize(SeqList seqList) {
return seqList.size;
} public Object getData(SeqList seqList, int i) {
if (isEmpty(seqList)){
System.out.println("已空没有可取元素");
return null;
}else if(i<0 || i>= seqList.size){
System.out.println("您给的位置有问题");
return null;
} return seqList.list[i];
} public void printf(SeqList seqList) {
if (isEmpty(seqList)){
System.out.println("已空,无需遍历");
return;
} for (int i = 0; i < seqList.size; i++) {
System.out.print(seqList.list[i] + " ");
}
} public static void main(String[] args) {
SeqList seqList = new SeqList(); System.out.println("元素个数: "+ seqList.getSize(seqList));
seqList.printf(seqList);
for (int i = 0; i < seqList.MaxSize; i++) {
seqList.insertList(seqList,i,i);
} seqList.deleteList(seqList,0); seqList.insertList(seqList,0,10);
System.out.println("元素个数: "+ seqList.getSize(seqList));
seqList.printf(seqList); } }

2、创建顺序队列用户广度优先遍历

public class SeqQueue {

    public final int MaxSize = 8;

    public Object seqqueue[];
public int front; // 队头
public int rear;
public int size; public SeqQueue() {
this.size = 0;
this.rear = 0;
this.front = 0;
this.seqqueue = new Object[MaxSize];
} public boolean isFull(SeqQueue seqQueue) {
if (seqQueue.size > 0 && seqQueue.rear == seqQueue.front) {
return true;
}
return false;
} public boolean isEmpty(SeqQueue seqQueue) {
if (seqQueue.size <= 0) {
return true;
}
return false;
} public void queueAppend(SeqQueue seqQueue, Object data) {
if (isFull(seqQueue)) {
System.out.println("已满无法插入");
return;
}
seqQueue.seqqueue[seqQueue.rear] = data;
seqQueue.rear = (seqQueue.rear + 1) % MaxSize;
seqQueue.size++;
} public Object queueDelete(SeqQueue seqQueue) {
if (isEmpty(seqQueue)) {
System.out.println("已空");
return null;
}
Object x = seqQueue.seqqueue[seqQueue.front]; seqQueue.front = (seqQueue.front + 1) % MaxSize;
seqQueue.size--;
return x;
} public static void main(String[] args) {
SeqQueue seqQueue = new SeqQueue();
seqQueue.queueDelete(seqQueue); for (int i = 0; i < 9; i++) {
seqQueue.queueAppend(seqQueue, i);
} for (int i = 0; i < 8; i++) {
System.out.println( seqQueue.queueDelete(seqQueue) + " ");
;
} }
}

3、创建需要插入的图信息类

public class CreateE {
public int row; //行下标
public int col; //列下标
public int weight; // 权重 public CreateE() {
} public CreateE(int row, int col, int weight) {
this.row = row;
this.col = col;
this.weight = weight;
}
}

4、图的实现

/**
* 图的邻接矩阵实现
* —— Wij (vi,vj)或<vi,vj>
* |
* aij = —— 无穷 i != j
* |
* —— 0 i = j
*/
public class Graph { public final int MaxWeight = 1000; //定义为无穷大(用于存储)
public final int MaxVertices = 10; //顶点的最大值 SeqList vertices; //存放顶点的顺序表
int edge[][]; //存放边的邻接矩阵
int numberedge; //边的条数 public Graph() {
edge = new int[MaxVertices][MaxVertices]; //初始化边的最大数组(这个和顺序表差不多)
} /**
* @param graph :要初始化的图
* @param n :给图分配几个顶点
*/
public Graph initGraph(Graph graph, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
graph.edge[i][j] = 0; //对角线全为0
} else {
graph.edge[i][j] = MaxWeight; //无穷大
}
}
} graph.numberedge = 0; //初始边条数0
graph.vertices = new SeqList(); //顶点顺序表初始化 return graph;
} /**
* 插入顶点
*
* @param graph :需要插入顶点的图
* @param vertex :插入的顶点值
*/
public void insertVertex(Graph graph, Object vertex) {
graph.vertices.insertList(graph.vertices, graph.vertices.size, vertex);
} /**
* 插入边
*
* @param graph : 需要插入边的图
* @param vi :边的一个顶点
* @param vj :边的另一顶点
* @param weight :边上的权重
*/
public void insertEdge(Graph graph, int vi, int vj, int weight) {
if (vi < 0 || vi >= graph.vertices.size || vj < 0 || vj >= graph.vertices.size) {
System.out.println("参数vi,vj越界");
return;
}
graph.edge[vi][vj] = weight;
graph.numberedge++;
} /**
* 删除边
* @param graph : 需要处理的图
* @param vi :顶点i
* @param vj : 顶点j
*/
public void deleteEdge(Graph graph,int vi,int vj) {
if (vi < 0 || vi >= graph.vertices.size || vj < 0 || vj >= graph.vertices.size) {
System.out.println("参数vi,vj越界");
return;
}else if(graph.edge[vi][vj] == MaxWeight || vi == vj){
System.out.println("边不存在");
return;
}
graph.edge[vi][vj] = MaxWeight;
graph.numberedge--;
} /**
* 创建图
* @param graph :要创建的图
* @param V :顶点
* @param n :d顶点个数
* @param E :边
* @param e :边的个数
*/
public void CreateGraph(Graph graph,Object V[],int n,CreateE E[],int e) {
for (int i = 0; i < n; i++) {
graph.insertVertex(graph,V[i]);
}
for (int i = 0; i < e; i++) {
graph.insertEdge(graph,E[i].row,E[i].col,E[i].weight);
}
} /**
* 获取图的边的条数
* @param graph : 需要操作的图
*/
public void getNumberEdge(Graph graph) {
if (graph == null){
System.out.println("该图不存在");
return;
}
System.out.println("边的条数: " + graph.numberedge);
} /**
* 取第一个邻接顶点
* @param graph :将要操作的图
* @param v : 某个顶点开始的第一个邻接顶点
* @return :找到返回邻接顶点下标,找不到反回-1,错误返回-1
*/
public int getFirstVex(Graph graph, int v) {
if (v < 0 || v >= graph.vertices.size) {
System.out.println("获取第一个邻接顶点参数有问题");
return -1;
}
for (int col = 0; col < graph.vertices.size; col++) {
if (graph.edge[v][col] > 0 && graph.edge[v][col] < MaxWeight){ //找到本顶点的二位数组中大与0小于无穷的第一个值,就是第一个邻接顶点
return col;
}
}
return -1;
} /**
* 获取下一连接顶点
* @param graph :需要操作的图
* @param v1 :第一个顶点
* @param v2 :第一个顶点的邻接顶点
*/
public int getNextVex(Graph graph,int v1,int v2) {
if (v1 <0 || v1 >= graph.vertices.size || v2 <0 || v2 >= graph.vertices.size){
System.out.println("您要获取的下一邻接顶点参数有问题");
return -1;
}
for (int col = v2 + 1; col < graph.vertices.size; col++) {
if (graph.edge[v1][col] >0 && graph.edge[v1][col] < MaxWeight){
return col;
}
}
return -1;
} /**
* 连通图的深度优先遍历
* @param graph 需要操作的图
* @param v : 以某个顶点开始遍历
* @param visited :改点是否被访问
*/
public void DepthSearch(Graph graph,int v,int visited[]) {
System.out.print(graph.vertices.list[v] + " "); //先打印第一个访问的顶点
visited[v] = 1 ; //让改点为已经访问过 1 :访问过 0 : 未访问 int col = graph.getFirstVex(graph,v); //获取访问顶点的下一顶点 while (col != -1){ //如果该节点存在
if (visited[col] == 0){
graph.DepthSearch(graph,col,visited);
}
col = graph.getNextVex(graph,v,col);
}
} /**
* 非连通图的深度优先遍历
*/
public void DepthFirstSearch(Graph graph) {
int visited[] = new int[graph.vertices.size];
for (int i = 0; i < graph.vertices.size; i++) {
visited[i] = 0; //未访问标记初始值为0
}
for (int i = 0; i < graph.vertices.size; i++) {
if (visited[i] == 0){
graph.DepthSearch(graph,i,visited);
}
}
} /**
* 连通图的广度优先遍历
* @param graph
* @param v
* @param visited
*/
public void BroadSearch(Graph graph,int v,int visited[]) {
SeqQueue seqQueue = new SeqQueue();
System.out.print(graph.vertices.list[v]+" ");
visited[v] = 1;
seqQueue.queueAppend(seqQueue,v); //初始顶点入队
while (!seqQueue.isEmpty(seqQueue)){ //队列未空
int n = (int)seqQueue.queueDelete(seqQueue);
int col = graph.getFirstVex(graph,n);
while (col != -1){
if (visited[col] == 0){
System.out.print(graph.vertices.list[col] + " ");
visited[col] = 1; //设为已访问
seqQueue.queueAppend(seqQueue,col); //邻接顶点入队
}
col = graph.getNextVex(graph,n,col);
} }
} public void BroadFirstSearch(Graph graph) {
int visited[] = new int[graph.vertices.size];
for (int i = 0; i < graph.vertices.size; i++) {
visited[i] = 0; //访问标记初始为0
}
for (int i = 0; i < graph.vertices.size; i++) {
if (visited[i] == 0){
BroadSearch(graph,i,visited);
}
}
} public static void main(String[] args) {
Graph graph = new Graph();
int n = 6,e=6;
graph.initGraph(graph,n); Object V[] = {'A','B','C','D','E','F'}; CreateE E[] = {new CreateE(0,1,10),new CreateE(0,4,20),new CreateE(1,3,30),new CreateE(2,1,40),new CreateE(3,2,50),new CreateE(0,5,30)}; graph.CreateGraph(graph,V,n,E,e); System.out.print("顶点集合:");
for (int i = 0; i < graph.vertices.size; i++) {
System.out.print(graph.vertices.list[i]+ " ");
}
System.out.println(); System.out.println("权值集合");
for (int i = 0; i < graph.vertices.size; i++) {
for (int j = 0; j < graph.vertices.size; j++) {
System.out.print(graph.edge[i][j]+"\t\t");
}
System.out.println();
}
graph.getNumberEdge(graph); System.out.println("取第一个邻接顶点 : " + graph.vertices.list[graph.getFirstVex(graph,0)]); //这里取不到就会报错哦。因为取不到,我这设置返回-1
System.out.println("取下一个邻接顶点 : " +graph.vertices.list[graph.getNextVex(graph,0,graph.getFirstVex(graph,0))]); System.out.print("图的深度优先遍历 :"); graph.DepthFirstSearch(graph);
System.out.println(); System.out.print("图的广度优先遍历 :"); graph.BroadFirstSearch(graph);
System.out.println();
graph.deleteEdge(graph,0,1);
graph.getNumberEdge(graph);
System.out.println("权值集合");
for (int i = 0; i < graph.vertices.size; i++) {
for (int j = 0; j < graph.vertices.size; j++) {
System.out.print(graph.edge[i][j]+"\t\t");
}
System.out.println();
} } }

5、实现结果

顶点集合:A B C D E F
权值集合
0 10 1000 1000 20 30
1000 0 1000 30 1000 1000
1000 40 0 1000 1000 1000
1000 1000 50 0 1000 1000
1000 1000 1000 1000 0 1000
1000 1000 1000 1000 1000 0
边的条数: 6
取第一个邻接顶点 : B
取下一个邻接顶点 : E
图的深度优先遍历 :A B D C E F
图的广度优先遍历 :A B E F D C
边的条数: 5
权值集合
0 1000 1000 1000 20 30
1000 0 1000 30 1000 1000
1000 40 0 1000 1000 1000
1000 1000 50 0 1000 1000
1000 1000 1000 1000 0 1000
1000 1000 1000 1000 1000 0

7、创建图及图的遍历(java实现)的更多相关文章

  1. 数据结构代码整理(线性表,栈,队列,串,二叉树,图的建立和遍历stl,最小生成树prim算法)。。持续更新中。。。

    //归并排序递归方法实现 #include <iostream> #include <cstdio> using namespace std; #define maxn 100 ...

  2. graph-tool文档(一)- 快速开始使用Graph-tool - 1.创建和操纵图

    目录: 快速开始使用graph-tool 创建和操纵图 -- 遍历顶点和边 ----- 遍历所有顶点或边 ----- 遍历一个顶点的neighbourhood 名词解释: instante:实例 di ...

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

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

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

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

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

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

  6. cocos2d-x创建的九宫图变白块

    用UIImageView 创建的九宫图变白,直接用CCScale9Sprite创建的也是变白,找了半天原来是自己为了调整UI方便,开启了CCSprite边缘画线导致的,在ccConfig.h下 宏CC ...

  7. 如何在Ubuntu 16.04中创建GIF动图

    导读 FFmpeg 是一款开源的音.视转换器,使用 FFmpeg 我们可以非常容易地转换和录制音视频文件,而 ImageMagick 是一款用于创建.编辑和合并位图图像的一款开源软件. 大家经常在新浪 ...

  8. UML建模—EA创建Class(类图)

    1.新建类图 2.添加类或接口 在类图可以捕获系统-类-和模型组件的逻辑结构.它是一个静态模型,描述存在什么,有哪些属性和行为,而不管如何去做. 说明关系之间的类和接口; 泛化. 聚合和关联是在分别反 ...

  9. 如何合理利用iMindMap中的模板创建思维导图

    思维导图的制作并不是一项简单的工作,尤其是对许多工作或学习有特殊要求的朋友而言,当我们需要应对不同场景制作不同的思维导图时,总不能都靠自己从头制作,这样难度比较大也比较耗时.而iMindMap(win ...

随机推荐

  1. java高并发系列 - 第25天:掌握JUC中的阻塞队列

    这是java高并发系列第25篇文章. 环境:jdk1.8. 本文内容 掌握Queue.BlockingQueue接口中常用的方法 介绍6中阻塞队列,及相关场景示例 重点掌握4种常用的阻塞队列 Queu ...

  2. ZooKeeper系列(五)—— ACL 权限控制

    一.前言 为了避免存储在 Zookeeper 上的数据被其他程序或者人为误修改,Zookeeper 提供了 ACL(Access Control Lists) 进行权限控制.只有拥有对应权限的用户才可 ...

  3. django前后端分离部署

    部署静态文件: 静态文件有两种方式1:通过django路由访问2:通过nginx直接访问 方式1: 需要在根目录的URL文件中增加,作为入口 url(r'^$', TemplateView.as_vi ...

  4. 构建docker虚拟化平台

    安装epel-release扩展包 yum install epel-release -y 安装docker yum install docker-ce 启动docker systemctl star ...

  5. FastStone Capture(FSCapture) 注册码

    FastStone Capture 是一款极好用的图像浏览.编辑和截屏工具,支持 BMP.JPG.JPEG.GIF.PNG.TIFF.WMF.ICO 和 TGA 在内的主流图片格式,其独有的光滑和毛刺 ...

  6. C++函数中,两个自动释放内存的动态内存申请类

    最近做一个事情,实现一个流程交互,其中主交互流程函数中,涉及较多的内存申请, 而健康的函数,都是在函数退出前将手动申请不再需要的内存释放掉, 使用很多方法,都避免不了较多的出错分支时,一堆的if fr ...

  7. AVL树(二叉平衡树)详解与实现

    AVL树概念 前面学习二叉查找树和二叉树的各种遍历,但是其查找效率不稳定(斜树),而二叉平衡树的用途更多.查找相比稳定很多.(欢迎关注数据结构专栏) AVL树是带有平衡条件的二叉查找树.这个平衡条件必 ...

  8. Nacos(七):Nacos共享配置

    前言 本文参考文章: SpringCloud Alibaba - Nacos Config 自定义共享配置 前景回顾: Nacos(六):多环境下如何"管理"及"隔离&q ...

  9. 最近学习了JDK SPI

    JDK SPI是什么 最近工作中听几个同事说了好几次SPI这个名词,虽然和我没关系,但是心里默默想还是学习一下,不然下次和我说到SPI,连是什么都不知道那就尴尬了. 所以SPI是什么呢?SPI全称Se ...

  10. [开源] FreeSql.AdminLTE 功能升级

    前言 FreeSql 发布至今已经有9个月,功能渐渐完善,自身的生态也逐步形成,早在几个月前写过一篇文章<ORM 开发环境之利器:MVC 中间件 FreeSql.AdminLTE>,您可以 ...