Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 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 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 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 5 Chapter 1: Introduction Which solution is better when (1) N ~ k and (2) N » k ? why?

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

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

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

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

10 10 1.2: Mathematics Review 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

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

12 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 13 1.3 Recursion 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.

14 14 1.3 Recursion : n factorial Recursive processIterative process

15 15 1.3 Recursion : n factorial

16 16 1.3 Recursion: Fibonacci numbers 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);

17 17 1.3 Recursion:Binary Search 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; }

18 18 1.3 Recursion:Binary Search

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

20 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 21 1.3 Recursion: Tower of Hanoi Take n = 3

22 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 23 1.3 Recursion: Tower of Hanoi move disk 1 from peg Ato pegB move disk2from peg Ato pegC move disk1from pegBto pegC move disk3from pegAto pegB move disk1from pegCto pegA move disk2from pegCto pegB move disk1from pegAto pegB move disk4from pegAto pegC move disk1from pegBto pegC move disk2from pegBto pegA move disk1from pegCto pegA move disk3from pegBto pegC move disk1from pegAto pegB move disk2from pegAto pegC move disk1from pegBto pegC n = 4

24 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 25 1.3 Recursion 4 Basic Rules of Recursion - Base cases - Making progress - Design rule - Compound interest rule


Download ppt "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."

Similar presentations


Ads by Google