introduction algorithms and conventions the design and analysis of algorithms is the core subject...

11
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want to find an algorithm to solve the problem (create) prove that the algorithm solves the problem correctly (validate) prove that we cannot solve the problem any faster (analyse) implement the algorithm test the program (debugging and profiling) August 28, 2022 1

Upload: loraine-perry

Post on 31-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want

IntroductionAlgorithms and Conventions The design and analysis of algorithms is the

core subject matter of Computer Science. Given a problem, we want to find an algorithm to solve the problem (create) prove that the algorithm solves the problem

correctly (validate) prove that we cannot solve the problem any

faster (analyse) implement the algorithm test the program (debugging and profiling)

April 19, 2023 1

Page 2: Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want

Analysis Designing an algorithm for a computational problem involves knowledge

of the problem domain, a thorough knowledge of the data structures that are available and suitable, and quite a lot of creativity.

It covers the design of algorithms for various types of problems, as well as a mathematical analysis of those algorithms done independently of any actual computational experiments (a theoretical vs. empirical study).

Analysis of algorithms is less obviously necessary, but has several purposes: Analysis can be more reliable than experimentation. If we experiment, we only know the behavior of a program on certain

specific test cases, while analysis can give us guarantees about the performance on all inputs.

It helps one choose among different solutions to problems. As we will see, there can be many different solutions to the same problem. A careful analysis and comparison can help us decide which one would be

the best for our purpose, without requiring that all be implemented and tested.

We can predict the performance of a program before we take the time to write code.

In a large project, if we waited until after all the code was written to discover that something runs very slowly, it could be a major disaster, but if we do the analysis first we have time to discover speed problems and work around them.

By analyzing an algorithm, we gain a better understanding of where the fast and slow parts are, and what to work on or work around in order to speed it up.

April 19, 2023 2

Page 3: Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want

What is an algorithm?An algorithm is an outline or idea behind a program. Generating an algorithm, itself, is a step-by-step

process, getting more specific with each version. It usually starts with a precise statement to solve a

problem on a computer but ultimately consists of a sequence of definite instructions to do a certain job.

We express algorithms in pseudo-code: something resembling C or Java, but with some statements in English rather than within the programming language.

It is expected that one could translate each pseudo-code statement to a small number of lines of actual code, easily and mechanically.

April 19, 2023 3

Page 4: Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want

Definition [Algorithm]: An algorithm is a finite set of instructions that, if

followed, accomplishes a particular task. In addition, all algorithms must satisfy the following

criteria: 1. Input. Zero or more quantities are externally

supplied. 2. Output. At least one quantity is produced.

(function vs procedure) 3. Definiteness. Each instruction is clear and

unambiguous. 4. Finiteness. If we trace out the instructions of an

algorithm, then for all cases, the algorithm terminates after a finite number of steps.

5. Effectiveness. Each instruction must be basic so that it can be carried out, in principle, by a person using only pencil and paper. It is not enough that each operation be definite as in criterion 3; it also must be feasible.

April 19, 2023 4

Page 5: Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want

Design of AlgorithmsDevising the algorithm (i.e, method) Expressing the algorithm (computer

language) Validating the algorithm (proof of

correctness) translational semantics operational semantics denotational semantics axiomatic semantics

April 19, 2023 5

Page 6: Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want

Algorithm Analysis of Algorithms

Determination of time and space requirements

Implementation and Program TestingNot in this class

April 19, 2023 6

Page 7: Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want

Pseudocode Conventions Comments: // Blocks: {} Identifiers and Records: Since pseudocode, code is not object-oriented. The use of records is how pre-OO languages grouped information into

compound datatypes or structures. Note the use of * to indicate links to other records.

Assignment: <variable> := <expression> Booleans: true and false Arrays: A[i,j] Loops: Use of for,while,repeat-until Conditionals:

if<condition> then<statement1>else<statement2 Case statement (switch) case {    : <condition 1> : <statement 1>       .       .       .    : <condition n> : <statement n>    : else : <statement n+1> }

I/O: since pseudocode use read and write rather than specific call Procedures: Algorithm Name (<paramenter list>) Use of return

April 19, 2023 7

Page 8: Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want

Example of AlgorithmsSuppose you had the Binary search identified in

the text (Example1.1). Check out how the text will "walk-through" the

conceptualization (Algorithm 1.1 and 1.2)through proof of the algorithm (Thm. 1.1).

In addition, the OO code would look something like this:

void BinarySearch(Type a[], int n) ……Go and read abt recursion, induction and

mathematical functions appendix section of text

April 19, 2023 8

Page 9: Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want

Recursion Recursion is a general method of solving problems by

reducing them to simpler problems of a similar type. General Algorithm (problem solving tool) of Divide

and Conquer Postpone work

(if defined recursively (inductively), work is easy) For a web-oriented example, consider designing a

"web-crawler" that will search every hyperlink that is accessible from a specific page.

Pseudo-code - yes, I do know that this little example would have a problem with circular links Recursive_URL_search(link) Repeat

find next_link Recursive_URL_search (next_link)

until No_more_links

April 19, 2023 9

Page 10: Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want

Recursion ctd..Three important aspects to always remember in code

consider halt make sure get to halt must call self

When ... why recursion? If problem itself is recursively defined To describe a backtracking procedure Provability of correctness (induction) Ease of programming

Why not? space efficiency time efficiency One can always take a recursive program and do it non-

recursively.

April 19, 2023 10

Page 11: Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want

Recursion ctd…For any recursive problem one needs to consider

two features: Are solutions easy to give for special states (stop

state)?(need to identify a "trivial" case)

Given a state (not the stop state) are there clear rules for proceeding to a new state that is either a stop state leads to a stop state

I.e., find a method to solve the "complex" case in terms of a "simpler" case (the conquer is easier if divide)

Quiz write the binary search, factorial and fibonacci series using recursion

April 19, 2023 11