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

2025-05-29 0 17

图的概念

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

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

无向图 有向图

图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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java编程无向图结构的存储及DFS操作代码详解 https://www.kuaiidc.com/113555.html

相关文章

发表评论
暂无评论