- By
**hoang** - Follow User

- 142 Views
- Uploaded on

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

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

- 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);

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

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

Chapter 1: Introduction

- Which solution is better when (1) N ~ k and (2) N » k ? why?

1.2: Mathematics Review

- Basic formulas for derive and reviews basic proof techniques
- Exponents

1.2: Mathematics Review

- Logarithms
- All log are to be base 2 unless specified otherwise.
- Definition:

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 ReviewSeries: Arithmetic series

Example: To find the sum

2+5+8+….+ (3k-1)

= 3(1+2+3+…+k)

- (1+1+1+….+1)

1.2: Mathematics ReviewThe 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 Review1.3 Recursion

- What is recursion?

- self-reference

- recursive function: based upon itself

e.g. n factorial:

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

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 RecursionUsing 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 numbersUsing 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 Search1.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.

1.3 Recursion: Tower of Hanoi

- Take n = 3

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.

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

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);

}

}

1.3 Recursion

- 4 Basic Rules of Recursion

- Base cases

- Making progress

- Design rule

- Compound interest rule

Download Presentation

Connecting to Server..