ebook img

Data Structure and Algorithmic Thinking with Python: Data Structure and Algorithmic Puzzles PDF

436 Pages·2020·6.55 MB·english
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Data Structure and Algorithmic Thinking with Python: Data Structure and Algorithmic Puzzles

Data Structures And Algorithmic Thinking With Python -To All My Readers And Parents By Narasimha Karumanchi Concepts Problems Interview Questions Copyright© 2020 by (cid:1829)(cid:1853)(cid:1870)(cid:1857)(cid:1857)(cid:1870)(cid:1839)(cid:1867)(cid:1866)(cid:1863).(cid:1855)(cid:1867)(cid:1865) All rights reserved. Designed by (cid:1840)(cid:1853)(cid:1870)(cid:1853)(cid:1871)(cid:1861)(cid:1865)ℎ(cid:1853) (cid:1837)(cid:1853)(cid:1870)(cid:1873)(cid:1865)(cid:1853)(cid:1866)(cid:1855)ℎ(cid:1861) ISBN978-8192107592 Copyright ©2020 CareerMonk Publications. All rights reserved. All rights reserved. No part of this book may be reproduced in any form or by any electronic or mechanical means, including information storage and retrieval systems, without written permission from the publisher or author. This book has been published with all efforts taken to make the material error-free after the consent of the author. However, the author and the publisher do not assume and hereby disclaim any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from negligence, accident, or any other cause. While every effort has been made to avoid any mistake or omission, this publication is being sold on the condition and understanding that neither the author nor the publishers or printers would be liable in any manner to any person by reason of any mistake or omission in this publication or for any action taken or omitted to be taken or advice rendered or accepted on the basis of this work. For any defect in printing or binding the publishers will be liable only to replace the defective copy by another copy of this work then available. Acknowledgements (cid:1839)(cid:1867)(cid:1872)ℎ(cid:1857)(cid:1870) and (cid:1832)(cid:1853)(cid:1872)ℎ(cid:1857)(cid:1870), it is impossible to thank you adequately for everything you have done, from loving me unconditionally to raising me in a stable household, where your persistent efforts and traditional values taught your children to celebrate and embrace life. I could not have asked for better parents or role-models. You showed me that anything is possible with faith, hard work and determination. This book would not have been possible without the help of many people. I would like to express my gratitude to all of the people who provided support, talked things over, read, wrote, offered comments, allowed me to quote their remarks and assisted in the editing, proofreading and design. In particular, I would like to thank the following individuals:  (cid:1839)(cid:1867)ℎ(cid:1853)(cid:1866) (cid:1839)(cid:1873)(cid:1864)(cid:1864)(cid:1853)(cid:1868)(cid:1873)(cid:1856)(cid:1861), IIT Bombay, Architect, dataRPM Pvt. Ltd.  (cid:1840)(cid:1853)(cid:1874)(cid:1861)(cid:1866) (cid:1837)(cid:1873)(cid:1865)(cid:1853)(cid:1870) (cid:1836)(cid:1853)(cid:1861)(cid:1871)(cid:1875)(cid:1853)(cid:1864), Senior Consultant, Juniper Networks Inc.  (cid:1846)(cid:1870)(cid:1861)(cid:1866)(cid:1853)(cid:1872)ℎ (cid:1837)(cid:1867)(cid:1877)(cid:1853), Software Developer, TinMen −(cid:1840)(cid:1853)(cid:1870)(cid:1853)(cid:1871)(cid:1861)(cid:1865)ℎ(cid:1853) (cid:1837)(cid:1853)(cid:1870)(cid:1873)(cid:1865)(cid:1853)(cid:1866)(cid:1855)ℎ(cid:1861) M-Tech, (cid:1835)(cid:1835)(cid:1846) (cid:1828)(cid:1867)(cid:1865)(cid:1854)(cid:1853)(cid:1877) Founder, (cid:1829)(cid:1853)(cid:1870)(cid:1857)(cid:1857)(cid:1870)(cid:1839)(cid:1867)(cid:1866)(cid:1863).(cid:1855)(cid:1867)(cid:1865) Preface Dear Reader, Please hold on! I know many people typically do not read the Preface of a book. But I strongly recommend that you read this particular Preface. The study of algorithms and data structures is central to understanding what computer science is all about. Learning computer science is not unlike learning any other type of difficult subject matter. The only way to be successful is through deliberate and incremental exposure to the fundamental ideas. A beginning computer scientist needs practice so that there is a thorough understanding before continuing on to the more complex parts of the curriculum. In addition, a beginner needs to be given the opportunity to be successful and gain confidence. This textbook is designed to serve as a text for a first course on data structures and algorithms. In this book, we cover abstract data types and data structures, writing algorithms, and solving problems. We look at a number of data structures and solve classic problems that arise. The tools and techniques that you learn here will be applied over and over as you continue your study of computer science. It is not the main objective of this book to present you with the theorems and proofs on (cid:1856)(cid:1853)(cid:1872)(cid:1853) (cid:1871)(cid:1872)(cid:1870)(cid:1873)(cid:1855)(cid:1872)(cid:1873)(cid:1870)(cid:1857)(cid:1871) and (cid:1853)(cid:1864)(cid:1859)(cid:1867)(cid:1870)(cid:1861)(cid:1872)ℎ(cid:1865)(cid:1871). I have followed a pattern of improving the problem solutions with different complexities (for each problem, you will find multiple solutions with different, and reduced, complexities). Basically, it’s an enumeration of possible solutions. With this approach, even if you get a new question, it will show you a way to think about the possible solutions. You will find this book useful for interview preparation, competitive exams preparation, and campus interview preparations. As a (cid:1862)(cid:1867)(cid:1854) (cid:1871)(cid:1857)(cid:1857)(cid:1863)(cid:1857)(cid:1870), if you read the complete book, I am sure you will be able to challenge the interviewers. If you read it as an (cid:1861)(cid:1866)(cid:1871)(cid:1872)(cid:1870)(cid:1873)(cid:1855)(cid:1872)(cid:1867)(cid:1870), it will help you to deliver lectures with an approach that is easy to follow, and as a result your students will appreciate the fact that they have opted for Computer Science / Information Technology as their degree. This book is also useful for (cid:1831)(cid:1866)(cid:1859)(cid:1861)(cid:1866)(cid:1857)(cid:1857)(cid:1870)(cid:1861)(cid:1866)(cid:1859) (cid:1856)(cid:1857)(cid:1859)(cid:1870)(cid:1857)(cid:1857) (cid:1871)(cid:1872)(cid:1873)(cid:1856)(cid:1857)(cid:1866)(cid:1872)(cid:1871) and (cid:1839)(cid:1853)(cid:1871)(cid:1872)(cid:1857)(cid:1870)(cid:1871) (cid:1856)(cid:1857)(cid:1859)(cid:1870)(cid:1857)(cid:1857) (cid:1871)(cid:1872)(cid:1873)(cid:1856)(cid:1857)(cid:1866)(cid:1872)(cid:1871) during their academic preparations. In all the chapters you will see that there is more emphasis on problems and their analysis rather than on theory. In each chapter, you will first read about the basic required theory, which is then followed by a section on problem sets. In total, there are approximately 700 algorithmic problems, all with solutions. If you read the book as a (cid:1871)(cid:1872)(cid:1873)(cid:1856)(cid:1857)(cid:1866)(cid:1872) preparing for competitive exams for Computer Science / Information Technology, the content covers (cid:1853)(cid:1864)(cid:1864) the (cid:1870)(cid:1857)(cid:1869)(cid:1873)(cid:1861)(cid:1870)(cid:1857)(cid:1856) topics in full detail. While writing this book, my main focus was to help students who are preparing for these exams. In all the chapters you will see more emphasis on problems and analysis rather than on theory. In each chapter, you will first see the basic required theory followed by various problems. For many problems, (cid:1865)(cid:1873)(cid:1864)(cid:1872)(cid:1861)(cid:1868)(cid:1864)(cid:1857) solutions are provided with different levels of complexity. We start with the (cid:1854)(cid:1870)(cid:1873)(cid:1872)(cid:1857) (cid:1858)(cid:1867)(cid:1870)(cid:1855)(cid:1857) solution and slowly move toward the (cid:1854)(cid:1857)(cid:1871)(cid:1872) (cid:1871)(cid:1867)(cid:1864)(cid:1873)(cid:1872)(cid:1861)(cid:1867)(cid:1866) possible for that problem. For each problem, we endeavor to understand how much time the algorithm takes and how much memory the algorithm uses. It is recommended that the reader does at least one complete reading of this book to gain a full understanding of all the topics that are covered. Then, in subsequent readings you can skip directly to any chapter to refer to a specific topic. Even though many readings have been done for the purpose of correcting errors, there could still be some minor typos in the book. If any are found, they will be updated at (cid:1875)(cid:1875)(cid:1875).(cid:1829)(cid:1853)(cid:1870)(cid:1857)(cid:1857)(cid:1870)(cid:1839)(cid:1867)(cid:1866)(cid:1863).(cid:1855)(cid:1867)(cid:1865). You can monitor this site for any corrections and also for new problems and solutions. Also, please provide your valuable suggestions at: (cid:1835)(cid:1866)(cid:1858)(cid:1867)@(cid:1829)(cid:1853)(cid:1870)(cid:1857)(cid:1857)(cid:1870)(cid:1839)(cid:1867)(cid:1866)(cid:1863).(cid:1855)(cid:1867)(cid:1865). I wish you all the best and I am confident that you will find this book useful. −(cid:1840)(cid:1853)(cid:1870)(cid:1853)(cid:1871)(cid:1861)(cid:1865)ℎ(cid:1853) (cid:1837)(cid:1853)(cid:1870)(cid:1873)(cid:1865)(cid:1853)(cid:1866)(cid:1855)ℎ(cid:1861) M-Tech, (cid:1835)(cid:1835)(cid:1846) (cid:1828)(cid:1867)(cid:1865)(cid:1854)(cid:1853)(cid:1877) Founder, (cid:1829)(cid:1853)(cid:1870)(cid:1857)(cid:1857)(cid:1870)(cid:1839)(cid:1867)(cid:1866)(cid:1863).(cid:1855)(cid:1867)(cid:1865) Data Structure Operations Cheat Sheet Space Data Average Case Time Complexity Worst Case Time Complexity Complexity Structure Accessing Accessing Name Search Insertion Deletion Search Insertion Deletion Worst Case (cid:1866)(cid:3047)(cid:3035)element (cid:1866)(cid:3047)(cid:3035)element Arrays O(1) O((cid:1866)) O((cid:1866)) O((cid:1866)) O(1) O((cid:1866)) O((cid:1866)) O((cid:1866)) O((cid:1866)) Stacks O((cid:1866)) O((cid:1866)) O(1) O(1) O((cid:1866)) O((cid:1866)) O(1) O(1) O((cid:1866)) Queues O((cid:1866)) O((cid:1866)) O(1) O(1) O((cid:1866)) O((cid:1866)) O(1) O(1) O((cid:1866)) Binary O((cid:1866)) O((cid:1866)) O((cid:1866)) O((cid:1866)) O((cid:1866)) O((cid:1866)) O((cid:1866)) O((cid:1866)) O((cid:1866)) Trees Binary Search O((cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1866)) O((cid:1866)) O((cid:1866)) O((cid:1866)) O((cid:1866)) Trees Balanced Binary O((cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1864)(cid:1867)(cid:1859)(cid:1866)) Search Trees Hash N/A O(1) O(1) O(1) N/A O((cid:1866)) O((cid:1866)) O((cid:1866)) O((cid:1866)) Tables Note: For best case operations, the time complexities are O(1). Sorting Algorithms Cheat Sheet Space Sorting Time Complexity Complexity Is Sorting Class Algorithm Remarks Best Average Worst Stable? Type Name Worst Case Case Case Case Bubble Sort O((cid:1866)) O((cid:1866)(cid:2870)) O((cid:1866)(cid:2870)) O(1) Yes Comparison Not a preferred sorting algorithm. In the best case (already sorted), Insertion Sort O((cid:1866)) O((cid:1866)(cid:2870)) O((cid:1866)(cid:2870)) O(1) Yes Comparison every insert requires constant time Even a perfectly sorted array Selection Sort O((cid:1866)(cid:2870)) O((cid:1866)(cid:2870)) O((cid:1866)(cid:2870)) O(1) Yes Comparison requires scanning the entire array On arrays, it requires O((cid:1866)) space; Merge Sort O((cid:1866)(cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1866)(cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1866)(cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1866)) Yes Comparison and on linked lists, it requires constant space By using input array as storage for Heap Sort O((cid:1866)(cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1866)(cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1866)(cid:1864)(cid:1867)(cid:1859)(cid:1866)) O(1) No Comparison the heap, it is possible to achieve constant space Randomly picking a pivot value can help avoid worst case Quick Sort O((cid:1866)(cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1866)(cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1866)(cid:2870)) O((cid:1864)(cid:1867)(cid:1859)(cid:1866)) No Comparison scenarios such as a perfectly sorted array. Performing inorder traversal on Tree Sort O((cid:1866)(cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1866)(cid:1864)(cid:1867)(cid:1859)(cid:1866)) O((cid:1866)(cid:2870)) O((cid:1866)) Yes Comparison the balanced binary search tree. (cid:1863) is the range of the non-negative Counting Sort O((cid:1866)+(cid:1863)) O((cid:1866)+(cid:1863)) O((cid:1866)+(cid:1863)) O((cid:1863)) Yes Linear key values. Bucket sort is stable, if the Bucket Sort O((cid:1866)+(cid:1863)) O((cid:1866)+(cid:1863)) O((cid:1866)(cid:2870)) O((cid:1866)) Yes Linear underlying sorting algorithm is stable. Radix sort is stable, if the Radix Sort O((cid:1856)(cid:1866)) O((cid:1856)(cid:1866)) O((cid:1856)(cid:1866)) O((cid:1856)+(cid:1866)) Yes Linear underlying sorting algorithm is stable. Table of Contents 0. Organization of Chapters ------------------------------------------------------------------------------------------------------------------------- 15 0.1 What Is This Book About? --------------------------------------------------------------------------------------------------------------------------------- 15 0.2 Should I Buy This Book? ----------------------------------------------------------------------------------------------------------------------------------- 15 0.3 Organization of Chapters------------------------------------------------------------------------------------------------------------------------------------- 15 0.4 Some Prerequisites --------------------------------------------------------------------------------------------------------------------------------------------- 18 1. Introduction -------------------------------------------------------------------------------------------------------------------------------------------- 19 1.1 Variables ------------------------------------------------------------------------------------------------------------------------------------------------------------ 19 1.2 Data Types --------------------------------------------------------------------------------------------------------------------------------------------------------- 19 1.3 Data Structures --------------------------------------------------------------------------------------------------------------------------------------------------- 20 1.4 Abstract Data Types (ADTs) ------------------------------------------------------------------------------------------------------------------------------- 20 1.5 What is an Algorithm? ---------------------------------------------------------------------------------------------------------------------------------------- 20 1.6 Why the Analysis of Algorithms? ------------------------------------------------------------------------------------------------------------------------- 20 1.7 Goal of the Analysis of Algorithms ----------------------------------------------------------------------------------------------------------------------- 20 1.8 What is Running Time Analysis? ------------------------------------------------------------------------------------------------------------------------- 20 1.9 How to Compare Algorithms------------------------------------------------------------------------------------------------------------------------------- 21 1.10 What is Rate of Growth? ----------------------------------------------------------------------------------------------------------------------------------- 21 1.11 Commonly Used Rates of Growth --------------------------------------------------------------------------------------------------------------------- 21 1.12 Types of Analysis ---------------------------------------------------------------------------------------------------------------------------------------------- 22 1.13 Asymptotic Notation ----------------------------------------------------------------------------------------------------------------------------------------- 22 1.14 Big-O Notation ------------------------------------------------------------------------------------------------------------------------------------------------- 22 1.15 Omega-Ω Notation -------------------------------------------------------------------------------------------------------------------------------------------- 23 1.16 Theta- Notation---------------------------------------------------------------------------------------------------------------------------------------------- 24 1.17 Why is it called Asymptotic Analysis?----------------------------------------------------------------------------------------------------------------- 25 1.18 Guidelines for Asymptotic Analysis -------------------------------------------------------------------------------------------------------------------- 25 1.19 Simplifying properties of asymptotic notations ---------------------------------------------------------------------------------------------------- 26 1.21 Commonly used Logarithms and Summations ---------------------------------------------------------------------------------------------------- 26 1.21 Master Theorem for Divide and Conquer Recurrences --------------------------------------------------------------------------------------- 27 1.22 Divide and Conquer Master Theorem: Problems & Solutions ----------------------------------------------------------------------------- 27 1.23 Master Theorem for Subtract and Conquer Recurrences------------------------------------------------------------------------------------- 28 1.24 Variant of Subtraction and Conquer Master Theorem----------------------------------------------------------------------------------------- 28 1.25 Method of Guessing and Confirming ----------------------------------------------------------------------------------------------------------------- 28 1.26 Amortized Analysis ------------------------------------------------------------------------------------------------------------------------------------------- 29 1.27 Algorithms Analysis: Problems & Solutions -------------------------------------------------------------------------------------------------------- 30 2. Recursion and Backtracking--------------------------------------------------------------------------------------------------------------------- 41 2.1 Introduction ------------------------------------------------------------------------------------------------------------------------------------------------------- 41 2.2 What is Recursion? --------------------------------------------------------------------------------------------------------------------------------------------- 41 2.3 Why Recursion? ------------------------------------------------------------------------------------------------------------------------------------------------- 41 2.4 Format of a Recursive Function --------------------------------------------------------------------------------------------------------------------------- 41 2.5 Recursion and Memory (Visualization) ----------------------------------------------------------------------------------------------------------------- 42 2.6 Recursion versus Iteration ----------------------------------------------------------------------------------------------------------------------------------- 42 2.7 Notes on Recursion -------------------------------------------------------------------------------------------------------------------------------------------- 42 2.8 Example Algorithms of Recursion------------------------------------------------------------------------------------------------------------------------ 43 2.9 Recursion: Problems & Solutions ------------------------------------------------------------------------------------------------------------------------- 43 2.10 What is Backtracking?--------------------------------------------------------------------------------------------------------------------------------------- 43 2.11 Example Algorithms of Backtracking ----------------------------------------------------------------------------------------------------------------- 44 2.12 Backtracking: Problems & Solutions------------------------------------------------------------------------------------------------------------------- 44 3. Linked Lists -------------------------------------------------------------------------------------------------------------------------------------------- 47 3.1 What is a Linked List? ---------------------------------------------------------------------------------------------------------------------------------------- 47 3.2 Linked Lists ADT----------------------------------------------------------------------------------------------------------------------------------------------- 47 3.3 Why Linked Lists? --------------------------------------------------------------------------------------------------------------------------------------------- 47 3.4 Arrays Overview ------------------------------------------------------------------------------------------------------------------------------------------------- 47 3.5 Comparison of Linked Lists with Arrays and Dynamic Arrays ------------------------------------------------------------------------------- 48 3.6 Singly Linked Lists ---------------------------------------------------------------------------------------------------------------------------------------------- 49 3.7 Doubly Linked Lists ------------------------------------------------------------------------------------------------------------------------------------------- 54 3.8 Circular Linked Lists ------------------------------------------------------------------------------------------------------------------------------------------- 58 3.9 A Memory-efficient Doubly Linked List --------------------------------------------------------------------------------------------------------------- 62 3.11 Skip Lists ---------------------------------------------------------------------------------------------------------------------------------------------------------- 67 3.12 Linked Lists: Problems & Solutions ------------------------------------------------------------------------------------------------------------------- 70 4. Stacks ----------------------------------------------------------------------------------------------------------------------------------------------------- 90 4.1 What is a Stack? ------------------------------------------------------------------------------------------------------------------------------------------------- 90 4.2 How Stacks are Used ------------------------------------------------------------------------------------------------------------------------------------------ 90 4.3 Stack ADT --------------------------------------------------------------------------------------------------------------------------------------------------------- 90 4.4 Applications ------------------------------------------------------------------------------------------------------------------------------------------------------- 91 4.5 Implementation -------------------------------------------------------------------------------------------------------------------------------------------------- 91 4.6 Comparison of Implementations ------------------------------------------------------------------------------------------------------------------------- 95 4.7 Stacks: Problems & Solutions ------------------------------------------------------------------------------------------------------------------------------ 95 5. Queues ------------------------------------------------------------------------------------------------------------------------------------------------- 111 5.1 What is a Queue? --------------------------------------------------------------------------------------------------------------------------------------------- 111 5.2 How are Queues Used -------------------------------------------------------------------------------------------------------------------------------------- 111 5.3 Queue ADT ----------------------------------------------------------------------------------------------------------------------------------------------------- 111 5.4 Exceptions -------------------------------------------------------------------------------------------------------------------------------------------------------- 111 5.5 Applications ----------------------------------------------------------------------------------------------------------------------------------------------------- 111 5.6 Implementation ------------------------------------------------------------------------------------------------------------------------------------------------ 112 5.7 Queues: Problems & Solutions -------------------------------------------------------------------------------------------------------------------------- 116 6. Trees ---------------------------------------------------------------------------------------------------------------------------------------------------- 125 6.1 What is a Tree? ------------------------------------------------------------------------------------------------------------------------------------------------ 125 6.2 Glossary ----------------------------------------------------------------------------------------------------------------------------------------------------------- 125 6.3 Binary Trees ----------------------------------------------------------------------------------------------------------------------------------------------------- 126 6.4 Types of Binary Trees --------------------------------------------------------------------------------------------------------------------------------------- 126 6.5 Properties of Binary Trees--------------------------------------------------------------------------------------------------------------------------------- 126 6.6 Binary Tree Traversals -------------------------------------------------------------------------------------------------------------------------------------- 128 6.7 Generic Trees (N-ary Trees) ------------------------------------------------------------------------------------------------------------------------------ 146

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.