深度广度搜索

前端之家收集整理的这篇文章主要介绍了深度广度搜索前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<table class="text"><tr class="li1">
<td class="ln"><pre class="de1">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
135
136
137
138
139

删除结点     public void deleteEdge(int v1,int v2) {         edges[v1][v2]=0;         numOfEdges--;     }       //得到第一个邻接结点的下标     public int getFirstNeighbor(int index) {         for(int j=0;j0) {                 return j;             }         }         return -1;     }       //根据前一个邻接结点的下标来取得下一个邻接结点     public int getNextNeighbor(int v1,int v2) {         for (int j=v2+1;j0) {                 return j;             }         }         return -1;     }         //私有函数,深度优先遍历     private void depthFirstSearch(boolean[] isVisited,int  i) {         //首先访问该结点,在控制台打印出来         System.out.print(getValueByIndex(i)+"  ");         //置该结点为已访问         isVisited[i]=true;                 int w=getFirstNeighbor(i);//         while (w!=-1) {             if (!isVisited[w]) {                 depthFirstSearch(isVisited,w);             }             w=getNextNeighbor(i,w);         }     }         //对外公开函数,深度优先遍历,与其同名私有函数属于方法重载     public void depthFirstSearch() {         for(int i=0;i函数,广度优先遍历     private void broadFirstSearch(boolean[] isVisited,int i) {         int u,w;         LinkedList queue=new LinkedList();                 //访问结点i         System.out.print(getValueByIndex(i)+"  ");         isVisited[i]=true;         //结点入队列         queue.addlast(i);         while (!queue.isEmpty()) {             u=((Integer)queue.removeFirst()).intValue();             w=getFirstNeighbor(u);             while(w!=-1) {                 if(!isVisited[w]) {                         //访问该结点                         System.out.print(getValueByIndex(w)+"  ");                         //标记已被访问                         isVisited[w]=true;                         //入队列                         queue.addLast(w);                 }                 //寻找下一个邻接结点                 w=getNextNeighbor(u,w);             }         }     }         //对外公开函数,广度优先遍历     public void broadFirstSearch() {         for(int i=0;i

猜你在找的程序笔记相关文章