1 / 25

Chapter 1: Introduction

Chapter 1: Introduction. What you have learnt in Comp1220 or Comp1170? What will be taught in Comp 1200? - more Abstract Data Types - efficient algorithms for problem solving (very useful in handling large amount of data) - algorithm analysis. Chapter 1: Introduction.

hoang
Download Presentation

Chapter 1: Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 1: Introduction • What you have learnt in Comp1220 or Comp1170? • What will be taught in Comp 1200? - more Abstract Data Types - efficient algorithms for problem solving (very useful in handling large amount of data) - algorithm analysis

  2. Chapter 1: Introduction • COMP 1220/1170 Revision • Basic Programming Concept • Type, Operators and Expressions • Control Flow • if else, while, do while, for, switch..... etc • Functions and Program Structure - advantages; • declare, define, invoke a function; • return value, call by value, call by address (reference);

  3. Chapter 1: Introduction • COMP 1220/1170 Revision con’t • Arrays and Pointers • group of related variables; • character strings – searching/matching/validation; • pointers and functions argument; • Structures • basic of structures, user define type; • pointers to structures, self referential structure; • linked list; • Storage Class and Type Qualifiers • Input and Output

  4. Chapter 1: Introduction • Problem: Given a group of N numbers, determine the kth largest, where N > k . • Solution 1: • (1) read N number into an array, (2) sort the array in decreasing order, (3) return the element in position k. • Solution 2: • (1) read the first k elements into an array and sort them in decreasing order, (2) each remaining element is read one by one, (2.1) it is ignored if it is smaller than the kth element in the array (2.2) it is placed in its correct spot in the array, bumping one element out of the array. (3) the element in the kth position is returned as the answer

  5. Chapter 1: Introduction • Which solution is better when (1) N ~ k and (2) N » k ? why?

  6. 1.2: Mathematics Review • Basic formulas for derive and reviews basic proof techniques • Exponents

  7. 1.2: Mathematics Review • Logarithms • All log are to be base 2 unless specified otherwise. • Definition:

  8. Series: Geometric series Derivation Let S = 1+A+A2+…… (1) where, 0<A<1 then AS = A+A2+A3+…(2) Subtracting (1) and (2), we get S-AS = 1, i.e. 1.2: Mathematics Review

  9. Series: Arithmetic series Example: To find the sum 2+5+8+….+ (3k-1) = 3(1+2+3+…+k) - (1+1+1+….+1) 1.2: Mathematics Review

  10. The P word - to proof a false statement: proof by counter example - to proof a correct statement - proof by induction (1) proving a base case (2) inductive hypothesis - proof by contradiction (1) assume it is false (2) show that this assumption is false 1.2: Mathematics Review

  11. 1.3 Recursion • What is recursion? - self-reference - recursive function: based upon itself e.g. n factorial:

  12. 1.3 Recursion • Characteristics of a recursive definition - It has a stopping point (base case). - It (recursively) allows evaluation of an expression involving a variable n from a higher value to a lower value of n. • Just like the process in finding a vocabulary from a dictionary

  13. Recursive process Recursive definition defines an object in terms of a simpler case of itself. Iterative process Iterative evaluation calls for explicit repetition of similar computations. Such computations can generally be carried out in a for loop operation. 1.3 Recursion

  14. Recursive process Iterative process 1.3 Recursion : n factorial

  15. 1.3 Recursion : n factorial

  16. Using recursion fib (int n) { int x, y; if (n <= 1) return (n); else { x = fib (n - 1); y = fib (n - 2); return (x + y); } } Using iterative if (n <= 1) return (n); lofib = 0; hifib = 1; for (i=2; i<=n; i++) { x = lofib; lofib = hifib; hifib = x + lofib; } return (hifib); 1.3 Recursion: Fibonacci numbers

  17. Using recursion int binsrch (int a[], int x, int low, int high) { int mid; if (low > high) return (-1); else { mid = (low + high) / 2; return (x == a [mid] ? mid : x < a [mid] ? binsrch (a, x, low, mid-1) : binsrch (a, x, mid+1, high): ); } } Using iterative while (low <= high) { mid = (low + high) / 2; if (x == a [mid]) return (mid); else if (x < a [mid]) high = mid - 1; else low = mid + 1; } 1.3 Recursion:Binary Search

  18. 1.3 Recursion:Binary Search

  19. 1.3 Recursion • Questions: - When should we use recursion? - When should we use simple for loop?

  20. 1.3 Recursion: Tower of Hanoi • Problem: • constraints: (1) only one disk can be moved at a time, and (2) at no time may a disk be placed on top of a smaller disk.

  21. 1.3 Recursion: Tower of Hanoi • Take n = 3

  22. 1.3 Recursion: Tower of Hanoi • Solution - By making use of an auxiliary peg, move a stack of disks from one peg to another, making sure that a larger disk is always below a smaller disk at any time - Strategy If n = 1, move the single disk from A to C and stop; Otherwise move the top n-1 disks from A to B, using C as auxiliary, move the remaining disk from A to C, move the n-1 disks from B to C, using A as auxiliary.

  23. 1.3 Recursion: Tower of Hanoi • move disk 1 from peg A to peg B • move disk 2 from peg A to peg C • move disk 1 from peg B to peg C • move disk 3 from peg A to peg B • move disk 1 from peg C to peg A • move disk 2 from peg C to peg B • move disk 1 from peg A to peg B • move disk 4 from peg A to peg C • move disk 1 from peg B to peg C • move disk 2 from peg B to peg A • move disk 1 from peg C to peg A • move disk 3 from peg B to peg C • move disk 1 from peg A to peg B • move disk 2 from peg A to peg C • move disk 1 from peg B to peg C n = 4

  24. 1.3 Recursion: Tower of Hanoi towers (int n, char frompeg, char topeg, char auxpeg) { /* If only one disk, make the move and return */ if (n == 1) printf (“\n%s%c%s%c”, “move disk 1 from peg “, frompeg, “ to peg “, topeg); else { /* move top n-1 disks from A to B, C as auxiliary*/ towers (n-1, frompeg, auxpeg, topeg); /* move remaining disk from A to C */ printf (“n%s%d%s%c%s%c”, “move disk “, n,“ from peg “, frompeg, “ to peg “, topeg); /* move n-1 disks from B to C, A as auxiliary */ towers (n-1, auxpeg, topeg, frompeg); } }

  25. 1.3 Recursion • 4 Basic Rules of Recursion - Base cases - Making progress - Design rule - Compound interest rule

More Related