Recursion and backtracking)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.