当前位置: 移动技术网 > IT编程>开发语言>Java > java编程无向图结构的存储及DFS操作代码详解

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

图的概念

图是算法中是树的拓展,树是从上向下的数据结构,结点都有一个父结点(根结点除外),从上向下排列。而图没有了父子结点的概念,图中的结点都是平等关系,结果更加复杂。

无向图                                                       有向图

图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

总结

以上就是本文关于java编程无向图结构的存储及dfs操作代码详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

java编程实现基于图的深度优先搜索和广度优先搜索完整代码

深度优先与广度优先java实现代码示例

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网