源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

java编程无向图结构的存储及DFS操作代码详解

  • 时间:2020-01-13 09:42 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:java编程无向图结构的存储及DFS操作代码详解
[b]图的概念[/b] 图是算法中是树的拓展,树是从上向下的数据结构,结点都有一个父结点(根结点除外),从上向下排列。而图没有了父子结点的概念,图中的结点都是平等关系,结果更加复杂。 [img]http://files.jb51.net/file_images/article/201712/2017128101700188.png?201711810179[/img] [img]http://files.jb51.net/file_images/article/201712/2017128101739988.png?2017118101749[/img] 无向图                                                       有向图 图G=(V,E),其中V代表顶点Vertex,E代表边edge,一条边就是一个定点对(u,v),其中(u,v)∈V。 这两天遇到一个关于图的算法,在网上找了很久没有找到java版的关于数据结构中图的存储及其相关操作。于是找了一本java版的数据结构书看了一下,以下是根据书上的讲解整理的一个关于无向图的存储和对图的深度优先遍历。不过这个遍历只能遍历连通图,要想遍历非连通图,还需要修改。在这里分享一下代码希望对有需要的人有帮助。
package com.homework;
/** 
 * 定义栈类 
 */
class StackX{
 private final int size = 20;
 private int[] st;
 private int top;
 //初始化栈 
 public StackX(){
  st = new int[size];
  top = -1;
 }
 //进栈 
 public void push(int j){
  st[++top] = j;
 }
 //出栈 
 public int pop(){
  return st[top--];
 }
 //返回栈顶元素 
 public int peak(){
  return st[top];
 }
 //判断栈是否为空 
 public Boolean isEmpty(){
  return (top==-1);
 }
}
/** 
 * 定义图中的节点类 
 * @author Administrator 
 * 
 */
class Vertex{
 public char label;
 public Boolean wasVisited;
 public Vertex(char lab){
  label = lab;
  wasVisited = false;
 }
}
/** 
 * 定义图类 
 * @author Administrator 
 * 
 */
class Graph{
 private final int num = 20;
 private Vertex vertexList[];
 //图中节点数组 
 private int adjMat[][];
 //节点矩阵 
 private int nVerts;
 //当前节点数 
 private StackX theStack;
 //定义一个栈 
 //初始化图的结构 
 public Graph(){
  vertexList = new Vertex[num];
  adjMat = new int[num][num];
  nVerts = 0;
  for (int i=0; i<num; i++){
   for (int j=0; j<num; j++) 
           adjMat[i][j] = 0;
  }
 }
 //添加节点 
 public void addVertex(char lab){
  vertexList[nVerts++] = new Vertex(lab);
 }
 //添加某两个节点之间的边 
 public void addEdge(int start,int end){
  adjMat[start][end] = 1;
  adjMat[end][start] = 1;
 }
 //输出某个节点 
 public void displayVertex(int v){
  System.out.print(vertexList[v].label);
 }
 //获取未被访问的几点 
 public int getAdjUnvisitedVertex(int v){
  for (int j=0; j<nVerts; j++){
   if(adjMat[v][j]==1 && vertexList[j].wasVisited==false) 
           return j;
  }
  return -1;
 }
 //深度优先遍历(DFS) 
 public void dfs(){
  vertexList[0].wasVisited=true;
  displayVertex(0);
  theStack= new StackX();
  theStack.push(0);
  while(!theStack.isEmpty()){
   int v = getAdjUnvisitedVertex(theStack.peak());
   if(v==-1)//若不存在该节点 
   theStack.pop(); else 
         {
    vertexList[v].wasVisited = true;
    displayVertex(v);
    theStack.push(v);
   }
  }
  for (int j=0; j<nVerts; j++) 
        vertexList[j].wasVisited = false;
 }
}
public class GraphConnect {
 public static void main(String[] args){
  {
   Graph theGraph = new Graph();
   theGraph.addVertex('A');
   theGraph.addVertex('B');
   theGraph.addVertex('C');
   theGraph.addVertex('D');
   theGraph.addVertex('E');
   theGraph.addEdge(0, 1);
   //AB 
   theGraph.addEdge(1, 2);
   //BC 
   theGraph.addEdge(0, 3);
   //AD 
   theGraph.addEdge(3, 4);
   //DE 
   theGraph.addEdge(2, 4);
   //CE 
   System.out.print("The order visited:");
   theGraph.dfs();
   System.out.println();
  }
 }
}
程序运行的结果:
The order visited:ABCED
[b]总结[/b] 以上就是本文关于java编程无向图结构的存储及DFS操作代码详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站: [url=http://www.1sucai.cn/article/130072.htm][b]Java编程实现基于图的深度优先搜索和广度优先搜索完整代码[/b][/url] [url=http://www.1sucai.cn/article/130070.htm][b]深度优先与广度优先Java实现代码示例[/b][/url] [url=http://www.1sucai.cn/article/130005.htm][b]java编程两种树形菜单结构的转换代码[/b][/url] 如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部