图的概念
图是算法中是树的拓展,树是从上向下的数据结构,结点都有一个父结点(根结点除外),从上向下排列。而图没有了父子结点的概念,图中的结点都是平等关系,结果更加复杂。
无向图 有向图
图g=(v,e),其中v代表顶点vertex,e代表边edge,一条边就是一个定点对(u,v),其中(u,v)∈v。
这两天遇到一个关于图的算法,在网上找了很久没有找到java版的关于数据结构中图的存储及其相关操作。于是找了一本java版的数据结构书看了一下,以下是根据书上的讲解整理的一个关于无向图的存储和对图的深度优先遍历。不过这个遍历只能遍历连通图,要想遍历非连通图,还需要修改。在这里分享一下代码希望对有需要的人有帮助。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
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();
}
}
}
|
程序运行的结果:
?
1
|
the order visited:abced
|
总结
以上就是本文关于java编程无向图结构的存储及dfs操作代码详解的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
原文链接:http://blog.csdn.net/sober_123/article/details/49716961
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 17
-
2025-05-29 45
-
2025-05-27 101
-
2025-05-27 75
-
2025-06-04 58
热门评论