bsit 41 kuvempu university
DESCRIPTION
This is a pdf file of 4th semester algorithm book ko kuvempu university.....hav fun reading....TRANSCRIPT
INFORMATION TECHNOLOGY PROGRAMMESBachelor of Science in Information Technology - B.Sc.(IT)Master of Science in Information Technology - M.Sc. (IT)
Incollaboration
with
KUVEMPU UNIVERSITY
B.Sc.(IT) - 4th Semester
BSIT - 41 AlgorithmsBSIT - 42 Java Programming
Directorate of Distance EducationKuvempu University
Shankaraghatta, Shimoga District, Karnataka
Universal Education TrustBangalore
II
Titles in this Volume :BSIT - 41 AlgorithmsBSIT - 42 Java Programming
Prepared by UNIVERSAL EDUCATION TRUST (UET)Bangalore
First Edition : May 2005Second Edition : May 2012
Copyright © by UNIVERSAL EDUCATION TRUST, BangaloreAll rights reserved
No Part of this Book may be reproducedin any form or by any means without the writtenpermission from Universal Education Trust, Bangalore.
All Product names and company names mentionedherein are the property of their respective owners.
NOT FOR SALEFor personal use of Kuvempu UniversityIT - Programme Students only.
Corrections & Suggestionsfor Improvement of Study materialare invited by Universal Education Trust, Bangalore.
E-mail : [email protected]
Printed at :Pragathi Print CommunicationsBangalore - 20Ph : 080-23340100
III
ALGORITHMS(BSIT - 41)
: Contributing Authors :
Dr. D.S. GuruProfessor
Department of Studies in Computer ScienceUniversity of Mysore
Mysore
T.N. VikramDepartment of Studies in Computer Science
University of MysoreMysore
&
P.B. MallikarjunaDepartment of Studies in Computer Science
University of MysoreMysore
IV
Blank Page
V
a
Preface
In this course material, we address the issues related to problem solving and algorithm design. Thismaterial provides you an insight into the field of designing algorithms.
Algorithms are a specific way by which we formulate the problems before we code them in to aprogram. i.e. we are converting the problem from the way human beings look at to the manner in whicha computer can look at it.
However, there is no unique way of designing the algorithms. Given a problem to be executed on acomputer, different people can convert it into algorithms in different ways. Then, what is it that thealgorithms course is about? Even though it may not be possible to exactly teach the “art” of algorithms, itis possible to teach the “science” portion of the same. There are some basic concepts, which can holdgood irrespective of the problem domain and these can be used as “off the shelf” pieces of informationwhile devising the algorithm. In this course, you are being introduced to some of the basic algorithms wefollow.
Once the algorithm has been designed, there should also be a mechanism to decide on the “goodness”of the algorithm. This becomes more important because often more than one method of solving the givenproblem is available so, we should be able to compare one solution with another.
An algorithm not only depends on its own nature, but depends on the supporting data structures too.Thus, in this material we also cover some of the preliminary aspects of related data structures. In some ofthe parts of the material, we have assumed that the reader has a background of at least one conventionalprogramming language such as Pascal, C, C++ etc. and as well a little knowledge of data structures.
The material has been organized as a collection of 6 chapters. Chapter 1 presents in detail the necessityof taking up a course on algorithms through exploration of significance of algorithms in the field of computer
VI Preface
science. The essential knowledge on the prerequisite data structures has been, to some extent, covered inChapter 2. The chapter 3, deals with some simple problems and associated algorithms. The concept ofsearching and sorting is been introduced in chapter 4 while the concept of recursion is presented inchapter 5. In chapter 6 we have covered the concept of binary tree representation techniques and binarytree traversal schemes.
VII
a
Cont en t s
Chapter 1
REVIEW 1
1.0 Objectives........................................................................................ 11.1 Concept of algorithm......................................................................... 11.2 Characteristics of Algorithm............................................................... 21.3 Problem - Solving Aspect................................................................... 31.4 How to devise the algorithms ......................................................... 41.5 How to validate the algorithms........................................................... 51.6 How to Test the algorithms................................................................ 51.7 Algorithmic Notations........................................................................ 5
Summary.......................................................................................... 7Exercise........................................................................................... 7
Chapter 2
ELEMENTARY DATA STRUCTURES 8
2.0 Objectives........................................................................................ 82.1 Fundamentals................................................................................... 82.2 Linear Data Structures..................................................................... 9
2.2.1 Array and its representation................................................ 92.2.2 Stacks................................................................................ 112.2.3 Queues.............................................................................. 132.2.4 Circular Queue................................................................... 17
2.3 Linked Lists...................................................................................... 192.4 Non-Linear data structures................................................................ 21
VIII
2.4.1 Introduction to Graph theory................................................ 212.4.1.1 Finite and infinite graphs....................................... 222.4.1.2 Incidence and degree............................................ 222.4.1.3 Isolated vertex, pendent vertex and Null Graph....... 232.4.1.4 Walk, Path and Connected Graph........................... 23
2.4.2 Matrix representation of Graphs.......................................... 242.4.2.1 Adjacency Matrix................................................. 242.4.2.2 Incidence Matrix.................................................. 25
2.4.3 Trees................................................................................. 262.4.3.1 Some properties of trees....................................... 26
Summary.......................................................................................... 27Exercise........................................................................................... 27
Chapter 3
SOME SIMPLE ALGORITHMS 28
3.0 Objectives........................................................................................ 283.1 Addition of two numbers.................................................................... 293.2 Exchanging the values of two variables............................................... 293.3 Input three numbers and output them in ascending order...................... 293.4 To find the Quadrant of a given Co-ordinate position........................... 323.5 To Find the Roots of a Quadratic Equation......................................... 333.6 Checking for Prime........................................................................... 353.7 Factorial of a Number....................................................................... 373.8 To Generate Fibonacci Series............................................................ 383.9 Sum of ‘N’ Numbers and Average..................................................... 393.10 To Add Two Matrices....................................................................... 40
Summary.......................................................................................... 41Exercise........................................................................................... 42
Chapter 4
SEARCHING AND SORTING 434.0 Objectives........................................................................................ 434.1 Searching......................................................................................... 43
4.1.1 Sequential Search............................................................... 434.1.2 Binary Search.................................................................... 44
4.2 Sorting............................................................................................. 464.2.1 Insertion Sorting................................................................. 464.2.2 Selection Sorting................................................................. 484.2.3 Bubble Sort......................................................................... 50Summary.......................................................................................... 52Exercise........................................................................................... 52
Contents
IXContents
Chapter 5
RECURSION 54
5.0 Objectives........................................................................................ 545.1 What is Recursion?........................................................................... 545.2 Why do we need Recursion?............................................................. 555.3 When to use Recursion?................................................................... 555.4 Factorial of a Positive Number........................................................... 565.5 Finding the nth Fibonacci Number........................................................ 575.6 Sum of First N Integers..................................................................... 585.7 Binary Search .................................................................................. 585.8 Maximum and Minimum in the given list of N Elements....................... 595.9 Merge Sort....................................................................................... 615.10 Quick Sort ....................................................................................... 635.11 The Towers of Hanoi Problem........................................................... 66
5.11.1 The Recursive Algorithm..................................................... 665.12 Demerits of Recursion...................................................................... 69
Summary.......................................................................................... 70Exercise........................................................................................... 70
Chapter 6
REPRESENTATION AND TRAVERSAL OF A BINARY TREE 72
6.0 Objectives........................................................................................ 726.1 Binary Tree...................................................................................... 726.2 Representation of a Binary Tree........................................................ 74
6.2.1 Adjacency Matrix Represntation......................................... 746.2.2 Single Dimensional Array Representation............................. 756.2.3 Linked Representation of Binary Trees................................. 766.2.4 Binary Tree as a Data Structure........................................... 79
6.3 Traversal of a Binary Tree................................................................. 806.3.1 Traversal of a Binary Tree Represented in an Adjacency
Matrix .............................................................................. 816.3.2 Binary Tree Traversal of a Binary Tree From One
Dimensional Array Representation...................................... 836.3.3 Binary Tree Traversal in Linked Representation................... 85
6.4 Operations on Binary Tree................................................................. 86Summary.......................................................................................... 93Exercise........................................................................................... 93
References.............................................................................. 94
Chapter 1
Review
1.0 OBJECTIVES
After studying this chapter, we will be able to explain the following :
Basic concepts of algorithm
Characteristics of an algorithm
Problem solving aspects
Validation of the algorithms
Testing of algorithms
1.1 CONCEPT OF ALGORITHM
A common man’s belief is that a computer can do anything and everything that he imagines. It is verydifficult to make people realize that it is not really the computer but the man behind computer who doeseverything.
In the modern internet world, man feels that just by entering what he wants to search into the computershe can get information as desired by him. He believes that, this is done by computer. A common manseldom understands that a man made procedure called search has done the entire job and the only supportprovided by the computer is the execution speed and organized storage of information.
BSIT 41 Algorithms 1
22
In the above instance, a designer of the information system should know what one frequently searchesfor. He should make a structured organization of all those details to store in memory of the computer.Based on the requirement, the right information is brought out. This is accomplished through a set ofinstructions created by the designer of the information system to search the right information matching therequirement of the user. This set of instructions is termed as program. It should be evident by now that itis not the computer, which generates automatically the program but it is the designer of the informationsystem who has created this.
Thus, the program is the one, which through the medium of the computer executes to perform all theactivities as desired by a user. This implies that programming a computer is more important than thecomputer itself while solving a problem using a computer and this part of programming has got to be doneby the man behind the computer. Even at this stage, one should not quickly jump to a conclusion thatcoding is programming. Coding is perhaps the last stage in the process of programming. Programminginvolves various activities form the stage of conceiving the problem up to the stage of creating a model tosolve the problem. The formal representation of this model as a sequence of instructions is called analgorithm and coded algorithm in a specific computer language is called a program.
One can now experience that the focus is shifted from computer to computer programming and thento creating an algorithm. This is algorithm design, heart of problem solving.
1.2 CHARACTERISTICS OF AN ALGORITHM
Let us try to present the scenario of a man brushing his own teeth(natural denture) as an algorithm asfollows:
Step 1. Take the brush
Step 2. Apply the paste
Step 3. Start brushing
Step 4. Rinse
Step 5. Wash
Step 6. Stop
If one goes through these 6 steps without being aware of the statement of the problem, he couldpossibly feel that this is the algorithm for cleaning a toilet. This is because of several ambiguities whilecomprehending every step. Step-1 may imply tooth brush, paint brush, toilet brush, etc. Such an ambiguityarises from the instruction of the above algorithmic step. Thus every step has to be made unambiguous.An unambiguous step is called definite instruction. Even if step 2 is rewritten as ‘apply the tooth paste’, toeliminate ambiguities, yet the conflicts such as, where to apply the tooth paste and where is the source ofthe tooth paste, need to be resolved. Hence, the act of applying the toothpaste is not mentioned. Althoughunambiguous, such unrealizable steps can’t be included as algorithmic instruction as they are not effective.
Chapter 1 - Review
3BSIT 41 Algorithms
The definiteness and effectiveness of an instruction implies the successful termination of that instruction.However the above two may not be sufficient to guarantee the termination of the algorithm. Therefore,while designing an algorithm care should be taken to provide a proper termination for algorithm.
Thus, every algorithm should have the following five characteristic feature
1) Input
2) Output
3) Definiteness
4) Effectiveness
5) Termination
Therefore, an algorithm can be defined as a sequence of definite and effective instructions, whichterminates with the production of correct output from the given input.
In other words, viewed little more formally, an algorithm is a step by step formalization of a mappingfunction to map input set onto an output set.
The problem of writing down the correct algorithm for the above problem of brushing the teeth is leftto the reader.
1.3 PROBLEM - SOLVING ASPECT
Problem-solving is a creative process which largely defies systematization and mechanization. Evenif one is not naturally skilled at problem-solving there are a number of steps that can be taken to raise thelevel of one’s performance. The plain fact of the matter is that there is no universal method. Differentstrategies appear to work for different people.
The first step in problem-solving is understand the problem in hand i.e., problem definition phase. Thisphase implies work out what must be done rather how to do it. In other words, extract a set of preciselydefined tasks from the problem statement. The next step in problem solving is how to start on a problem.A block often occurs at this point because people becomes concerned with details of the implementationbefore they have completely understood or worked out an implementation-independent solution. Thereare number of ways to get start a problem-a specific example of the general problem we wish to solveand try to work out the mechanism that will allow us to solve this particular problem and aware of thesimilarities among problems. In the first case it is very easy to fall into the trap of thinking that the solutionto a specific problem or a specific class of problems is also a solution to the general problem. In secondcase sometimes it blocks us from discovering a desirable or better solution to a problem. In trying to geta better solution to a problem, it is usually wise, in the first instance at least, to try to independently solve
4
the problem. A skill that it is important to try to develop in problem-solving is a ability to view a problemfrom a variety of angles. Once one has developed this skill it should be possible to get started on anyproblem.
There are number of general and powerful computational strategies for example, divide-and-conqueran dynamic programming. These general strategies will tend to guide a computation in such a way thatthe minimum amount of effort is expended on exploring solutions.
1.4 HOW TO DEVISE THE ALGORITHMS
The process of devising an algorithm is both an art and a science. This is one part that cannot beautomated fully. Given a problem description, one have to think of converting this into a series of steps,which, when executed in a given sequence solve the problem. To do this, one has to be familiar with theproblem domain and also the computer domains. This aspect may never be taught fully and most often,given a problem description, how a person proceeds to covert it into an algorithm becomes a matter of his“style” – no firm rules become applicable here.
For the purpose of clarity in understanding, let us consider the following example.
Problem: Finding the largest value among n>=1 numbers.
Input: the value of n and n numbers
Output: the largest value
Steps :
1. Let the value of the first be the largest value denoted by BIG
2. Let R denote the number of remaining numbers. R=n-1
3. If R != 0 then it is implied that the list is still not exhausted. Therefore look the next numbercalled NEW.
4. Now R becomes R-1
5. If NEW is greater than BIG then replace BIG by the value of NEW
6. Repeat steps 3 to 5 until R becomes zero.
7. Print BIG
8. Stop
Chapter 1 - Review
5BSIT 41 Algorithms 5
End of algorithm
1.5 HOW TO VALIDATE THE ALGORITHMS
Once an algorithm has been devised, it becomes necessary to show that it works. i.e it computes thecorrect answer to all possible, legal inputs. One simple way is to code it into a program. However,converting the algorithms into programs is a time consuming process. Hence, it is essential to be reasonablysure about the effectiveness of the algorithm before it is coded. This process, at the algorithm level, iscalled “validation”. Several mathematical and other empirical methods of validation are available. Providingthe validation of an algorithm is a fairly complex process and most often a complete theoretical validation,though desirable, may not be provided. Alternately, algorithm segments, which have been proved elsewhere may be used and the overall working algorithm may be empirically validated for several test cases.Such methods, although suffice in most cases, may often lead to the presence of unidentified bugs or sideeffects later on.
1.6 HOW TO TEST THE ALGORITHMS
If there are more then one possible way of solving a problem, then one may think of more than onealgorithm for the same problem. Hence, it is necessary to know in what domains these algorithms areapplicable. Data domain is an important aspect to be known in the field of algorithms. Once we have morethan one algorithm for a given problem, how do we choose the best among them? The solution is to devisesome data sets and determine a performance profile for each of the algorithms. A best case data set canbe obtained by having all distinct data in the set.
The ultimate test of an algorithm is that the programs based on the algorithm should run satisfactorily.Testing a program really involves two phases a) debugging and b) profiling. Debugging is the process ofexecuting programs with sample datasets to determine if the results obtained are satisfactory. Whenunsatisfactory results are generated, suitable changes are made in the program to get the desired results.On the other hand, profiling or performance measurement is the process of executing a correct programon different data sets to measure the time and space that it takes to compute the results.
However, it is pointed out that “debugging can only indicate the presence of errors but not the absenceof it”. i.e., a program that yields unsatisfactory results with a sample data set is definitely faulty, but justbecause a program is producing the desirable results with one/more data sets cannot prove that theprogram is ideal. Even after it produces satisfactory results with say 10000 data sets, it’s results may befaulty with the 10001th set. In order to actually prove that a program is perfect, a process called “proving”is taken up. Here, the program is analytically proved to correct and in such cases, it is bound to yieldperfect results for all possible sets of data.
6
1.7 ALGORITHMIC NOTATIONS
In this section we present the pseudo code that we use through out the book to describe algorithms.The pseudo code used resembles PASCAL and C language control structures. Hence, it is expected thatthe reader be aware of PASCAL/C. Even otherwise at least now it is required that the reader shouldknow preferably C to practically test the algorithm in this course work.
However, for the sake of completion we present the commonly employed control constructs present inthe algorithms.
1. A conditional statement has the following form
If < condition> then
Block 1
Else
Block 2
If end.
This pseudo code executes block1 if the condition is true otherwise block2 is executed.
2. The two types of loop structures are counter based and conditional based and they are as follows
For variable = value1 to value2 do
Block
For end
Here the block is executed for all the values of the variable from value 1 to value 2.
There are two types of conditional looping, while type and repeat type.
While (condition) do
Block
While end.
Here block gets executed as long as the condition is true.
Repeat
Block
Until<condition>
Chapter 1 - Review
7BSIT 41 Algorithms
Here block is executed as long as condition is false. It may be observed that the block is executed atleast once in repeat type.
SUMMARY
In this chapter, the concepts of algorithm are presented and the properties of the algorithm are alsogiven. The concept of designing algorithm is explained with a specific example. The concepts of validatingthe algorithms and test the devised algorithms is also presented.
EXERCISE
1. __________ is the process of executing a correct program on data sets and measuring the time and space ittakes to compute the results.
2. Define algorithm? What are its properties
3. What is debugging and What is profiling?
4. One of the properties of an algorithm is beauty (true / false)
5. What is pseudo code ? Give an example.
6. How did you validate the algorithms ?
7. What is Problem solving ? Illustrate the different aspects in it.
8. What are the general steps in solving a problem ?
9. Name the important problem types.
10. How to device an algorithm ? Explain ?
Chapter 2
Elem en t ar y Dat a St r u ct u r es
2.0 OBJECTIVES
After studying this chapter, we will be able to explain the following :
Define the Java technology
Concept of data structure
Linear data structures : Arrays, Stacks, Queues, Circular queue and Linked lists
Concept of non linear data structures
Properties of Graphs
Different representation of graphs
2.1 FUNDAMENTALS
Data structure is a method of organizing data with a sort of relationship with an intension of enhancingthe qualities of associated algorithms. In this chapter, we review only the non-primitive data structures.The non-primitive data structures can be broadly classified into two types linear and non-linear datastructures. Under linear data structures Arrays, Stacks and Queues and under non-linear data structuresgraphs and trees are reviewed.
8 Chapter 2 - Elementary Data Structures
2.2 LINEAR DATA STRUTURES
A data structure in which every data element has got exactly two neighbors or two adjacent elementsexcept two elements having exactly one data element is called a linear data structure. Otherwise it iscalled a nonlinear data structure.
2.2.1 Array and its representation
Array is a finite ordered list of data elements of same type. In order to create an array it is required toreserve adequate number of memory locations. The allocated memory should be contiguous in nature.The size of the array is finite and fixed up as a constant. Some of the important operations related toarrays are,
Creation () A[n], Array created (reservation of adequate number of memory locations) memoryreserved;
Write (A, i, e) Updated array with ‘e’ at ith position;
Compare (i, j, Relationl operator) Boolean;
Read (A, i) ‘e’ element at ith position;
Search (A, e) Boolean;
REPRESENTATION OF A SINGLE DIMENSIONAL ARRAY
Any array is associated with a lower bound l and an upper bound u in general. When the array iscreated then it starts at some base address B. Since the computer is byte addressable, each address canhold a byte. If an integer takes 2 bytes for representation and if B=1000 and if l=1, then
the 1st element is at (1000 + 0)th location
2nd element is at (1000 + 2)th location
3rd element is at (1000 + 4)th location
..
..
..
ith element is at (1000 + (i-1)*2 )th location
1 2 3 4 5 6
3 7 -8 10 15 5 …… 50 20
1000 1002 1004 1006 1008 1010
BSIT 41 Algorithms 9
10 Chapter 2 - Elementary Data Structures
In general, if w is the size of the data element, then ith element is at (B + (i – l) w)th location. i.e., A[i]= B + (i – l) w.
REPRESENTATION OF A TWO DIMENSIONAL ARRAY
The two dimensional array A[l1..u
1] [l
2..u
2] may be interpreted as n = u
1-l
1+1 rows and m = u
2-l
2+1
columns i.e., each row consists of u2-l
2+1 elements.
Although we have an n´m matrix representation the allocations is not in the form of matrix but iscontiguous in nature and will be as shown below. Incase if l
1=1, and l
2=1 the indices start at [1, 1].
Given the indices (i, j), the address can be computed using A[i, j] = B + (i-1) m+ (j-1).
In general, if li£ i £u
i and l
j£ j £u
j , then A[i, j] = B + { (i - l
i ) (u
j – l
j +1) +(j - l
j )}w.
For higher n-dimensional arrays, the address can be computed as
A( i1, i
2, i
3, ….i
n) where l
1£ i
1£u
1, l
2£ i
2£u
2, … l
n£ i
n£u
n
A( i1, i
2, i
3, ….i
n) = B + { (i
1 - l
1) (u
2 - l
2 +1)(u
3-l
3+1) ….(u
n- l
n+1) + (i
2 - l
2) (u
3 - l
3 +1)(u
4-l
4+1) ….
(un- l
n+1) + (i
3 - l
3) (u
4 - l
4 +1) (u
5-l
5+1) ….(u
n- l
n+1)
+ ……+
(in-1
- ln-1
) (un - l
n +1) + (i
n- l
n) }w
The algorithm thus designed to compute the row major address is as follows
Algorithm: Address Computation (Row Major)
Input: (1) n, dimension
1,1 1,2 1,3 …. 1,m 2,1 2,2 2,3 … 2,m …. ….. n,1 n,2 …. n,m
l2 l2+1 l2+2 l2+3 l2+4 …. u2
l1
l1+1
l1+2
.
u1
11BSIT 41 Algorithms
(2) l1, l
2, l
3, … l
n n lower limits
(3) u1, u
2, u
3, … u
n n upper limits
(4) w, word size
(5) i1, i
2, i
3, …., i
n values of subscripts
(6) B, the base address
Output: ‘A’ address of the element at (i1, i
2, i
3, …., i
n)
Method:
Algorithm ends.
2.2.2 Stacks
A stack is an ordered list in which all insertions and deletions are made at one end, called the top. Stackis a linear data structure which works based on the strategy last-in-first out (LIFO). It is a linear datastructure which is open for operations at only one end (both insertions and deletions are defined at onlyone end). One natural example of stack, which arises in computer programming, is the processing ofprocedure calls and their terminations. Stacks are generally used to remember things in reverse. It findsa major use in backtracking approaches.
Some of the functions related to a stack are
Create ( ) S;
Insertion(S, e) updated S;
Deletion (S) S, e;
Isfull(S) Boolean;
Isempty(S) Boolean;
Top(S) e;
Destroy(S)
n
jkkkj
n
jjjj
luPwhere
wPliBA
1
1
)1(
)(
12
It has to be noted that with respect to a stack, insertion and deletion operations are, in general, calledPUSH and POP operations respectively. Following are the algorithms for some functions of stack.
Algorithm: Create
Output: S, Stack created
Method:
Declare S[SIZE] //Array of size=SIZE
Declare and Initialize T=0 //Top pointer to remember the number of elements
Algorithm ends
Algorithm: Isempty
Input: S, stack
Output: Boolean
Method:
If (T==0)
Return (yes)
Else
Return (no)
If end
Algorithm ends
Algorithm: Isfull
Input: S, stack
Output: Boolean
Method:
If (T==SIZE)
Return (yes)
Else
Return (no)
If end
Algorithm ends
Chapter 2 - Elementary Data Structures
13BSIT 41 Algorithms
Algorithm: Push
Input: (1) S, stack; (2) e, element to be inserted; (3) SIZE, size of the stack;
(4) T, the top pointer
Output: (1) S, updated; (2) T, updated
Method:
If (Isfull(S)) then
Print (‘stack overflow’)
Else
T=T+1;
S[T] =e
If end
Algorithm ends
Algorithm: Pop
Input: (1) S, stack;
Output: (1) S, updated; (2) T, updated (3) ‘e’ element popped
Method:
If (Isempty(S)) then
Print (‘stack is empty’)
Else
e = S[T]
T=T-1;
If end
Algorithm ends
2.2.3 Queues
A queue is an ordered list in which all insertions take place at one end called the rear end, while alldeletions take place at the other end called the front end. Queue is a linear data structure which worksbased on the strategy first-in-first out (FIFO). Unlike stacks, queues also arise quite naturally in the
14 Chapter 2 - Elementary Data Structures
computer solution of many problems. Perhaps the most common occurrence of a queue in computerapplications is for scheduling of jobs. A minimal set of useful operations on queue includes the following.
Create ( ) Q;
Insertion (Q, e) updated Q;
Deletion (Q) Q, e;
Isfull (Q) Boolean;
Isempty (Q) Boolean;
Front (Q) e;
Back (Q);
Destroy (Q);
Following are the algorithms for some functions of queue.
Algorithm: Create
Output: Q, Queue created
Method:
Declare Q[SIZE] //Array with size=SIZE
Declare and Initialize F=0, R=0
//Front and Rear pointers to keep track of the front element and the rear element respectively
Algorithm ends
Algorithm: Isempty
Input: Q, Queue
Output: Boolean
Method:
If (F==0)
Return (yes)
Else
Return (no)
If end
15BSIT 41 Algorithms
Algorithm ends
Algorithm: Isfull
Input: Q, Queue
Output: Boolean
Method:
If (R==SIZE)
Return (yes)
Else
Return (no)
If end
Algorithm ends
Algorithm: Front
Input: Q, Queue
Output: element in the front
Method:
If (Isempty (Q))
Print ‘no front element’
Else
Return (Q[F])
If end
Algorithm ends
Algorithm: Rear
Input: Q, Queue
Output: element in the rear
16 Chapter 2 - Elementary Data Structures
Method:
If (Isempty (Q))
Print ‘no back element’
Else
Return (Q[R])
If end
Algorithm ends
Algorithm: Insertion
Input: (1) Q, Queue; (2) e, element to be inserted; (3) SIZE, size of the Queue;
(4) F, the front pointer; (5) R, the rear pointer
Output: (1) Q, updated; (2) F, updated; (3) R, updated
Method:
If (Isfull (Q)) then
Print (‘overflow’)
Else
R=R+1;
Q[R]=e
If (F==0)
F=1;
If end
If end
Algorithm ends
Algorithm: Deletion
Input: (1) Q, Queue; (2) SIZE, size of the Queue; (3) F, the front pointer; (4) R, the rear pointer
Output: (1) Q, updated; (2) F, updated; (3) R, updated; (4) e, element if deleted;
17BSIT 41 Algorithms
Method:
If (Isempty (Q)) then
Print (‘Queue is empty’)
Else
e = Q[F]
If (F==R)
F=R=0;
Else
F=F+1;
If end
If end
Algorithm ends
However, the linear queue makes less utilization of memory i.e., the ‘return (isfull (Q)) =yes’ does notnecessarily imply that there are n elements in the queue. This can be overcome by the using an alternatequeue called the circular queue.
2.2.4 Circular Queue
A circular queue uses the same conventions as that of linear queue. Using Front will always point oneposition counterclockwise from the first element in the queue. In order to add an element, it will benecessary to move rear one position clockwise. Similarly, it will be necessary to move front one positionclockwise each time a deletion is made. Nevertheless, the algorithms for create (), Isfull (), Isempty (),Front () and Rear () are same as that of linear queue. The algorithms for other functions are
Algorithm: Insertion
Input: (1) CQ, Circular Queue; (2) e, element to be inserted; (3) SIZE, size of the Circular Queue;
(4) F, the front pointer; (5) R, the rear pointer
Output: (1) CQ, updated; (2) F, updated; (3) R, updated
Method:
If (Isfull (CQ)) then
Print (‘overflow’)
18 Chapter 2 - Elementary Data Structures
Else
R=R mod SIZE + 1;
CQ[R] =e
If (Isempty (CQ))
F=1;
If end
If end
Algorithm ends
Algorithm: Deletion
Input: (1) CQ, Circular Queue; (2) SIZE, size of the CQ; (3) F, the front pointer; (4) R, the rearpointer
Output: (1) CQ, updated; (2) F, updated; (3) R, updated; (4) e, element if deleted;
Method:
If (Isempty (CQ)) then
Print (‘Queue is empty’)
Else
e = CQ[F]
If (F==R)
F=R=0;
Else
F=F mod SIZE +1;
If end
If end
Algorithm ends
19BSIT 41 Algorithms
2.3 LINKED LISTS
The linear data structures such as stacks and queues can be realized using sequential allocation techniquei.e. arrays. Since arrays represent contiguous locations in memory, implementing stacks and queues usingarrays offers several advantages.
Accessing data is faster. Since arrays works on computed addressing, direct addressing of datais possible and hence data access time is constant and faster.
Arrays are easy to understand and use. Hence implementing stacks and queues is simple andstraight forward.
In arrays, the data, which are logically adjacent are also physically adjacent and hence a loss ofdata element does not affect other part of the list.
However, the sequential allocation technique i.e. arrays suffers from serious drawbacks.
Arrays are static in nature. A fixed size of memory is allocated for a program before executionand cannot be changed during its execution. The amount of memory required by most of theapplications cannot be predicted in advance. If all the memory allocated to an application is notutilized then it results in wastage of memory space. If an application requires more than thememory allocated then additional amount of memory cannot be allocated during execution.
For realizing linear data structures using arrays, sufficient amount of contiguous storage spaceis required. Sometimes, even though enough storage space is available in the memory as chunksof contiguous locations, it can be used because they are not contiguous.
Insertion and deletion operations are time consuming and tedious tasks, if linear data structuresare implemented using contiguous allocation technique – arrays.
In order to overcome the above limitations of contiguous allocation techniques, the linear data structuressuch as stacks and queues can be implemented using linked allocation technique. Linked list structurescan be used to realize linear data structures efficiently and is a versatile mechanism suitable for use inmany kinds of general-purpose data-storage applications.
A linked representation of a data structure known as a linked list is a collection of nodes. Each nodeis a collection of fields categorized as data items and links. The data item fields hold the informationcontent or the data to be represented by the node. The link fields hold the addresses of the neighboringnodes or the nodes associated with the given node as dictated by the application. Figure 2.1(a) below,shows the general structure of a node in a linked list, where the field, which holds data item is called asInfo and the field, which holds the address is called as a Link and Figure 2.1(b) shows an instance of anode where the info field contains an integer number 90 and the address field contains the address 2468of the next node in a list. Figure 2.2 shows an example linked list of integer numbers.
20 Chapter 2 - Elementary Data Structures
(a) (b)
Figure 2.1: (a) Structure of a node (b) An instance of a node
Figure 2.2: Pictorial representation of a linked list.
From the Figure 2.2, we have observed that unlike arrays no two nodes in a linked list need bephysically contiguous. All the nodes in a linked list data structure may in fact be strewn across the storagememory making effective use of a little available space to represent a node.
In order to implement linked lists, we need to use the following mechanisms:
A mechanism to frame chunks of memory into nodes with the desired number of data items andfields.
A mechanism to determine which nodes are free and which nodes have been allocated for use.
A mechanism to obtain nodes from the free storage area for use.
A mechanism to return or dispose nodes from the reserved area to the free area after use.
We can realize the above mechanisms using the features like records or structures to define nodes anddynamic memory allocation functions to get chunks of memory and other related functions supported bymost of the programming languages.
Irrespective of the number of data items fields, a linked list is categorized as singly linked list, doublylinked list, circularly singly linked list, circularly doubly linked list.
Irrespective of the types of linked list, we can perform some general operations such as
Creation of a linked list.
Displaying the contents of a linked list.
21BSIT 41 Algorithms
Inserting an element at the front or at the rear end of a linked list.
Inserting an element at the specified position of a linked list.
Inserting an element in to a sorted linked list.
Deleting an element from a linked list at the front or at the rear end.
Deleting an element form a linked list at the specified position.
Deleting an element e from a linked list if present.
Searching for an element e in the linked list.
Reversing a linked list.
Concatenation of two linked lists.
Merging of two ordered linked lists.
Splitting a linked list.
2.4 NON-LINEAR DATA STRUCTURES
A data structure which is not linear data structure is said to be non-linear data structure. That is, a datastructure is said to be non-linear if data elements are allowed to have more than two adjacent elements.Meaning that, a data element can have any number of relations with other data elements.
The number of elements adjacent to a given element is called ‘arity’ or the degree of theelement. i.e., degree = number of relations the element has with others.
There is no upper limit placed on this number.
Some of the examples in general are : Friendship among classmates; family structure; University withevery component being a sub-component and the relations among them being non-linear; the nervoussystem of the human body, etc.
Few of the specific to Computer Science are: graph, tree.
2.4.1 Introduction to Graph Theory
A graph G = (V, E) consists of a set of objects V = {v1, v
2, …} called vertices, and another set E = {e
1,
e2, …} whose elements are called edges. Each edge e
k in E is identified with an unordered pair (v
i, v
j) of
vertices. The vertices vi, v
j associated with edge e
k are called the end vertices of e
k. The most common
representation of graph is by means of a diagram, in which the vertices are represented as points andeach edge as a line segment joining its end vertices.
22 Chapter 2 - Elementary Data Structures
Fig. 2.3
In the Fig. 2.1 edge e1 having same vertex as both its end vertices is called a self-loop. There may be
more than one edge associated with a given pair of vertices, for example e4 and e5 in Fig. 2.1. Suchedges are referred to as parallel edges.
A graph that has neither self-loop nor parallel edges are called a simple graph, otherwise it is calledgeneral graph. It should also be noted that, in drawing a graph, it is immaterial whether the lines aredrawn straight or curved, long or short: what is important is the incidence between the edges and vertices.
Because of its inherent simplicity, graph theory has a very wide range of applications in engineering,physical, social, and biological sciences, linguistics, and in numerous other areas. A graph can be used torepresent almost any physical situation involving discrete objects and a relationship among them.
2.4.1.1 Finite and Infinite Graphs
Although in the definition of a graph neither the vertex set V nor the edge set E need be finite, in mostof the theory and almost all applications these sets are finite. A graph with a finite number of vertices aswell as a finite number of edges is called a finite graph; otherwise, it is an infinite graph.
2.4.1.2 Incidence and Degree
When a vertex viis an end vertex of some edge e
j, v
i and e
j are said to be incident with (on or to) each
other. In Fig. 2.1, for example, edges e2, e
6, and e
7 are incident with vertex v
4. Two nonparallel edges are
said to be adjacent if they are incident on a common vertex. For example, e2 and e
7 in Fig. 2.1 are
adjacent. Similarly, two vertices are said to be adjacent if they are the end vertices of the same edge. InFig. 3.1, v
4 and v
5 are adjacent, but v
1 and v
4 are not.
The number of edges incident on a vertex vi, with self-loops counted twice is called the degree, d(v
i),
of vertex vi. In Fig. 2-1, for example, d(v
1) = d(v
3) = d(v
4) = 3, d(v
2) = 4, and d(v
5) = 1. Since each edge
contributes two degrees, the sum of the degrees of all vertices in G is twice the number of edges in G.
Fig. 2.1
23BSIT 41 Algorithms
2.4.1.3 Isolated Vertex, Pendent Vertex and Null Graph
A vertex having no incident edge is called an isolated vertex. In other words, isolated vertices arevertices with zero degree. Vertex v
4 and v
7 in Fig. 2.2, for example, are isolated vertices. A vertex of
degree one is called a pendent vertex or an end vertex. Vertex v3 in Fig. 2.2 is a pendant vertex. Two
adjacent edges are said to be in series if their common vertex is of degree two. In Fig. 2.2, the two edgesincident on v
1 are in series.
Fig. 2.4 Graph containing isolated vertices, series edges and a pendant vertex.
Fig. 2.5 Null graph of six vertices
In the definition of a graph G = (V, E), it is possible for the edge set E to be empty. Such a graph,without any edges, is called a null graph. In other words, every vertex in a null graph is an isolated vertex.A null graph of six vertices is shown in Fig. 2-3. Although the edge set E may be empty, the vertex set Vmust not be empty; otherwise, there is no graph. In other words, by definition, a graph must have at leastone vertex.
2.4.1.4 Walk, Path and Connected Graph
A “walk” is a sequence of alternating vertices and edges, starting with a vertex and ending with avertex with any number of revisiting vertices and retracing of edges. If a walk has the restriction of norepetition of vertices and no edge is retraced it is called a “path”. If there is a walk to every vertex fromany other vertex of the graph then it is called a “connected” graph.
24
2.4.2 Matrix Representation of Graphs
Although a pictorial representation of a graph is very convenient for a visual study, other representationsare better for computer processing. A matrix is a convenient and useful way of representing a graph to acomputer. Matrices lend themselves easily to mechanical manipulations. Besides, many known results ofmatrix algebra can be readily applied to study the structural properties of graphs from an algebraic pointof view. In many applications of graph theory, such as in electrical network analysis and operationresearch, matrices also turn out to be the natural way of expressing the problem.
2.4.2.1 Adjacency matrix Representation
Since the edges are the relationship between two vertices, the graph can be represented by a matrix.Consider a 2D matrix X of size |v| × |v where |v| is the number of vertices in
Fig. 2.6
Thus the matrix where each column and row corresponds to the vertex in the graph G shown in Fig.2.6 is
This matrix X uniquely represents the graph. It is indeed possible to reconstruct the graph back fromthe matrix. The high entry in the matrix says the vertex corresponding to the row and vertex corresponding
G. The entries in the matrix are either zero or one. i.e.,
otherwise
EtobelongsvvifX
ji
0
),(
V1 V2 V3 V4 V5 V6V1 0 1 0 0 1 0
V2 1 0 1 1 1 0
V3 0 1 0 0 0 0
V4 0 1 0 0 1 1 (2)V5 1 1 0 1 0 0V6 0 0 0 1 (2) 0 0
Chapter 2 - Elementary Data Structures
25BSIT 41 Algorithms
to the column is adjacent. Since, two vertices are adjacent if there is an edge connecting them, and thematrix represents the same, the above matrix X is called an adjacency matrix.
Adjacency matrix is a symmetric matrix. If the diagonal elements are high then the graph has a selfloop, the respective row index gives the vertex label with self loop. The sum of the ith row elements givesthe degree of the vertex V
i. While calculating the row sum of the adjacency matrix of a non-simple graph,
a weightage 1 has to be given if the diagonal cell is high and additional weightage to every parallel edge.
2.4.2.2 Incidence Matrix
Let G be a graph with n vertices, e edges, and no self-loops. Define an n by e matrix A =[aij], whose
n rows correspond to the n vertices and the e columns correspond to the e edges, as follows:
The matrix element
Aij = 1, if jth edge e
j is incident on ith vertex v
i, and
= 0, otherwise.
(b)
Fig. 2.7 Incidence matrix of the graph in Fig. 2.6.
Such a matrix A is called the vertex-edge incidence matrix, or simply incidence matrix. Matrix Afor a graph G is sometimes also written as A(G). A graph and its incidence matrix are shown in Fig. 2.6and Fig. 2.7 respectively. The incidence matrix contains only two elements, 0 and 1. Such a matrix iscalled a binary matrix or a (0, 1)-matrix.
The following observations about the incidence matrix A can readily be made:
1. Since every edge is incident on exactly two vertices, each column of A has exactly two1’s.
2. The number of 1’s in each row equals the degree of the corresponding vertex.
3. A row with all 0’s, therefore, represents an isolated vertex.
a b c d e f g h
v1 0 0 0 1 0 1 0 0
v2 0 0 0 0 1 1 1 1
v3 0 0 0 0 0 0 0 1
v4 1 1 1 0 1 0 0 0
v5 0 0 1 1 0 0 1 0
v6 1 1 0 0 0 0 0 0
26
4. Parallel edges in a graph produce identical columns in its incidence matrix, for example, columns1 and 2 in Fig. 2.7.
2.4.3 Trees
The concept of a tree is probably the most important in graph theory, especially for those interested inapplications of graphs.
A tree is a connected graph without any circuits. The graph in Fig 2.8 for instance, is a tree. It followsimmediately from the definition that a tree has to be a simple graph, that is, having neither a self-loop norparallel edges (because they both form circuits).
Fig. 2-8. Tree
Trees appear in numerous instances. The genealogy of a family is often represented by means of atree. A river with its tributaries and sub-tributaries can also be represented by a tree. The sorting of mailaccording to zip code and the sorting of punched cards are done according to a tree (called decision treeor sorting tree).
2.4.3.1 Some Properties of Tree
1. There is one and only one path between every pair of vertices in a tree, T.
2. A tree with n vertices has n-1 edges.
3. Any connected graph with n vertices and n-1 edges is a tree.
4. A graph is a tree if and only if it is minimally connected.
Therefore a graph with n vertices is called a tree if
1. G is connected and is circuit less, or
2. G is connected and has n-1 edges, or
3. G is circuit less and has n-1 edges, or
4. There is exactly one path between every pair of vertices in G, or
5. G is a minimally connected graph.
Chapter 2 - Elementary Data Structures
27BSIT 41 Algorithms
SUMMARY
This chapter devotes itself to present an overview of very fundamental data structures essential forthe design of algorithms. One of the basic technique for improving algorithms is to structure the data insuch a way that the resulting operations can be carried out efficiently. Though the chapter does notpresent all the data structure, we have selected several which occur more frequently in this book. Thischapter also presents some terminologies used in the graph. The notions such as arrays, stack, queue,graphs and trees have been exposed.
EXERCISE
1. Give at least 5 real life examples where we use stack operations.
2. Give at least 5 real life applications where queue is used.
3. Name 10 situations that can be represented by means of graphs. Explain what each vertex and edge represent.
4. Draw a connected graph that becomes disconnected when any edge is removed from it.
5. Draw all trees of n labeled vertices for n=1,2,3,4 and 5.
6. Sketch all binary trees with six pendent edges.
7. Write adjacency and incidence matrix for all the graphs developed.
8. What is linked list? Explain singly linked list with a pictorial representation.
9. List the general operations on linked list.
10. Explain queue operation.
PROBLEMS
1. Give a detailed algorithm to compute address of a data element using row major address computation.
2. Similar to row major address computation workout a column major address computation formula.
3. Implement all stack and queue operations.
4. What are the advantages of circular queue?
Chapter 3
Som e Sim p le Algo r i t hm s
3.0 OBJECTIVES
After studying this chapter, we will be able to design algorithms for problems such as
addition of two numbers
ascending order of three input numbers
finding the quadrant of a given co-ordinate position
finding the roots of a quadratic equation
checking a given number is prime or not
finding a factorial of a number
generating Fibonacci series
finding sum and average of ‘N’ numbers
addition of two matrices
Algorithm is a blueprint, the program follows to achieve the end result. The end result thus depends onthe proper understanding of the logic constructs rather than the programming constructs specific to theprogramming language. In this chapter we present some simple problems and discuss the issues in solvingthe problem and finally design the algorithms for the same.
28 Chapter 3 - Some Simple Algorithms
3.1 ADDITION OF TWO NUMBERS
In a programming situation if we need to add two numbers , we require two memory locations to takethe input and one more memory location to store the result, totally three memory locations.
Consider the following example :
3 + 6 = 9
a b c
The ‘a’ ,‘b’ ,‘c’ are the memory locations from now on called the variables. When we generalize this,it appears as
c= a + b
To make it even more simple, we have two inputs and one output. To store one output and two inputswe need three variables or memory locations. The Algorithm to add two numbers is now given below.
Algorithm : Add_two_numbers
Input : a, b, two numbers to be added
Output : c updated
Method
c= a + b
Display c
Algorithm ends
3.2 EXCHANGING THE VALUES OF TWO VARIABLES
The problem of interchanging the values associated with two variables involves a very fundamentalmechanism that occurs in many sorting and data manipulation algorithms.
Algorithm 1: Temporary variable based
Algorithm: Exchange_2_Variables
Input : a, b, the two variables whose values to be exchanged
29BSIT 41 Algorithms
30 Chapter 3 - Some Simple Algorithms
Output : Exchanged values of a and b
Method :
t = a // Save the original value of ‘a’ in‘t’.
a = b // Assign to ‘a’ original value of ‘b’.
b = t // Assign to ‘b’ the original value of ‘a’ that is stored in‘t’
Algorithm ends
Algorithm 2: Computation based
Algorithm: Exchange_2_Variables
Input : a, b, the two variables whose values to be exchanged
Output : Exchanged values of a and b
Method :
a = a + b // sum the values of ‘a’ and ‘b’ and assign sum value to ‘a’.
b= a - b // Subtract ‘b’ from ‘a’ and assign subtracted result to ‘b’
a = a - b / // Subtract ‘b’ from ‘a’ and assign subtracted result to ‘a’
Algorithm ends
3.3 INPUT THREE NUMBERS AND OUTPUT THEM INASCENDING ORDER
The problem of sorting is major computer science problem. The problem of sorting is dealt in depth inthe next chapter, but here only the problem of sorting of three numbers is presented.
To sort three numbers in ascending order, one has to follow the procedure described here. Find thesmallest among the three numbers, that becomes the first element in the sorted list, and find the smallestamong the remaining two elements and that becomes the second element and remaining becomes the lastelement.
Consider the following example: 3 , 1, 7 is the input
The smallest amongst these three is 1, therefore 1 becomes the first element in the sorted list. Amongst3 and 7, 3 is smaller, hence 3 is the second element and 7 is left out and it becomes the last element. Hencethe sorted list becomes 1, 3, 7.
31BSIT 41 Algorithms
Algorithm : Sorting_3_Elements
Input : a, b, c, the three numbers to be sorted
Output : The Ascending order sequence of the 3 elements
Method:
small = a // Assign ‘a’ to small
k1 = b
k2 = c
if (small > b )
b = small
k1 = a
k2 = c
end_if
if (small> c)
c = small
k1 = a
k2 = b
end_if
Display small
If (k1 > k2 )
Display k2
Display k1
Else
Display k1
Display k2
end_if
Algorithm ends
32 Chapter 3 - Some Simple Algorithms
3.4 TO FIND THE QUADRANT OF A GIVEN CO-ORDINATEPOSITION
The problem of finding the Quadrant in which a given co-ordinate position(x,y) lies is a ComputerGraphics problem. In a Cartesian system if both X and Y co-ordinates are positive then it is said to be FirstQuadrant of the Cartesian plane. If X and Y are both negative then it said to be Third Quadrant of theCartesian plane. If X is negative and Y is positive then it is said to be Second Quadrant and in case if X ispositive and Y is negative then it said to be in the Fourth Quadrant of the Cartesian plane. The Diagramof the Cartesian plane is given in Fig. 3.1 below to make facts more clearer.
Fig. 3.1 Cartesian plane
The co-ordinate positions thus entered have to fall in either of those Quadrant based on the sign of theX and Y co-ordinate positions.
Algorithm : Quadrant_Finder
Input : x, X co-ordinate
y, Y co-ordinate
Output : Corresponding Co-ordinate
Method
If( x >=0)
If(y>=0)
Display ‘I –Quadrant’
Else
33BSIT 41 Algorithms
Display ‘IV-Quadrant’
end_if
else
If(y>=0)
Display ‘II –Quadrant’
Else
Display ‘III-Quadrant’
end_if
end_if
Algorithm ends
3.5 TO FIND THE ROOTS OF A QUADRATIC EQUATION
The general form of the Quadratic equation is ax2+bx+c=0.
And these are two roots of the equation.
The term (b2-4ac) in the solution to the root is called the discriminant of the root. Basically the rootscan be real or imaginary in nature. If the roots are real in nature then they can be either real and distinct,or both of them can be equal. If they are imaginary the roots exists in complex conjugates.
The problem of identifying the nature of the roots is achieved in checking the nature of the discriminant.The nature of the discriminant reflects the nature of the solution of the quadratic equation.
If the discriminant is negative then the roots will be of imaginary in nature.
i.e if (b2-4ac) = some –ve value.
And these are two roots of the equation.
The term (b2-4ac) in the solution to the root is called the discriminant of the root. Basically the rootscan be real or imaginary in nature. If the roots are real in nature then they can be either real and distinct,
a
acbbR
2
41
2
a
acbbR
2
42
2
34 Chapter 3 - Some Simple Algorithms
or both of them can be equal. If they are imaginary the roots exists in complex conjugates.
The problem of identifying the nature of the roots is achieved in checking the nature of the discriminant.The nature of the discriminant reflects the nature of the solution of the quadratic equation.
If the discriminant is negative then the roots will be of imaginary in nature.
i.e if (b2-4ac) = some –ve value.
Then, is complex in nature, because square root of any negative number always results inan imaginary result.
Now the roots R1 and R2 has imaginary parts and hence they are imaginary in nature.
Now the other possibility is that the roots being real. For that to happen the disciminant (b2-4ac) = 0.If it is equal to zero than the discriminant vanishes and hence the roots are real and equal,which are R1= R2 = -b/2a.
In case (b2-4ac) > 0 , then the roots are real and distinct. The algorithm to find the solution to aquadratic equation is given below.
Algorithm: Quadratic_solver
Input : a,b,c the co-efficients of the Quadratic Equation
Output : The two roots of the Equation
Method
disc = ((b*b) –(4*a*c))
if (disc = 0)
display ‘roots are real and equal’
r1 = -b/2a
r2 = -b/2a
display r1
display r2
else
if(disc>0)
display ‘ roots are real and distinct’
acb 42
35BSIT 41 Algorithms
r1 = (-b+sqrt(disc))/2a
r2 = (-b-sqrt(disc))/2a
else
display ‘roots are complex’
display ‘real part’,-b/2a
display ‘imaginary part’, sqrt(absolute_value_of(disc))
display ‘the two root exists in conjugates’
end_if
Algorithm ends
3.6 CHECKING FOR PRIME
Prime number checking has been a very interesting problem for computer science for a very longtime. A prime number is divisible by either 1 or itself and not by any other number. Prime number checkingis technically called as Primality testing.
First Approach
Here we keep dividing a number from 2 to half the value of that number. For ex: if 47 is the numberunder consideration for primality testing then we would divide 47 form 2 to 47/2(23.5 approx. 24) andcheck whether any number in this interval performs a division operation on 47 such that there is noremainder. If so then the given number is not prime. But in the case of 47 no number between 2 and 24performs such sort of division operation and hence it is declared as a prime number.
Algorithm: Primality_Testing (First approach)
Input: n , number
flag, test condition
Output: flag updated
Method
flag = 0
for(i=2 to n/2 in steps of +1 and flag = 0)
36 Chapter 3 - Some Simple Algorithms
if( n % i = 0) // n mod i
flag = 1
end-if
end-for
if(flag = 0)
display ‘Number is prime’
else
display ‘Number is not prime’
end_if
Algorithm ends
Second Approach
It is proved in number theory that instead of setting the interval of divisor to n/2 we can simply set thatto square root of the given number under consideration of Primality and it would work perfectly fine as theprevious algorithm. Note that in this algorithm we achieve the same result with lesser number of operationsbecause we reduce the size of the interval.
Algorithm: Primality_Testing (Second approach)
Input : n , number
flag, test condition
Output : flag updated
Method
flag = 0
for(i=2 to square_root(n) in steps of +1 and flag = 0)
if( n % i = 0) // n mod i
flag = 1
end_if
end-for
37BSIT 41 Algorithms
if(flag = 0)
display ‘Number is prime’
else
display ‘Number is not prime’
end_if
Algorithm ends
3.7 FACTORIAL OF A NUMBER
Finding Factorial of a given number is another interesting problem. Mathematically represented as n! .For ex: 5! = 5*4*3*2*1.
Not to forget that 1! = 1 and 0! = 1.
We can now generalize the factorial of a given number which is any thing other than zero and one asthe product of all the numbers ranging from given number to 1.
i.e n! = n * (n – 1) * (n – 2 ) * . . . *1
Algorithm : Factorial
Input : n
Output : Factorial of n
Method
fact = 1
for i = n to 1 in steps of –1 do
fact = fact*i
end_for
display ‘factorial = ‘,fact
Algorithm ends
38 Chapter 3 - Some Simple Algorithms
In the above algorithm we have implemented the logic of the equation
n! = n * (n – 1) * (n – 2 ) * . . . *1.
The same can be achieved by the following algorithm which follows incremental steps rather thandecremental steps of the given algorithm.
Algorithm : Factorial
Input : n
Output : Factorial of n
Method
fact = 1
for i = 1 to n in steps of 1 do
fact = fact*i
end_for
display ‘factorial = ‘,fact
Algorithm ends
3.8 TO GENERATE FIBONACCI SERIES
The Fibonacci Series is as follows 0 , 1 , 1 , 2 , 3 , 5 , 8 , …
The property of this series is that any given element in the series after the third element is the sum ofits first and second predecessor. For example, consider 8. Its first and second predecessors are 5 and 3.Therefore, 5 + 3 = 8. Consider 3. Its first and second predecessors are 2 and 1. 2 + 1 = 3. The propertyof Fibonacci series holds.
The following is the algorithm to generate the Fibonacci series up to a given number of elements.
Algorithm : Fibonacci_Series
Input : n, the number of elements in the series
Output : fibonacci series upto the nth element
Method
a = -1
39BSIT 41 Algorithms
b = 1
for(i=1 to n in steps of +1 do)
c = a + b
display ‘c’
a = b
b = c
end_for
Algorithm ends
3.9 SUM OF ‘N’ NUMBERS AND AVERAGE
Consider the addition of five numbers 12 , 15 , 10 , 5 and 1.
Initially add 12 and 15 => 12 +15 = 27
Subsequently add 10 to 27 => 10 + 27 = 37
Subsequently add 5 to 37 => 37 + 5 = 42
And Finally add 1 to 42 => 42 + 1 = 43
Which is nothing but 12 + 15 + 10 + 5 + 1 = 43.
The following logic of adding two successive elements and iterating the same process over the otherremaining elements is described in the algorithm given below.
The resulting sum divided by the number of elements yields the average of the domain of input set.
Algorithm : Sum_and_Average
Input : n , number of elements
a(n) , array of n elements
Output : Sum and Average of ‘n’ array elements
Method
Display ‘Enter the number of elements ‘
Accept ‘n’
Display ‘ Enter the elements one by one’
For (i = 1 to n in steps of +1 do)
40 Chapter 3 - Some Simple Algorithms
Accept a(i)
end_for
sum = 0
For (i = 1 to n in steps of +1 do)
sum = sum + a(i)
end_for
Display ‘Sum = ’,sum
Display ‘Average =’,sum/n
Algorithm ends
3.10 TO ADD TWO MATRICES
Consider
The above example is of matrix addition. Matrix addition is possible iff orders of the two matrices aresame. Respective matrix elements are added together in a third matrix and the results are thus obtained.The procedure is thus described below.
The two Matrices has to read initially in a two dimensional array and then the respective row elementand column elements in the Matrices have to be added to get a third matrix, which leads to the realizationof Matrix addition.
Algorithm : Matrix Addition
Input : n , order of the matrices
a(n,n),b(n,n) the two input matrices
Output : c(n,n) the resultant Sum matrix
Method
{
Accept ‘n’
333231
232221
131211
aaa
aaa
aaa
+
333231
232221
131211
bbb
bbb
bbb
=
333332323131
232322222121
131312121111
bababa
bababa
bababa
41BSIT 41 Algorithms
for(i = 1 to n in steps + 1 do)
for(j= 1 to n in steps of + 1do)
accept a(i,j)
end_for
end_for
for(i = 1 to n in steps + 1 do)
for(j= 1 to n in steps of + 1do)
accept b(i,j)
end_for
end_for
for(i = 1 to n in steps + 1 do)
for(j= 1 to n in steps of + 1do)
c(i,j) = a(i,j) + b(i,j)
end_for
end_for
for(i = 1 to n in steps + 1 do)
for(j= 1 to n in steps of + 1do)
display c(i,j)
end_for
end_for
Algorithm ends
SUMMARY
In this chapter some simple problems, the problem solving concepts and the concept of designingalgorithms for the problems has been presented. For the ease of students some simple problems areconsidered and the algorithms are developed. The students are expected to implement all the aboveexplained algorithms and experience the way they work.
EXERCISE
1. Design and Develop algorithms for multiplying n integers.
Hint: Follow the algorithm to add n numbers given in the text.
2. Design and develop algorithm for finding the middle element in the three numbers.
3. Develop algorithm to find the number of Permutations and Combinations for a given n and r.
4. Design a algorithm to generate all prime numbers within the limits l1 and l2.
5. Design an algorithm to find the reverse of a number.
6. A number if said to be a palindrome if the reverse of a number is same as the original. Design a algorithm to
check whether a number is palindrome or not.
6. Design a algorithm to check whether a given string is palindrome or not.
7. Implement all the devised algorithms and also the algorithms discussed in the chapter.
8. Bring out the pros and cons of the algorithms for exchanging the values of two variables using computation
based and temporary based variable. Hand simulates both on a pair of values of your interest.
Chapter 3 - Some Simple Algorithms42
Chapter 4
Sear ch in g an d So r t in g
4.0 OBJECTIVES
After studying this chapter, we will be able to explain the following :
Searching : Sequential search and Binary search
Sorting: Insertion sort, Selection sort and Bubble sort
4.1 SEARCHING
Let us assume that we have a sequential file and we wish to retrieve an element matching with key‘k’, then, we have to search the entire file from the beginning till the end to check whether the elementmatching k is present in the file or not.
There are a number of complex searching algorithms to serve the purpose of searching. The linearsearch and binary search methods are relatively straight forward methods of searching.
4.1.1 Sequential search
In this method, we start to search from the beginning of the list and examine each element till the endof the list. If the desired element is found we stop the search and return the index of that element. If theitem is not found and the list is exhausted the search returns a zero value.
In the worst case the item is not found or the search item is the last (nth) element. For both situationswe must examine all n elements of the array.
43BSIT 41 Algorithms
44
The algorithm for sequential search is as follows,
Algorithm : sequential search
Input : A, vector of n elements
K, search element
Output : j –index of k
Method : i=1
While(i<=n)
{
if(A[i]=k)
{
write(“search successful”)
write(k is at location i)
exit();
}
else
i++
if end
while end
write (search unsuccessful);
algorithm ends.
4.1.2 Binary Search
Binary search method is also relatively simple method. For this method it is necessary to have thevector in an alphabetical or numerically increasing order. A search for a particular item with X resemblesthe search for a word in the dictionary. The approximate mid entry is located and its key value is examined.If the mid value is greater than X, then the list is chopped off at the (mid-1)th location. Now the list getsreduced to half the original list. The middle entry of the left-reduced list is examined in a similar manner.This procedure is repeated until the item is found or the list has no more elements. On the other hand, ifthe mid value is lesser than X, then the list is chopped off at (mid+1)th location. The middle entry of theright-reduced list is examined and the procedure is continued until desired key is found or the searchinterval is exhausted.
The algorithm for binary search is as follows,
Chapter 4 - Searching and Sorting
45BSIT 41 Algorithms
Algorithm : binary search
Input : A, vector of n elements
K, search element
Output : low –index of k
Method : low=1,high=n
While(low<=high-1)
{
mid=(low+high)/2
if(k<a[mid])
high=mid
else
low=mid
if end
}
while end
if(k=A[low])
{
write(“search successful”)
write(k is at location low)
exit();
}
else
write (search unsuccessful);
if end;
Algorithm ends.
46
4.2 SORTING
One of the major applications in computer science is the sorting of information in a table. Sortingalgorithms arrange items in a set according to a predefined ordering relation. The most common types ofdata are string information and numerical information. The ordering relation for numeric data simplyinvolves arranging items in sequence from smallest to largest and from largest to smallest, which is calledascending and descending order respectively.
The items in a set arranged in non-decreasing order are {7,11,13,16,16,19,23}. The items in a setarranged in descending order is of the form {23,19,16,16,13,11,7}
Similarly for string information, {a, abacus, above, be, become, beyond}is in ascending order and {beyond, become, be, above, abacus, a}is in descending order.
There are numerous methods available for sorting information. But, not even one of them is best for allapplications. Performance of the methods depends on parameters like, size of the data set, degree ofrelative order already present in the data etc.
4.2.1 Insertion Sorting
The first class of sorting algorithm that we consider comprises algorithms that sort by insertion. Analgorithm that sorts by insertion takes the initial, unsorted sequence,
}...{ 3:2:1 nssssS , and computes a series of sorted sequences nSSS ':...:'' 1:0 , as follows:
1. The first sequence in the series, 0'S is the empty sequence. i.e., {}'0 S .
2. Given a sequence 0'S in the series, for ni 0 , the next sequence in the series,
1' iS , is obtained by inserting the thi )1( element of the unsorted sequence
1' iS into the correct position in iS ' .
Each sequence iS ' , ni 0 , contains the first i elements of the unsorted sequence S.
Therefore, the final sequence in the series, nS ' , is the sorted sequence we seek. i.e.,
nSS '' .
Fig. 4.1 illustrates the insertion sorting algorithm. The figure shows the progression of the
insertion sorting algorithm as it sorts an array of ten integers. The array is sorted in place.
I.e., the initial unsorted sequence, S, and the series of sorted sequences, nSSS ':...:'' 1:0 , ,
occupy the same array.
Chapter 4 - Searching and Sorting
47BSIT 41 Algorithms
As shown in Fig. 4.1, the first step (i=0) is trivial—inserting an element into the empty list involves nowork. Altogether, n-1 non-trivial insertions are required to sort a list of n elements.
Fig. 4.1 Insertion sort
In the ith step, the element at position i in the array is inserted into the sorted sequence
iS ' which occupies array positions 0 to (i-1). After this is done, array positions 0 to i
contain the i+1 elements of 1' iS . Array positions (i+1) to (n-1) contain the remaining n-i-
1 elements of the unsorted sequence S.
48
Algorithm : Insertion Sort
Input : n, Size of the input domain
a[1..n], array of n elements
Output : a[1..n] sorted
Method
for j= 2 to n in steps of 1 do
item = a[j]
i = j-1
while((i>=1) and (item<a[i])) do
a[i+1] = a[i]
i = i-1
while end
a[i+1] = item
for end
Algorithm ends
4.2.2 Selection Sorting
Such algorithms construct the sorted sequence one element at a time by adding elements to the sortedsequence in order. At each step, the next element to be added to the sorted sequence is selected from theremaining elements.
Because the elements are added to the sorted sequence in order, they are always added at one end.This is what makes selection sorting different from insertion sorting. In insertion sorting elements areadded to the sorted sequence in an arbitrary order. Therefore, the position in the sorted sequence at whicheach subsequent element is inserted is arbitrary.
Both selection sorts described in this section sort the arrays in place. Consequently, the sorts areimplemented by exchanging array elements. Nevertheless, selection differs from exchange sorting becauseat each step we select the next element of the sorted sequence from the remaining elements and then wemove it into its final position in the array by exchanging it with whatever happens to be occupying thatposition.
Chapter 4 - Searching and Sorting
49BSIT 41 Algorithms
Straight Selection Sorting
The simplest of the selection sorts is called straight selection . Fig.4.2 illustrates how straight selectionworks. In the version shown, the sorted list is constructed from the right (i.e., from the largest to thesmallest element values).
At each step of the algorithm, a linear search of the unsorted elements is made in order to determinethe position of the largest remaining element. That element is then moved into the correct position of thearray by swapping it with the element which currently occupies that position.
Fig. 4.2 Selection sort
50
For example, in the first step shown in Fig 4.2, a linear search of the entire array reveals that 9 is thelargest element. Since 9 is the largest element, it belongs in the last array position. To move it there, weswap it with the 4 that initially occupies that position. The second step of the algorithm identifies 6 as thelargest remaining element an moves it next to the 9. Each subsequent step of the algorithm moves oneelement into its final position. Therefore, the algorithm is done after n-1 such steps.
Algorithm : Selection Sort
Input : n, Size of the input domain
a[1..n], array of n elements
Output: a[1..n] sorted
Method:
for i = 1 to n in steps of 1 do
j = i
for k = i+1 to n in steps of 1 do
if(a[k]< a[j]) then j = k
for end
Interchange a[i] and a[j]
For end
Algorithm ends.
Two more sorting algorithms are discussed in the next chapter.
4.2.3 Bubble Sort
Almost all sorting methods rely on exchanging data to achieve the desired ordering. The method wewill now consider relies heavily on an exchange mechanism. Suppose we start out with the followingrandom data set:
With the data as it stands there is very little order present. What we are always looking for in sortingis a way of increasing the order in the array. We notice that the first two elements are “out of order” in thesense that no matter what the final sorted configuration 30 will need to appear later than 12. If the 30 and12 are interchanged we will have in a sense “increased the order” in the data. This leads to the configurationbelow:
Chapter 4 - Searching and Sorting
a[1] a[2] …. a[n]30 12 18 8 14 41 3 39
51BSIT 41 Algorithms
After examining the new configuration we see that the order in the data can be increased further bynow comparing and swapping the second and third elements. With this new change we get the configuration
The investigation we have made suggests that the order in the array can be increased using thefollowing steps:
1. For all adjacent pairs in the array do
(a) if the current pair of elements is not in non-descending order then exchange the two elements.
After applying this idea to all adjacent pairs in our current data set we get the configuration below:
On studying the mechanism carefully we see that it guarantees that the biggest element 41 will beforced into the last position in the array. In effect the last element is at this stage “sorted”. The array is stillfar from being sorted.
If we start at the beginning and apply the same mechanism again we will be able to guarantee that thesecond biggest value (i.e. 39) will be in the second last position in the array. In the second pass through thearray there is no need to involve the last element in comparison of adjacent pairs because it is already inits correct place. By the same reasoning, when a third pass is made through the data the last two elementsare in their correct place and so they need not be involved in the comparison-exchange phase.
The repeated exchange method we have been developing guarantees that with each pass through thedata one additional element is sorted. Since there are n elements in the data this implies that (n-1) passes(of decreasing length) must be made through the array to complete the sort. It is evident that (n-1) passesrather than n passes are required because by definition once (n-1) values have been sorted the nth valuemust be in its correct place (in this instance it would be the first element). Combining these observationswith our earlier mechanism for comparing and exchanging adjacent pairs the overall structure of ouralgorithm becomes:
Algorithm: Bubble Sort
Input : An array A [1 ... n] of orderable elements
a[1] a[2] …. a[n]12 30 18 8 14 41 3 39
12 18 30 8 14 41 3 39
12 18 30 8 14 41 3 39
52
ANNEXURE 4B
Method
for j 1 to n-1 do
for j 1 to n-1- i do
if A[j +1] < A[ j]
{
t = A [j +1]
A [j + 1] = A [j]
A[j] = t
}
Output : An array A [1…..n] sorted in ascending order
SUMMARY
In this chapter two techniques for checking whether an element is presented in the list of elements ispresented. Linear search is best employed when data searching operation is used minimally. But whendata searching on the same data set has to be done several times then it is better to sort that data set andapply binary search. Binary search reduces the effort in searching as in case of Linear search. Thechapter also presents some sorting techniques in sequel to searching. Selection sort is simplest of allsorting algorithms and it goes for the selection of the largest element at each iteration. Insertion sort buildsa sorted list by inserting elements to a small sub sorted list.
EXERCISE
1. What are the serious short comings of the binary search method and sequential search method.
2. Consider a data set of nine elements {10, 30, 45, 54, 56, 78, 213, 415, 500} and trace the linear search algorithm
to find whether the keys 30, 150, 700 are present in the data set or not.
3. Trace the Binary search algorithm on the same data set and same key elements of problem 2.
4. Try to know more sorting techniques and make a comparative study of them.
5. Hand Simulate Insertion Sort on the data set { 13 , 45 , 12, 9 , 1, 10, 40}.
6. Implement all the algorithms designed in the chapter.
Chapter 4 - Searching and Sorting
53BSIT 41 Algorithms
7. Explain insertion sort with an example.
8. Explain Straight selection sorting technique with an example.
9. Hand simulate straight selection sort on the dataset {89, 45, 68, 90, 29, 34, 17}
10. Explain bubble sorting technique with an example.
11. Hand simulate bubble sort on the same dataset of problem 9.
Chapter 5 - Recursion
Chapter 5
Recu r sio n
1.0 OBJECTIVES
After studying this chapter, we will be able to explain the following :
explain the concepts of and importance of recursion
design an algorithms for problems such as finding a factorial of a number, finding aNth Fibonacci number, finding the sum of first N integers, binary search, findingmaximum and minimum in a given list of N elements, using recursion
design algorithms for Merge sort and Quick sort using recursion
5.1 WHAT IS RECURSION?
We look at the concept of recursion, which is one of the very powerful programming concepts,supported by most of the languages. At the same time, it is also a fact that most beginners areconfused by the way it works and are unable to use it effectively. Also, some languages may
not support recursion. In such cases, it may become necessary to rewrite recursive functions into non-recursive ones. All these and many other aspects are dealt with in this chapter.
Recursion is an offshoot of the concept of subprograms. A subprogram as we know is the concept ofwriting separate modules, which can be called from other points in the program or other programs. Thencame a concept wherein any subprogram can call any other subprogram. The control goes to the calledsubprogram, performs the assigned tasks and comes back to the caller programs.
54
Then comes the question – can a program call itself? Theoretically it is possible. If so, where do weuse them normally? A subprogram is called to perform a function, which the caller subprogram cannotperform itself. But if the caller program calls itself, what purpose does it serve? The answer is, thesubprogram no doubt, calls itself, but with a different value of the parameter. In fact, in most cases, thecalling continues until some specific value of the parameter is reached.
We now understand that recursion is a process of defining a process/ problem/ an object in terms ofitself. Recursion is one of the applications of stacks. The recursive mechanisms are extremely powerful,but even more importantly; many times they can express an otherwise complex process, very clearly. Anyprogram can be written using recursion. Of course, the recursive program in that case could be tougher tounderstand. Hence, recursion can be used when the problem itself can be defined recursively.
The general procedure for any recursive algorithm is as follows,
1. Save the parameters, local variables and return addresses.
2. If the termination criterion is reached perform final computation and go to step 3, otherwiseperform final computations and go to step 1.
3. Restore the most recently saved parameters, local variables and return address and go to thelatest return address.
5.2 WHY DO WE NEED RECURSION?
When iteration can be easily used and also supported by most programming languages, why do weneed recursion at all? The answer is that iteration has certain demerits as is made clear below:
1. Mathematical functions such as factorial and fibonacci series generation can be easilyimplemented using recursion than iteration.
2. In iterative techniques looping of statement is very much necessary.
5.3 WHEN TO USE RECURSION?
Recursion can be used for repetitive computations in which each action is stated in terms of previousresult. There are two conditions that must be satisfied by any recursive procedure.
1. Each time a function calls itself it should get nearer to the solution.
2. There must be a decision criterion for stopping the process.
In making the decision about whether to write an algorithm in recursive or non-recursive form, it is
55BSIT 41 Algorithms
56 Chapter 5 - Recursion
always advisable to consider a tree structure for the problem. If the structure is simple then use non-recursive form. If the tree appears quite bushy, with little duplication of tasks, then recursion is suitable.
Recursion is a top down approach to problem solving. It divides the problem into pieces or selects outone key step, postponing the rest. Whereas, iteration is more of a bottom up approach. It begins with whatis known and from this constructs the solution step by step.
Let us now look at some basic examples which are often devised using recursion.
5.4 FACTORIAL OF A POSITIVE INTEGER
The factorial of a number ‘n’ = n * (n-1) * (n-2)* … * 3 * 2 * 1. An iterative way of obtaining thefactorial of a given number is to put a ‘for’ loop to repeat the multiplication n times. We start with 1, thenevaluate 1 * 2, then that product * 3, ….*(n-1). The factorial of a number can also be obtained recursively.
Suppose we are asked to evaluate N!. If we somehow know (N-1)! then we can evaluate N! = N*(N-1)!. But how do we get (N-1)!? The same logic can be employed to evaluate (N-1)! = (N-1) * (N-2)!.The problem of finding the factorial of a given number can be recursively defined as
Thus, the algorithm developed to compute the factorial of a given number is
Algorithm: Factorial
Input: n, the integer value whose factorial is to be computed
Output: factorial of n
Method:
If (n==1) then
Return (1)
Else
Return (n * factorial (n-1)
If end
Algorithm ends
The students are advised to try various values of n to actually see how the method works.
Factorial (n) [where n is a positive integer] =
11
1)1(
nif
nifnFactorialn
57BSIT 41 Algorithms
5.5 FINDING THE NTH FIBONACCI NUMBER
As already introduced in Chapter 3, a Fibonacci series is a sequence of integers 0,1,1,2,3,5…… i.e.,The Fibonacci sequence starts from 0, 1 and after that each new term will be the sum of the previous twoterms. .We shall here, at finding out what could be the nth Fibonacci number in the series. For instance, ifwe say the first Fibonacci number then it is 0. The second Fibonacci number is 1. Thus, if the 6th Fibonaccinumber asked then we are expected to produce the number 5. In general, if kth Fibonacci number isexpected then that can be obtained by summing up (k-1)th and (k-2)th fibonacci numbers. This processcan be recursively done and finally one can obtain the kth Fibonacci number.
Thus, following is the recursive algorithm designed to find the nth Fibonacci number
Algorithm: Fibonacci
Input: n, the position at which the Fibonacci number has to be computed
Output: nth Fibonacci number
Method:
If (n==0)
Return (0)
Else
If (n == 1)
Return (1)
Else
Return (Fibonacci (n-1) + Fibonacci (n-2))
If end
If end
Algorithm ends
Again, the correctness can be checked for various input values.
We use a third example, though normally this method is not used to explain recursion, nevertheless it isa very useful method.
58 Chapter 5 - Recursion
5.6 SUM OF FIRST N INTEGERS
The sum of the integers to n is the sum of the integers through n -1 + n. The sum of the integers to n-1 is the sum to n -2 to n -1, etc. Eventually, we know that the sum of the first positive integer is 1.Therefore, we can define a terminating condition for some small subset of the problem. The recursivealgorithm to achieve this is as follows
Algorithm : SumPosInt
Input : n, the upper limit
Output : Sum of first n positive integers
Method:
if (n <= 0) // We only want positive integers
return 0;
else
if (n == 0) // Our terminating condition
return 1;
else
return (n + SumPosInt( n -1 ); // recursive step
if end
if end
Algorithm ends
5.7 BINARY SEARCH
Binary search method as explained earlier is a process of searching for the presence or absence of akey element in the sorted list. The approximate mid entry is located and its key value is examined. If themid value is greater than X, then the list is chopped off at the (mid-1)th location. Now the list gets reducedto half the original list. The middle entry of the left-reduced list is examined in a similar manner. Thus, onecan always think of using a recursive algorithm to solve the same.
The recursive algorithm for binary search is as follows,
Algorithm : binary search
59BSIT 41 Algorithms
Input : A, vector of n elements
K, search element
Low, the lower limit
High, the upper limit //initially low=1 and high=n the number of elements
Output : the position of the K
Method : if (low <= high)
mid=(low+high)/2
if( a[mid] == K)
return(mid)
else
if (a[mid]< K)
Binary search(A, K, Low, mid)
else
Binary Search(A, K, High, mid)
if end
if end
else
return(0)
if end
Algorithm ends.
5.8 MAXIMUM AND MINIMUM IN THE GIVEN LIST OF NELEMENTS
Here the problem is to find out the maximum values in a give list of n data elements. The recursivealgorithm designed to serve this purpose is as follows.
Algorithm: Max-Min
60 Chapter 5 - Recursion
Input: p, q, the lower and upper limits of the dataset
max, min, two variables to return the maximum and minimum values in the list
Output: the maximum and minimum values in the data set
Method:
If (p = q) Then
max = a(p)
min = a(q)
Else
If ( p – q-1) Then
If a(p) > a(q) Then
max = a(p)
min = a(q)
Else
max = a(q)
min = a(p)
If End
Else
m ¬ (p+q)/2
max-min(p,m,max1,min1)
max-min(m+1,q,max2,min2)
max large(max1,max2)
min small(min1,min2)
If End
If End
Algorithm Ends.
61BSIT 41 Algorithms
5.9 MERGE SORT
Sorting as stated in Chapter 4, is a process of arranging a set of given numbers in some order. Thebasic concept of merge sort is like this. Consider a series of n numbers, say A(1), A(2) ……A(n/2) andA(n/2 + 1), A(n/2 + 2) ……. A(n). Suppose we individually sort the first set and also the second set. Toget the final sorted list, we merge the two sets into one common set.
We first look into the concept of arranging two individually sorted series of numbers into a commonseries using an example:
Let the first set be A = {3, 5, 8, 14, 27, 32}. Let the second set be B = {2, 6, 9, 15, 18, 30}.
The two lists need not be equal in length. For example the first list can have 8 elements and the second5. Now we want to merge these two lists to form a common list C. Look at the elements A(1) and B(1),A(1) is 3, B(1) is 2. Since B(1) < A(1), B(1) will be the first element of C i.e., C(1)=2. Now compare A(1)=3 with B(2) =6. Since A(1) is smaller then B(2), A(1) will become the second element of C. C[ ] = {2, 3}
Similarly compare A(2) with B(2), since A(2) is smaller, it will be the third element and so on. Finally,C is built up as C[ ]= {2, 3, 5, 6, 8, 9, 14, 15, 18, 27, 30, 32}.
However the main problem remains. In the above example, we presume that both A & B are originallysorted. Then only they can be merged. But, how do we sort them in the first? To do this and show theconsequent merging process, we look at the following example. Consider the series A= (7 5 15 6 4). Nowdivide A into 2 parts (7, 5, 15) and (6, 4). Divide (7, 5, 15) again as ((7, 5) and (15)) and (6, 4) as ((6) (4)).Again (7, 5) is divided and hence ((7, 5) and (15)) becomes (((7) and (5)) and (15)).
Now since every element has only one number, we cannot divide again. Now, we start merging them,taking two lists at a time. When we merge 7 and 5 as per the example above, we get (5, 7) merge this with15 to get (5, 7, 15). Merge this with 6 to get (5, 6, 7, 15). Merging this with 4, we finally get (4, 5, 6, 7 and15). This is the sorted list.
You are now expected to take different sets of examples and see that the method always works.
We design two algorithms in the following. The main algorithm is a recursive algorithm (some whatsimilar to the binary search algorithm that we saw earlier) which calls at times the other algorithm calledMERGE. The algorithm MERGE does the merging operation as discussed earlier.
Algorithm: MERGESORT
Input: low, high, the lower and upper limits of the list to be sorted
A, the list of elements
Output: A, Sorted list
62 Chapter 5 - Recursion
Method:
If (low<high)
mid¬ (low + high)/2
MERGESORT(low, mid)
MERGESORT (mid, high)
MERGE(A, low, mid, high)
If end
Algorithm ends
You may recall that this algorithm runs on lines parallel to the binary search algorithm. Each time itdivides the list (low, high) into two lists(low, mid) and (mid+1, high). But later, calls for merging the twolists.
Algorithm: Merge
Input: low, mid, high, limits of two lists to be merged i.e., A(low, mid) and A(mid+1, high)
A, the list of elements
Output: B, the merged and sorted list
Method:
h = low, i = low, j = mid + 1;
While ((h dŠ mid) and (j dŠ high)) do
If (A(h) dŠ A(j) )
B(i) = a(h);
h = h+1;
else
B(i) = A(j);
j = j+1;
If end
i = i+1;
If (h > mid)
63BSIT 41 Algorithms
For k = j to high
B(i) = A(k);
i = i+1;
For end
Else
For k = h to mid
B(i) = A(k);
i = i+1
For end
If end
While end
Algorithm ends
The first portion of the algorithm works exactly similar to the explanation given earlier, except thatinstead of using two lists A and B to fill another array C, we use the elements of the same array A[low,mid]and A[mid+1,high] to write into another array B.
Now it is not necessary that both the lists from which we keep picking elements to write into B shouldget exhausted simultaneously. If the fist list gets exhausted earlier, then the elements of the second list aredirectly written into B, without any comparisons being needed and vice versa. This aspect will be takencare of by the second half of the algorithm.
5.10 QUICKSORT
This is another method of sorting that uses a different methodology to arrive at the same sorted result.It “Partitions” the list into 2 parts (similar to merge sort), but not necessarily at the centre, but at anarbitrarily “pivot” place and ensures that all elements to the left of the pivot element are lesser than theelement itself and all those to the right of it are greater than the element. Consider the following example.
75 80 85 90 95 70 65 60 55
To facilitate ordering, we add a very large element, say 1000 at the end. We keep in mind that this iswhat we have added and is not a part of the list.
75 80 85 90 95 70 65 60 55 1000
A(1) A(2) A(3) A(4) A(5) A(6) A(7) A(8) A(9) A(10)
64 Chapter 5 - Recursion
Now consider the first element. We want to move this element 75 to its correct position in the list. Atthe end of the operation, all elements to the left of 75 should be less than 75 and those to the right shouldbe greater than 75. This we do as follows:
Start from A(2) and keep moving forward until an element which is greater than 75 is obtained.Simultaneously start from A(10) and keep moving backward until an element smaller than 75 is obtained.
A(1) A(2) A(3) A(4) A(5) A(6) A(7) A(8) A(9) A(10)
75 80 85 90 95 70 65 60 55 1000
Now A(2) is larger than A(1) and A(9) is less than A(1). So interchange them and continue theprocess.
75 55 85 90 95 70 65 60 80 1000
Again A(3) is larger than A(1) and A(8) is less than A(1), so interchange them.
75 55 60 90 95 70 65 85 80 1000
Similarly A(4) is larger than A(1) and A(7) is less than A(1), interchange them
75 55 60 65 95 70 90 85 80 1000
In the next stage A(5) is larger than A(1) and A(6) is lesser than A(1), after interchanging we have
75 55 60 65 70 95 90 85 80 1000
In the next stage A(6) is larger than A(1) and A(5) is lesser than A(1), we can see that the pointershave crossed each other, hence Interchange A(1) and A(5).
70 55 60 65 75 95 90 85 80 1000
We have completed one series of operations. Note that 75 is at its proper place. All elements to its leftare lesser and to it’s right are greater.
Next we repeat the same sequence of operations from A(1) to A(4) and also between A(6) to A(10).This we keep repeating till single element lists are arrived at.
Now we suggest a detailed algorithm to do the same. As before, two algorithms are written. Themain algorithm, called QuickSort repeatedly calls itself with lesser and lesser number of elements. However,the sequence of operations explained above is done by another algorithm called PARTITION
Algorithm: QuickSort
Input: p, q, the lower and upper limits of the list of elements A to be sorted
Output: A, the sorted list
65BSIT 41 Algorithms
Method:
If (p < q)
j = q+1;
PARTITION (p, j)
QuickSort(P, j-1)
Quicksort(j+1, q)
If end
Algorithm ends
Algorithm: PARTITION
Input: m, the position of the element whose actual position in the sorted list has to be found
p, the upper limit of the list
Output: the position of mth element
Method:
v = A(m);
i = m;
Repeat
Repeat
i = i+1
Until (A(i) e> v);
Repeat
p = p - 1
Until (A(p) d” v);
If (i < p)
INTERCHANGE(A(i), A(p))
If end
Until (i e” p)
66
A(m) = A(p) ;
A(p) = v;
Algorithm ends
5.11 THE TOWERS OF HANOI PROBLEM
The Towers of Hanoi is an ancient puzzle consisting of a number of disks placed on three needles, asshown in Figure 5.1.
Figure 5.1: The Towers of Hanoi.
The disks all have different diameters and holes in the middle so they will fit over the needles. All thedisks start out on column A. The object of the puzzle is to transfer all the disks from needle A to needle C.Only one disk can be moved at a time, and no disk can be placed on a disk that’s smaller than itself.
5.11.1 The Recursive Algorithm
The solution to the Towers of Hanoi puzzle can be expressed recursively using the notion of subtrees.Suppose we want to move all the disks from a source tower (S) to a destination tower (D). We have anintermediate tower available (T). Assume that there are n disks on tower S. We can carry out thealgorithm as follows:
Move the subtree consisting of the top n–1 disks from S to T.
Move the remaining (largest) disk from S to D.
Move the subtree from T to D.
When we begin, the source tower is A, the intermediate tower is B, and the destination tower is C.Figure 5.2 shows the three steps for this situation.
Chapter 5 - Recursion
67BSIT 41 Algorithms
First, the subtree consisting of disks 1, 2, and 3 is moved to the intermediate tower B. Then the largestdisk, 4, is moved to tower C. Then the subtree is moved from B to C.
Of course, this doesn’t solve the problem of how to move the subtree consisting of disks 1, 2, and 3 totower B because we can’t move a subtree all at once; we must move it one disk at a time. Moving the 3-disk subtree is not so easy. However, it’s easier than moving 4 disks.
As it turns out, moving 3 disks from A to the destination tower B can be done with the same 3 steps asmoving 4 disks. That is, move the subtree consisting of the top 2 disks from tower A to intermediate towerC; then move disk 3 from A to B. Then move the subtree back from C to B.
How to move a subtree of two disks from A to C? Move the subtree consisting of only one disk (1)from A to B. This is the base case: when we are moving only one disk, we just move it; there is nothingelse to do. Then move the larger disk (2) from A to C, and replace the subtree (disk 1) on it. Figure 5.2illustrated the method of solving the problem.
Figure 5.2 Recursive solution to Towers of Hanoi.
68
The above recursive procedure can be algorithmically expressed as follows:
TOH(n, S, T, D) =
The above procedure is illustrated by considering Tower-of-Hanoi of 3 discs in Figure 5.3.
Figure 5.3 Sequence of disc movements in Towers of Hanoi puzzle for n = 3.
TOH (n -1, S, D, T)
Move a disc from S to D if n > 0
TOH (n-1, T, S, D)
A C
A B
C B
A C
B A
B C
A C
Chapter 5 - Recursion
69BSIT 41 Algorithms
From the above example, we draw the following observations:
1. No explicit repetition using for, while etc. only implicit repetition.
2. For 2 discs 3 (22 – 1) movements are required.
3. For 3 discs 7 (23 – 1) movements are required.
4. For 4 discs 15 (24 – 1) movements are required.
5. In general, for n discs 2n – 1 disc movements are required.
The recursive tree diagram for the Tower-of-Hanoi problem for 3 discs is shown in Figure 5.4.
Figure. 5.4 Recursion three diagram for Tower of Hanoi for 3 discs.
In the above diagram, the leaf nodes indicate the termination condition. If we traverse the tree ininorder, we get the sequence of moves as follows:
A C, A B, C B, A C, B A, B C, A C
5.12 DEMERITS OF RECURSION
Now, are you thinking that recursion is the best programming technique to be followed? Well, you arewrong. Recursion has some demerits too, which often make it a not-so-favoured solution to a problem.Some of the demerits of recursive algorithms are listed below:
1. Many programming languages do not support recursion; hence recursive mathematical functionis implemented using iterative methods.
70
2. Even though mathematical functions can be easily implemented using recursion it is always atthe cost of execution time and memory space.
Fig. 5.5 Time Space tree of the algorithm Fibonacci
For example, the recursion tree for generating 6 numbers in a fibonacci series generation is given in fig5.5. A fibonacci series is of the form 0,1,1,2,3,5,8,13…etc, where the third number is the sum of precedingtwo numbers and so on. It can be noticed from the fig 5.5 that, f (n-2) is computed twice, f (n-3) iscomputed thrice, f (n-4) is computed 5 times.
3. A recursive procedure can be called from within or outside itself and to ensure its properfunctioning it has to save in some order the return addresses so that, a return to the properlocation will result when the return to a calling statement is made.
4. The recursive programs needs considerably more storage and will take more time.
SUMMARY
In this chapter, the concepts of recursion are introduced. Some simple problems which were discussedin the earlier chapters are reconsidered and the recursive algorithms are designed to achieve the sameoutcome. Two sorting algorithms namely Quick sort and merge sort are presented and the recursivealgorithms are designed.
EXERCISE
1. Trace out the algorithm Merge Sort on the data set {1,5,2,19,4,17, 45, 12, 6}
2. Trace out the algorithm Quick Sort on the data set {12 , 1, 5,7,19,15, 8, 9, 10}
3. Implement all the algorithms designed in this chapter.
Chapter 5 - Recursion
71BSIT 41 Algorithms
4. Trace out the algorithm MaxMin on a data set consisting of atleast 8 elements.
5. List out the merits and demerits of Recursion.
6. The data structure used by recursive algorithms is _____________
7. When is it appropriate to use recursion?
8. What is recursion? Give an algorithm to solve factorial of a number using recursion.
9. State the Towers of Hanoi Problem and design an algorithm to solve Towers of Hanoi problems.
10. What are the applications of recursion?
11. Give an algorithm to solve sum of first N integers using recursion.
Chapter 6
Rep r esen t at io n an d Tr aver sal o f aBin ar y Tr ee
6.0 OBJECTIVES
After studying this chapter, we will be able to explain the following :
Explain the basic terminologies of trees.
discuss the importance of Binary Tree Data Structure.
describe the representation of binary trees based on Sequential Allocation.
explain the linked representation of binary trees.
List out the advantages of representing Binary Trees using linked allocation.
Explain the traversal of binary trees.
List out the various ways of traversing a binary tree.
Discuss the various operations on binary trees.
Evaluate the usefulness of binary search trees
6.1 BINARY TREE
As already seen in Chapter 2, a tree is a minimally connected graph without a circuit. For a graph withn vertices to be minimally connected there have to be n-1 edges. A tree is, therefore, a connected graph
Chapter 6 - Representation and Traversal of a Binary Tree72
with n vertices and (n-1) edges. It can also be said to be a graph with n vertices and (n-1) edges withouta circuit.
Sometimes a specific node is given importance. Then, it is called a “rooted” tree. This tree is acollection of vertices and edges in which a vertex has been identified as the root and the remainingvertices are categorized into many classes (k >= 0), where each class is a rooted tree. Such a rooted treeclass is called the sub-tree of the given tree.
Fig. 6.1 (a) A complete binary tree (b) A full binary tree (c)A binary tree showing levels binary tree
In the above definition if k is restricted to be maximum of 2 then the rooted tree is called “binarytree”. If k = 0, it implies that there are no children. A node without any children is called a “leaf” node.
The number of levels in the tree is called the “depth” of the tree. A “complete” binary tree is onewhich allows sequencing of the nodes and all the previous levels are maximally accommodated before thenext level is accommodated. i.e., the siblings are first accommodated before the children of any one ofthem. And a binary tree which is maximally accommodated with all leaves at the same level is called“full” binary tree. A full binary tree is always complete but complete binary tree need not be full. Fig. 6.1(a) and (b) shows the examples for complete and full binary trees.
The maximum number of vertices at each level in a binary tree can be found out as follows:
At level 0: 20 number of vertices
At level 1: 21 number of vertices
At level 2: 22 number of vertices
…
At level i: 2i number of vertices
73BSIT 41 Algorithms
(b)
(a) (c)
74
Therefore, maximum number of vertices in a binary tree of depth ‘l’ is:
A binary indicating the levels is shown in Fig. 6.1 (c)
6.2 REPRESENTATION OF A BINARY TREE
Binary Tree can be represented using the two methods:
Static allocation, and
Dynamic allocation
In static allocation, we have two ways of representing the binary tree. One is through the use ofAdjacency matrices and the other through the use of Single dimensional array representation.
6.2.1 Adjacency Matrix Representation
A two dimensional array can be used to store the adjacency relations very easily and can be used torepresent a binary tree. In this representation, to represent a binary tree with n vertices we use a n×nmatrix. Here the row indices correspond to the parent nodes and the column corresponds to the childnodes. i.e., A row corresponding to the vertex v
i having the entries ‘L’ and ‘R’ indicate that v
i has as its
left child the index corresponding to the column with the entry ‘L’ and the index corresponding to thecolumn with ‘R’ entry as its right child. The column with no entries corresponds to the root node. All othercolumns have only one element. Each row may have 0, 1 or 2 entries. Zero entry in the column indicatesthat the node is the root. Zero entry in the row means that, that element is a leaf node, only one entrymeans that the node has only one child and two entries denote that the node has both the children. “L” isused to represent left child and “R” is used to represent right child entries.
Fig. 6.2 A Binary tree
Chapter 6 - Representation and Traversal of a Binary Tree
l2...2222 3210
i.e., 122 1
10
l
k
k
75BSIT 41 Algorithms
The adjacency matrix representation for the binary tree shown in Fig. 6.2 is given below.
Now, let us see the space utilization of this method of binary tree representation. Let ‘n’ be the numberof vertices. There is space allocated for n x n matrix. i.e., we have n2 space allocated, but we have onlyn-1 entries in the matrix. Therefore, the percentage of space utilization is given as follows:
The space utilization decreases as n increases. For large ‘n’, the percentage of utilization becomesnegligible. This is, therefore, not the most efficient method of representing a binary tree.
6.2.2 Single Dimensional Array Representation
Since the two dimensional array is a sparse matrix, we can consider the prospect of mapping it onto asingle dimensional array for better space utilization. In this representation, we have to note the followingpoints:
The left child of the ith node is placed at the 2ith position.
The right child of the ith node is placed at the (2i+1) th position.
The parent of the ith node is at the (i/2) th position in the array.
Thus the single dimensional array representation for the binary tree shown in Fig.6.2 turns out to be asfollows.
%10011
2
n
nn
n
A B C D E F G H I J KA L RB L RC LDE L RF RGHI L RJK
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
D E F G H I J K
76
If l is the depth of the binary tree then, the number of possible nodes in the binary tree is 2l+1-1. Henceit is necessary to have 2l+1-1 locations allocated to represent the binary tree.
If ‘n’ is the number of nodes, then the percentage of utilization is
For a complete and full binary tree, there is 100% utilization and there is a maximum wastage if thebinary tree is right skewed or left skewed, where only l+1 spaces are utilized out of the 2l+1 – 1 spacespossible.
i.e., is the worst possible utilization in the single dimensional array representation.
An important observation to be made here is that the organization of the data in the binary tree decidesthe space utilization of the representation used.
The other alternative way of representing the binary tree is based on Linked Allocation (dynamicallocation method).
6.2.3 Linked Representation of Binary Trees
The linked representation of a binary tree has the node structure shown in Figure 6.3. Here, the nodebesides the DATA field needs two pointers LCHILD and RCHILD to point to the left and right childnodes respectively. The tree is accessed by remembering the pointer to the root node of the tree. Figure6.4 shows an example binary tree and Figure 6.5 shows its linked representation.
Figure 6.3 Structure of Node in Binary Tree
Figure 6.4 A Binary Tree
Chapter 6 - Representation and Traversal of a Binary Tree
1002
11
l
nl
10012
11
l
l
77BSIT 41 Algorithms
Figure 6.5 Linked representation of a Binary Tree
In the binary tree T shown in Figure 6.5, LCHILD (T) refers to the node storing B and RCHILD (T)refers to the node storing C and so on. The following are some of the important observations regarding thelinked representation of a binary tree:
· If a binary tree has n nodes then the number of pointers used in its linked representation is 2 * n
· The number of null pointers used in the linked representation of a binary tree with n nodes is n + 1.
However, in linked representation, it is difficult to determine a parent given a child node. In any case,if an application so requires, a fourth field PARENT may be included in the structure.
Figure 6.6 illustrates the node structure of a binary tree, which incorporates the parent information andFigure 6.7 shows an instance of a binary tree with additional parent information.
Figure 6.6 Node structure with parent link
From Figure 6.6, we can observe that the PLINK field of the node contains the address of its parentnode. If we represent a binary tree with this node structure as shown in Figure 6.7, we can find the parentnode of any given node by just following the PLINK pointer. Also observe that the PLINK field of theroot node is null, which indicates that there is no parent node a root node.
78
Figure 6.7 Linked representation of a binary tree with access to parent node.
In this representation, ordering of nodes is not mandatory and we require a header to point to the rootnode of the binary tree. If there are ‘n’ nodes to be represented, only ‘n’ node-structures will be allocated.This means that there will be 2n link fields. Out of 2n link fields only (n-1) will be actually used to point tothe next nodes and the remaining are wasted. Therefore the percentage of utilization is given as:
The linked representation of a binary tree discussed above can be used to effectively represent thearithmetic expressions and decision process as described below.
Arithmetic Expression Tree
Binary tree associated with an arithmetic expression is called an arithmetic expression tree. Theinternal nodes represent the operators and the external nodes represent the operands. Figure 6.8 showsthe arithmetic expression trees.
(a) (2 * (a “ 1) + (3 * b)) (b) (a * b) + (c / d)
Chapter 6 - Representation and Traversal of a Binary Tree
79BSIT 41 Algorithms
(c) ((a + b) + c) + d) (d) ((-a) + (x + y)) / ((+ b) * (c * a))
Figure 6.8 Example expression trees
Decision Tree
Binary tree associated with a decision process is called a decision tree. The internal nodes representthe questions with yes/no answer and the external nodes represent the decisions. Figure 6.9 shows thedecision tree for the biggest of three numbers.
Figure 6.9 Decision tree
6.2.4 Binary Tree as a Data Structure
A collection of vertices is said to be a binary tree if there is a special vertex called root and theremaining vertices can be partitioned into utmost two such that each partition is a binary tree. To describea binary tree as a data structure, we need to define its domain, a set of functions permitted on binary treeand the axioms associated with it.
Domain: Application Dependent
Functions:
Create-BT () —————————————————— Allocation
Construction (BT1, BT
2, data) ——————— BT
80
Traversal (BT, option) ———————————— Sequence
Heap (BT) ——————————————————— Heap
Root (BT) ——————————————————— Data
Lchild (BT) —————————————————— BT
Rchild (BT) —————————————————— BT
Search (BT, data) ——————————————— Boolean
Insertion (BT, e) ——————————————— Updated BT
Deletion (BT, e) ——————————————— Updated BT
Isempty (BT) ————————————————— Boolean
Isfull (BT) —————————————————— Boolean
Axioms:
Isempty (Create-BT()) ——————————— True
Isfull (Create-BT()) ————————————— False
Root (Construction(BT1, BT
2, e)) ————— e
Lchild (Construction(BT1, BT
2, e)) ———— BT
1
Rchild (Construction(BT1, BT
2, e)) ———— BT
2
Search ((Insertion(BT, e), e) ———————— True
BT (D, F, A): Binary tree as a data structure.
6.3 TRAVERSAL OF A BINARY TREE
Traversal is the most important operation done on a binary tree. Traversal is the process of visiting allthe vertices of the tree in a systematic order. Systematic means that every time the tree is traversed itshould yield the same result.
There are three major methods of traversals and the rest are reversals of them. They are:
1. Pre-order traversal
2. In-order traversal
3. Post-order traversal
Chapter 6 - Representation and Traversal of a Binary Tree
81BSIT 41 Algorithms
Pre-Order Traversal
In this traversal, the nodes are visited in the order of root, left child and then right child. i.e.,
1 Visit the root node first.
2 Go to the first left sub-tree.
3 After the completion of the left sub-tree, Go to the right sub-tree.
Here, the leaf nodes represent the stopping criteria. We go to the sibling sub-tree after the traversal ofa sub-tree. The pre-order traversal sequence for the binary tree shown in Fig. 6.3 is : A B D E G H C FI J K
In-Order Traversal
In this traversal, the nodes are visited in the order of left child, root and then right child. i.e., the leftsub-tree is traversed first, then the root is visited and then the right sub-tree is traversed. Here also, theleaf nodes denote the stopping criteria. The in-order traversal sequence for the above considered example(Fig. 6.3) is: D B G E H A F J I K C
Post_Order Traversal
In this traversal, the nodes are visited in the order of left child, right child and then root. i.e., the leftsub-tree is traversed first, then the sibling is traversed next. The root is visited last. The post-ordertraversal for the above example (Fig. 6.3) is: D G H E B J K I F C A
6.3.1 Traversal of a Binary Tree Represented in an AdjacencyMatrix
The steps involved in traversing a binary tree from the adjacency matrix representation, firstly requiresto find out the root node. Then it entails to traverse through the left subtree and then the right subtree inspecific orders. In order to remember the nodes already visited it is necessary to maintain a stack datastructure. Thus, following are the steps involved in traversing trough the binary tree given in an adjacencymatrix representation.
1 Locate the root (the column sum is zero for the root)
2 Display
3 Push in to the stack
4 Scan the row in search of ‘L’ for the left child information
5 Pop from the stack
6 Scan the row in search of ‘R’ for the right child information
82
7 Check if array IsEmpty().
8 Stop
Sequencing the above stated steps helps us in arriving at preorder, inorder and postorder traversalsequences.
Preorder Traversal
Fig.6.4 shows flow graph of preorder traversal.
Fig. 6.10 Preorder flow graph
Note that before popping, we need to check whether the stack is empty or not.
Inorder Traversal
The in-order traversal will also have the same steps explained above. Only the flow graph sequencechanges, and is given in Fig. 6.5.
Chapter 6 - Representation and Traversal of a Binary Tree
‘L’ found
83BSIT 41 Algorithms
Fig. 6.11 Inorder flow graph
Designing a flow graph for post order traversal is left as an exercise to the students.
6.3.2 Binary Tree Traversal from one Dimensaional ArrayRepresentation
Preorder Traversal
Algorithm: Preorder Traversal
Input: A[], one dimensional array representing the binary tree
i, the root address //initially i=1
Output: Preorder sequence
Method:
If (A[i] != 0)
Display(A[i])
Preorder Traversal (A, 2i)
Preorder Traversal (A, 2i +1)
If end
Algorithm ends
84
Inorder Traversal
Algorithm: Inorder Traversal
Input: A[], one dimensional array representing the binary tree
i, the root address //initially i=1
Output: Inorder sequence
Method:
If (A[i] != 0)
Inorder Traversal (A, 2i)
Display(A[i])
Inorder Traversal (A, 2i +1)
If end
Algorithm ends
Postorder Traversal
Algorithm: Postorder Traversal
Input: A[], one dimensional array representing the binary tree
i, the root address //initially i=1
Output: Postorder sequence
Method:
If (A[i] != 0)
Postorder Traversal (A, 2i)
Postorder Traversal (A, 2i +1)
Display(A[i])
If end
Algorithm ends
We shall now look into the tree traversals in linked representation
Chapter 6 - Representation and Traversal of a Binary Tree
85BSIT 41 Algorithms
6.3.3 Binary Tree in Linked Representation
As we already studied, every node has a structure which has links to the left and right children. Thealgorithms for traversing the binary tree in linked representation are given below.
Algorithm: Pre-order Traversal
Input: bt, address of the root node
Output: Preorder sequence
Method:
if(bt != NULL)
{
Display([bt].data)
Pre-order Traversal([bt].Lchild)
Pre-order Traversal([bt].Rchild)
}
Algorithm ends.
Algorithm: In-order Traversal
Input: bt, address of the root node
Output: Inorder sequence
Method:
if(bt != NULL)
{
In-order Traversal([bt].Lchild)
Display([bt].data)
In-order Traversal([bt].Rchild)
}
Algorithm ends.
86
Algorithm: Post-order Traversal
Input: bt, address of the root node
Output: Postorder sequence
Method:
if(bt != NULL)
{
Post-order Traversal([bt].Lchild)
Post-order Traversal([bt].Rchild)
Display([bt].data)
}
Algorithm ends.
6.4 OPERATIONS ON BINARY TREE
We have already discussed an important operation performed on binary trees called traversal. Thevarious other operations that can be performed on binary trees are discussed as follows.
Insertion
To insert a node containing an item into a binary tree, first we need to find the position for insertion.Suppose the node pointed to by temp has to be inserted whose information field contains the item J asshown in Figure 6.12, we need to maintain an array say D, which contains only the directions where thenode temp has to be inserted.
Figure 6.12 To insert a item J
Chapter 6 - Representation and Traversal of a Binary Tree
87BSIT 41 Algorithms
If D contains ‘LRLR’, from the root node, first move towards left (L), then right (R), then left (L) andfinally move towards right (R). If the pointer points to null at that position, node temp can be insertedotherwise, it cannot be inserted. To achieve this, one has to start from the root node. Let us use twopointers prev and cur where prev always points to parent node and cur points to child node. Initially curpoints to root node and prev points to null. To start with one can write the following statements.
prev = null
cur = root
Now, keep updating the node pointed to by cur towards left if the direction is ‘L’ otherwise, updatetowards right. Once all directions are over, if current points to null, insert the node temp towards left orright based on the last direction. Otherwise, display error message. This procedure can be algorithmicallyexpressed as follows.
Algorithm: Insert Node
Input: root, address of the root node
e, element to be inserted
D, direction array
Output: Tree updated
Method:
1. temp = Getnode()
2. temp.info = e
3. temp.llink = temp.rlink = null
4. if (root = null) return temp // Node inserted for the first time
5. prev = null, cur = root, k = 0
6. While ( k < strlen(D)) DO
If (cur = null) exit
prev = cur
if (D[k] = ‘L’) then
cur = cur.llink
else
cur = cur.rlink
Whileend
88
7. If ((cur ‘“ null) OR k ‘“ strlen(D)) then
Display “Insertion not possible”
Free(temp)
Return(root)
8. If (D[k – 1] = ‘L’) then
prev.llink = temp
Else
prev.rlink = temp
Ifend
9. root
10.stop
Algorithm ends
Searching
To search for an item in a tree, we can traverse a tree in any of the (inorder, preorder, postorder) orderto visit the node. As we visit the node, we can compare the item to be searched with the data item storedin information field of the node. If found then the search is successful otherwise, search is unsuccessful.A recursive inorder traversal technique used for searching an item in binary tree is presented below.
Algorithm: Search(item, root, flag)
Input: item, data to be searched
root, address of the root node
flag, status variable
Output: Item found or not found
Method:
1. if (root = null then
flag = false
exit
ifend
Chapter 6 - Representation and Traversal of a Binary Tree
89BSIT 41 Algorithms
2. Search (item, root.llink, flag)
3. if (item = root.info) then
flag = true
exit
ifend
4. Search (item, root.rlink, flag)
5. if (flag = true) then display “Data item is found”
else display “Data item is not found”
ifend
6. stop
Algorithm ends
Deletion
Deletion of a node from a binary tree involves searching for a node which contains the data item. Ifsuch a node is found then that node is deleted; otherwise, appropriate message is displayed. If the node tobe deleted is a leaf node then the deletion operation is a simple task. Otherwise, appropriate modificationsneed to be done to update the binary tree after deletion. This operation is explained in detail consideringanother form of a binary tree called binary search tree.
Binary Search Tree
Binary Search Tree (BST) is an ordered Binary Tree in that it is an empty tree or value of root node isgreater than all the values in Left Sub Tree (LST) and less than all the values of Right Sub Tree (RST).Right and Left sub trees are again binary sub trees by themselves. Figure 6.13(a) shows an examplebinary tree where as Figure 6.13(b) is not a binary search tree but a binary tree.
Figure 6.13(a) A binary search tree Figure 6.13(b) Not a binary search tree
90
We will be using BST structure to demonstrate features of Binary Trees. The operations possible on abinary tree are
Create a Binary Tree
Insert a node in a Binary tree
Delete a node in a Binary Tree
Search for a node in Binary search Tree
Algorithm: Creating Binary Tree
Step 1: Do step 2 to 3 till stopped by the user
Step 2: Obtain a new node and assign value to the node
Step 3: Insert on to a Binary Search tree
Step 4: return
Algorithm: Insertion of node into a Binary Search Tree (BST)
InsertNode (node, value)
Check if Tree is empty
if (empty ) then Enter the node as root
else // find the proper location for insertion
if (value < value of current node)
If (left child is present)
InsertNode( LST, Value)
ifend
else
allocate new node and make LST pointer point to it
ifend
else if (value > value of current node)
if ( right child is present)
InsertNode( RST, Value);
else
allocate new node and make RST pointer point to it
ifend
ifend
ifend
Chapter 6 - Representation and Traversal of a Binary Tree
91BSIT 41 Algorithms
Figure 6.14 (a – b) illustrates an insertion operation.
Figure 6.14(a) Before insertion Figure 6.14(b) After insertion of item 5
Deleting a node from a Binary Search Tree
There are three distinct cases to be considered when deleting a node from a BST. They are
a) Node to be deleted is a leaf node.
Make its parent to point to NULL and free the node. For example, to delete node 4 the right pointer ofits parent node 5 is made to point to NULL and free node 4. Figure 6.15 shows an instance of deleteoperation.
Figure 6.15 Deleting a leaf node 4
(b) Deleting a node with one child only, either left child or Right child.
For example, delete node 9 that has only a right child as shown in Figure 6.16. The right pointer of node7 is made to point to node 11. The new tree after deletion is shown in 6.17.
Figure 6.16 Deletion of node with only one child Figure 6.17 New tree after deletion
92
(c) Node to be deleted is an intermediate node.
To perform operation RemoveElement(k), we search for key k
Assume key k is in the tree, and let v be the node storing k
If node v has a leaf child w, we remove v and w from the tree with operationRemoveAboveExternal(w)
Example: Remove 4
Figure 6.18 Before deletion Figure 6.19 After deletion
Searching for a node in a Binary Search Tree
To search for a key k, we trace a downward path starting at the root
The next node visited depends on the outcome of the comparison of k with the key of thecurrent node
If we reach a leaf, the key is not found and we return NO_SUCH_KEY
Example: findElement(4)
Algorithm: findElement (k, v)
if T.isExternal (v)
return NO_SUCH_KEY
if k < key(v)
return findElement(k, T.leftChild(v))
else if k = key(v)
return element(v)
Chapter 6 - Representation and Traversal of a Binary Tree
93BSIT 41 Algorithms
Algorithm ends
Figure 6.20 shows an instance of searching for a node containing element 4.
Figure 6.20 Searching operation
SUMMARY
In this chapter the binary trees are introduced. Trees form the core of non-linear data structure andBinary tree helps in storing the data more efficiently although the access is not as simple as arrays. Thevarious ways of representing the binary tree, linear, two dimensional arrays and linked representation arediscussed. The algorithms for traversing the binary tree represented in various representations are alsopresented.
EXERCISE
1. What is a binary tree
2. Differentiate complete and full binary trees
3. What is the maximum number of nodes in a binary tree of level 7, 8 and 9
4. What are the wastage of memory for a binary tree with 16 nodes represented in a 1D array, 2D array and a linkedrepresentation.
5. For a atleast 5 binary trees of different depths greater than or equal to 6 of your choice obtain the preorder,
postorder and inorder sequences.
6. Explain linked representation of a binary tree with its typical node structure.
return element(v)
else { k > key(v) }
return findElement(k, T.rightChild(v))
7. prove that there are n+1 null pointers in linked representation of a binary tree of n nodes
8. Explain expression tree.
9. Explain decision tree.
10. Present various operations on binary tree.
References
1. Sartaj Sahni, 2000, Data structures, algorithms and applications in C++, McGraw Hill international edition.
2. Horowitz and Sahni, 1983, Fundamentals of Data structure, Galgotia publications
3. Horowitz and Sahni, 1998, Fundamentals of Computer algorithm, Galgotia publications.
4. Narsingh Deo, 1990, Graph theory with applications to engineering and computer science, Prentice hall
publications.
5. Tremblay and Sorenson, 1991, An introduction to data structures with applications, McGraw Hill edition.
6. Horowitz, Sahni and Rajasekaran, 2000, Computer algorithms, Galgotia publications
7. Dromey R. G., 1999, How to solve it by computers, Prentice Hall publications, India.
~ ~ ~
Chapter 6 - Representation and Traversal of a Binary Tree94