Lec12_Algorithms - Discrete Structures TDS1191 - Lecture 12...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Discrete Structures 1 Discrete Structures TDS1191 - Lecture 12 - Algorithms [Lecture 12][Algorithms]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Discrete Structures 2 Contents Introduction to algorithm Euclidean algorithm Recursive algorithm Analysis of algorithms [Lecture 12][Algorithms]
Background image of page 2
Discrete Structures 3 [Lecture 12][Algorithms] Introduction to Algorithm Definition 1: Algorithm: a finite set of precise instructions for performing a computation or for solving a problem. Characteristics of algorithm: Input: what it receives Output: what to produce Definiteness: precisely define the steps of an algorithm Correctness: produce correct output Finiteness: can produce the desired output after a finite number of steps Effectiveness: perform within a finite amount of time Generality: apply to same form of problems Note: Algorithm is steps to solve a computation problem. It allows us to translate the problem into any programming language
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Discrete Structures 4 [Lecture 12][Algorithms] Algorithm 1: Finding the maximum value in a sequence Purpose: This algorithm finds the largest number among a series of numbers Input : s [an array of integers], n [an integer] procedure max (s, n) { large := s 1 for i = 2 to n if s i > large then large := s i return (large) } end max Output : large [an integer] Name the algorithm with the list of inputs/variables that it must receives a brief description output (must mention the data type) input (must mention the data type) Statements: use your logical thinking to design the algorithm Algorithm Design Open brace Closeb race
Background image of page 4
Discrete Structures 5 [Lecture 12][Algorithms] Algorithm Design (Refer Algorithm 1) When to use for/while? Use when processes are repeating E.g.: Comparison need to make for each number in the sequence. Assign Large = first number, start the comparison from the second number and onwards till the end of all n numbers When to use if/else? Use when involve decision making E.g.: The algorithm need to get the largest number. When compare to the second number and onwards, any number that is larger than the previous one, will be stored in the variable Large
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Algorithm Design [Lecture 12][Algorithms] Algorithm 2: Determine the prime number for a given integer Purpose: This algorithm finds the prime number for a given integer [prime number: number that divide by one and itself] Input : n [an integer] procedure Prime (n) { prime := true for i = 2 to n – 1 if (n % i) = = 0 then prime := false return (prime) return (prime) } end Prime Output : prime [boolean] Discrete Structures 6
Background image of page 6
Discrete Structures 7 [Lecture 12][Algorithms] Euclidean algorithm Definition 1: Let m and n be integers that are not both zero. The greatest common divisor (gcd) of m and n is denoted by gcd(m, n) and is defined to be the largest integer that divides both m and n . Definition 2:
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/03/2012 for the course IT 1191 taught by Professor Yong during the Spring '11 term at Multimedia University, Cyberjaya.

Page1 / 31

Lec12_Algorithms - Discrete Structures TDS1191 - Lecture 12...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online