Basic Design Techniques - PowerPoint PPT Presentation

About This Presentation
Title:

Basic Design Techniques

Description:

keep doing S until a condition is true. Recursion = a procedure ... King and his wines. using binary search idea. 000 001 010 011 100 101 110 111. 0xx. x0x ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 15
Provided by: rong94
Category:

less

Transcript and Presenter's Notes

Title: Basic Design Techniques


1
Basic Design Techniques
  • iteration versus recursion
  • divide-and-conquer
  • an example merge sort

2
Iteration versus Recursion how? what?
  • When an operation needs to be repeatedly
    performed,
  • We may use either iteration or recursion
  • Iteration looping
  • ? keep doing S until a condition is true
  • Recursion a procedure calling itself
  • ? do(n) if(n0) stop else do(n-1)

3
An examplesum of a list of numbers
  • Iteration
  • sum(L)
  • s 0
  • while(L is not empty)
  • s s first(L)
  • L tail(L)
  • first(L) returns the 1st item of the list L
  • Recursion
  • sum(L)
  • if(L is empty) return 0
  • else return
  • (first(L) sum(tail(L))
  • tail(L) returns sublist of L with its 1st item
    removed

4
Binary Search Treeinsertion using recursion
  • public void insertInOrder(String input_item)
  • if(valuenull) value input_item// insert
    here
  • else if(Func.lessThan(input_item, value))
  • // insert to left subtree
  • if(leftnull) left new BinTree(input_item)
  • else left.insertInOrder(input_item)
  • else // insert to right subtree
  • if(rightnull) right new
    BinTree(input_item)
  • else right.insertInOrder(input_item)

5
Binary Search Tree insertion using iteration
  • public void insertInOrder(String input_item)
  • BinTree t this
  • for() // loop until find the place to
    insert
  • if(t.valuenull) t.value input_item
    break
  • else if(Func.lessThan(input_item, value))
  • // move to left subtree
  • if(t.leftnull)
  • t.left new BinTree(input_item)
    break
  • else t t.left
  • else // move to right subtree
  • if(t.rightnull)
  • t.right new BinTree(input_item)
    break
  • else t t.right

6
Recursion Powerful but Simple
  • Based on inductive definitions
  • ? define a base case (when terminate)
  • ? assume that the problem can be solved for
    size (n-1), define how to solve the problem of
    size n
  • Iterations can be replaced by recursions
  • In many cases, recursions CANNOT be replaced by
    iterations

7
Divide-and-conquer
  • The most widely applicable techniques for
    designing efficient algorithms
  • Divide - breaking a problem into smaller problems
    of same kind
  • Conquer - from solutions to the smaller problems
    constructing a solution to the entire problem.

8
Binary search seq. search O(n) ? O(log n)
?
  • N posts, ordered by their heights
  • Find one which has exactly same height as a given
    post
  • solution keep dividing posts into 2 halves

9
King and his winesusing binary search idea
000 001 010 011 100 101 110
111
1
0xx
2
x0x
xx0
3
All alive
All died
Tester 1 and 3 died
10
Merge sort input an unordered listoutput
an ordered list
  • Phase 1 dividing the input list into 2, 4, 8,
    until nothing can be divided
  • Phase 2 merge the split lists into right order,
    pair by pair

11
Merge sort
85 24 63 45 17 31 96 90
17 24 31 45 50 63 85 96
17 31 90 96
24 45 63 85
85 24 63 45
17 31 96 90
85 24
63 45
17 31
96 90
24 85
45 63
17 31
90 96
85 24 63 45 17 31 96 90
85 24 63 45 17 31 96 90
conquer
divide
12
Merging two sorted lists
  • merge(list1,list2,list3)
  • while(both list1 and list2 are not empty)
  • insert min(head(list1), head(list2)) to
    the end of list3,
  • remove this min value from list1 or list2
  • if(either list1 or list2 is not empty) add it
    to list3

17
10
9
4
4
5
7
18
30
13
Merge two sorted lists java code for List.java
  • merge(int fm1, int to1, int fm2, int to2) //
    assume fm2 to11
  • int i fm1 int j fm2 int k fm1
  • while(i lt to1 j lt to2)
  • if(Func.lessThan(datai, dataj)) tmpk
    datai
  • else tmpk dataj
  • while(i lt to1) tmpk datai
  • while(j lt to2) tmpk dataj
  • i j fm1 // copy them back to input data
    list
  • for( jltk j) datai tmpj
  • // we use tmp as a buffer, it is defined in
    List class
  • // if we dont use tmp, we can use a local
    array. It is slower.

14
Merge sort - java code
  • public void mergesort()
  • mergesort(0, current_size-1)
  • public void mergesort(int fm, int to)
  • if(fmto) return // reached the bottom, do
    nothing
  • int middle ?? // find the middle position
  • mergesort(fm,middle)
  • mergesort(middle1,to)
  • merge(fm,middle,middle1,to)
  • // can you rewrite it by using iteration?
Write a Comment
User Comments (0)
About PowerShow.com