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实现ZooKeeper的zNode监控

    上一篇文章已经完成了ZooKeeper的基本搭建和使用的介绍,现在开始用代码说话.参考 https://zookeeper.apache.org/doc/current/javaExample.htm ...

  2. Oracle中的字符函数

    Oracle中常用的字符串函数有以下几种: 1.upper()---将字符串的内容全部转换为大写.lower()---将字符串的内容全部转换为小写.具体用法: select  upper('test' ...

  3. 建立apk定时自动打包系统第二篇——自动上传文件

    在<建立apk定时自动打包系统第一篇——Ant多渠道打包并指定打包目录和打包日期>这篇文章中介绍多渠道打包的流程.很多时候我们需要将打包好的apk上传到ftp中,这时候我可以修改custo ...

  4. springboot整合html时的页面的跳转404

    在用springboot对html的页面进行渲染时,页面找不到报404(type=Not Found, status=404)., 解决办法:是在ctroller层加相应的           @Re ...

  5. idea中pom如何加载jar包依赖

    1.需求分析    在特定需求的情况下,idea需要加载jar包,那么如何在idea中正确的配置jar依赖呢?今天博主就这个问题给大伙讲解下,希望对大伙有所帮助 2.实现方案①在工程src目录下新建l ...

  6. iOS 视图渲染数据转CVPixelBuffer

    近两年一直从事视频行业的开发, 加班也比较严重, 好久没有写文章了, 最近稍微有些时间, 前来写点文章, 记录一些开发中遇到的问题, 和解决方法! 做视频会议项目, 当然是离不开音视频啦, 也常常和W ...

  7. 用代码说话:如何在Java中实现线程

    并发编程是Java语言的重要特性之一,"如何在Java中实现线程"是学习并发编程的入门知识,也是Java工程师面试必备的基础知识.本文从线程说起,然后用代码说明如何在Java中实现 ...

  8. UVA 10699 Count the factors 题解

    Time limit 3000 ms OS Linux Write a program, that computes the number of different prime factors in ...

  9. unity之初级必备知识

    C#中有两种常见类型:值类型,引用类型.值类型存放在内存中栈里,引用类型在内存中栈里存放引用,实际存放在内存中的堆里.值类型继承自System.ValueType.System.ValueType继承 ...

  10. 【朝花夕拾】Handler篇(二)

    前言 一年前写过一篇文章[朝花夕拾]Handler篇,随着这一年来对Handler更多的认识和理解,本文对Handler知识点做的一些补充. 一.为什么要引入Handler Handler的主要作用是 ...