A Textbook of Data Structures and Algorithms, Volume 1 G. A. Vijayalakshmi Paiinstant Download
A Textbook of Data Structures and Algorithms, Volume 1 G. A. Vijayalakshmi Paiinstant Download
https://ebookmass.com/product/a-textbook-of-data-structures-and-
algorithms-volume-1-g-a-vijayalakshmi-pai/
https://ebookmass.com/product/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-mastering-nonlinear-data-structures-vijayalakshmi-
pai/
https://ebookmass.com/product/data-structures-and-algorithms-in-c-2nd-
edition/
https://ebookmass.com/product/data-structures-algorithms-in-
python-1st-edition-john-canning-2/
https://ebookmass.com/product/data-structures-algorithms-in-
python-1st-edition-john-canning/
https://ebookmass.com/product/fundamentals-of-python-data-
structures-2nd-edition-kenneth-a-lambert/
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
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:
www.iste.co.uk www.wiley.com
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.
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 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
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
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
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.
– 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.
The book comprises three volumes, namely, Volume 1: Chapters 1–7, Volume 2:
Chapters 8–12 and Volume 3: Chapters 13–22.
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.
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
Chapters 19–21 detail the algorithm design strategies of divide and conquer, the
greedy method and dynamic programming and their applications.
For a full list of the contents of Volumes 2 and 3, see the summary at the end of
this book.
Target audience
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.
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
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?
Industry Transportation
Computer
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.
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.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.
• 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 comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
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.
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.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
ebookmasss.com