廣度優先搜索

下面算法中,假定輸入圖G=(V,E)是以鄰接鏈表所標示的。該算法爲途中每一個節點賦予了一些額外屬性:我們將每個節點u的顏色存放在屬性u.color裏(白色表示沒有發現過的節點,灰色表示已經發現但其鄰接節點沒有被全部發現,黑色表示已經被發現且鄰接節點全部被發現),將u的前驅結點存放在屬性u.pi裏。如果u沒有前驅節點(例如,如果u=s或者節點u尚未被發現),則u.pi = NIL。屬性u.d記錄的是廣度優先搜索算法所計算出的從源節點s到節點u之間的距離。該算法使用了一個先進先出隊列Q來管理灰色結點集。

BFS(G,s)
1  for each vertex u belong to G.V-{s}
2    u.color = WHITE
3    r.d = infty
4    u.pi = NIL
5  s.color = GRAY
6  s.d = 0
7  s.pi = NIL
8  Q = emptyset
9  ENQUEUE(Q,s)
10 while Q != empty
11   u = DEQUEUE(Q)
12   for each v belong to G.Adj[u]
13     if v.color == WHITE
14       v.color = GRAY
15       v.d = u.d + 1
16       v.pi = u
17       ENQUEUE(Q,v)
18   u.color = BLACK

java代碼:

    /** * 廣度優先搜索 * @param graphContent * @param startID */
    @SuppressWarnings("rawtypes")
    public static void BFS(String graphContent, int startID){
        List<Vertex> vertexs = Route.getVertexs(graphContent);
        LinkedList[] list = Route.getLinkedList(graphContent);

        for (Vertex vertex : vertexs){
            vertex.setColor(Color.WHITE);
            vertex.setD(50000);
            vertex.setParent(null);
        }
        Vertex start = Vertex.getVertex(startID);
        start.setColor(Color.GRAY);
        start.setD(0);
        start.setParent(null);

        LinkedList<Vertex> Q = new LinkedList<Vertex>();
        Q.addLast(start);
        while (!Q.isEmpty()){
            Vertex u = Q.removeFirst();
            for (Object o : list[u.getID()]){
                Vertex v = (Vertex)o;
                if (v.getColor().equals(Color.WHITE)){
                    v.setColor(Color.GRAY);
                    v.setD(u.getD() + 1);
                    v.setParent(u);
                    Q.addLast(v);
                }
            }
            u.setColor(Color.BLACK);
        }
    }

    /** * 打印最小路徑 * @param graphContent * @param startID * @param endID */
    public static void print_Path(String graphContent, int startID, int endID){
        BFS(graphContent, startID);
        Vertex s = Vertex.getVertex(startID);
        Vertex v = Vertex.getVertex(endID);
        if (v.equals(s)){
            System.out.print(s + "--");
        }else if (v.getParent() == null){
            System.out.println("\nno path from \"s\" to \"v\" exists");
        }else {
            print_Path(graphContent, startID, v.getParent().getID());
            System.out.print(v + "--");
        }
    }

    /** * 獲取鄰接鏈表 * @param graphContent * @return */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static LinkedList[] getLinkedList(String graphContent){
        List<Vertex> vertexs = getVertexs(graphContent);
        LinkedList[] list = new LinkedList[vertexs.size()];
        int[] datas = getDatas(graphContent);

        for (int i = 0; i < vertexs.size(); i++){
            int n = vertexs.get(i).getID();
            list[n] = new LinkedList();
            list[n].add(vertexs.get(i));
            for (int j = 0; j < datas.length/4; j++){
                if (vertexs.get(i).equal(datas[j*4+1])){
                    list[n].add(Vertex.getVertex(datas[j*4+2]));
                }
            }
        }
        return list;
    }

    /** * 將圖的信息從字符串轉換爲數字 * @param graphContent * @return */
    public static int[] getDatas(String graphContent){
        String[] strings = graphContent.split("[,\n]");
        int[] ans = new int[strings.length];
        for (int i = 0; i < ans.length; i++){
            ans[i] = Integer.valueOf(strings[i]);
        }
        return ans;
    }

    /** * 獲取頂點 * @param graphContent * @return */
    public static List<Vertex> getVertexs(String graphContent){
        int[] datas = getDatas(graphContent);
        int n = datas.length/4;
        for (int i = 0; i < n; i++){
            Vertex.create(datas[i*4+1]);
            Vertex.create(datas[i*4+2]);
        }
        return Vertex.getVertexs();
    }

其中graphContent爲
0,0,1,1
1,0,2,2
2,0,3,1
3,2,1,3
4,3,1,1
5,2,3,1
6,3,2,1
型字符串,其中每行第一個數爲邊的編號,第二第三爲起點終點編號,第四個數爲權值(廣度優先搜索沒有用到權值)。

点赞