1. 程式人生 > >漢密爾頓路徑(哈密頓路徑)解析

漢密爾頓路徑(哈密頓路徑)解析

漢密爾頓路徑(哈密頓路徑)

哈密頓路徑也稱作哈密頓鏈,指在一個圖中沿邊訪問每個頂點恰好一次的路徑。尋找這樣的一個路徑是一個典型的NP-完全(NP-complete)問題。後來人們也證明了,找一條哈密頓路的近似比為常數的近似演算法也是NP完全的.

演算法思路(尋找圖中所有的哈密頓路)

  1. 首先用一個鄰接矩陣儲存圖
  2. 將每一個頂點作為起點,查詢哈密頓路
  3. 查詢哈密頓路的思路:採用遞迴遍歷的方式在遞迴的搜尋的過程中同時需要不斷的修改邊可以連結的狀態。0不可以連結 1 可以連結。
findHamiltonpath(int[][] M, int x, int y, int l) {
        int
i; for (i = x; i < len; i++) { // Go through row if (M[i][y] != 0) { // 2 point connect if (detect(path, i + 1))// if detect a point that already in the // path => duplicate continue; l++; // Increase path length due to 1 new point is connected
path[l] = i + 1; // correspond to the array that start at 0, // graph that start at point 1 if (l == len - 1) {// Except initial point already count // =>success connect all point count++; if
(count == 1) System.out.println("Hamilton path of graph: "); display(path); l--; continue; } M[i][y] = M[y][i] = 0; // remove the path that has been get and findHamiltonpath(M, 0, i, l); // recursively start to find new // path at new end point l--; // reduce path length due to the failure to find new path M[i][y] = M[y][i] = 1; // and tranform back to the inital form // of adjacent matrix(graph) } } path[l + 1] = 0; // disconnect two point correspond the failure to find // the.. }
  1. 找到圖中所有的哈密頓路徑並且列印。

演算法Java原始碼

/**
 * @Author PaulMrzhang
 * @Version 2016年11月21日 下午6:59:19
 * @DESC 說明:這份程式碼是同時給我的,在這裡感謝程式碼的原作者!
 */
public class HamiltonPath {

    public static void main(String[] args) {
        HamiltonPath obj = new HamiltonPath();

        int[][] x = {
                { 0, 1, 0, 1, 0 }, // Represent the graphs in the adjacent
                                    // matrix forms
                { 1, 0, 0, 0, 1 }, { 0, 0, 0, 1, 0 }, { 1, 0, 1, 0, 1 },
                { 0, 1, 0, 1, 0 } };

        int[][] y = { { 0, 1, 0, 0, 0, 1 }, 
                      { 1, 0, 1, 0, 0, 1 },
                      { 0, 1, 0, 1, 1, 0 }, 
                      { 0, 0, 1, 0, 0, 0 },
                      { 0, 0, 1, 0, 0, 1 }, 
                      { 1, 1, 0, 0, 1, 0 } };

        int[][] z = { { 0, 1, 1, 0, 0, 1 }, { 1, 0, 1, 0, 0, 0 },
                { 1, 1, 0, 1, 0, 1 }, { 0, 0, 1, 0, 1, 0 },
                { 0, 0, 0, 1, 0, 1 }, { 1, 0, 1, 0, 1, 0 } };

        obj.allHamiltonPath(x); // list all Hamiltonian paths of graph
        // obj.HamiltonPath(z,1); //list all Hamiltonian paths start at point 1

    }

    static int len;
    static int[] path;
    static int count = 0;

    public void allHamiltonPath(int[][] x) { // List all possible Hamilton path
                                                // in the graph
        len = x.length;
        path = new int[len];
        int i;
        for (i = 0; i < len; i++) { // Go through column(of matrix)
            path[0] = i + 1;
            findHamiltonpath(x, 0, i, 0);
        }
    }

//  public void HamiltonPath(int[][] x, int start) { // List all possible
//                                                      // Hamilton path with
//                                                      // fixed starting point
//      len = x.length;
//      path = new int[len];
//      int i;
//      for (i = start - 1; i < start; i++) { // Go through row(with given
//                                              // column)
//          path[0] = i + 1;
//          findHamiltonpath(x, 0, i, 0);
//      }
//  }

    private void findHamiltonpath(int[][] M, int x, int y, int l) {

        int i;
        for (i = x; i < len; i++) { // Go through row

            if (M[i][y] != 0) { // 2 point connect

                if (detect(path, i + 1))// if detect a point that already in the
                                        // path => duplicate
                    continue;

                l++; // Increase path length due to 1 new point is connected
                path[l] = i + 1; // correspond to the array that start at 0,
                                    // graph that start at point 1
                if (l == len - 1) {// Except initial point already count
                                    // =>success connect all point
                    count++;
                    if (count == 1)
                        System.out.println("Hamilton path of graph: ");
                    display(path);
                    l--;
                    continue;
                }

                M[i][y] = M[y][i] = 0; // remove the path that has been get and
                findHamiltonpath(M, 0, i, l); // recursively start to find new
                                                // path at new end point
                l--; // reduce path length due to the failure to find new path
                M[i][y] = M[y][i] = 1; // and tranform back to the inital form
                                        // of adjacent matrix(graph)
            }
        }
        path[l + 1] = 0; // disconnect two point correspond the failure to find
                            // the..
    } // possible hamilton path at new point(ignore newest point try another
        // one)

    public void display(int[] x) {

        System.out.print(count + " : ");
        for (int i : x) {
            System.out.print(i + " ");
        }
        System.out.println();
    }

    private boolean detect(int[] x, int target) { // Detect duplicate point in
                                                    // Halmilton path
        boolean t = false;
        for (int i : x) {
            if (i == target) {
                t = true;
                break;
            }
        }
        return t;
    }
}

原始碼解析

採用遞迴遍歷+極限窮舉。
好的演算法就是更好的對思路的實現,還要有更好的狀態控制。
這裡的
M[i][y] = M[y][i] = 0;//
M[i][y] = M[y][i] = 1; //
還有對函式的遞迴呼叫使用的非常棒!