{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lec12_Algorithms

# Lec12_Algorithms - Discrete Structures TDS1191 Lecture 12...

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

Discrete Structures 1 Discrete Structures TDS1191 - Lecture 12 - Algorithms [Lecture 12][Algorithms]

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

View Full Document
Discrete Structures 2 Contents Introduction to algorithm Euclidean algorithm Recursive algorithm Analysis of algorithms [Lecture 12][Algorithms]
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

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

View Full Document
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
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

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

View Full Document
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
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: If a , b and q are integers such that b 0, and a = , then we say that b divides a and is denoted by b | a . In this case, we call q the quotient and b a divisor of a .

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}