90 likes | 279 Views
Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال). Chapter 1 Introduction to Algorithms. Computational problems. A computational problem specifies an input-output relationship What does the input look like? What should the output be for each input? Example 1:
E N D
Design and Analysis of Algorithmsتصميم وتحليل الخوارزميات (311 عال) Chapter 1 Introduction to Algorithms
Computational problems • A computational problem specifies an input-output relationship • What does the input look like? • What should the output be for each input? • Example 1: • Input: an integer number n • Output: Is the number odd ( هل الرقم فردي أو لا)? true • Example 2: • Input: array of numbers • Output: the minimum number in the array • Example 3: • Input: array of numbers • Output: the same array sorted (ascending تصاعديا)
Input Algorithm Output Algorithms • Algorithm is: • A tool for solving a well-specified computational problem • A sequence of instructions describing how to do a task or process • Algorithms must be: • Correct: For each input produce an appropriate output • Efficient: run as quickly as possible, and use as little memory as possible
The Problem-solving Process Analysis Problem specification Design Algorithm Implementation Program Compilation Executable (solution)
Algorithm: A sequence of instructions describing how to do a task (or process) Problem C Program From Algorithms to Programs
Simple Algorithms Example1 (Min Algorithm) • INPUT: a sequence of n numbers, • T is an array of n elements • T[1], T[2], …, T[n] • OUTPUT: the smallest number among them • Max, Min take time = n min = T[1] for i = 2 to n do { if T[i] < min min =T[i] } Output min
Simple Algorithms (cont.) • Algorithms can be implemented in any programming language • Usually we use “pseudo-code” to describe algorithms • Example 2 (MAX algorithm): • INPUT: a sequence of n numbers, • T is an array of n elements • T[1], T[2], …, T[n] • OUTPUT: the largest (biggest) number among them Max = T[1] for i = 2 to n do { if T[i] > Max Max =T[i] } Output Max