Sahni provides several applications for each data structure and algorithm design method discussed, taking examples from topics such as bu, compression and coding, and image processing. To think algorithmically and get the spirit of how algorithms are designed. Recursion is more expensive than corresponding iterative solutions since bookkeeping is necessary. In each step reduce the range of the search space by half.

Author:Kazisar Gokazahn
Language:English (Spanish)
Published (Last):18 October 2013
PDF File Size:6.38 Mb
ePub File Size:6.51 Mb
Price:Free* [*Free Regsitration Required]

The expr may be omitted in which case a return is made to the calling procedure. The execution of an end at the end of procedure implies a return. A procedure may be invoked by using a call statement call NAME parameter list Procedures may call themselves, direct recursion, or there may be a sequence resulting in indirect recursion. Though recursion often carries with it a severe penalty at execution time, it remains all elegant way to describe many computing processes.

This penalty will not deter us from using recursion. Many such programs are easily translatable so that the recursion is removed and efficiency achieved. All procedures are treated as external, which means that the only means for communication between them is via parameters. This may be somewhat restrictive in practice, but for the purpose of exposition it helps to list all variables explicitly, as either local or parameter.

The association of actual to formal parameters will be handled using the call by reference rule. This means that at run time the address of each parameter is passed to the called procedure. Parameters which are constants or values of expressions are stored into internally generated words whose addresses are then passed to the procedure. We avoid the problem of defining a "format" statement as we will need only the simplest form of input and output.

The command stop halts execution of the currently executing procedure. Comments may appear anywhere on a line enclosed by double slashes, e. An n-dimensional array A with lower and upper bounds li, ui, 1 i n may be declared by using the syntax declare A l1:u1, We have avoided introducing the record or structure concept. These are often useful features and when available they should be used.

However, we will persist in building up a structure from the more elementary array concept. Since most of the SPARKS programs will be read many more times than they will be executed, we have tried to make the code readable. This is a goal which should be aimed at by everyone who writes programs.

The SPARKS language is rich enough so that one can create a good looking program by applying some simple rules of style. See the book The Elements of Programming Style by Kernighan and Plauger for more examples of good rules of programming. This method uses the philosophy: write something down and then try to get it working. Surprisingly, this method is in wide use today, with the result that an average programmer on an average job turns out only between five to ten lines of correct code per day.

We hope your productivity will be greater. But to improve requires that you apply some discipline to the process of creating programs. To understand this process better, we consider it as broken up into five phases: requirements, design, analysis, coding, and verification. Make sure you understand the information you are given the input and what results you are to produce the output. Try to write down a rigorous description of the input and output which covers all cases.

You are now ready to proceed to the design phase. Designing an algorithm is a task which can be done independently of the programming language you eventually plan to use.

In fact, this is desirable because it means you can postpone questions concerning how to represent your data and what a particular statement looks like and concentrate on the order of processing. You may have several data objects such as a maze, a polynomial, or a list of names. For each object there will be some basic operations to perform on it such as print the maze, add two polynomials, or find a name in the list.

Assume that these operations already exist in the form of procedures and write an algorithm which solves the problem according to the requirements. Use a notation which is natural to the way you wish to describe the order of processing.

Can you think of another algorithm? If so, write it down. Next, try to compare these two methods. It may already be possible to tell if one will be more desirable than the other. You must now choose representations for your data objects a maze as a two dimensional array of zeros and ones, a polynomial as a one dimensional array of degree and coefficients, a list of names possibly as an array and write algorithms for each of the operations on these objects.

The order in which you do this may be crucial, because once you choose a representation, the resulting algorithms may be inefficient. Modern pedagogy suggests that all processing which is independent of the data representation be written out first. By postponing the choice of how the data is stored we can try to isolate what operations depend upon the choice of data representation. You should consider alternatives, note them down and review them later. Finally you produce a complete version of your first program.

It is often at this point that one realizes that a much better program could have been built. Perhaps you should have chosen the second design alternative or perhaps you have spoken to a friend who has done it better.

This happens to industrial programmers as well. If you have been careful about keeping track of your previous work it may not be too difficult to make changes. It is usually hard to decide whether to sacrifice this first attempt and begin again or just continue to get the first version working.

Different situations call for different decisions, but we suggest you eliminate the idea of working on both at the same time. If you do decide to scrap your work and begin again, you can take comfort in the fact that it will probably be easier the second time.

In fact you may save as much debugging time later on by doing a new version now. This is a phenomenon which has been observed in practice. The graph in figure 1. For each compiler there is the time they estimated it would take them and the time it actually took. For each subsequent compiler their estimates became closer to the truth, but in every case they underestimated.

Unwarrented optimism is a familiar disease in computing. But prior experience is definitely helpful and the time to build the third compiler was less than one fifth that for the first one. Figure 1.

Verification consists of three distinct aspects: program proving, testing and debugging. Each of these is an art in itself.

Before executing your program you should attempt to prove it is correct. Proofs about programs are really no different from any other kinds of proofs, only the subject matter is different. If a correct proof can be obtained, then one is assured that for all possible combinations of inputs, the program and its specification agree.

Testing is the art of creating sample data upon which to run your program. If the program fails to respond correctly then debugging is needed to determine what went wrong and how to correct it. One proof tells us more than any finite amount of testing, but proofs can be hard to obtain. Many times during the proving process errors are discovered in the code. This is another use of program proving, namely as a methodology for discovering errors.

Finally there may be tools available at your computing center to aid in the testing process. One such tool instruments your source code and then tells you for every data set: i the number of times a statement was executed, ii the number of times a branch was taken, iii the smallest and largest values of all variables.

As a minimal requirement, the test data you construct should force every statement to execute and every condition to assume the value true and false at least once. One thing you have forgotten to do is to document. But why bother to document until the program is entirely finished and correct?

Because for each procedure you made some assumptions about its input and output. If you have written more than a few procedures, then you have already begun to forget what those assumptions were. If you note them down with the code, the problem of getting the procedures to work together will be easier to solve. The larger the software, the more crucial is the need for documentation. The previous discussion applies to the construction of a single procedure as well as to the writing of a large software system.

Let us concentrate for a while on the question of developing a single procedure which solves a specific task. The design process consists essentially of taking a proposed solution and successively refining it until an executable program is achieved.

The initial solution may be expressed in English or some form of mathematical notation. At this level the formulation is said to be abstract because it contains no details regarding how the objects will be represented and manipulated in a computer. If possible the designer attempts to partition the solution into logical subtasks. Each subtask is similarly decomposed until all tasks are expressed within a programming language. This method of design is called the top-down approach.

Inversely, the designer might choose to solve different parts of the problem directly in his programming language and then combine these pieces into a complete program. This is referred to as the bottom-up approach. Experience suggests that the top-down approach should be followed when creating a program. However, in practice it is not necessary to unswervingly follow the method. A look ahead to problems which may arise later is often useful.

Underlying all of these strategies is the assumption that a language exists for adequately describing the processing of data at several abstract levels. Let us examine two examples of top-down program development. Suppose we devise a program for sorting a set of n given by the following 1 distinct integers.

One of the simplest solutions is "from those integers which remain unsorted, find the smallest and place it next in the sorted list" This statement is sufficient to construct a sorting program.

However, several issues are not fully specified such as where and how the integers are initially stored and where the result is to be placed. One solution is to store the values in an array in such a way that the i-th integer is stored in the i-th array position, A i 1 i n.


Download: Fundamentals Of Data Structures In C++, Second Edition.pdf





Related Articles