377
DATA STRUCTURES AND ALGORITHM ALGORITHMS WITH ANALYSES MULTIPLE CHOICE QUESTIONS AND ANSWERS QUESTIONS OF UNIVERSITIES, GATES AND SOLUTIONS SAMPLE PROGRAMS FOR LINKED REPRESENTATION OF STACK, QUEUE AND TREES Dr.Arup Kr.Bhaumik Santanu Haldar Subhrajit Sinha Roy

Data Structures and Algorithms Indian Quest

Embed Size (px)

DESCRIPTION

SAMPLE PROGRAMS FOR LINKED REPRESENTATION OF STACK, QUEUE AND TREES

Citation preview

  • DATA STRUCTURES AND ALGORITHM

    ALGORITHMS WITH ANALYSES

    MULTIPLE CHOICE QUESTIONS AND ANSWERS

    QUESTIONS OF UNIVERSITIES, GATES AND SOLUTIONS

    SAMPLE PROGRAMS FOR LINKED REPRESENTATION OF STACK, QUEUE AND TREES

    Dr.Arup Kr.Bhaumik

    Santanu Haldar

    Subhrajit Sinha Roy

  • CONTENTS

    Preface

    Acknowledgements

    1. INTRODUCTION1.1 Information and Data1.2 Basic Operation of a Computer1.3 Solvability of Problem and Data Structure1.4 Mathematical Modeling of Data1.5 Representation of Numbers in Computer1.6 Group Data or Structured Data 1.7 Algorithm and Notations Used in The Book1.8 MCQ Chapter 11.9 Exercises Chapter 1

    2. PRELIMINARIES2.1 Definition of algorithm2.2 Properties of an Algorithm2.3 Algorithm Development Life Cycle2.4 Example of some Algorithms2.5 Complexity of Algorithms (Analyzing phase)

    2.5.1 Asymptotic notations 2.5.2 How can we estimate the time complexity in big Oh(O) notation?2.5.3 How can we improve the complexity of algorithms?2.5.4 Properties of Big Oh2.5.5 Rate of growth of Big Oh notation

    2.6 Some popular Mathematical Notations and Functions2.7 Algorithmic Notation2.8 MCQ Chapter 22.9 Exercises Chapter 2

    3. ARRAY3.1 Introduction and Definition3.2 Declaration of Array3.3 Memory Representation of Array3.4 Insertion in one dimentional Array3.5 Deletion from one dimentional Array3.6 Travarsing one dimentional Array3.7 Merging two one dimentional Array3.8 Matrix Addition3.9 Sparse Matrix3.10 Polynomial Representation3.11 MCQ Chapter 3

  • 3.12 Exercises Chapter 3

    4 STRUCTURE AND POINTER4.1 Definition of Structure4.2 Basic of Pointer4.3 Structure and Pointer4.4 Passing Structure to Pointer4.5 Self Refferential Structure4.6 Dynamic Memory Allocation4.7 MCQ Chapter 4 4.8 Exercises Chapter 4

    5. STRINGS 5.1 Definition5.2 Concatenation and substrings5.3 String operations

    5.3.1 Alphabet of a string5.3.2 String substitution5.3.3 String homomorphism5.3.4 String projection5.3.5 String Right quotient5.3.6 String Syntactic relation5.3.7 String Right cancellation5.3.8 String Prefixes

    5.4 String Topology5.5 String datatypes5.6 String length5.7 String Representations5.8 String as Vectors5.9 String processing algorithms

    5.9.1 String searching algorithms5.9.1.1 Basic classification5.9.1.2 Single pattern algorithms5.9.1.3 Algorithms using finite set of patterns5.9.1.4 Algorithms using infinite number of patterns

    5.9.2 String manipulation algorithms or String Functions5.9.3 String Sorting algorithms5.9.4 Regular Expression Algorithms5.9.5 Parsing a String

    5.10 String Implementations5.11 MCQ Chapter 55.12 Exercises Chapter 5

    6. STACK6.1 Definition6.2 Basic Operations6.3 Implementation of stack using array6.4 Implementation of stack using dynamic memory allocation6.5 Arithmetic Expressions

  • 6.6 Converting Infix expressions to postfix notation6.7 Converting Infix expression to prefix notation6.8 Evaluating Postfix Expression6.9 Evaluating Prefix Expression6.10 Application of Stack6.11 Reversing a string6.12 Checking balanced parenthesis6.13 Multiple Stacks6.14 MCQ Chapter 66.15 Exercises Chapter 6

    7. QUEUE7.1 Definition7.2 Implementation of queue using array7.3 Implementation of Queue using dynamic memory allocation7.4 Limitation of linear queue7.5 Circular Queue7.6 Deque(Double Ended Queue)7.7 Priority Queue7.8 Implementation of queue using stacks7.9 Application of Queue7.10 MCQ Chapter 77.11 Exercises Chapter 7

    8. LINKED LISTIntroduction DefinitionAdvantagesDisadvantagesOperations on Linked ListTypes of Linked ListSingly Linked List Reversing a Singly Linked ListPolynomial using singly linked list Advantages and disadvantages of singly linked list Circular linked list Double linked list MCQ Chapter 8 Exercises Chapter 8

    9. RECURSIONDefinitionTypes of recursionLinear recursionBinary recursionMultiple recursionSome recursive algorithmsFactorial functionGCD of two numbers

  • Exponential PowerFibonacci sequenceTowers of HanoiTail of recursionDisadvantages of recursionRecursion Vs Iteration MCQ Chapter 9Exercises Chapter 9

    10. TREE Definition Basic terminologies Binary tree Types of binary trees Properties of binary trees Representation of binary tree Binary tree traversal methods Creation of binary tree from preorder and inorder traversal Creation of binary tree from postorder and inorder traversalExpression treeBinary search treeHeapWeight balanced binary treeAVL Search tree / Height balanced binary treeThreaded binary treeM-way search treeB-Tree/Balanced M-way search treeRed/Black treeMCQ Chapter 10Exercises Chapter 10

    11. GRAPH Definition Basic Terminologies Representation of a graph Operations on a graph Traversal of a graphBreadth first searchDepth first search Shortest path calculation (Dijkstras algorithm) Warshalls algorithm MCQ Chapter 11 Exercises Chapter 11

    12. SORTING12.1 Definition 12.2 The family of sorting methods12.3 Bubble Sort12.4 Insertion Sort

  • 12.5 Selection Sort12.6 Merge Sort12.7 Quick Sort12.8 Heap Sort12.9 Radix Sort12.10 Comparison study of different sorting12.11 MCQ Chapter 1212.12 Exercise Chapter 12

    13. SEARCHING13.1 Introduction13.2 Sequential Search13.3 Binary Search13.4 MCQ Chapter 1313.5 Exercises Chapter 13

    14. HASHING14.1 Introduction14.2 Hash Table14.3 Hash Functions14.4 Collision14.5 Clustering14.6 Collision resolution Technique14.7 Load Factor14.8 Analysis of Open Addressing and Chaining14.9 MCQ Chapter 1414.10 Exercises Chapter 14

    15. FILES 15.1 Introduction15.2 Definition15.3 File Classification

    15.3.1 Non Structured file15.3.1.1 Text File15.3.1.2 System File

    15.3.1.2.1 Command File (.COM):15.3.1.2.2 Executable File (.EXE)15.3.1.2.3 Dynamic Link Laboratory File(.DLL)

    15.4 Structured File15.4.1 Sequential File15.4.2 Index Sequential File

    15.5 MCQ Chapter 1515.6 Exercises Chapter 15

    16. Various Questions on Data Structure

  • Preface

    The data nowadays play very important role in computation. Versatile usage of computers in every spheres of life enhances handling of different kinds of data and its representation in computers memory. But a programmer must have clear idea about requirement of data structure and algorithm for writing solution of a problem in computer. Otherwise, efficiency will never be achieved. Though there are several books on data structure available in the market but in a particular book all the topics like algorithms, analyses, equivalent programs in C language , Multiple Choice Questions (MCQ) and excersies are not available. On delivering lectures for 10 years since in theory and lab classes in this subject we have faced many questions of students and their suggestions. In many occasions we have used our intuition to make the students understanding in different topics of this subject. We have incorporated all of these strategies in our book and also programs are written in C language to encourage students to try for testing of algorithms.

    The book has been written by covering syllabi of B.Tech, B.E., MCA, BCA & Bsc. Computer sc. courses of all most all Indian and Foreign Universities. Ho.pe everybody from new learner to expert programmer may get help from the book.

  • Acknowledgements

    The most inspiaratation that all of us have received from our beloved students, thus we must thank them at first. Secondly, Mr. S.K.Bagal and Mr. Lahiri of S. Chand & Co.Ltd. both of them have tendered their help, courage and moral support to write the book, all of us are very thankful to both of them. While preparation of this manuscript we have received help and criticisims from our colleague Mr. Animesh Hazra, Lecturer Computer Sc. & Engg. So we convey our thanks to Mr.Hazra.

  • Chapter 1Introduction

    1.1: Information and Data

    Information: The Oxford dictionary meaning is (1) Facts or Knowledge provided or learned. (2) Computer Data.

    Data: The Oxford dictionary meaning is (1) Facts or statistics used for reference or analysis (2) The quantities, characters or symbols operated on by a Computer.

    Thus, from the definitions of the dictionary one can assume that the terms information and data can be used interchangeably. But in context of the computer any recorded or stored fact, which needs for processing by the Central Processing Unit (CPU) is called data i.e. for example Students name, Address, Marks obtained in different subjects when recorded is called data and when a program is written to find grades of students on the basis of marks obtained in various subjects the output will appear as A, B, C etc. or First Class, 2nd Class etc., which are information. Moreover, if grades are recorded or stored for further computation then these will be termed as data also. 1.2 Basic Operation of a Computer

    Any computer has three distinct units, viz. Input/Output, Memory and Central Processing Unit (CPU). The input unit is used to feed data to the computers memory and the output unit is used to show the result after computation. The memory unit is used to store data and program. The CPU is the main unit comprising of two sub units viz. Control unit (CU) and the Arithmetic Logic Unit (ALU).

    The CPU does following operations in steps in repeated manner:Step 1: Fetch an instruction from memoryStep 2: Decode the instructionStep 3: Fetch the operands needed for the operationStep 4: Execute the instruction in ALU

    It is clear from the steps of operations that first of all computer needs an instruction i.e. a command (to do some operations) and then operand (or data) is required for completion of operation.The operations performed by a bare CPU are very basic such as Addition, Subtraction, Multiplication, AND, OR, NOT etc. on binary data. Thus, if everything i.e. instructions and data are represented in binary then the binary sequence is called machine code. But to work with machine code is very difficult either to write a full program or later on to debug it. To make the understanding of machine code just easy the equivalent hexadecimal representation is used.

    Thus, for a bare CPU the data types like integers, real etc. has no meaning, these are the descriptions of data done through high level language (HLL) and the translator program i.e. either compiler or interpreter takes responsibility to convert integer, real etc. data type to binary format and vice versa.

    1.3 Solvability of Problem and Data Structure

    There are various types of problem framed in different aspects of life in our society e.g. computation of salary, income tax etc. for employees, debit, credit analysis and budgeting of any firm, weather forecasting, designing and analyzing structures for cars, airplane, building etc., analysis for gene etc. The quicker solutions of these

  • problems ensure progress of a society. Nowadays, computers are used in every sphere of life to get solutions of problems in a quickest and accurate way. But computer needs appropriate program for each problem. Problems like computation of factorial of a positive integer, sorting a list of names, computation of salary of employees, computation of free memory blocks etc. are required different methods with different data set (values) to solve each of these. Whenever a problem is to be solved through a computer, a program is required to develop, and in a program data are passed through variables, where variables are used to represent memory references either in single location or in a group of locations to store atomic or group data respectively.

    1.4 Mathematical Modeling of Data

    In our mathematics any real value is classified into:

    i) Integer numbers (both +ve and ve) e.g. 0, 1, 2, 3, . . . etc

    ii) Rational numbers: Number derived from division of two integers (x, y) such as x/y, where y 0. The number x/y is also called a fraction and if the magnitude of x is less than y then the fraction is proper. Otherwise, if magnitude of x is greater than y then the fraction is improper fraction.

    Thus, 1/2 , 5/2, 1/3, 1/7 etc. are all fractions out of which 5/2 is only improper fraction. But 1/3 and 1/7 do not provide exact values since divisions will never terminate. These are called periodic or recurring fractions. In mathematics value of 1/3 is written as 0.3, which represents 0.333333333 and value of 1/7 is written as 0.1428571, which represents 0.14285711428571 .

    iii) Irrational Numbers: These are the numbers obtained from non-rational roots of algebraic equations such as 2, 3, 5 etc. These numbers cannot be expressed either as terminating decimal or a periodic decimal. These are represented geometrically by points, namely the gaps between rational points on a line.

    1.5 Representation of Numbers in Computer

    Almost all Compilers / Interpreters written for high level languages e.g. FORTRAN, C, COBOL, PASCAL etc. allow data to be declared in following types:

    A. Numerical (i) Integer: generally two bytes i.e. 16 bits are considered to represent both negative and positive integers. With 16 bits or two bytes decimal integers, numbers from -32768 to +32767 only can be represented. Where for representation of ve numbers 2s complement notation is used. Since in 2s complement notation -32768 is 1000 0000 0000 0000 & +32767 is 0111 1111 1111 1111. However, in case of only positive numbers the range of decimal numbers 0 65535 is represented. Therefore it is observed that a very small decimal value can be represented by 16 bits or two bytes of data. But in our environment often we need large values to represent physical quantities such as net asset value of a small company 10,00,000,000/- (Rs.10 Crore), population of any country 100,00,000,000 ( 100 Crore) etc. Thus further grouping of bytes are required to represent large numbers. The simple integers are called primitive data types. Almost all high level languages provide additional data types to accommodate large values in computers. For example, when 4 bytes are grouped, the range of decimal values -2,147,483,648 to 2,147,483,647 are

  • represented . In general the following formula is used to establish relationships between binary numbers and the equivalent decimal integers.

    -2n-1 +1 N 2n-1 -1 , Where n = no. of bits, N= decimal integers.

    Negative numbers are represented by 2s complement notation.

    In C-language the following data types are used to represent integers.

    In high level languages there is another data type available to represent fractional decimal numbers, which is called float in C Language and Real in FORTRAN.But in C language to accommodate more decimal digits double and long double types are also used, which is described below:

    B.Character

    Single characters e.g. letters, digits, punctuation symbols etc. are represented by character data type in C language the keyword char is used to declare character data type. Either ASCII or EBCDIC format is used for depicting a character and one byte of space occupied in memory for storing the same. The ASCII format has been used in C language to describe character data type and if anybody wants to write a in computer, actually the binary sequence 10010111, equivalent to ASCII value 97 is stored in memory.

    1.6 Group Data or Structured Data

    The most rigorous usage of computers happen in the areas of banking, insurance, health, university etc. where data are generally represented in the form of record, which represents a group of molecular or elemental information. As for example a record of a registered student of any university looks like:

    103000532 Manmohan Sing B.Tech CSE 2007

    Where the first element represents Registration number, the second element is name, the third one is the course of study, fourth one is the subject and the last one is the year of registration. All the elements together comprise

    Type Range Bytes Represents int -32768 - 32767 2 Whole numbersUnsigned int 0 - 65535 2 -do-long -2,147,438,648

    2,147,438,6474 -do-

    Unsigned long 0 - 429,496,7295 4 -do-

    Type Range Bytes Represents From To

    Float 3.4 x 10-38 - 3.4 x 10-38 4 Fractional numbers double 1.7 x 10-308 1.7 x 10-308 8 -do-long double 3.4 x 10-4932 3.4 x 10-4932 10 -do-

  • a record of a registered student and carry meaningful information. But no single (primitive) data types available in any HLL to represent the entire record in computer. However, all third generation languages have provision of declaration of record to support different data types. But do not allow populate data in file with different structues, e.g. COBOL language allows three kinds of file structure like Sequential, Random and Index Sequential. On the contrary the 'C' Language does not allow Random and Index Sequential file structure in general to populate data, special programs are required for these files.

    1.7 Algorithm and Notations Used in The Book

    We have used 'C' Language and it's notation in this book for writing algorithms, but the full syntaxes of the language have not been considered. Thus, one can convert algorithms to equivalent programs very easily just by maintaing syntaxes only. Moreover, very simple conventions have been followed to name variables or identifiers in our program e.g. if the variables start with i it represents integers data type etc. ---------------------- write for others.

    The book has been divided into --- nos of chapters, where in chapter-1, following topis are covered ---------

    we have provided questions and solutions of these, where questions are collected from different Universities to provide guide line for preparation of different examinations.

    1.8 MCQ Chapter 1

    1. The most widely used method for interpreting bit setting as non-negative integer is(a)Binary number system (b)BCD system (c)ASCII system (d)None of the above

    2. Any string of bits of length n represents a unique non-negative integer between(a) 0 and 2n-1-1 (b) 0 and 2n-1 (c) 1 and 2n-1 (d) None of the above

    3. With 1' s complement method, the range of the numbers that can be represented using n bitsis :(a) a 1 followed by using n - 1 zeroes to a 0 followed by n - 1 ones (b) a 0 followed by using n - 1 ones to a 1 followed by n - 1 zeroes(c) 2n-1-1 to 2n-1-1(d) None of the above.

    4. Using 2's complement method, the range of the numbers that can be represented using n bitsis :(a) a 1 followed by using n - 1 zeroes to a 0 followed by n - 1 ones (b) a 0 followed by using n - 1 ones to a 1 followed by n - 1 zeroes(c) 2n-1-1 to 2n-1-1(d) None of the above.

    5. Key concept for representing a real number is a mantissa times(a) 10 raised to an exponent (b) 2 raised to an exponent(c) base raised to an exponent (d) None of the above.

  • 6. The set of native data types that a particular computer can support is determined by : (a) type of hardware company(b) what functions have been wired into hardware(c) what software support is required(d) None of these.

    7. While considering data structure implementation, the factor(s) under consideration is (are) :(a) time (b) time and space(c) time, space and processor (d) None of these.

    8. Which is not true ?(a) Abstract data type is the useful tool for specifying the logical properties of the data type (b) While defining an abstract data type as a mathematical concept, the space and efficiency

    is not of major concern(c) Every abstract data type can be implemented using any programming language(d) None of these.

    9. The number of 1s in the binary representation of 3 * 4096 + IS * 256 + 5 * 16 + 3 are(a) 8 (b) 9 (GATE-1995)(c) 10 (d) 12.

    10. Consider the following floating point representation (GATE- 1996)31 24 23 0Exponent Mantissa

    The exponent is in 2' s complement representation and mantissa is in the sign magnitude representation. The range of the magnitude of .the normalized numbers in this representation is(a) 0 to 1 (b) 0.5 to 1(c) 2-23 to 0.5 (d) 0.5 to (1-2-23).

    11. The octal representation of an integer is 3428. If this were to be treated as an eight bit integer,its decimal equivalent is (GATE-1998)(a) 226 (b) -98(c) 76 (d) -30

    12. Zero has two representations in: (GATE-1999)(a) Sign magnitude (b) 2's complement(c) All of the above (d) None of the above.

    13. Consider the values A = 2.0 x 1030, B = - 2.0 x 1030, C = 1.0, and the sequence (GATE-2000)X := A + BY := A + CX := X + CY:= Y + B

    executed on a computer where floating point numbers are represented with 32 bits. The valuesfor X and Y will be

  • (a) X = 1.0, Y = 1.0 (b) X = 1.0, Y = 0.0 (c) X = 0.0, Y = 1.0 (d) X = 0.0, Y = 0.0

    14. The number 43 in 2' s complement representation is(a) 01010101 (b) 11010101 (d) 10101011 (c) 00101011

    15. Which of the following is (are) example(s) of data abstraction?(a) List of student information(b) File of employee records(c) Bank account database(d) All of the above.

    16. Data structure means(a) The separation of the representation of data from the applications that use the data at a logical level(b)the logical picture of a data type, plus the specifications of the operations required to create and manipulate objects of this data type(c)a collection of data elements whose organization is characterized by accessing operations that are used to store and retrieve the individual data elements(d) None of these.

    17. Abstract Data Type means that(a) The separation of the representation of data from the applications that use the data at a logical level(b) The logical picture of a data type, plus the specifications of the operations required to create and manipulate objects of this data type(c) A collection of data elements whose organization is characterized by accessing operations that are used to store and retrieve the individual data elements(d) None of these.

    18. Why is writing easily modifiable code important? a) Easily modifiable code generally has a quicker run time. b) Most real world programs require change at some time. c) Most text editors make it easy to modify code.

    d) Several people may be writing the same function at the same time. 19. Which phase of the software life-cycle is usually the most expensive?

    a) Analysis and specification of the task b) Design c) Implementation d) Maintenance and evolution

    20. What will happen if a function is executed and the precondition for the function is not met? a) An error message will be printed. b) The program will loop indefinitely. c) The system will crash. d) Any of the above results could happen.

    21. If the precondition fails, it is a good idea to write a useful error message and then halt the program. Why is the program halted?

    a) Most operating systems forbid continuation. b) The function is no longer guaranteed to make the postcondition true. c) The function's memory requires have become exponential (or worse). d) The function's running time has become exponential (or worse).

  • 22. Which of these is used to stop the program execution when a precondition is not met. a) assert(); b) exit(); c) return(); d) void();

    23. Which of these statements will always cause a program to halt? (x is an int variable). a) assert(10 > 0); b) assert(10 < 0); c) assert(x < 0); d) None of the above will always cause a program to halt.

    24. What does a run-time analysis usually count? a) The number of arithmetic and other operations required for the program to run b) The number of megabytes required for the program to run c) The number of seconds required for the program to run d) The number of seconds plus the number of megabytes e) The number of seconds times the number of megabytes

    25. Why is it important to test boundary values when testing programs? a) Calculating by hand, it's easy to find the right answers for boundary values. b) Debuggers are easier to use when testing boundary values. c) In practice, a large proportion of errors arise from boundary values. d) The correct execution of a function on all boundary values proves a function is correct.

    26. How may boundary values for a function be found? a) Pick values that are one step away from different behavior. b) Pick values that make the precondition equal to the postcondition. c) Pick values where the precondition is false. d) Pick values where the postcondition is false.

    27. Which software tool will best help you determine whether your test cases are fully exercising your code? a) Compiler b) Debugger c) Make d) Pine e) Profiler

    28. A text is made up of the characters a, b, c , d, e each occurring with the probability .12, .4, .15, .08 and .25 respectively. The optimal coding technique will have the average length of

    (a) 2.15 (b) 3.01 (c) 2.3 (d) 1.78

    29. In the previous question, which of the following characters will have codes of length 3?(a) Only c (b) Only b (c) band c (d) Only d

    30. Which of the following abstract data types can be used to represent a many to many relation?(a) Tree (b) Plex (c) Graph (d) Queue

    31. The principle of locality justifies the use of(a) interrupts (b) DMA (c) polling (d) cache memory

    32. Unrestricted use of goto is harmful, because it(a) makes debugging difficult(b) increases the running time of programs(c) increases memory requirement 9f programs(d) results in the compiler generating longer machine code

  • 33. The main() function is always(a)a called function(b) a calling function(c) recursive function(d) used at the end of the program(e) None of these.

    34. The first digit of a decimal constant must be (a) zero(b) a non-zero number(e) a negative number(d) an integer(e) None of these.

    35. Floating point numbers are used instead of integers to(a) permit the use of decimal points in numbers(b) avoid being too specific about what value a number has(c) conceal the true value of the numbers (d) All of the above(e) None of these.

    36. An expression(a) is a collection of data objects and operators that can be evaluated to a single value(b) is a name that substitutes for a sequence of characters(c) causes the computer to carry out some action(d) All of the above(e) None of these.

    37. Consider the following arithmetic expression2 * x / (3 * y)Suppose x and y are floating-point variables that have been assigned the values x = 8.8 and y = 3.5. What would be the value of the expression?(a)1.61373 (b) 20.53333 (c)1.67619 (d) None of these (e) 2.51429

    38. The statement (i = (j = 4) + (k = 9):(a) assigns a value 13 to i(b) assigns a value 4 to i(c) gives an error message(d) assigns a value 7 to i(e) None of these.

    39. If p and q are assigned the values 2 and 3 respectively then the statementp = q++(a) gives an error message(b) assigns a value 4 to p (c) assigns a value 3 to p (d) assigns a value 5 to p (e) None of these.

    40. If the variables i,j and k are assigned the values 5, 3 and 2 respectively, then the expression i=j+(k++=6)+7):(a) gives an error message(b) assigns a value 16 to i (c) assigns'a value 18 to i (d) assigns a value 19 to i (e) None of these.

  • 41. Which of the following is not a programming control structure?(a) Repetition (b) selection (c) Sequency (d) Sorting.

    42. External documentation includes(a) a printout of the program's code (b) flowcharts(c) IPO charts(d) pseudocode(e) All of the above.

    43. Errors in a program are called(a) accidents (b) annoyances (c) bugs(d) mistakes (e) typing errors.

    44. Typing the instruction grosspay = hoursWorked hourlypay is an example of(a) an entry error(b) a function error(c) a logic error(d) a syntax error.

    45. The step-by-step instructions that solve a problem are called(a) an algorithm(b) a list(c) a plan(d) a sequential structure.

    46. The set of instructions for how to tie a bow is an example of the structure(a) control (b) repetition (c) selection (d) sequence (e) switching.

    47. Which of the following control structures is used in every program?(a) Repetition (b) Selection(c) Sequence (d) Switching.

    48. The instruction "If it's raining outside, then take an umbrella to work" is an example of the structure(a) control(b) Repetation(c) selection(e) switching.

    49. The recipe instruction "Beat until smooth" is an example of the structure(a) control (b) repetition(c) selection (d) sequence(e) switching.

    50. Sending a copy of data to a program module is called(a) passing a value(b) making a reference (c) recursion(d) setting a condition.

    51. Paying attention to the important properties while ignoring inessential details is known as.(a) selectiveness(b) polymorphism

  • (c) abstraction(d) summarizing.

    52. A program that predicts the exact sequence in which events will take place is said to be(a) compiled(b) interpreted(c) procedural(d) object-oriented.

    53. Using a statement at the wrong time or with an inappropriate object creates a(a) logical error(b) syntax error(c) compiler error(d) language error.

    54. A translator that notes whether you have used a language correctly may be called a(a) theasurus (c) coder(b) compiler (d) decoder.

    Solutions:1. a 2. d 3. a 4. a 5. c 6. b 7. c 8. c 9. c 10. d 11. d 12. a 13. d 14. c 15.d16. c 17. b 18. b 19. d 20. d 21. 22. b 23. d 24. a 25. d 26. 27. b 28. a 29. a 30. b/c31. d 32. a 33. b 34. b 35. b 36. a 37. b 38. a 39. c 40. a 41. c 42. a 43. c 44. c 45. a46. d 47. c 48. c 49. b 50. a 51. d 52. c 53. a 54. b

    1.10 Exercise Chapter 11. Describe one good method for precisely specifying what a function must do, without indicating how

    the function accomplishes its work. Provide an example, using a small function. 2. What is a precondition? What is a post-condition? 3. It's recommended that the precondition be checked at the start of a function. What's a good approach

    if a function discovers that its precondition is not true? 4. Is it always possible for a function to check that its precondition is true? 5. Suppose that you accidentally call a correctly implemented function, but the precondition is false. Is

    the function guaranteed to halt with a nice message? Is the function allowed to erase everything on your hard drive?

    6. Write the first few lines of this function so that it uses the assert facility to check its precondition:

    void exam(int i) // Precondition: i is not equal to 42. ...

    7. Give a concise formula that gives the approximate number of digits in a positive integer. The integer is written in base 10.

    8. Why is the order of an algorithm generally more important than the speed of the processor? 9. With about three or four sentences, explain the basic features of your debugger and how they help

    you find bugs.

    Chapter - 2

  • PRELIMINARIES

    2.1 Definition of algorithmAn algorithm is a set of steps to solve a particular problem. When we write the different steps for the preparation of a cup of tea or coffee then it is also an algorithm.

    The word algorithm is a Persian term derived from the name of a Persian author and great mathematician Abu Abd Allah Jafar Mohammad ibn Musba al Khowarizmi. He was born on 780 A.D. in Baghdad. He worked on algebra, geometry and astronomy.

    2.2 Properties of an AlgorithmProperties of an algorithm include the following criteria:1) Input: An algorithm should have some inputs. Example: If we want to write an algorithm to check a given number is odd or even, we can take the number as its input.2) Output: At least one output should be returned by the algorithm after the completion of the specific task based on the input(s) given.Example: If we want to write an algorithm to check a given number is odd or even, we can return 0 indicating that the number is odd and 1 indicating the number is even.3) Definiteness: Every statement of the algorithm should be clear and unambiguous.Example: If we write a statement like iResult = iNumber % x or y, then this is not clear that what operation should be done. That statement should be either iResult = iNumber % x or iResult = iNumber % y.4) Finiteness: No infinite loop should be allowed in an algorithm.Example: while(1

  • 3) Testing Phase: After writing an algorithm, it is necessary to check that the algorithm gives correct result for every valid input. 4) Analyzing Phase: Suppose for a problem P, ten correct algorithms are designed. Now which one is to be chosen that depends on the performance of them. Mainly this performance is judged based on how much time and space the algorithm takes. That is after testing phase of an algorithm it is required to analyze the time complexity and space complexity of it. In section 2.5 this is discussed in details.

    2.4 Example of some AlgorithmsExample 1: Write an algorithm to find the sum of two integer numbers.Inputs : Two numbers.Formula: sum of two numbers is sum = number1 + number2; input two numbers in number1 & number2 variables and add these two by addition operation and put the result in third variable sum.Output : Sum of these two input numbers.

    1. Algorithm fnSum(iNumber1,iNumber2)2. // Purpose : This algorithm finds the summation of two numbers.3. // Inputs : Two numbers are iNumber1 and iNumber2.4. // Output : iSum = Sum of iNumber1 and iNumber2.; the variables are labled with i as prefixed

    because //integer numbers are considered. 5. {6. iSum = iNumber1 + iNumber2; //Find the summation.7. return(iSum); //Return the value.8. }//End of Algorithm

    Example 2: Write an algorithm to find the roots of a quadratic equation Ax2 + Bx + C = 0.Inputs: Values of the coefficients A, B and C.Formula: X = (-B + (B^2 4*A*C)) / 2 * A and X = (-B - (B^2 4*A*C)) / 2 * AOutput: Roots for real cases and for imaginary roots, output is Imaginary Root

    1. Algorithm fnQuadraticEquation(fA,fB,fC)2. // Purpose : This algorithms computes the roots of a quadratic equation.3. // Input : fA,fB and fC are the variables to hold values of the coefficients A, B and C respectively.4. // fA, fB and fC are real or float variables, and to represent float variables f is used as prefix.5. // Output : Roots for real cases for imaginary roots, output is Imaginary Root6. {7. fY = fB2 4*fA*fC;8. if(fY>0) //If two different roots exist.9. {10. fX1 = (-fB + sqrt(fY))/2*fA; //Compute the first root.11. fX2 = (-fB sqrt(fY))/2*fA; //Compute the second root.12. print(fX1,fX2); //Print the calculated values.13. }14. elseif(fY=0) //If only one root exists.15. {16. fX=-fB/2*fA; //Compute the only root.17. print(fX); //Print the calculated value of the only root.18. }19. else //If no real solution exists.20. print(Imaginary Root);21. }//End of Algorithm

  • Example 3: Write an algorithm to find the largest element from a non-empty array.Inputs: Total number of element and the array.Formula: The logic; assume initially first element is the largest one. Check rest n-1 elements of the array with the first one one by one and if any one is found larger the first update the first the current larger and so Output: The largest element.

    1. Algorithm fnFindLargestElement(iN, arrData[])2. // Purpose : This algorithms find the largest element from a non-empty array.3. // Input : iN is total number of elements and arrData[] is the array .4. // Output : iLargest which contains the largest element.5. {6. iLargest = arrData[0]; //Assume initially first element is the largest one.7. for (iCounter = 1;iCounter

  • Big Oh (O) notation: The function f(n) = O(g(n)) (read as f of n is big Oh of g of n) iff f(n) c*g(n) for all n, n n0 where c and n0 are positive constants. The function g(n) indicates the upper bound of f(n) and so g(n) should be as small as possible for which the statement f(n) = O(g(n)) is true.

    Example: Suppose f(n) = 2n3 + 3n2 + n + 10Now we can write f(n) = 2n3 + 3n2 + n + 10

    5n3 for all n 2i.e. here c = 5,n0 = 2 and g(n) = n3.

    Hence we can write f(n) = O(g(n)) = O(n3)

    See here the statements f(n) = O(n4) or f(n) = O(n5) and so on are also true. But we should not write so because the values of g(n) for the statements are not as small as possible for which the statement f(n) = (g(n)) is true.

    c*g(n)

    f(n)

    n0

    n (Problem Size)

    Figure 2.1: Upper Bound of an algorithm (Big Oh (O) notation)

    Omega () notation: The function f(n) = (g(n)) (read as f of n is omega of g of n) iff f(n) c*g(n) for all n, n n0 where c and n0 are positive constants. The function g(n) is only a lower bound of f(n) and so g(n) should be as large as possible for which the statement f(n) = (g(n)) is true.

    Example: Suppose f(n) = 2n3 + 3n2 + n + 10Now we can write f(n) = 2n3 + 3n2 + n + 10

    2n3 for all n 1i.e. here c = 2,n0 = 1 and g(n) = n3.

    Hence we can write f(n) = (g(n)) = (n3)

    Observe that, for the above example the statements f(n) = (n2) or f(n) = (n) or f(n) = (1) are also correct. But we should not write so because the values of g(n) for the statements are not as large as possible for which the statement f(n) = (g(n)) is true.

    Timei

  • f(n)

    c*g(n)

    n0

    n(Problem Size)

    Figure 2.2: Lower Bound of an algorithm (Omega () notation)

    Theta () notation: The function f(n) = (g(n)) (read as f of n is theta of g of n) iff c1*g(n) f(n) c2*g(n) for all n, n n0 where c1,c2 and n0 are positive constants. The function g(n) is a tight bound of f(n) because g(n) is both upper and lower bound on f(n).

    Example: Suppose f(n) = 2n3 + 3n2 + n + 10Now we can write, 5n3 2n3 + 3n2 + n + 10 2n3 for all n 2

    i.e. here c1 = 5, c2 = 2, n0 = 2 and g(n) = n3. Hence we can write f(n) = (g(n)) = (n3)

    c1*g(n)

    f(n)

    c2*g(n)

    n0

    n (Problem Size)

    Figure 2.3: Tight bound of an algorithm (Theta () notation)

    Little oh (o) notation: The function f(n) =o(g(n)) (read as f of n is little oh of g of n) iff

    f(n)lim = 0

    n g(n)

    Time

    Time

  • Example: Suppose f(n) = 2n3 + 3n2 + n + 10Now we can write limn (2n3 + 3n2 + n + 10)/n4

    = limn (2/n + 3/n2 + 1/n3 + 10/n4)= 0

    i.e. here g(n) = n4. Hence we can write f(n) = o(g(n)) = o(n4)

    Little omega () notation: The function f(n) = (g(n)) (read as f of n is little omega of g of n) iff g(n)lim = 0

    n f(n)Example: Suppose f(n) = 2n3 + 3n2Now we can write limn n/(2n3 + 3n2)

    = limn (1/2n2 + 1/3n)= 0

    i.e. here g(n) = n. Hence we can write f(n) = (g(n)) = (n)

    During the execution of a program, estimating the upper bound of time (CPU time) that the program takes to finish its execution is the main factor. If it takes the less time than our estimation then the CPU performance will be improved. But if it takes more time than our calculation then it will cause the degradation of CPU performance. Now we know well that the Big Oh (O) notation gives the upper bound estimation and thats why we will discuss about big Oh (O) notation in details through out the book.

    2.5.2 How can we estimate the time complexity in big Oh(O) notation? Assumptions:1) All the statements take equal time for execution.2) Each statement takes unit time to execute.Example 1: Calculate the time complexity for the following algorithm.1. Algorithm fnSearch(arrData[],iN,iItem)2. // Purpose : This algorithm searches a particular element in an array.3. // Input : arrData[] is an array of iN(= n) integers. Search for the element iItem in the array.4. // Output : The algorithm returns 1 for successful search and 0 for an unsuccessful search.5. {6. for(iCounter = 0;iCounter

  • 3. {4. for(iX=0;iX
  • = (2n 1)/(2-1)= 2n 1 2n for all n 1

    Therefore, from the definition of the big Oh (O) notation we can conclude f(n) = O(2n).

    Example 4: Calculate the time complexity for the following recurrence relation.T(n) = 2T(n/2) + n for n > 1 = 1 for n = 1

    Solution: T(n) = 2T(n/2) + n= 2[2T(n/22) + n/2] + n= 22T(n/22) + 2n= = = 2kT(n/2k) + kn [up to k terms]= nT(1) + nlogn [ Let 2k = n. So, k = logn]= n + nlogn [ as T(1) = 1] 2nlogn for all n 2

    Hence, from the definition of the big Oh (O) notation we can write f(n) = O(nlogn).

    2.5.3 How can we improve the complexity of algorithms?Example 1: Write an algorithm to calculate the sum of first n natural numbers.Procedure 1:

    Inputs: iNumber which contains the value of nOutput: iSum which contains the calculated sum1. Algorithm Summation(iNumber)2. {3. iSum = 1;4. for(iCounter = 2;iCounteriNumber;iCounter++)5. iSum = iSum + iCounter;6. return(iSum);7. }//End of Algorithm

    Complexity: Line 3 takes unit time for execution.Line 4 takes n times for execution.Line 5 takes unit time for execution.Line 6 takes unit time for execution.

    Therefore f(n) = TL3 + TL4*TL5 + TL6= 1 + n*1 + 1= n + 2 2n for all n 2

    So, f(n) = O(n)

    Procedure 2:The problem is S = 1+2+3+4+. + n

    = n(n+1)/2Now we can directly compute the value of S without using any loop.

    Inputs: iNumber which contains the value of nOutput: iSum which contains the calculated sum1. Algo Summation(iNumber)

    2. {3. iSum = iNumber*(iNumber + 1)/2;

  • 4. return(iSum);5. }//End of Algo

    Complexity: Line 3 takes unit time.Line 4 takes unit time.

    Therefore, f(n) = TL3 + TL4= 2

    So, f(n) = O(1) i.e. the algorithm of procedure 2 takes constant time and obviously the second approach is better than the first one in respect of time complexity.

    Example 2: Write an algorithm to compute the value of xn where n is integral power of 2.Procedure 1:

    Inputs: iX = the value of x and iN = the value of n.Output: iResult = the calculated result. 1. Algorithm Power(iX,iN)2. {3. iResult = 1;4. for(iCounter = 1;iCounteriN;iCounter++)5. iResult = iResult*iX;6. return(iResult);7. }//End of Algo

    Complexity: Line 3 takes unit time for execution.Line 4 takes n times for execution.Line 5 takes unit time for execution.Line 6 takes unit time for execution.

    Therefore, f(n) = TL3 + TL4*TL5 + TL6= 1 + n*1 + 1= n + 2 2n for all n 2

    So, f(n) = O(n)Procedure 2:

    Suppose n=8.We can calculate the value I following way:

    iResult = xiResult = iResult * iResult = x2 iResult = iResult * iResult = x4

    iResult = iResult * iResult = x8

    i.e. after logn time we are getting the desired result.

    Inputs: iX = the value of x and iN = the value of n.Output: iResult = the calculated result. 1. Algo Power(iX,iN)2. {3. iResult = x;4. for(iCounter = 1;iCounterlog(iN);iCounter++)5. iResult = iResult*iResult;6. return(iResult);7. }//End of Algo

    Complexity: Line 3 takes unit time for execution.Line 4 takes logn times for execution.Line 5 takes unit time for execution.Line 6 takes unit time for execution.

  • Therefore, f(n) = TL3 + TL4*TL5 + TL6= 1 + (logn)*1 + 1= logn + 2 4logn for all n 2

    So, f(n) = O(logn). Hence we are getting better time complexity in second approach.

    In this section we examine the asymptotic behavior of polynomials in n. In particular, we will see that as n gets large, the term involving the highest power of n will dominate all the others. Therefore, the asymptotic behavior is determined by that term.

    Theorem: Consider a polynomial in n of the form

    where . Then .

    Proof: Each of the terms in the summation is of the form . Since n is non-negative, a particular term will be

    negative only if . Hence, for each term in the summation, . Recall too that we have

    stipulated that the coefficient of the largest power of n is positive, i.e., .

    Note that for integers , for . Thus

    Now the constants and , such that for all , . Thus,

    .

    This property of the asymptotic behavior of polynomials is used extensively. In fact, whenever we have a

    function, which is a polynomial in n, we will immediately ``drop'' the less significant terms (i.e., terms involving powers of n which are less than m), as well

    as the leading coefficient, , to write .

  • 2.5.4 Properties of Big OhIn this section we examine some of the mathematical properties of big oh. In particular, suppose we know that f1(n) = O(g1(n)) and f2(n) = O(g2(n)).

    Theorem 1: If f1(n) = O(g1(n)) and f2(n) = O(g2(n)), then f1(n) + f2(n) = O(max(g1(n), g2(n))).Proof: From the definition of big O notation, there exist four positive constants n1, n2, c1 and c2 such that

    f1(n) c1*g1(n) for n n1 and f2(n) c2*g2(n) for n n2Let n0 = max(n1,n2) and c0 = 2*max(c1,c2).

    Now, f1(n) + f2(n) c1*g1(n) + c2*g2(n) for n n0 c0*(g1(n) + g2(n))/2 c0*max(g1(n), g2(n))

    Thus, f1(n) + f2(n) = O(max(g1(n),g2(n))) Theorem 2: If f1(n) = O(g1(n)) and f2(n) = O(g2(n)), then f1(n)*f2(n) = O(g1(n)*g2(n)).Solution: By Definition, there exist four positive constants, n1,n2, c1 and c2 such that f1(n) c1*g1(n) for all n n1 and f2(n) c2*g2(n) for all n n2. Let n0 = max (n1, n2) and c0 = c1*c2. Consider the product f1(n)*f2(n) for n n0.Now, f1(n) + f2(n) (c1*g1(n)) * (c2*g2(n)) for n n0

    c0*(g1(n)*g2(n))Thus, f1(n)*f2(n) = O(g1(n)*g2(n)). Theorem 3: If f1(n) = O(g1(n)) and g2(n) is a function whose value is non-negative for integers n0, then f1(n)*f2(n) = O(g1(n)*g2(n)).Solution: By definition, there exist two positive constants n1 and c1 such that f1(n)c1*g1(n) for all nn1. Since g2(n) is never negative,

    f1(n)*g2(n) c1*g1(n)*g2(n) for all n n1Thus, f1(n)*f2(n) = O(g1(n)*g2(n))

    (Transitive Property) Theorem 4: If f(n)=O(g(n)) and g(n)=O(h(n)) then f(n)=O(h(n)). Solution: By definition, there exist four positive constants, n1, n2, c1 and c2 such that f(n) c1*g(n) for all n n1 and g(n) c2*h(n) for all n n2.Let n0 = max (n1, n2) and c0 = c1*c2. Then, f(n) c1*g(n) for all n n1

    c1*(c2*h(n)) for all n n0 c0*h(n)

    Thus, f(n)=O(h(n)).

    2.5.5 Rate of growth of Big O notationSuppose there are two algorithms A and B. Now we say A is better than B in respect of time complexity if A takes less time to execute than B. Comparison between algorithms is generally done by some standard functions described in the following table.

    O(1) Constant timeO(logn)

    Logarithmic time

    O(n) Linear timeO(nc) Polynomial time

    O(cn) Exponential time

  • and their orders are:O(1) < O(logn) < O(n) < O(nlogn) < O(n2) < O(n3) < O(2n)

    Note that, time complexity O(1) of an algorithm does not mean that the algorithm takes unit time to execute rather it takes some constant time.

    2.6 Some popular Mathematical Notations and Functions1. Floor function ( ): This function takes the nearest lower integer value of a fractional number.

    e.g. floor(3/2) = 1.ss2. Ceiling function ( ): This function takes the nearest upper integer value of a fractional number.

    e.g. ceiling(3/2) = 2. 3. Mod function (%): This function gives the remainder value of a division operation.

    e.g. 12 mod 5 = 2.4. a0 + a1 + a2 + .. + an = (an-1 -1)/(a-1).5. 1 + 2 + 3 + ..+ n = n(n+1)/2 6. 12 + 22 + 32 + .+ n2 = n(n+1)(2n+1)/6 7. Logax = k(logbx) where k is some constant.

    2.7 Algorithmic NotationThis section describes the format that is used to write an algorithm throughout the book. As C is most popular language, we have followed most syntax of C.

    2.7.1 Algorithm number: Each algorithm is assigned a unique number to identify a particular algorithm. For example Algorithm 10.2 denotes second algorithm in chapter 10.

    2.7.2 Line number: In each algorithm we specify line numbers to analyze or describe it easily.

    2.7.3 Comments: The statement after double slash (//) denotes a comment .At the beginning of each algorithm we have specified purpose of this algorithm, input(s) needed for this algorithm, output returned by this algorithm and some special comments if necessary. Each line of an algorithm may contain some comments to specify its purpose.

    2.7.4 Variable names: The name of an integer variable starts with i, name of a float variable starts with f, name of a character variable starts with c, name of a pointer variable starts with ptr, name of an array starts with arr and name of a function or algorithm starts with fn. But in some algorithm we have not followed this syntax for simplicity.

    2.7.4 Operator: We will use basically three types of operators1. Assignment operator2. Relational operator3. Relational operator

    2.7.4.1 Assignment operator: Our assignment operator is = like as C. For example iElement = arrData[3] assigns the value of arrData[3] to iElement.

  • 2.7.4.2 Relational operator: These operators are used to compare two operands to check whether they are equal to each other or unequal or one is greater than the other. The following figure shows these operators along with their works.

    Operator Operation< Less than> Greater than

    = Greater than or equal to== Equal to!= Not equal to

    2.7.4.3 Logical operator: Logical operators are logical and denoted by &&, logical or denoted by || and logical not denoted by !. Their truth tables are described below:

    Logical and (&&) Logical or (||) Logical not(!)

    2.7.5 Input and Output: Data may be taken as input from the user by scan with the following form:

    scan(Variable names);Similarly to print some output print statement is used with the following form:

    print(statement and/or name of variables);2.7.6 Control structures: Control statements are also similar to C language. Basically there are three types control structures:

    1. Sequential: 2. Conditional 3. Repetitive

    2.7.6.1 Sequential logic: In a sequential approach all the statements are executed in the same order as they are written.

    2.7.6.2 Conditional logic: In this approach, based on some condition, the different sets of statements are executed. An if statement is a conditional control structure that executes a set of statements based upon some specified criteria. 7.6.1.1 Simple if statement: The syntax is

    if(conditions){

    --------------Statements--------------

    }The logic of this structure is if the condition is true then the statements in the if block are executed otherwise the statements are skipped.7.6.1.2 Simple else statement: The syntax is

    if(conditions){

    Exp1 Exp2 Result

    0011

    0101

    0001

    Exp1 Exp2 Result

    0011

    0101

    0111

    Exp1 Result

    01

    10

  • --------------Statement 1--------------

    }else{

    --------------Statement 2--------------

    }If the condition of if statement is true then statement 1 is executed otherwise statement 2 is executed.7.6.1.3 Nested if statement: This structure has the form:

    if(condition 1){

    --------------Statement 1--------------

    }else if(condition 2){

    --------------Statement 2--------------

    }else if(condition n){

    --------------Statement n--------------

    }else{

    --------------Statement n+1--------------

    }Here only one block is executed based on the specified criteria.

    7.7.6.3 Repetitive logic: Here different sets of statements are executed repeatedly for some time. Time of repetitions is called number of iteration. For this purpose we will use three kinds of loops:

    1. for loop 2. while loop3. do-while loop

    2.7.6.3.1 for loop: The syntax is:for(initialize counters; test condition; increment counters){

    -------------------Statements;

  • -------------------}

    2.7.6.3.2 while loop: The syntax is:while(conditions){

    -------------------Statements;-------------------

    }

    2.7.6.3.3 do-while loop: The syntax is:do{

    -------------------Statements;-------------------

    } while(conditions);

    2.8 MCQ Chapter 2

    1. Example(s) of O(1) algorithms is (are):(a) printing a character to the screen(b) incrementing a variable(c) adding two numbers together(d) All of the above.

    2. Example(s) of O(N) algorithms is (are):(a) initializing all of the elements in an one-dimensional array to zero (b) incrementing all the elements in an one-dimensional array

    (c) multiplying two numbers by performing successive addition operations(d) All of the above.

    3. Example(s) of O(N2) algorithms is (are)

    (a) initializing all the elements in a two dimensional array to zero(b) printing out all the elements in a two dimensional array(c) searching for the smallest element in an unsorted two-dimensional array(d) All of the above.

    4. Three algorithms do the same task. Algorithm 1 is O(N2), Algorithm 2 is O(N), and Algorithm 3 is O(Log2 N). Which algorithm should execute the fastest for large values of N?

    (a) O(N) (b)O(N) (c)O(log2 N) (d) None of these.

    5. Which of the following algorithm should execute the slowest for large values of N? (a) O(N)(b) O(N2)(c) O(log2 N)(d) None of these.

    6. What should never be found in the top level of a top-down design?

  • (a) Details(b) Coding(c) Decisions(d) None of these

    7. Describe Process Program File in terms of Big-O, if N refers to the number of lines in the Program File(a) O(N)(b) O(1) (c) O(log2 N)(d) O(N2).

    8. Describe Line Status of the Process Program- File in terms of Big-O, if N refers to the number of lines in the Program File

    (a) O(N)(b) O(1) (c) O(log2 N)(d) O(N2).

    9. Which of these is the correct big-O expression for 1+2+3+...+n? a. O(log n) b. O(n) c. O(n log n) d. O(n)

    10. Which of the following formulas in big-O notation best represent the expression n+35n+6? a. O(n) b. O(n) c. O(n) d. O(42)

    11. What term is used to describe an O(n) algorithm. a. Constant b. Linear c. Logarithmic d. Quadratic

    12. Here is some code for an integer variable n:

    while (n > 0) { n = n/10; // Use integer division

    }

    What is the worst-case time analysis for the above loop?

    a. O(1) b. O(log n) c. O(n) d. O(n)

    13. Express the formula (n - 2)*(n - 4) using big-O notation: a. O(1) b. O(n2) c. O(log n) d. O(n)

  • e. None of the above

    14. The running time of an algorithm T(n), where 'n' is the input size is given byT(n) = 8T(n/2) + qn, if n > 1

    = p, if n = 1where p, q are constants. The order of this algorithm is

    (a) n2 (b) nn (c) n3 (d) n

    15. Consider the following two functions.f(n) = n3, if 0 :s; n < 10,000

    n2, otherwiseg (n) = n, if 0 :s; n < 100

    n2+5n, otherwiseWhich of the following is/are true?(a) f(n) is O(n3) (b) g(n) is O(n3)(c) O(f(n)) is same as O(g(n)) (d) g(n) is O(n2)

    16. The recurrence relation that arises in relation with the complexity of binary search is(a) T(n) = T(n/2) + k, where k is a constant(b) T(n) = 2T(n/2) + k, where k is a constant(c) T(n) = T(n/2) + log(n)(d) T(n) = T(n/2) + n

    17. . The order of an algorithm that finds whether a given Boolean function of 'n' variables, produces a 1 is(a) constant (b) linear (c) logarithmic (d) exponential

    18. The Ackermann's function(a) has quadratic time complexity (b) has exponential time complexity(c) can't be solved iteratively (d) has logarithmic time complexity

    19. The running time of an algorithm is given byT(n) = T(n - 1)+ T(n - 2) - T(n - 3), if n > 3

    = n, otherwise.The order of this algorithm is(a) n (b) log n (c) nn (d) n2

    20. What should be the relation between T(l), T(2) and T(3), so that the previous question, gives an algorithm whose order is constant?

    (a) T(l) + T(2) = T(3) (c) T(1) - T(3) = (2) (b) T(1) + T(3) = 2T(2) (d) T(1) + T(2) = T(3)

    21. The running time T(n), where 'n' is the input size of a recursive algorithm is given as follows.T(n) = c + T(n - 1), if n > 1

    d, if n = 1The order of this algorithm is(a) n2 (b) n (c) n3 (d) nn

    22. There are 4 different algorithms Al, A2 , A3 , A4 to solve a given problem with the orderlog(n), log(log(n)), nlog(n), n log(n) respectively. Which is the best algorithm?

    (a) Al (b) A2 (c) A4 (d) A3

  • 23. The concept of order (Big 0) is important because(a) it can be used to decide the best algorithm that solves a given problem(b) it determines the maximum size of a problem that can be solved in a given system, in a given amount of time(c) it is the lower bound of the growth rate of the algorithm(d) none of the above

    24. An algorithm is made up of 2 modules M1 and M2. If order of M1 is f (n) and M2 is g (n) then the order of the algorithm is(a) max (f (n) , g (n) ) (b) min (f (n) , g (n) )(c) f(n) + g(n) (d) f(n) * g(n)

    25. Let m, n be positive integers. Define Q(m,n) asQ(m, n) = 0, if m < n

    = Q(m - n, n) + p, if m nThen Q(m, 3) is (a div b, gives the quotient when a is divided by b)

    (a) a constant (b) p * (m mod 3) (c) p * (m div 3) (d) 3 * p

    Solutions:1. d 2. d 3. d 4. c 5. b 6. c 7. a 8. c 9. b 10. b 11. b 12. b 13. b 14. c 15.c, d16. a 17. d 18. c 19. a 20. a 21. b 22. b 23. a,b 24. a 25. c

    Excersies Chapter 2

    1. Find (a) 4.3 , - 4.3 (b) 4.3 , - 4.3

    2. Find (a) 50%6, 80%6 (b) -70%6, -45%10

    3. Define Algorithm development life cycle.

    4. Define Algorithm and design an algorithm to find out the total number of even and odd number in a group of 50 numbers.

    5. Explain the different ways of analyzing algorithms.

    6. What are the differences between bottom up and top down approach of programming?

    7. What is structured and modular programming? Differentiate between them.

    8. Solve the recurrent relationT(n) = T(n/2) + 1T(1) = 1 (GATE 1988)

    9. Solve the recurrent relationT(n) = T(n/2) + nT(1) = 1 (GATE 1989)

    10. Solve the recurrent relationXn = 2Xn-1 1, n>1X1=2

  • 11. Convert each time formula to the best possible big-O notation. Do not include any spurious constants in your big-O answer.

    Time Formula Big-O

    10n .

    2n .

    3 times log (base 2) of n .

    2n + 10n .

    12. Write the simplest big-O expression to describe the number of operations required for the following algorithm:

    for (i = 1; i < N; ++i) { ...statements that require exactly i operations... }

  • Chapter - 3ARRAY

    3.1 INTRODUCTION & DEFINITION

    Depending upon the requirement of data and its structures needed to represent various kinds of problems and deriving out solutions through computer the Data Structure is classified into linear and non linear classes. The array supports linear representation of homogeneous data elements, which facilitates to put a group of same data items together and hence it allows to formation of a structure. An array is referred by two things one is index i.e. the location and the other is value in the location. In Programers view point an array is seemed to be formed in contiguous locations of computer memory but it is not true always in systems perspective. One of the major advantages of array is that to use in a program is very easy but the main difficulty lies in the fact as array is formed in primary memory of a computer thus it is volatile. DEFINITIONAn array is defined as a finite ordered set of homogeneous elements. Finite means there is a specific number of elements. Ordered means the elements of the array are indexed. Homogeneous means all the elements of the array must be of same type (e.g. int, float, char etc.).

    3.2 DECLARATION OF ARRAY3.2.1 Declaration:One dimensional array:int iarrMarks[100] // Declares array iarrMarks[] with 100 integers float farrNum[100] // Declares array farrNum[] with 100 floating point numbers. char carrName[100] // Declares array carrName[] with 100 charactersIn General case, consider an array as

    a[l1..u1] Obviously the above array contains (u1 l1 + 1) elements. In C, the lower index l1 for an array is 0.

    Two dimensional array:int iarrMatrix[20][30] //Declares two dimensional array with order 20X30 (i.e. 20 rows and 30 columns). Now consider a two dimensional array in general as

    a[l1..u1][l2..u2] Then the above array contains (u1 l1 + 1) rows and (u2 l2 + 1) columns. So the total number of elements in the array is (u1 l1 + 1) (u2 l2 + 1).

    N dimensional array:If we interpret the indices to be N-dimensional (i1, i2, i3, in) then it is called an N-dimensional array.In the N-dimensional array, if the array is declared as a[l1u1][l2u2] [lnun] where li and ui are the lower bound and upper bound respectively of the ith dimension then The total number of elements = (u1-l1+1)(u2-l2+1)(un-ln+1)

    n = (ui-li+1)

    i=1

  • 3.3 MEMORY REPRESENTATION OF ARRAYThere are two ways to store an array into memory-1) Row major ordering and 2) Column major ordering. They are described in this section. Here it is considered that each element takes only one byte to store its value.3.3.1 Row major ordering: In row major ordering the rows of the array are stored first. Consider an one dimensional array a[l1..u1]. Suppose its base address (address of the first element) is .

    l1 l1+1 . i1 . u1

    Then the address of the element a[i1] is + i1 l1.

    Now consider a 2-dimensional array a[0..3][0..3]. Its rows and elements in each row are depicted below.

    a[0][0]

    Row 1a[0][1]

    a[0][2]

    a[1][0]Row 2a[1][1]

    a[1][2]

    a[2][0]Row 3a[2][1]

    a[2][2]

    a[3][0]Row 4a[3][1]

    a[3][2]

    For a general case suppose the declaration of a 2-dimensional array be a[l1u1][l2u2] and the base address is .

    l2 l2+1 . i2 . u2l1l2 +1.i1 -1i1.u1

  • Now to calculate the address of the element a[i1][i2], first traverse the i1-l1 rows (each row contains u2-l2+1 elements ) and then i2-l2 elements. Hence the address of a[i1][i2] = + (i1-l1)(u2-l2+1)+(i2-l2). Now consider a 3-dimensional array a[0..2][0..2][0..1].

    a[0][0][0]Row 1

    Page 1

    a[0][0][1]

    a[0][1][0]Row 2a[0][1][1]

    a[0][2][0]Row 3a[0][2][1]

    a[1][0][0]Row 1

    Page 2

    a[1][0][1]

    a[1][1][0]Row 2a[1][1][1]

    a[1][2][0]Row 3a[1][2][1]

    a[2][0][0]Row 1

    Page 3

    a[2][0][1]

    a[2][1][0]Row 2a[2][1][1]

    a[2][2][0]Row 3a[2][2][1]

    If a declaration of a 3-dimensional array be a[l1u1][l2u2][l3u3] and the base address be then the address of the element a[i1][i2][i3] = +(i1-l1)(u2-l2+1)(u3-l3+1)+(i2-l2) (u3-l3+1)+(i3-l3)

    Hence for an N-dimensional array a[l1u1][l2u2]..[lnun], if the base address be (i.e. the address of a[l1][l2]..[ln]), then the address of a[i1][i2]..[in] = + (i1-l1)(u2-l2+1)(u3-l3+1)( un-ln+1)

    + (i2-l2)(u3-l3+1)(u4-l4+1)( un-ln+1) + . + . + (in-ln)

    n = + j=1.n (ij-lj)aj with aj= (uk-lk+1)

    k=j+1

    an= 1

  • 3.3.2 Column major ordering: In this technique we store the columns of an array first. Consider an one dimensional array a[l1..u1]. Suppose its base address (address of the first element) is .

    l1 l1+1 . i1 . u1

    Then the address of the element a[i1] is + i1 l1.

    Consider a 2-dimensional array a[0..3][0..2]. Its columns and elements in each column are depicted below.

    a[0][0]

    Column 1a[1][0]

    a[2][0]

    a[3][0]

    a[0][1]

    Column 2a[1][1]

    a[2][1]

    a[3][1]

    a[0][2]

    Column 3a[1][2]

    a[2][2]

    a[3][2]

    Suppose the declaration of a 2-dimensional array be a[l1u1][l2u2] and the base address be .

    l2 l2+1 . i2-1 i2 . u2l1l2 +1.i1.u1

    Now to calculate the address of a[i1][i2] first traverse the i2-l2 columns (each column has u1-l1+1 elements) and then i1 l1 elements. Hence the address of a[i1][i2] = + (i1-l1)+(i2-l2) (u1-l1+1).

  • Now consider a 3-dimensional array a[0..2][0..2][0..1] ;

    a[0][0][0]Column 1

    Page 1

    a[0][1][0]

    a[0][2][0]

    a[0][0][1]Column 2a[0][1][1]

    a[0][2][1]

    a[1][0][0]Column 1

    Page 2

    a[1][1][0]

    a[1][2][0]

    a[1][0][1]Column 2a[1][1][1]

    a[1][2][1]

    a[2][0][0]Column 1

    Page 3

    a[2][1][0]

    a[2][2][0]

    a[2][0][1]Column 2a[2][1][1]

    a[2][2][1]

    If a declaration of a 3-dimensional array be a[l1u1][l2u2][l3u3] and the base address be then the address of a[i1][i2][i3] = +(i1-l1) +(i2-l2)(u1-l1+1)+(i3-l3)(u2-l2+1)(u1-l1+1)

    Hence for an N-dimensional array a[l1u1][l2u2][lnun], if the base address be (i.e. the address of a[l1,l2,..,ln]) then the address of a[i1][i2][in] = + (i1-l1)

    + (i2-l2)(u1-l1+1) + . + . + (in-ln) (un-1-ln-1+1)(u1-l1+1)

    1 = + j=1.n (ij-lj)aj with aj= (uk-lk+1)

    k=j-1

    a1= 1

  • 3.4 INSERTION INTO ONE DIMENSIONAL ARRAYAlgorithm 3.1 describes the process of insertion into one dimensional array.Algorithm 3.11. Algorithm fnInsertion_into_ 1D_Array(arrData, n, k, item)2. // Purpose : This algorithm inserts an element into one dimensional array.3. // Input : arrData[] is an one dimensional array with n number of elements. Element item is to be inserted

    into the kth position in the array.4. // Output : None.5. {6. for(i=n-1;i>=k-1;i--)7. arrData [i+1]= arrData [i];8. arrData[k-1]=item; // Insert the item.9. n=n+1; // Set size of the array.10. }// End of Algorithm.

    Example:Consider an array arrData[] ={10,30,40,50};

    Index 0 1 2 3Element 1

    030

    40 50

    We have to insert 20 in to the 2nd position in the array.So, here now-

    n=4, k=2, item=20

    fnInsertion_into_ 1D_Array (arrData, 4, 2, 20){

    for(i=3;i>=1;i--)i.e. the for loop will be executed for 3 times.i=3arrData[3+1]= arrData[3];

    Index 0 1 2 3 4Element 1

    030

    40

    50

    50

    i=2arrData[2+1]= arrData[2];

    Index 0 1 2 3 4Element 1

    030

    40

    40

    50

    i=1arrData[1+1]= arrData[1];

    Index 0 1 2 3 4Element 1

    030

    30

    40

    50

    Execution of for loop is stopped here.arrData[k-1]=item; or arrData[1]=20;

    Index 0 1 2 3 4Element 1

    020

    30

    40

    50

    n=n+1; or n=5;

  • } So finally-

    Index 0 1 2 3 4Element 1

    020

    30

    40

    50

    3.5 DELETION FROM ONE DIMENSIONAL ARRAYAlgorithm 3.2 describes the process of deletion from one dimensional array.Algorithm 3.21. Algorithm fnDeletion_from_1D_Array (arrData, n, k)2. // Purpose : This algorithm deletes an element from one dimensional array.3. // Input : arrData[] is an one dimensional array with n number of elements. Element item is to be deleted

    from the kth position of the array.4. // Output : Deleted element item.5. {6. item= arrData[k-1]; //Item deleted.7. for(i=k-1;i

  • So finally Index 0 1 2 3

    Element 10

    30

    40 50

    3.6 TRAVERSING ONE DIMENSIONAL ARRAYTraversing means to access all the elements of the array (starting from the first element up to the last one). Algorithm 3.3 describes the process.Algorithm 3.31. Algorithm fnTraverse_1D_Array(arrData, n)2. // Purpose : This algorithm prints all the elements of a one dimensional array.3. // Input: arrData[] is an one dimensional array with n number of elements.4. // Output : None.5. {6. for(i=0;i

  • 6. //First copy the elements of arrA into arrC.7. for(i=0;i
  • 5 0 0 20

    0 0 0 0 0 0 0

    6 0 0 0 0 0 31

    0 0 0 0

    7 0 0 0 -9

    0 0 0 0 0 0

    8 0 0 0 0 0 0 0 -2 0 0

    The above 9 X 10 sparse matrix takes 90 memory spaces to save it into memory according to row major or column major ordering. A common way of representing non-zero elements of a sparse matrix is the 3-tuple forms. In this technique the above matrix looks like:

    int arrSpmtx[12][3]={9 10 11 // 9X10 matrix having 11 non-zero elements.0 9 151 6 -82 0 -32 7 403 4 104 1 144 8 -15 2 206 5 317 3 -98 7 -2

    };The first row of the matrix arrSpmtx stores the order of the original matrix and number of nonzero elements. The next rows store the values of the nonzero elements and their positions in the original matrix. For the given example 12X3=36 memory spaces are required to store the whole matrix i.e. (90-36) = 54 memory spaces can be saved. Algorithm to create a 3-tuple of a given matrixAlgorithm 3.61. Algorithm fnSparse_Matrix_Using_3tuple(arrOriginal, iRow, iColumn, arrSpmtx)2. // Purpose : This algorithm creates a 3-tuple of a given matrix.3. // Input : arrOriginal is the original matrix with order iRow X iColumn and arrSpmtx is its 3-tuple form.4. // Output : None. 5. {6. Index = 0;7. iNon_Zero_Elements = fnCount_Non_Zero_Elements(arrOriginal, iRow, iColumn);8. arrSpmtx[Index][0] = iRow;9. arrSpmtx[Index][1] = iColumn; //Set first row10. arrSpmtx[Index][2] = iNon_Zero_Elements;11. Index++;12. for(i=0;i

  • 21. }//End of algorithm

    Algorithm 3.71. Algorithm fnCount_Non_Zero_Elements(arrOriginal, iRow, iColumn)2. // Purpose : This algorithm counts number of non-zero elements in a matrix.3. // Input : arrOriginal matrix with order iRow X iColumn.4. // Output : Returns number of non-zero elements.5. {6. iNon_Zero_Elements = 0;7. for(i=0;i

  • 2. // Purpose : This algorithm stores the elements of a lower triangular matrix to a one dimensional array.3. // Input : The non-zero values of the n X n lower triangular matrix a is to be stored into the one-

    dimensional array b[]. 4. // Output : None.5. {6. for(j=1;j

  • As array is static in nature, so this process to store a polynomial is not an efficient one. The efficient way is to store a polynomial in a linked list, which is described in details in chapter 8.

    3.11 MCQ Chapter 3

    1. Structured data type made up of finite collection of ordered elements, all of which is of same data type is: (a) record (b) array(c) file (d) None of these

    2. Elements of array are accessed by(a) accessing function in built-in data structure(b) mathematical function(c) index(d) None of these.

    3. Array is(a) Linear data structure (b) non-linear data structure(c) Complex data structure (d) None of these.

    4. Row-major order in two dimensional array refers to(a) all elements of a row are stored in memory in sequence followed by next row in sequence

    and so on.(b) all elements of a row are stored in memory in sequence followed by next column in

    sequence and so on.(c) all elements of a column are stored in memory in sequence followed by next column

    in sequence and so on.(d) None of these

    5. Suppose that a two - dimensional arraydeclared ac; "char a [5][6]," is internally stored incontiguous memory locations starting from the locations "1000" in a column-major manner. The address where a [i] U] would be stored is(a) 1000+i+j*5 (b) 1000+i*4+j(c) 1000 + i + j * 6 (d) 1000 + i * 6 + j

    6. A matrix. a, is called lower triangular if and only if for all j > i aij = O. If such a matrix is mapped to I-dimensional matrix A then it could be mapped to the following index of A .(a) 1/2 * i (i + 1) + j (b) i + j(c) i (i + 1) + j (d) None of the above

    7. In a compact single dimensional array representation for lower triangular matrices (i.e., an the elements above the diagonal are zero) of size n x n, nonzero elements (i.e., of the lower triangle) of each row are stored one after another. starting from the first row, the index cl: the (i,jih element of the lower triangular matrix in this representation is: (GATE-1994)

    (a) i + j (b) i + j - 1(c) j + i (i - 1)/2 (d) i + j (j - 1)/2

    8. Let A = (aij) be an n-rowed square matrix and /12 be the matrix obtained by interchangingthe first and second rows of the n-rowed Identity matrix. Then All2 is such that its first:

  • (GATE-1997)(a) row is the same as its second row(b) row is same as the second row of A(c) column is the same as the second column of A (d) row is all zero

    9. Let A be a two dimensional array declared as follows: A: array [1..10] [1..15] of integer;Assuming that each integer takes one memory location the array is stored in row-major order and the first element of the array is stored at location. 100, what is the address of the element A [l][J] ? (GATE-1998)

    (a) 15i + j + 84 (b) 15j + i + 84(c) Wi + j + 89 (d) 10j + i + 89

    10. The information about an array that is used in a program will be stored in(a) symbol table (b) activation record (c) system table (d) dope vector

    11. Which of the following expressions accesses the (i,j)lh entry of a (m x n) matrix stored in column major form?

    (a) n x (i-l) + j (b) m x(j -1) + i(c) m x(n-j) + j (d) n x (m-i) + j

    12. Sparse matrices have(a) many zero entries (b) many non-zero entries(c) higher dimension (d) none of the above

    Solutions:1. b 2. c 3. a 4. a 5. a 6. d 7. c 8. c 9. a 10. a 11. a 12. a

    3.12 Exercise Chapter 3

    1. Define an array. How can an array be declared? 2. What is row major and column major ordering of an array? Explain with a suitable example. 3. Write an algorithm to find out the maximum and the 2nd maximum number from an array of integers.4. Wtite a C function to find out whether an element aij in an array A[I,J} such that aij is the greatest value in ith row and smallest value in jth column. What is the time complexity of your function?5. Write a C program to find out whether a matrix is symmetric or not.6. Consider two single dimention arrays of size 20 and 30 respectively. Write an algorithm tofind out the elements which are common to both the arrays.7. An integer array is declared as A[I,J,K]. The starting address of this array is 1000. Considering integer size is of 4 bytes, find out the location of A[i,j,k] in column major fasion.8. An array A contains 25 positive integers. Write an algorithm which will find out all pairs of elements whose sum is 30.9. An array A contains 25 positive integers. Write an algorithm which will find out the number of odd and even numbers in that array.10. Write a C program which will input an integer array of various sizes and compute the average value of the elements of that array.

  • Chapter - 4STRUCTURE AND POINTER

    4.1 DEFINITION OF STRUCTUREWe have seen in chapter 3 that an array can be used to store the homogeneous data i.e. the data of same data types (e.g. int, float, char etc.). But using a structure we can create a user defined data type consisting of logically related data items of different data types. The general format of a structure definition is as follows:

    struct structure_name{

    ----------------------structure elements;----------------------

    };

    4.2 BASIC OF POINTERConsider the following statement:

    int iData = 10;When compiler compiles the above statement, it allocates a memory space from the heap (available memories) to store the variable iData like the following:

    Name of the variable: iData

    Value of the variable:

    Address of the variable: 2020

    Now we can access the value 10 by two ways: 1. By name and 2. By address. To store the address of iData into another variable, we need a pointer variable and the declaration is:

    int *iptr1; //Signifies that value at address iptr1 is some integer value.char *cptr1; //Signifies that value at address cptr1 is some character value.

    For pointer operations, two operators are available 1) * (value at) 2) &(address of). Suppose we want to store the address of iData into iptr1. The declaration is:

    iptr1 = &iData;

    10

  • Name of the variable: iptr1

    Value of the variable:

    Address of the variable: 4010Now we can print the value 10 in two different ways:

    printf(%d,iData);printf(%d,*iptr1);

    4.3 STRUCTURE AND POINTERIn the previous section we have seen a pointer pointing to an int or a pointer pointing to a char. Similarly we can have a pointer pointing to a struct. Consider the following program code:

    struct date{

    int dd;int mm;int yy;

    };main(){

    struct date d1={06,04,2008}; //Declare d1 as structure variable.struct date *d2; //Declare d2 as a pointer to a structure.d2 = &d1;printf(%d/%d/%d,d1.dd,d1.mm,d1.yy);printf(%d/%d/%d,d2->dd,d2->mm,d2->yy);

    }

    Name of the variable: d1.dd d1.mm d1.yy d2

    Value of the variable:

    Address of the variable: 4050 4052 4054 8020

    4.4 PASSING STRUCTURE TO FUNCTIONSstruct date{

    int dd;int mm;int yy;

    };main(){

    struct date d1; //Declare d1 as structure variable.fnGetDate(&d1); //Pass the structure by its address.fnShowDate(&d1); //Pass the structure by its address.

    }void fnGetDate( struct date *d2){

    *d2->dd = 06;*d2->mm = 04;

    2020

    06 04 2008 4050

  • *d2->yy = 2008;}void fnShowDate( struct date *d2){

    printf(%d/%d/%d,d2->dd,d2->mm,d2->yy);}

    4.5 SELF REFERENTIAL STRUCTUREConsider the following structure declaration:

    struct node{

    int iData;struct node *ptrNext;

    };Notice that here the structure element ptrNext contains the address of a node similar to itself. Such type of structure is called self referential structure.

    4.6 DYNAMIC MEMORY ALLOCATIONThe dynamic or run time memory allocation helps us to make efficient use of by allocating the required amount of memory whenever is needed. C provides the following dynamic allocation and de-allocation functions:

    (I) malloc() (II) calloc() (III) realloc() (IV) free()(I) malloc(): The general syntax of malloc() function is:

    (data_type *) malloc(size of one block * number of blocks)Suppose we want to allocate the memory space for previously defined structure node in run time using malloc() function. Then the statement is:

    struct node *ptrNode = (struct node*)malloc(sizeof(struct node));

    (II) calloc():The general syntax of calloc() function is:(data_type *) calloc(number of blocks , size of one block)

    Suppose we want to allocate the memory space for the structure node using calloc() function. Then the statement is:

    struct node *ptrNode = (struct node*)calloc(sizeof(struct node));

    (III) realloc(): In some situation, the previously allocated memory is insufficient to run the correct application. It is also possible that the amount of memory allocated is large enough than the required memory. In both the cases it is necessary to reallocate the memory spaces and this can be done using realloc() function. The general syntax of realloc() function is:

    (data_type *) realloc(ptr, new size)ptr is a pointer holding the starting address of the allocated memory block. Suppose now we want to allocate 2 block of node in the pointer variable ptrNode. Then the statement is:

    ptrNode = (struct node*)realloc(ptrNode, 2* sizeof(struct node));

    (IV) free(): Now to de-allocate the memory spaces we can use the function free() and the declaration is:free(name of pointer variable);

    4.7 MCQ Chapter 4

    1. Dynamic memory allocation use

  • a) Callocb) Mallocc) Freed) all of these

    2. Self referential structure refersa) own instanceb) own addressc) own variablesd) none of these

    3. Default allocation of variables in malloc() is equal toa) zerob) garbagec) any ve valued) any +ve value

    4. Default allocation of variables in calloc() is equal toa) zerob) garbagec) any ve valued) any +ve value

    5. A pointer contains a) address of other variablesb) address of other pointersc) both a) and b)d) none of these

    6. when compiled a structural code changes toa) Executable codeb) Non executable codec) Assignment coded) None of these

    7. A record structure can be represented asa) Hierarchical structureb) Canonical structurec) Linear structured) All of these

    8. Dynamic memory allocation is used toa) For program efficiencyb) For space managementc) For time managementd) All of these

    9. Advantage of using structure isa) To use logically related data items but of different datatypes. b) To represent the records in a hierarchical mannerc) To create an user defined data typed) None of these

    10. In C, size of a structure variable depend ona) The variable of the maximum size used in the structureb) The numbers of variables used in the structurec) Both a) and b)d) None of these

    Solutions:1. d 2. a 3. b 4. a 5. c 6. c 7. d 8. b 9. a 10. b

  • 4.8 Exercises Chapter 4

    1. What is a Structure? What is a Pointer?

    2. How can a structure be passed by a pointer?

    3. What is dynamic memory allocation? what is its advantages?

    4. What is self refferntial structure? Discuss with an example.

    5. How can a structure be passed to a function?6. The following is a list of entries, with level numbers, in a file of employee record:

    1. Employee(200)2. Name3. Last4. First5. Middle6. Address7. Street8. Area9. City10. State11. Country12. Pin Code13. Age14. Salary15. Dependents

    a) Draw the corresponding hierarchical structure.b) Which of the items are elementary items?

    Describe and write down a record in C to implement this structural representation of data.

  • Chapter 5STRINGS

    5.1 Definition:

    Let be an alphabet, a non-empty finite set. Elements of are called symbols or characters. A string (or word) over is any finite sequence of characters from . For example, if = {0, 1}, then 0101 is a string over .

    The length of a string is the number of characters in the string (the length of the sequence) and can be any non-negative integer. The empty string is the unique string over of length 0, and is denoted or .

    The set of all strings over of length n is denoted n. For example, if = {0, 1}, then 2 = {00, 01, 10, 11}. Note that 0 = {} for any alphabet .

    The set of all strings over of any length is the Kleene closure of and is denoted as *. In terms of n,

    For example, if = {0, 1}, * = {, 0, 1, 00, 01, 10, 11, 000, 001, 010, 011, }. Although * itself is countably infinite, all elements of * have finite length.

    A set of strings over (i.e. any subset of *) is called a formal language over . For example, if = {0, 1}, the set of strings with an even number of zeros ({, 1, 00, 11, 001, 010, 100, 111, 0000, 0011, 0101, 0110, 1001, 1010, 1100, 1111, }) is a formal language over .

    5.2 Concatenation and substrings

    Concatenation is an important binary operation on *. For any two strings s and t in *, their concatenation is defined as the sequence of characters in s followed by the sequence of characters in t, and is denoted st. For example, if = {a, b, , z}, s = data, and t = structure, then st =datastructure and ts = structuredata.

    String concatenation is an associative, but non-commutative operation. The empty string serves as the identity element; for any string s, s = s = s. Therefore, the set * and the concatenation operation form a monoid, the

  • free monoid generated by . In addition, the length function defines a monoid homomorphism from * to the non-negative integers.

    A string s is said to be a substring or factor of t if there exist (possibly empty) strings u and v such that t = usv. The relation "is a substring of" defines a partial order on *, the least element of which is the empty string.

    5.3 String operations

    A number of additional operations on strings commonly occur in Data Structure. They are explained below:

    5.3.1 Alphabet of a string

    The alphabet of a string is a list of all of the letters that occur in a particular string. If s is a string, its alphabet is denoted by Alph(s)

    5.3.2 String substitution

    Let L be a language, and let be its alphabet. A string substitution or simply a substitution is a mapping f that maps letters in to languages (possibly in a different alphabet). Thus, for example, given a letter a , one has

    f(a) = La where is some language whose alphabet is . This mapping may be extended to strings as f() = for the empty string , and f(sa) = f(s)f(a) for nonempty string . String substitution may be extended to the entire language as

    An example of string substitution occurs in regular languages, which are closed under string substitution. That is, if the letters of a regular language are substituted by other regular languages, the result is still a regular language.

    5.3.3 String homomorphism

    A string homomorphism is a string substitution such that each letter is replaced by a single string. That is, f(a) = s, where s is a string, for each letter a. String homomorphisms are homomorphisms, preserving the binary operation of string concatenation. Given a language L, the set f(L) is called the homomorphic image of L. The inverse homomorphic image of a string s is defined as

    f-1(s) = { w | f(w) = s}

    while the inverse homomorphic image of a language L is defined as

    f-1(L) = { s | f(s) L}

    Note that, in general, f(f-1(L)) L, while one does have

  • f(f-1(L)) L and L f-1(f(L))

    for any language L. Simple single-letter substitution ciphers are examples of string homomorphisms.

    5.3.4 String projection

    If s is a string, and is an alphabet, the string projection of s is the string that results by removing all letters which are not in . It is written as . It is formally defined by removal of letters from the right hand side:

    (s) = if s = , the empty string

    = (t) if s = ta and a

    = (t) if s = ta and a

    Here denotes the empty string. The projection of a string is essentially the same as a projection in relational algebra.

    String projection may be promoted to the projection of a language. Given a formal language L, its projection is given by

    (L) = { (s) | s L}

    5.3.5 String Right quotient

    The right quotient of a letter a from a string s is the truncation of the letter a in the string s, from the right hand side. It is denoted as s / a. If the string does not have a on the right hand side, the result is the empty string. Thus:

    (sa) / b = s if a = b

    = if a b

    The quotient of the empty string may be taken: / a =

    Similarly, given a subset S M of a monoid M, one may define the quotient subset as

    S / a = { s M | sa S}

    Left quotients may be defined similarly, with operations taking place on the left of a string.

    5.3.6 String Syntactic relation

    The right quotient of a subset S M of a monoid M defines an equivalence relation, called the right syntactic relation of S. It is given by

    ~s = { (s, t) M x M | S / s = S / t }

  • The relation is clearly of finite index (has a finite number of equivalence classes) if and only if the family right quotients is finite; that is, if {S / m | m M } is finite. In this case, S is a recognizable language, that is, a language that can be recognized by a finite state automaton. This is discussed in greater detail in the article on syntactic monoids.

    5.3.7 String Right cancellation

    The right cancellation of a letter a from a string s is the removal of the first occurrence of the letter a in the string s, starting from the right hand side. It is denoted as s a and is recursively defined as

    (sa) b = s if a=b

    = ( s b )a if a b

    The empty string is always cancellable: a =

    Clearly, right cancellation and projection commute:

    (L) a = (L) ( s a )

    5.3.8 String Prefixes

    The prefixes of a string is the set of all prefixes to a string, with respect to a given language:

    Pref L(s) = { t | s = tu for u L }

    The prefix closure of a language is

    A language is called prefix closed if Pref (s) = L. Clearly, the prefix closure operator is idempotent:

    Pref (Pref (L)) = Pref (L)

    The prefix relation is a binary relation s t such that if and only if s Pref L(t).

    Prefix grammars generate languages that are prefix-closed.

    5.4 String Topology

    Strings admit the following interpretation as nodes on a graph:

    Fixed length strings can be viewed as nodes on a hypercube; Variable length strings (of finite length) can be viewed as nodes on the k-ary tree, where k is the number

    of symbols in ; Infin