100% found this document useful (2 votes)
85 views40 pages

A Textbook of Data Structures and Algorithms, Volume 1 G. A. Vijayalakshmi Paiinstant Download

The document provides links to download various textbooks on data structures and algorithms, including works by G. A. Vijayalakshmi Pai and others. It includes detailed content descriptions for multiple volumes and editions, covering topics from algorithm design to specific data structures like arrays and stacks. Additionally, it emphasizes the importance of understanding both the means and ends of work in the context of learning algorithms.

Uploaded by

yayginretrac
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
85 views40 pages

A Textbook of Data Structures and Algorithms, Volume 1 G. A. Vijayalakshmi Paiinstant Download

The document provides links to download various textbooks on data structures and algorithms, including works by G. A. Vijayalakshmi Pai and others. It includes detailed content descriptions for multiple volumes and editions, covering topics from algorithm design to specific data structures like arrays and stacks. Additionally, it emphasizes the importance of understanding both the means and ends of work in the context of learning algorithms.

Uploaded by

yayginretrac
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

A Textbook of Data Structures and Algorithms,

Volume 1 G. A. Vijayalakshmi Pai pdf download

https://ebookmass.com/product/a-textbook-of-data-structures-and-
algorithms-volume-1-g-a-vijayalakshmi-pai/

Explore and download more ebooks at ebookmass.com


We have selected some products that you may be interested in
Click the link to download now or visit ebookmass.com
for more options!.

A Textbook of Data Structures and Algorithms, Volume 2 G.


A. Vijayalakshmi Pai

https://ebookmass.com/product/a-textbook-of-data-structures-and-
algorithms-volume-2-g-a-vijayalakshmi-pai/

A Textbook of Data Structures and Algorithms, Volume 2:


Mastering Nonlinear Data Structures Vijayalakshmi Pai

https://ebookmass.com/product/a-textbook-of-data-structures-and-
algorithms-volume-2-mastering-nonlinear-data-structures-vijayalakshmi-
pai/

Data Structures and Algorithms in C++ 2nd Edition

https://ebookmass.com/product/data-structures-and-algorithms-in-c-2nd-
edition/

Absolute Beginner's Guide to Algorithms: A Practical


Introduction to Data Structures and Algorithms in
JavaScript Kirupa Chinnathambi
https://ebookmass.com/product/absolute-beginners-guide-to-algorithms-
a-practical-introduction-to-data-structures-and-algorithms-in-
javascript-kirupa-chinnathambi/
Absolute Beginner’s Guide to Algorithms: A Practical
Introduction to Data Structures and Algorithms in
JavaScript (for True Epub) Kirupa Chinnathambi
https://ebookmass.com/product/absolute-beginners-guide-to-algorithms-
a-practical-introduction-to-data-structures-and-algorithms-in-
javascript-for-true-epub-kirupa-chinnathambi/

Data Structures & Algorithms in Python 1st Edition John


Canning

https://ebookmass.com/product/data-structures-algorithms-in-
python-1st-edition-john-canning-2/

Data Structures & Algorithms in Python 1st Edition John


Canning

https://ebookmass.com/product/data-structures-algorithms-in-
python-1st-edition-john-canning/

Fundamentals Of Python: Data Structures 2nd Edition


Kenneth A. Lambert

https://ebookmass.com/product/fundamentals-of-python-data-
structures-2nd-edition-kenneth-a-lambert/

Data Structures and Applications: A Simple and Systematic


Approach Padma Reddy

https://ebookmass.com/product/data-structures-and-applications-a-
simple-and-systematic-approach-padma-reddy/
A Textbook of Data Structures and Algorithms 1
One of the greatest lessons I have learnt in my life is
to pay as much attention to the means of work as to its end…
I have been always learning great lessons from that one principle,
and it appears to me that all the secret of success is there;
to pay as much attention to the means as to the end….
Let us perfect the means; the end will take care of itself.

– Swami Vivekananda
(Lecture Delivered at Los Angeles, California, January 4, 1900)
A Textbook of Data
Structures and Algorithms 1

Mastering Linear Data Structures

G A Vijayalakshmi Pai
First published 2022 in Great Britain and the United States by ISTE Ltd and John Wiley & Sons, Inc.

Previous edition published in 2008 as “Data Structures and Algorithms: Concepts, Techniques and
Applications” by McGraw Hill Education (India) Pvt Ltd. © McGraw Hill Education (India) Pvt Ltd. 2008

Apart from any fair dealing for the purposes of research or private study, or criticism or review, as
permitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced,
stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers,
or in the case of reprographic reproduction in accordance with the terms and licenses issued by the
CLA. Enquiries concerning reproduction outside these terms should be sent to the publishers at the
undermentioned address:

ISTE Ltd John Wiley & Sons, Inc.


27-37 St George’s Road 111 River Street
London SW19 4EU Hoboken, NJ 07030
UK USA

www.iste.co.uk www.wiley.com

© ISTE Ltd 2022


The rights of G A Vijayalakshmi Pai to be identified as the author of this work have been asserted by her
in accordance with the Copyright, Designs and Patents Act 1988.

Any opinions, findings, and conclusions or recommendations expressed in this material are those of the
author(s), contributor(s) or editor(s) and do not necessarily reflect the views of ISTE Group.

Library of Congress Control Number: 2022945771

British Library Cataloguing-in-Publication Data


A CIP record for this book is available from the British Library
ISBN 978-1-78630-869-6
Contents

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix

Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv

Chapter 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1. History of algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2. Definition, structure and properties of algorithms . . . . . . . . . . . . . 4
1.2.1. Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.2. Structure and properties . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3. Development of an algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4. Data structures and algorithms . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5. Data structures – definition and classification . . . . . . . . . . . . . . . 7
1.5.1. Abstract data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5.2. Classification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.6. Algorithm design techniques . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.7. Organization of the book . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Chapter 2. Analysis of Algorithms . . . . . . . . . . . . . . . . . . . . . . . 13


2.1. Efficiency of algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2. Apriori analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3. Asymptotic notations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.4. Time complexity of an algorithm using the O notation . . . . . . . . . . 19
2.5. Polynomial time versus exponential time algorithms . . . . . . . . . . . 20
2.6. Average, best and worst case complexities . . . . . . . . . . . . . . . . . 21
vi A Textbook of Data Structures and Algorithms 1

2.7. Analyzing recursive programs . . . . . . . . . . . . . . . . . . . . . . . . . 23


2.7.1. Recursive procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.7.2. Apriori analysis of recursive functions . . . . . . . . . . . . . . . . . 27
2.8. Illustrative problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

Chapter 3. Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.2. Array operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.3. Number of elements in an array . . . . . . . . . . . . . . . . . . . . . . . . 46
3.3.1. One-dimensional array . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.3.2. Two-dimensional array . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.3.3. Multidimensional array . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.4. Representation of arrays in memory . . . . . . . . . . . . . . . . . . . . . 48
3.4.1. One-dimensional array . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.4.2. Two-dimensional arrays . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.4.3. Three-dimensional arrays . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.4.4. N-dimensional array . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.5. Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
3.5.1. Sparse matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
3.5.2. Ordered lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
3.5.3. Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
3.5.4. Bit array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
3.6. Illustrative problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

Chapter 4. Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
4.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
4.2. Stack operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
4.2.1. Stack implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
4.2.2. Implementation of push and pop operations . . . . . . . . . . . . . . 74
4.3. Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
4.3.1. Recursive programming . . . . . . . . . . . . . . . . . . . . . . . . . . 76
4.3.2. Evaluation of expressions . . . . . . . . . . . . . . . . . . . . . . . . . 79
4.4. Illustrative problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

Chapter 5. Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101


5.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
5.2. Operations on queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
5.2.1. Queue implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
5.2.2. Implementation of insert and delete operations on a queue . . . . . 103
Contents vii

5.2.3. Limitations of linear queues. . . . . . . . . . . . . . . . . . . . . . . . 105


5.3. Circular queues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
5.3.1. Operations on a circular queue . . . . . . . . . . . . . . . . . . . . . . 106
5.3.2. Implementation of insertion and deletion operations in
circular queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
5.4. Other types of queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
5.4.1. Priority queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
5.4.2. Deques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
5.5. Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
5.5.1. Application of a linear queue . . . . . . . . . . . . . . . . . . . . . . . 119
5.5.2. Application of priority queues . . . . . . . . . . . . . . . . . . . . . . 120
5.6. Illustrative problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

Chapter 6. Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143


6.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
6.1.1. Drawbacks of sequential data structures . . . . . . . . . . . . . . . . 143
6.1.2. Merits of linked data structures . . . . . . . . . . . . . . . . . . . . . . 145
6.1.3. Linked lists – structure and implementation . . . . . . . . . . . . . . 145
6.2. Singly linked lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
6.2.1. Representation of a singly linked list . . . . . . . . . . . . . . . . . . 147
6.2.2. Insertion and deletion in a singly linked list . . . . . . . . . . . . . . 149
6.3. Circularly linked lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
6.3.1. Representation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
6.3.2. Advantages of circularly linked lists over singly linked lists . . . . 155
6.3.3. Disadvantages of circularly linked lists . . . . . . . . . . . . . . . . . 156
6.3.4. Primitive operations on circularly linked lists . . . . . . . . . . . . . 158
6.3.5. Other operations on circularly linked lists . . . . . . . . . . . . . . . 159
6.4. Doubly linked lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
6.4.1. Representation of a doubly linked list . . . . . . . . . . . . . . . . . . 161
6.4.2. Advantages and disadvantages of a doubly linked list . . . . . . . . 162
6.4.3. Operations on doubly linked lists . . . . . . . . . . . . . . . . . . . . 163
6.5. Multiply linked lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
6.6. Unrolled linked lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
6.6.1. Retrieval of an element . . . . . . . . . . . . . . . . . . . . . . . . . . 172
6.6.2. Insert an element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
6.6.3. Delete an element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
6.7. Self-organizing lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
6.8. Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
6.8.1. Addition of polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . 176
6.8.2. Sparse matrix representation . . . . . . . . . . . . . . . . . . . . . . . 178
6.9. Illustrative problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
viii A Textbook of Data Structures and Algorithms 1

Chapter 7. Linked Stacks and Linked Queues . . . . . . . . . . . . . . . 201


7.1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
7.1.1. Linked stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
7.1.2. Linked queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
7.2. Operations on linked stacks and linked queues . . . . . . . . . . . . . . . 203
7.2.1. Linked stack operations . . . . . . . . . . . . . . . . . . . . . . . . . . 203
7.2.2. Linked queue operations . . . . . . . . . . . . . . . . . . . . . . . . . . 204
7.2.3. Algorithms for Push/Pop operations on a linked stack . . . . . . . . 205
7.2.4. Algorithms for insert and delete operations in a linked queue. . . . 206
7.3. Dynamic memory management and linked stacks . . . . . . . . . . . . . 209
7.4. Implementation of linked representations . . . . . . . . . . . . . . . . . . 214
7.5. Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
7.5.1. Balancing symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
7.5.2. Polynomial representation . . . . . . . . . . . . . . . . . . . . . . . . . 218
7.6. Illustrative problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243

Summaries of other volumes . . . . . . . . . . . . . . . . . . . . . . . . . . . 245


Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
Preface

Efficient problem solving using computers, irrespective of the discipline or


application, calls for the design of efficient algorithms. The inclusion of appropriate
data structures is of critical importance to the design of efficient algorithms. In other
words, good algorithm design must go hand in hand with appropriate data
structures for an efficient program design to solve a problem.

Data structures and algorithms is a fundamental course in computer science,


which most undergraduate and graduate programs in computer science and other
allied disciplines in science and engineering offer during the early stages of the
respective programs, either as a core or as an elective course. The course enables
students to have a much-needed foundation for efficient programming, leading to
better problem solving in their respective disciplines.

Most of the well-known text books/monographs on this subject have discussed the
concepts in relation to a programming language – beginning with Pascal and spanning
a spectrum of them such as C, C++, C#, Java, Python and so on, essentially calling for
ample knowledge of the language, before one proceeds to try and understand the
data structure. There does remain a justification in this. The implementation of data
structures in the specific programming language need to be demonstrated or the
algorithms pertaining to the data structures concerned need a convenient medium of
presentation and when this is the case, why not a programming language?

Again, while some authors have insisted on using their books for an advanced
level course, there are some who insist on a working knowledge of the specific
programming language as a prerequisite to using the book. However, in the case of a
core course, as it is in most academic programs, it is not uncommon for a novice or a
sophomore to be bewildered by the “miles of code” that demonstrate or explain a
data structure, rendering the subject difficult to comprehend. In fact, the efforts that
one needs to put in to comprehend the data structure and its applications are
x A Textbook of Data Structures and Algorithms 1

distracted by the necessity to garner sufficient programming knowledge to follow


the code. It is indeed ironic that while a novice is taught data structures to appreciate
programming, in reality it turns out that one learns programming to appreciate data
structures!

In my decades-old experience of offering the course to graduate programs, which


admits students from diverse undergraduate disciplines, with little to no strong
knowledge of programming, I had several occasions to observe this malady. In fact,
it is not uncommon in some academic programs, especially graduate programs
which, due to their shorter duration, have a course in programming and data
structures running in parallel in the same semester, much to the chagrin of the
novice learner! That a novice is forced to learn data structures through their
implementation (in a specific programming language), when in reality it ought to be
learning augmented with the implementation of the data structures, has been the
reason behind the fallout.

A solution to this problem would be to


i) Frame the course such that the theory deals with the concepts, techniques and
applications of data structures and algorithms, not taking recourse to any specific
programming language, but instead settling for a pseudo-language, which clearly
expounds the data structure. Additionally, supplementing the course material with
illustrative problems, review questions and exercises to reinforce the students’ grasp
of the concepts would help them gain useful insights while learning.
ii) Augment the theory with laboratory sessions to enable the student to
implement the data structure in itself or as embedded in an application, in the
language of his/her own choice or as insisted upon in the curriculum. This would
enable the student who has acquired sufficient knowledge and insight into the data
structures to appreciate the beauty and merits of employing the data structure by
programming it themself, rather than “look” for the data structure in a prewritten
code.

This means that text books catering to the fundamental understanding of the data
structure concepts for use as course material in the classroom are as much needed as
the books that cater to the implementation of data structures in a programming
language for use in the laboratory sessions. While most books in the market conform
to the latter, bringing out a book to be classroom course material and used by
instructors handling a course on data structures and algorithms, comprehensive
enough for the novice students to benefit from, has been the main motivation in
writing this book.

As such, the book details concepts, techniques and applications pertaining to data
structures and algorithms, independent of any programming language, discusses
Preface xi

several examples and illustrative problems, poses review questions to reinforce the
understanding of the theory, and presents a suggestive list of programming
assignments to aid implementation of the data structures and algorithms learned.

In fact, the book may either be independently used as a textbook since it is self-
contained or serve as a companion for books discussing data structures and
algorithms implemented in specific programming languages such as C, C++, Java,
Python, and so on.

At this juncture, it needs to be pointed out that a plethora of programming


resources and freely downloadable implementations of the majority of the data
structures in almost all popular languages are available on the Internet, which can
undoubtedly serve as good guides for the learner. However, it has to be emphasized
that an earnest student of data structures and algorithms must invest a lot of time and
self-effort in trying to implement the data structures and algorithms learned, in a
language of one’s choice, all by oneself, in order to attain a thorough grasp of the
concepts.

About this edition

This edition is a largely revised and enlarged version of its predecessor,


published by McGraw Hill, USA. The earlier edition published in 2008 saw 15
reprints in its life span of 13 years (ending January 2022) and was recommended as
a text book for the course in several universities and colleges. It comprised 17
chapters categorized into five parts and reinforced learning through 133 illustrative
problems, 215 review questions and 74 programming assignments.

The features of this new edition are as follows:


– There are 22 chapters spread across three volumes that detail sequential linear
data structures, linked linear data structures, nonlinear data structures, advanced data
structures, searching and sorting algorithms, algorithm design techniques and NP-
completeness.
– The data structures of k-d trees and treaps have been elaborated in a newly
included chapter (Chapter 15) in Volume 3.
– The data structures of strings, bit rays, unrolled linked lists, self-organizing
linked lists, segment trees and k-ary trees have been introduced in the appropriate
sections of the existing chapters in Volumes 1 and 2.
– The concepts of counting binary search trees and Kruskal’s algorithm have
been detailed in the appropriate sections of the existing chapters in Volume 2.
xii A Textbook of Data Structures and Algorithms 1

– Skip list search, counting sort and bucket sort have been included in the
chapters on searching and sorting algorithms in Volume 3.
– The algorithm design techniques of divide and conquer, the greedy method
and dynamic programming have been elaborately discussed in Chapters 19–21 in
Volume 3.
– The concept of NP-completeness has been detailed in a newly included
chapter, Chapter 22 in Volume 3.
– Several illustrative problems, review questions and programming assignments
have been added to enrich the content and aid in understanding the concepts. The
new edition thus includes 181 illustrative problems, 276 review questions and 108
programming assignments.

Organization of the book

The book comprises three volumes, namely, Volume 1: Chapters 1–7, Volume 2:
Chapters 8–12 and Volume 3: Chapters 13–22.

Volume 1 opens with an introduction to data structures and concepts pertaining


to the analysis of algorithms, detailed in Chapters 1 and 2, which is essential to
appreciate the theories and algorithms related to data structures and their
applications.

Chapters 3–5 detail sequential linear data structures, namely, arrays, strings, bit
arrays, stacks, queues, priority queues and dequeues, and their applications.
Chapters 6 and 7 elucidate linked linear data structures, namely linked lists, linked
stacks and linked queues, and their applications.

Volume 2 details nonlinear data structures. Chapters 8 and 9 elaborate on the


nonlinear data structures of trees, binary trees and graphs, and their applications.
Chapters 10–12 highlight the advanced data structures of binary search trees, AVL
trees, B trees, tries, red-black trees and splay trees, and their applications.

Volume 3 details an assortment of data structures, algorithm design strategies


and their applications.

Chapters 13–15 discuss hash tables, files, k-d trees and treaps. Chapter 16
discusses the search algorithms of linear search, transpose sequential search,
interpolation search, binary search, Fibonacci search, skip list search and other
search techniques.
Preface xiii

Chapter 17 elaborates on the internal sorting algorithms of bubble sort, insertion


sort, selection sort, merge sort, shell sort, quick sort, heap sort, radix sort, counting
sort and bucket sort, and Chapter 18 discusses the external sorting techniques of
sorting with tapes, sorting with disks, polyphase merge sort and cascade merge sort.

Chapters 19–21 detail the algorithm design strategies of divide and conquer, the
greedy method and dynamic programming and their applications.

Chapter 22 introduces the theories and concepts of NP-completeness.

For a full list of the contents of Volumes 2 and 3, see the summary at the end of
this book.

Salient features of the book

The features of the book are as follows:


– all-around emphasis on theory, problems, applications and programming
assignments;
– simple and lucid explanation of the theory;
– inclusion of several applications to illustrate the use of data structures and
algorithms;
– several worked-out examples as illustrative problems in each chapter;
– list of programming assignments at the end of each chapter;
– review questions to strengthen understanding;
– self-contained text for use as a text book for either an introductory or advanced
level course.

Target audience

The book could be used both as an introductory or an advanced-level textbook


for undergraduate, graduate and research programs, which offer data structures and
algorithms as a core course or an elective course. While the book is primarily meant
to serve as a course material for use in the classroom, it could be used as a
companion guide during the laboratory sessions to nurture better understanding of
the theoretical concepts.

An introductory level course for a duration of one semester or 60 lecture hours,


targeting an undergraduate program or first-year graduate program or a diploma
xiv A Textbook of Data Structures and Algorithms 1

program or a certificate program, could include Chapters 1–7 of Volume 1, Chapter 8


of Volume 2, Chapters 13, 16 (sections 16.1, 16.2, 16.5) and 17 (sections 17.1–17.3,
17.5, 17.7) of Volume 3 in its curriculum.

A middle-level course for a duration of one semester or 60 lecture hours


targeting senior graduate-level programs and research programs such as MS/PhD
could include Chapters 1–7 of Volume 1, Chapters 8–11 of Volume 2, Chapter 13
and selective sections of Chapters 16–17 of Volume 3.

An advanced level course that focuses on advanced data structures and algorithm
design could begin with a review of Chapter 8 and include Chapters 9–12 of Volume 2,
Chapters 14 and 15 and selective sections of Chapters 16–18, and Chapters 19–22 of
Volume 3 in its curriculum based on the level of prerequisite courses satisfied.

Chapters 8–10 and Chapter 11 (sections 11.1–11.3) of Volume 2 and Chapters 13,
14 and 18 of Volume 3 could be useful to include in a curriculum that serves as a
prerequisite for a course on database management systems.

To re-emphasize, all theory sessions must be supplemented with laboratory


sessions to encourage learners to implement the concepts learned in an appropriate
language that adheres to the curricular requirements of the programs concerned.
Acknowledgments

The author is grateful to ISTE Ltd., London, UK, for accepting to publish the
book, in collaboration with John Wiley & Sons Inc., USA. She expresses her
appreciation to the publishing team, for their professionalism and excellent
production practices, while bringing out this book in three volumes.

The author expresses her sincere thanks to the Management and Principal, PSG
College of Technology, Coimbatore, India for the support extended while writing
the book.

The author would like to place on record her immense admiration and affection
for her father, Late Professor G. A. Krishna Pai and her mother Rohini Krishna Pai
for their unbounded encouragement and support to help her follow her life lessons
and her sisters Dr. Rekha Pai and Udaya Pai, for their unstinted, anywhere-anytime-
anything kind of help and support, all of which were instrumental and inspirational
in helping this author create this work.

G. A. Vijayalakshmi Pai
August 2022
1

Introduction

While looking around and marveling at the technological advancements of this


world – both within and without, one cannot help but perceive the intense and
intrinsic association of the disciplines of science and engineering and their allied and
hybrid counterparts, with the ubiquitous machines called computers. In fact, it is
difficult to spot a discipline that has distanced itself from the discipline of computer
science. To quote a few, be it a medical surgery or diagnosis performed by robots or
doctors on patients halfway across the globe, or the launching of space crafts and
satellites into outer space, or forecasting tornadoes and cyclones, or the more
mundane needs of the online reservation of tickets or billing at supermarkets, or the
control of washing machines, etc., one cannot help but deem computers to be
omnipresent, omnipotent, why even omniscient! (Figure 1.1).

In short, any discipline that calls for problem solving using computers looks up
to the discipline of computer science for efficient and effective methods of solving
the problems in their respective fields. From the view point of problem solving, the
discipline of computer science could be naively categorized into the following four
sub areas, notwithstanding the overlaps, extensions and gray areas within
themselves:
– Machines: What machines are appropriate or available for the solution of the
problem? What is the machine configuration – its processing power, memory
capacity, etc. – that would be required for the efficient execution of the problem
solution?
– Languages: What is the language or software with which the problem solution
needs to be coded? What are the software constraints that would hamper the efficient
implementation of the solution to the problem?
2 A Textbook of Data Structures and Algorithms 1

– Foundations: What is the problem model and its solution? What methods need
to be employed for the efficient design and implementation of the solution? What is
its performance measure?
– Technologies: What are the technologies that need to be incorporated to solve
the problem? For example, does the solution call for a web-based implementation,
need activation from mobile devices, call for hand shaking broadcasting devices or
merely need to interact with high-end or low-end peripheral devices?

Agriculture Business Health care

Industry Transportation
Computer

Weather Science Space Technology

Figure 1.1. Omnipresence of computers. For a color version


of this figure, see www.iste.co.uk/pai/algorithms1.zip

Figure 1.2 illustrates the categorization of the discipline of computer science


from the perspective of problem solving.

One of the core fields that belongs to the foundations of computer science
addresses the design, analysis and implementation of algorithms for the efficient
Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
Exploring the Variety of Random
Documents with Different Content
*** END OF THE PROJECT GUTENBERG EBOOK FRIAREN FRÅN
LANDET: LUSTSPEL ***

Updated editions will replace the previous one—the old editions will
be renamed.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States copyright in
these works, so the Foundation (and you!) can copy and distribute it
in the United States without permission and without paying
copyright royalties. Special rules, set forth in the General Terms of
Use part of this license, apply to copying and distributing Project
Gutenberg™ electronic works to protect the PROJECT GUTENBERG™
concept and trademark. Project Gutenberg is a registered trademark,
and may not be used if you charge for an eBook, except by following
the terms of the trademark license, including paying royalties for use
of the Project Gutenberg trademark. If you do not charge anything
for copies of this eBook, complying with the trademark license is
very easy. You may use this eBook for nearly any purpose such as
creation of derivative works, reports, performances and research.
Project Gutenberg eBooks may be modified and printed and given
away—you may do practically ANYTHING in the United States with
eBooks not protected by U.S. copyright law. Redistribution is subject
to the trademark license, especially commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the free


distribution of electronic works, by using or distributing this work (or
any other work associated in any way with the phrase “Project
Gutenberg”), you agree to comply with all the terms of the Full
Project Gutenberg™ License available with this file or online at
www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand, agree
to and accept all the terms of this license and intellectual property
(trademark/copyright) agreement. If you do not agree to abide by all
the terms of this agreement, you must cease using and return or
destroy all copies of Project Gutenberg™ electronic works in your
possession. If you paid a fee for obtaining a copy of or access to a
Project Gutenberg™ electronic work and you do not agree to be
bound by the terms of this agreement, you may obtain a refund
from the person or entity to whom you paid the fee as set forth in
paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only be


used on or associated in any way with an electronic work by people
who agree to be bound by the terms of this agreement. There are a
few things that you can do with most Project Gutenberg™ electronic
works even without complying with the full terms of this agreement.
See paragraph 1.C below. There are a lot of things you can do with
Project Gutenberg™ electronic works if you follow the terms of this
agreement and help preserve free future access to Project
Gutenberg™ electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright law
in the United States and you are located in the United States, we do
not claim a right to prevent you from copying, distributing,
performing, displaying or creating derivative works based on the
work as long as all references to Project Gutenberg are removed. Of
course, we hope that you will support the Project Gutenberg™
mission of promoting free access to electronic works by freely
sharing Project Gutenberg™ works in compliance with the terms of
this agreement for keeping the Project Gutenberg™ name associated
with the work. You can easily comply with the terms of this
agreement by keeping this work in the same format with its attached
full Project Gutenberg™ License when you share it without charge
with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.

1.E. Unless you have removed all references to Project Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project Gutenberg™
work (any work on which the phrase “Project Gutenberg” appears,
or with which the phrase “Project Gutenberg” is associated) is
accessed, displayed, performed, viewed, copied or distributed:
This eBook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this eBook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is derived


from texts not protected by U.S. copyright law (does not contain a
notice indicating that it is posted with permission of the copyright
holder), the work can be copied and distributed to anyone in the
United States without paying any fees or charges. If you are
redistributing or providing access to a work with the phrase “Project
Gutenberg” associated with or appearing on the work, you must
comply either with the requirements of paragraphs 1.E.1 through
1.E.7 or obtain permission for the use of the work and the Project
Gutenberg™ trademark as set forth in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is posted


with the permission of the copyright holder, your use and distribution
must comply with both paragraphs 1.E.1 through 1.E.7 and any
additional terms imposed by the copyright holder. Additional terms
will be linked to the Project Gutenberg™ License for all works posted
with the permission of the copyright holder found at the beginning
of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files containing a
part of this work or any other work associated with Project
Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute this


electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the Project
Gutenberg™ License.

1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt
that s/he does not agree to the terms of the full Project
Gutenberg™ License. You must require such a user to return or
destroy all copies of the works possessed in a physical medium
and discontinue all use of and all access to other copies of
Project Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
damaged disk or other medium, a computer virus, or computer
codes that damage or cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except for


the “Right of Replacement or Refund” described in paragraph 1.F.3,
the Project Gutenberg Literary Archive Foundation, the owner of the
Project Gutenberg™ trademark, and any other party distributing a
Project Gutenberg™ electronic work under this agreement, disclaim
all liability to you for damages, costs and expenses, including legal
fees. YOU AGREE THAT YOU HAVE NO REMEDIES FOR
NEGLIGENCE, STRICT LIABILITY, BREACH OF WARRANTY OR
BREACH OF CONTRACT EXCEPT THOSE PROVIDED IN PARAGRAPH
1.F.3. YOU AGREE THAT THE FOUNDATION, THE TRADEMARK
OWNER, AND ANY DISTRIBUTOR UNDER THIS AGREEMENT WILL
NOT BE LIABLE TO YOU FOR ACTUAL, DIRECT, INDIRECT,
CONSEQUENTIAL, PUNITIVE OR INCIDENTAL DAMAGES EVEN IF
YOU GIVE NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of receiving
it, you can receive a refund of the money (if any) you paid for it by
sending a written explanation to the person you received the work
from. If you received the work on a physical medium, you must
return the medium with your written explanation. The person or
entity that provided you with the defective work may elect to provide
a replacement copy in lieu of a refund. If you received the work
electronically, the person or entity providing it to you may choose to
give you a second opportunity to receive the work electronically in
lieu of a refund. If the second copy is also defective, you may
demand a refund in writing without further opportunities to fix the
problem.

1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the Foundation,


the trademark owner, any agent or employee of the Foundation,
anyone providing copies of Project Gutenberg™ electronic works in
accordance with this agreement, and any volunteers associated with
the production, promotion and distribution of Project Gutenberg™
electronic works, harmless from all liability, costs and expenses,
including legal fees, that arise directly or indirectly from any of the
following which you do or cause to occur: (a) distribution of this or
any Project Gutenberg™ work, (b) alteration, modification, or
additions or deletions to any Project Gutenberg™ work, and (c) any
Defect you cause.

Section 2. Information about the Mission


of Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many
small donations ($1 to $5,000) are particularly important to
maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.
Project Gutenberg™ eBooks are often created from several printed
editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.

More than just a book-buying platform, we strive to be a bridge


connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.

Join us on a journey of knowledge exploration, passion nurturing, and


personal growth every day!

ebookmasss.com

You might also like