用JAVA实现深度优先搜索

来源:互联网 发布:外国人发现淘宝 编辑:程序博客网 时间:2024/06/06 23:17

深度优先与广度优先搜索代码实现很相似,前者是利用了栈这种数据结构;而后者是利用了队列这种数据结构。下面看看代码是怎么实现的

/*****************************深度优先***********************************/

     基本思路:   1、找到初始结点,标记,然后入栈;

                          2、找到和初始结点相邻的一个未被访问的结点,标志,入栈;

                          3、直到最后一个结点没有相邻的未被访问结点;然后出栈;再找下一个和初始结点相邻的未被访问                                   的结点,重复2、;

                          4、只要栈不为空,循环这个过程;

代码如下:

 package dfs;


public class Graph
{
private int max = 20;
private Vertex[] verList;
private int[][] adjMat;
private int nVertes;
private Stack stack;


public Graph()
{
adjMat = new int[max][max];
verList = new Vertex[max];
stack = new Stack();
nVertes = 0;
for (int i = 0; i < max; i++)
{
for (int j = 0; j < max; j++)
{
adjMat[i][j] = 0;
}
}


}

//深度优先搜索
public void dfs()
{
verList[0].wasVisited = true;
displayVertex(0);
stack.push(0); // 入栈
while (!stack.isEmpty())
{
int v = getUnvisitedVertex(stack.peek());
// System.out.println(v);
if (v == -1)
{
stack.pop();
}
else
{
verList[v].wasVisited = true;
displayVertex(v);
stack.push(v);


}
}// end while
for (int i = 0; i < nVertes; i++)
{
verList[i].wasVisited = false;
}
}

//找到下一个未被访问的结点
public int getUnvisitedVertex(int v)
{
for (int i = 0; i < nVertes; i++)


if (adjMat[v][i] == 1 && verList[i].wasVisited == false)


return i;
return -1;


}


public void addEdge(int start, int end)
{
adjMat[start][end] = 1;
adjMat[end][start] = 1;
}


public void addVertex(char lab)
{
verList[nVertes++] = new Vertex(lab);
}


public void displayVertex(int v)
{
System.out.println(verList[v].label);
}
}

下面是用到的数据结构的代码

//************************************************************************//

package dfs;


public class Stack
{
private int top;
 public int n=20;
 private int[] adjMat;
 public Stack(){
adjMat = new int[n];
top = -1;
 }
 public int peek(){
return adjMat[top];
 }
 public int pop(){
return adjMat[top--];
 }
 public void push(int j){
adjMat[++top]  = j;
 }
 public boolean isEmpty(){
return top==-1;
 }
}

//************************************************************************//

package dfs;


public class Vertex
{
  public char label;
  public  boolean wasVisited ;


 public Vertex(char lab){
this.label = lab;
wasVisited = false;
 }
 
}

//************************************************************************//

下面是测试代码

package dfs;


public class test
{


public static void main(String[] args)
{
Graph graph = new Graph();
graph.addVertex('A');
graph.addVertex('B');
graph.addVertex('F');
graph.addVertex('H');
graph.addVertex('C');
graph.addVertex('D');
graph.addVertex('G');
graph.addVertex('I');
graph.addVertex('E');
graph.addEdge(0,1);
graph.addEdge(1,2);
graph.addEdge(2,3);
graph.addEdge(0,4);
graph.addEdge(0,5);
graph.addEdge(5,6);
graph.addEdge(6,7);
graph.addEdge(8,9);
graph.dfs();
System.out.print(" ");
// TODO Auto-generated method stub

}
}

//************************************************************************//

输出结果为:

A B F H C D G I  

0 0