遞迴和回溯(recursion and backtracking)

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   ar   for   art   div   

1. Concept

Backtracking is a refinement of the brute force approach, which systematically searches for a solution to a problem among all available options. It does so by assuming that the solutions are represented by vectors (v1, ..., vm) of values and by traversing, in a depth first manner, the domains of the vectors until the solutions are found.

When invoked, the algorithm starts with an empty vector. At each stage it extends the partial vector with a new value. Upon reaching a partial vector (v1, ..., vi) which can’t represent a partial solution, the algorithm backtracks by removing the trailing value from the vector, and then proceeds by trying to extend the vector with alternative values.

The classic textbook example of the use of backtracking is the eight queens puzzle that asks for all arrangements of eight chess queens on a standard chessboard so that no queen attacks any other.

 

2. Examples

1) a simple example

recursion:

    public int fun(int a, int b){        if(a > 5){            return 2;        }else{            int c = fun(a+1, b*2) + a + b;            return c;        }    } 

 

backtracking:

public int backtracking(int a, int b){        Stack<Integer> stack = new Stack<Integer>();        int status = 0;        int sum = 0;        while(!stack.empty() || status == 0){            if(status == 0){                if( a > 5){                    status = 1;                    sum = 2;                }else{                    stack.push(a);                    stack.push(b);                    a = a + 1;                    b = b * 2;                    status = 0;                }            }else{                b = stack.pop();                a = stack.pop();                sum += a + b;                status = 1;            }        }        return sum;            }

 

2) inorder traverse of a binary tree

recursion:

    public void inorder(Node root){        if(root == null) return;        inorder(root.leftChild);        System.out.print(root.data + "  ");        inorder(root.rightChild);    }

 

backtracking:

public static void backtracking(Node root){        Stack<Node> stack = new Stack<Node>();        boolean status1 = true;        while(!stack.empty() || status1 == true){            if(status1 == true){                if(root == null){                    status1 = false;                    continue;                }                stack.push(root);                root = root.leftChild;                status1 = true;            }else{                root = stack.pop();                System.out.print(root.data + "  ");                root = root.rightChild;                status1 = true;            }        }                }

 

if we add a statement after the second recursion function as follows:

    public void inorder(Node root){        if(root == null) return;        inorder(root.leftChild);        System.out.print(root.data + "  ");        inorder(root.rightChild);        System.out.print("  finish  ");    }

then we need to decide where to return

    public void backtracking(Node root){        Stack<Node> stack = new Stack<Node>();        Stack<Integer> statusStack = new Stack<Integer>();        int status = 0;        while(!stack.empty() || status == 0){            if(status == 0){                if(root == null){                    status = statusStack.pop();                    continue;                }                stack.push(root);                root = root.leftChild;                statusStack.push(1);                status = 0;            }else if(status == 1){                root = stack.pop();                System.out.print(root.data + "  ");                root = root.rightChild;                stack.push(root);                statusStack.push(2);                status = 0;            }else{                stack.pop();                if(!statusStack.empty()){                    status = statusStack.pop();                }                System.out.print("  finish  ");            }        }            }

 

3) quicksort

recursion:

    public  int partition(int[] arr, int begin, int end){        int pivot = arr[begin + (end-begin)/2];        while(begin < end){            while(arr[begin] < pivot){                begin++;            }            while(arr[end] > pivot){                end--;            }            if(begin <= end){                int temp = arr[begin];                arr[begin] = arr[end];                arr[end] = temp;                begin++;                end--;            }        }        return begin;    }        public void quicksort(int[] arr, int begin, int end){        int pivot = partition(arr, begin, end);        if(pivot-1 > begin){            quicksort(arr, begin, pivot -1);        }        if(pivot < end){            quicksort(arr, pivot, end);        }    }

 

backtracking:

public void bactrackingk(int[] arr, int begin, int end){        Stack<Integer> stack = new Stack<Integer>();        int status = 0;        while(!stack.empty() || status == 0){            if(status == 0){                int pivot = partition(arr, begin, end);                if(pivot-1 <= begin){                    status = 1;                    continue;                }                stack.push(end);                stack.push(pivot);                end = pivot - 1;                status = 0;            }else{                int pivot = stack.pop();                end = stack.pop();                begin = stack.pop();                if(pivot > end){                    status = 1;                    continue;                }                begin = pivot - 1;                status = 0;            }        }            }

 

more:

http://en.wikipedia.org/wiki/Backtracking

http://web.cse.ohio-state.edu/~gurari/course/cis680/cis680Ch19.html

http://www.cnblogs.com/steven_oyj/archive/2010/05/22/1741376.html

遞迴和回溯(recursion and backtracking)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.