csc-305 design and analysis of algorithmsbs(cs) -6 fall-2014csc-305 design and analysis of...

13
CSC-305 Design and Analysis of Algorithms BS(CS) -6 Fall-2014 CSC-305 Design and Analysis of Algorithms BS(CS) -6 Fall-2014 Design and Analysis of Algorithms Khawaja Mohiuddin Assistant Professor, Department of Computer Sciences Bahria University, Karachi Campus, Contact: [email protected] Lecture # 2 – Algorithm Basics

Upload: marilyn-blankenship

Post on 20-Jan-2018

213 views

Category:

Documents


0 download

DESCRIPTION

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Algorithm Basics 3  What is Algorithm? What do you think?

TRANSCRIPT

Page 1: CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS)…

CSC-305 Design and Analysis of Algorithms BS(CS) -6 Fall-2014CSC-305 Design and Analysis of Algorithms BS(CS) -6 Fall-2014

Design and Analysis of Algorithms

Khawaja MohiuddinAssistant Professor,

Department of Computer Sciences

Bahria University, Karachi Campus,

Contact:

[email protected]

Lecture # 2 – Algorithm Basics

Page 2: CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS)…

CSC-305 Design and Analysis of Algorithms BS(CS) -6 Fall-2014

Algorithm Basics2

Basic Concerns Computer programming is both an art and science Elegance and beauty of expression on one hand Accuracy, correctness, reliability and efficiency on the other hand These concerns arise trying to balance the needs of two classes of people The Programmers: who would like to be elegant and expressive, while delivering

correctness and efficiency The Users: who would like accuracy, reliability, correctness and efficiency in a

program In general, both are concerned with a good solution to a problem A good solution is economical in the use of computing and human resources

Page 3: CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS)…

CSC-305 Design and Analysis of Algorithms BS(CS) -6 Fall-2014

Algorithm Basics3

What is Algorithm? What do you think?

Page 4: CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS)…

CSC-305 Design and Analysis of Algorithms BS(CS) -6 Fall-2014

Algorithm Basics4

Algorithm The Concise Oxford Dictionary defines Algorithm (also “Algorism”) as: a process or

a set of rules used for calculation or problem solving, especially with a computer The word algorithm is derived by a distortion of the name Al-Khuwarizmi, a Persian

Mathematician of 9th Century A.D. In Computer Science, algorithm has a special significance to mean “a precise method

usable by a computer for the solution of a problem” An algorithm is composed of a finite number of steps, each of which may require one

or more operations. These operations must be unambiguous. This implies that each of these operations must be definite, clearly specifying what is meant to be done

All steps in the algorithm should be effective, that is, they should produce the desired result in a finite time frame

The algorithm should terminate after a finite number of operations. If the algorithm does not terminate in certain time limit, we do have a serious problem.

Thus, an algorithm should be definite, effective and finite

Page 5: CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS)…

CSC-305 Design and Analysis of Algorithms BS(CS) -6 Fall-2014

Algorithm Basics5

Program A program is an expression of algorithm in a programming language e.g. C, C++, Java,

FORTRAN etc.

Five Distinct Areas in the Study of Algorithms To devise an algorithm: This is an activity where human intelligence is definitely

required; some of the strategies used have a general applicability like dynamic programming, divide-and conquer, back-tracking, and so on.

To express an algorithm: An algorithm can be expressed in various ways: flow-chart, pseudo-code, program, and the like. Out of these only the program in certain programming languages are acceptable to a computer.

Page 6: CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS)…

CSC-305 Design and Analysis of Algorithms BS(CS) -6 Fall-2014

Algorithm Basics6

Five Distinct Areas in the Study of Algorithms To validate an algorithm: This means that a program satisfies its precise specifications.

This involves proving the correctness of an algorithm. Once an algorithm is shown to be correct, only then should the program coding begin. The program also has to be verified by testing, profiling and so on.

To analyze an algorithm: This field is called analysis of algorithms. It is concerned with the amount of computer time and storage that is required by the algorithm. This study allows us to compare valid algorithms for efficiency.

To test a program: Testing a program consists of two parts – debugging and profiling. Debugging can only point to the presence of errors and never their absence. Profiling is a process of executing a correctly working program with the appropriate data sets, and measuring the time and memory space it takes to execute various parts of the program.

Page 7: CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS)…

CSC-305 Design and Analysis of Algorithms BS(CS) -6 Fall-2014

Algorithm Basics7

Algorithm and Data Structure An algorithm is a recipe for performing a certain task A data structure is a way of arranging data to make solving a particular problem

easier A data structure could be a way of arranging values in an array, a linked list that

connects items in a certain pattern, a tree, a graph, a network etc. An algorithm does not necessarily need a data structure. For example, many of the

numeric algorithms do not use data structures On the other hand, there is a need to use some sort of algorithm to build the data

structure.

Page 8: CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS)…

CSC-305 Design and Analysis of Algorithms BS(CS) -6 Fall-2014

Algorithm Basics8

Qualitative Aspects of Good Solutions (Algorithms) They are simple yet powerful and general solutions They can be easily understood by others which means that the implementation is

clear and concise in the use of without being tricky They can be easily modified if necessary They give correct results for all clearly specified cases, including extreme cases They are economical in the use of computer time, computer storage, and peripherals They are properly documented, so that they can be used by others for their own

applications They are not dependent on any particular computer hardware or operating system on

which they are run or implemented – system independence (portability) They can be used as sub-procedures for other problems and applications They are pleasing and satisfying to their designer – no patch-work solution

Page 9: CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS)…

CSC-305 Design and Analysis of Algorithms BS(CS) -6 Fall-2014

Algorithm Basics9

Quantitative Aspects of Good Solutions (Algorithms) Quantitative measures are valuable as they provide a way of directly predicting and

evaluating the performance of a good solution and comparing the relative performance of two or more solutions

More efficient solutions result in saving in computations, which results in saving in computer resource usage which in turn saves time and money

Algorithm Features A good algorithm must have three features: correctness, maintainability and

efficiency

Page 10: CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS)…

CSC-305 Design and Analysis of Algorithms BS(CS) -6 Fall-2014

Algorithm Basics10

Correctness A program works correctly, outputting the expected behaviour and results in the

complete range of data that is required to handle One cannot demonstrate the correctness of the program by simply taking each of the

possible input data, feeding it to the program, and showing that the result is correct.

Example: A boy sent to the market to buy a match box and asked to check the box for any moisture before buying it. The boy went on lighting one match-stick after another, till all were burnt. When asked why he did so, he replied

“But then how can I be sure if the other matches will light?” Unfortunately, the computer programmers are in the same situation. How can they be sure that the program is correct without exhaustive testing?

Page 11: CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS)…

CSC-305 Design and Analysis of Algorithms BS(CS) -6 Fall-2014

Algorithm Basics11

Correctness He could have argued that if the box had absorbed moisture, there was a good

probability that all the match-sticks would be equally moist. So if he had tested any one of them, it should tell him about the whole box

Thus, he could have used limited testing plus proof instead of exhaustive testing

Efficiency A program should be efficient, both in terms of execution time and the amount of

resources like main memory it uses However, the correctness requirement is fundamental, what will one do with a

program that is very fast but does not give correct results?

Page 12: CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS)…

CSC-305 Design and Analysis of Algorithms BS(CS) -6 Fall-2014

Algorithm Basics12

Maintainability If an algorithm isn’t maintainable, it is dangerous to use in a program. If an

algorithm is simple, intuitive, and elegant, one can be confident that it is producing correct results, and one can fix it if it does not.

If the algorithm is intricate, confusing, and convoluted, one may have a lot of trouble implementing it, and one will have even more trouble fixing it if a bug arises.

If it is hard to understand, how can one know if it is producing correct results?

Page 13: CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS)…

CSC-305 Design and Analysis of Algorithms BS(CS) -6 Fall-2014

Algorithm Basics13

Pseudo-code Pseudo-code is text that is a lot like a programming language but that is not really a programming language The idea is to give the structure and details that would need to implement the algorithm in code without

tying the algorithm to a particular programming language The following snippet shows an example of pseudo-code for an algorithm that calculates the greatest

common divisor (GCD) of two integers: // Find the greatest common divisor of a and b. // GCD (a, b) = GCD(b, a Mod b). Integer: Gcd (Integer: a, Integer: b) While (b != 0) // Calculate the remainder.

Integer: remainder = a Mod b // Calculate GCD (b, remainder). a = b b = remainder End While Return a // GCD (a, 0) is a.

End Gcd