无向图的广度搜索实例

说明一下:下边的例子就是<Java数据结构和算法>书的一个例子,并非我所写的,这一本书我觉得是最好 的一本java数据结构的入门书籍.现在把这一个例子记录在blog上,当作温习一下啦。

无向图的广度搜索的规则有如下:

规则1、访问下一个未来访问的邻接点(如果存在),这个顶点必须是当前顶点的邻接点,标点它,并把它插入到队列中。

规则2、如果因为已经没有未访顶点而不能执行规则1,那么从队列取一个顶点(如果存在),并使其成为当前的顶点。

规则3、如果因为队列为空而不能执行规则2,则搜索结束。

下面是实例的代码:

Java代码 复制代码
  1. class Queue   
  2.    {   
  3.    private final int SIZE = 20;   
  4.    private int[] queArray;   
  5.    private int front;   
  6.    private int rear;   
  7.   
  8.    public Queue()            // constructor   
  9.       {   
  10.       queArray = new int[SIZE];   
  11.       front = 0;   
  12.       rear = -1;   
  13.       }   
  14.   
  15.    public void insert(int j) // put item at rear of queue   
  16.       {   
  17.       if(rear == SIZE-1)   
  18.          rear = -1;   
  19.       queArray[++rear] = j;   
  20.       }   
  21.   
  22.    public int remove()       // take item from front of queue   
  23.       {   
  24.       int temp = queArray[front++];   
  25.       if(front == SIZE)   
  26.          front = 0;   
  27.       return temp;   
  28.       }   
  29.   
  30.    public boolean isEmpty()  // true if queue is empty   
  31.       {   
  32.       return ( rear+1==front || (front+SIZE-1==rear) );   
  33.       }   
  34.   
  35.    }  // end class Queue   
  36.   
  37. class Vertex   
  38.    {   
  39.    public char label;        // label (e.g. 'A')   
  40.    public boolean wasVisited;   
  41.   
  42.    public Vertex(char lab)   // constructor   
  43.       {   
  44.       label = lab;   
  45.       wasVisited = false;   
  46.       }   
  47.   
  48.    }  // end class Vertex   
  49. /*Graph类的bfs()方法和dfs()方法类似的,只是用队列代替了栈,嵌套的循环代替了单层 *循环。外层循环等待队列为空,而内层循环依次寻找当前顶点的未访问邻接点。  
  50. **/  
  51. class Graph   
  52.    {   
  53.    private final int MAX_VERTS = 20;   
  54.    private Vertex vertexList[]; // list of vertices   
  55.    private int adjMat[][];      // adjacency matrix   
  56.    private int nVerts;          // current number of vertices   
  57.    private Queue theQueue;   
  58.   
  59.    public Graph()               // constructor   
  60.       {   
  61.       vertexList = new Vertex[MAX_VERTS];   
  62.                                           // adjacency matrix   
  63.       adjMat = new int[MAX_VERTS][MAX_VERTS];   
  64.       nVerts = 0;   
  65.       for(int j=0; j<MAX_VERTS; j++)      // set adjacency   
  66.          for(int k=0; k<MAX_VERTS; k++)   //    matrix to 0   
  67.             adjMat[j][k] = 0;   
  68.       theQueue = new Queue();   
  69.       }  // end constructor   
  70.   
  71.    public void addVertex(char lab)   
  72.       {   
  73.       vertexList[nVerts++] = new Vertex(lab);   
  74.       }   
  75.   
  76.    public void addEdge(int start, int end)   
  77.       {   
  78.       adjMat[start][end] = 1;   
  79.       adjMat[end][start] = 1;   
  80.       }   
  81.   
  82.    public void displayVertex(int v)   
  83.       {   
  84.       System.out.print(vertexList[v].label);   
  85.       }   
  86.   
  87.    //核心代码   
  88.    public void bfs()                   // breadth-first search   
  89.       {                                // begin at vertex 0   
  90.       vertexList[0].wasVisited = true// mark it   
  91.       displayVertex(0);                // display it   
  92.       theQueue.insert(0);              // insert at tail   
  93.       int v2;   
  94.   
  95.       while( !theQueue.isEmpty() )     // until queue empty,   
  96.          {   
  97.          int v1 = theQueue.remove();   // remove vertex at head   
  98.          // until it has no unvisited neighbors   
  99.          while( (v2=getAdjUnvisitedVertex(v1)) != -1 )   
  100.             {                                  // get one,   
  101.             vertexList[v2].wasVisited = true;  // mark it   
  102.             displayVertex(v2);                 // display it   
  103.             theQueue.insert(v2);               // insert it   
  104.             }   // end while   
  105.          }  // end while(queue not empty)   
  106.   
  107.       // queue is empty, so we're done   
  108.       for(int j=0; j<nVerts; j++)             // reset flags   
  109.          vertexList[j].wasVisited = false;   
  110.       }  // end bfs()   
  111.   
  112.    // returns an unvisited vertex adj to v   
  113.    public int getAdjUnvisitedVertex(int v)   
  114.       {   
  115.       for(int j=0; j<nVerts; j++)   
  116.          if(adjMat[v][j]==1 && vertexList[j].wasVisited==false)   
  117.             return j;   
  118.       return -1;   
  119.       }  // end getAdjUnvisitedVertex()   
  120.   
  121.    }  // end class Graph   
  122.   
  123. class BFSApp   
  124.    {   
  125.    public static void main(String[] args)   
  126.       {   
  127.       Graph theGraph = new Graph();   
  128.       theGraph.addVertex('A');    // 0  (start for bfs)   
  129.       theGraph.addVertex('B');    // 1   
  130.       theGraph.addVertex('C');    // 2   
  131.       theGraph.addVertex('D');    // 3   
  132.       theGraph.addVertex('E');    // 4   
  133.   
  134.       theGraph.addEdge(01);     // AB   
  135.       theGraph.addEdge(12);     // BC   
  136.       theGraph.addEdge(03);     // AD   
  137.       theGraph.addEdge(34);     // DE   
  138.   
  139.       System.out.print("Visits: ");   
  140.       theGraph.bfs();             // breadth-first search   
  141.       System.out.println();   
  142.       }  // end main()   
  143.    }  // end class BFSApp  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值