This document is the preface of a book on data structures and algorithms by Narasimha Karumanchi. It introduces the book's purpose of helping beginners learn fundamental data structures and algorithms concepts through practice problems of varying complexities. The preface emphasizes understanding problem-solving over theory and encourages readers to think of multiple solutions to problems. It also acknowledges those who helped with the book's creation and provides an overview of the book's organization into chapters covering topics like recursion, linked lists, trees and sorting algorithms.
The document provides an overview of ancient literature from different parts of the world, including Egypt, Mesopotamia, Persia, India, China, and Greece. It discusses some of the earliest and most influential works, such as the Egyptian Book of the Dead, the Epic of Gilgamesh, writings from Confucius and Laozi in China, and the epics and plays of Homer, Hesiod, Aeschylus, Sophocles and Euripides in Greece. The document emphasizes how these classical works shaped later literature and thought.
The document discusses the activity selection problem, which aims to select the maximum number of non-conflicting activities that can be executed by a single person or machine within a given time frame. It describes how each activity has a start and finish time, and activities are compatible if their time intervals do not overlap. It then provides an algorithm that sorts activities by finish time and greedily selects activities with start times later than the current finish time.
Discrete Mathematics is a branch of mathematics involving discrete elements that uses algebra and arithmetic. It is increasingly being applied in the practical fields of mathematics and computer science. It is a very good tool for improving reasoning and problem-solving capabilities.
1) The document summarizes the characters and events of the Trojan War as told in Greek mythology. It describes the origins of the war stemming from the Judgment of Paris and the abduction of Helen of Troy by Paris.
2) It outlines the major Greek and Trojan characters as well as the gods who took sides in the war, with Athena, Hera, and Poseidon supporting the Greeks and Aphrodite, Ares, and Apollo supporting the Trojans.
3) It recounts some of the major battles and events of the 10-year war, including the sacrifice of Iphigenia, the deaths of Protesilaus and Hector, and the eventual killing
Bashō was a renowned 17th century Japanese haiku poet known for his travel journals and poetry that fused with nature. In his later years, he undertook a journey along the northern coastal route of Japan's main island to gain spiritual enlightenment. His famous travel journal, The Narrow Road to the Deep North, documents the hardships he faced on his journey including insect bites while sleeping, difficult mountain passages, and exhaustion. However, he also found enlightenment in nature's beauty and solitude, as shown in his poems about wisteria flowers and cicadas among temple rocks.
- A linked list is a data structure where each node contains a data field and a pointer to the next node.
- It allows dynamic size and efficient insertion/deletion compared to arrays.
- A doubly linked list adds a pointer to the previous node, allowing traversal in both directions.
- A circular linked list connects the last node back to the first node, making it a continuous loop.
- Variations require changes to the node structure and functions like append/delete to handle the added previous/next pointers.
The document provides an overview of ancient literature from different parts of the world, including Egypt, Mesopotamia, Persia, India, China, and Greece. It discusses some of the earliest and most influential works, such as the Egyptian Book of the Dead, the Epic of Gilgamesh, writings from Confucius and Laozi in China, and the epics and plays of Homer, Hesiod, Aeschylus, Sophocles and Euripides in Greece. The document emphasizes how these classical works shaped later literature and thought.
The document discusses the activity selection problem, which aims to select the maximum number of non-conflicting activities that can be executed by a single person or machine within a given time frame. It describes how each activity has a start and finish time, and activities are compatible if their time intervals do not overlap. It then provides an algorithm that sorts activities by finish time and greedily selects activities with start times later than the current finish time.
Discrete Mathematics is a branch of mathematics involving discrete elements that uses algebra and arithmetic. It is increasingly being applied in the practical fields of mathematics and computer science. It is a very good tool for improving reasoning and problem-solving capabilities.
1) The document summarizes the characters and events of the Trojan War as told in Greek mythology. It describes the origins of the war stemming from the Judgment of Paris and the abduction of Helen of Troy by Paris.
2) It outlines the major Greek and Trojan characters as well as the gods who took sides in the war, with Athena, Hera, and Poseidon supporting the Greeks and Aphrodite, Ares, and Apollo supporting the Trojans.
3) It recounts some of the major battles and events of the 10-year war, including the sacrifice of Iphigenia, the deaths of Protesilaus and Hector, and the eventual killing
Bashō was a renowned 17th century Japanese haiku poet known for his travel journals and poetry that fused with nature. In his later years, he undertook a journey along the northern coastal route of Japan's main island to gain spiritual enlightenment. His famous travel journal, The Narrow Road to the Deep North, documents the hardships he faced on his journey including insect bites while sleeping, difficult mountain passages, and exhaustion. However, he also found enlightenment in nature's beauty and solitude, as shown in his poems about wisteria flowers and cicadas among temple rocks.
- A linked list is a data structure where each node contains a data field and a pointer to the next node.
- It allows dynamic size and efficient insertion/deletion compared to arrays.
- A doubly linked list adds a pointer to the previous node, allowing traversal in both directions.
- A circular linked list connects the last node back to the first node, making it a continuous loop.
- Variations require changes to the node structure and functions like append/delete to handle the added previous/next pointers.
Introduction to data structures and AlgorithmDhaval Kaneria
This document provides an introduction to algorithms and data structures. It defines algorithms as step-by-step processes to solve problems and discusses their properties, including being unambiguous, composed of a finite number of steps, and terminating. The document outlines the development process for algorithms and discusses their time and space complexity, noting worst-case, average-case, and best-case scenarios. Examples of iterative and recursive algorithms for calculating factorials are provided to illustrate time and space complexity analyses.
The document discusses various works that deal with morality and ethics, including Plato's dialogues, Matthew Arnold's Culture and Anarchy, F. Scott Fitzgerald's The Great Gatsby, Shakespeare's Othello, and an unspecified movie about the clash between capitalism and morality. It also outlines Matthew Arnold's views that literature should possess "high truth" and "high seriousness" while acknowledging its ability to corrupt religion and undermine morality.
Egyptian literature traces its beginnings to ancient Egypt and is some of the earliest known literature. Indeed, the Egyptians were the first culture to develop literature as we know it today, that is, the book.
Aristotle was a Greek philosopher who lived from 384-322 BC. He was a student of Plato and founded his own school called the Lyceum in Athens. Aristotle made significant contributions across many fields, including logic, physics, biology and ethics. He emphasized empirical observation and reasoning in his works. Aristotle rejected some of Plato's theories like the theory of forms. He is considered one of the most influential ancient philosophers and developed logical systems still used today.
Deconstructionism was initiated by Jacques Derrida in the 1960s as a theory of literary criticism that questions traditional assumptions of certainty, identity, and truth. It asserts that words can only refer to other words and denies being a doctrine or method. Derrida and other scholars like Paul de Man and J. Hillis Miller developed deconstructionism at Yale University between the 1960s and 1980s. Deconstructionism examines the tensions between hierarchical opposites in texts like nature/culture, speech/writing, and mind/body. It calls attention to the role of absence and difference in fundamental philosophical concepts like truth, reality, and being. Despite some criticism of being nihilistic, ahistorical and apolitical,
The document outlines 6 rules of inference used to derive logical conclusions: 1) Conjunctive simplification, 2) Disjunctive amplification, 3) Hypothetical syllogism, 4) Disjunctive syllogism, 5) Modus ponens, and 6) Modus tollens. Each rule is symbolically represented and includes an explanation of its application using truth tables to prove the validity of the logical inferences.
The document provides an overview of propositional logic including:
1. It defines statements, logical connectives, and truth tables. Logical connectives like negation, conjunction, disjunction and others are explained.
2. It discusses various logical concepts like tautology, contradiction, contingency, logical equivalence, and logical implications.
3. It outlines propositional logic rules and properties including commutative, associative, distributive, De Morgan's laws, identity law, idempotent law, and transitive rule.
4. It provides an example of using truth tables to test the validity of an argument about bachelors dying young.
- Greek philosophy began with the Pre-Socratic period, including early philosophers like Thales, Anaximander, and Anaximenes who attempted to explain the universe through singular elements like water or air.
- Additional early philosophers included Heraclitus, who believed in constant change, and the atomists Leucippus and Democritus, who were the first to theorize that matter was made of tiny, indivisible particles called atoms.
- Socrates disagreed with the Sophists' teachings and used questioning to help students discover ethical truths for themselves, influencing both Plato and Aristotle, two of the most important Greek philosophers.
Equality and justice are related but distinct concepts. Equality refers to treating all people the same, while justice considers fairness and individual circumstances and outcomes. True justice cannot be achieved through equality alone. Different cultures understand justice in varying ways based on their shared history and beliefs. Key debates around justice include whether it stems from divine commands, natural law, human design, or a balance of consequences. Theories of justice also consider how to distribute goods fairly in a society.
The document discusses the history of Roman literature in three periods. The first period from 240-80 BC saw the development of drama, epics, satire and prose influenced by Greek models. The golden age from 80 BC to AD 14 represented the highest excellence in prose and poetry, including famous writers like Cicero, Sallust and Caesar. The third period from AD 14 to 180 saw a decline with the loss of freedom and interest in public life, though literature remained popular among some. Major Roman and Greek gods and goddesses are also listed with brief descriptions of their domains.
The document discusses divide and conquer algorithms. It describes divide and conquer as a design strategy that involves dividing a problem into smaller subproblems, solving the subproblems recursively, and combining the solutions. It provides examples of divide and conquer algorithms like merge sort, quicksort, and binary search. Merge sort works by recursively sorting halves of an array until it is fully sorted. Quicksort selects a pivot element and partitions the array into subarrays of smaller and larger elements, recursively sorting the subarrays. Binary search recursively searches half-intervals of a sorted array to find a target value.
The document provides an overview and analysis of Homer's epic poem "The Iliad". It discusses why the poem is studied, defines the epic hero and how Achilles exemplifies this as the central hero of the work. It explores themes of glory, excellence, and the literary device of the "aristeia". Key sections analyze the definition of an aristeia and highlight important details from several books in "The Iliad", including the introduction of Achilles' rage in Book 1 and Helen's review of the champions in Book 3.
Linear search, also called sequential search, is a method for finding a target value within a list by sequentially checking each element until a match is found or all elements are searched. It works by iterating through each element of a data structure (such as an array or list), comparing it to the target value, and returning the index or position of the target if found. The key aspects covered include definitions of data, structure, data structure, and search. Pseudocode and examples of linear search on a phone directory are provided. Advantages are that it is simple and works for small data sets, while disadvantages are that search time increases linearly with the size of the data set.
This is a presentation on Arrays, one of the most important topics on Data Structures and algorithms. Anyone who is new to DSA or wants to have a theoretical understanding of the same can refer to it :D
The document discusses different types of lyric poetry including Italian or Petrarchan sonnets which are divided into an octave stanza and sextet stanza, Shakespearean or Spenserian sonnets which are divided into three quatrains and a couplet, elegies which express grief and death, odes which express noble feelings with no definite structure, psalms which praise God or Mary, awit songs which have 12 syllable measures and are sung to guitar, and corridos which have 8 syllable measures and are recited to a beat. The document was presented by Princess Adelane Gonzales and expresses gratitude at the end.
Insertion sort bubble sort selection sortUmmar Hayat
The document discusses three sorting algorithms: insertion sort, bubble sort, and selection sort. Insertion sort has best-case linear time but worst-case quadratic time, sorting elements in place. Bubble sort repeatedly compares and swaps adjacent elements, having quadratic time in all cases. Selection sort finds the minimum element and exchanges it into the sorted portion of the array in each pass, with quadratic time.
The document describes the quicksort algorithm. It covers the divide and conquer approach, partitioning the array around a pivot element, recursively sorting the subarrays, and combining the results. It provides pseudocode for quicksort and an example. It analyzes the time complexity in the best, worst, and average cases. The best case of O(n log n) occurs when the array is evenly partitioned on each recursive call. The worst case of O(n^2) happens when the partitioning produces very uneven subproblems. The average case is closer to the best case complexity of O(n log n).
The document discusses stacks and queues as data structures. It defines stacks as first-in last-out (LIFO) structures where elements are added and removed from one end of the list. Queues are defined as first-in first-out (FIFO) structures where elements are added to one end and removed from the other. The document provides examples of using stacks and queues in programming and describes the common operations that can be performed on each type of data structure.
A course certificate from University of Michigan and Coursera confirms that Abdullah Al Mamun successfully completed an online Python Data Structures course. The certificate, issued on April 24, 2016 and signed by Charles Severance, a Clinical Associate Professor at the University of Michigan's School of Information, verifies Al Mamun's identity and participation in the non-credit course through Coursera.
Introduction to data structures and AlgorithmDhaval Kaneria
This document provides an introduction to algorithms and data structures. It defines algorithms as step-by-step processes to solve problems and discusses their properties, including being unambiguous, composed of a finite number of steps, and terminating. The document outlines the development process for algorithms and discusses their time and space complexity, noting worst-case, average-case, and best-case scenarios. Examples of iterative and recursive algorithms for calculating factorials are provided to illustrate time and space complexity analyses.
The document discusses various works that deal with morality and ethics, including Plato's dialogues, Matthew Arnold's Culture and Anarchy, F. Scott Fitzgerald's The Great Gatsby, Shakespeare's Othello, and an unspecified movie about the clash between capitalism and morality. It also outlines Matthew Arnold's views that literature should possess "high truth" and "high seriousness" while acknowledging its ability to corrupt religion and undermine morality.
Egyptian literature traces its beginnings to ancient Egypt and is some of the earliest known literature. Indeed, the Egyptians were the first culture to develop literature as we know it today, that is, the book.
Aristotle was a Greek philosopher who lived from 384-322 BC. He was a student of Plato and founded his own school called the Lyceum in Athens. Aristotle made significant contributions across many fields, including logic, physics, biology and ethics. He emphasized empirical observation and reasoning in his works. Aristotle rejected some of Plato's theories like the theory of forms. He is considered one of the most influential ancient philosophers and developed logical systems still used today.
Deconstructionism was initiated by Jacques Derrida in the 1960s as a theory of literary criticism that questions traditional assumptions of certainty, identity, and truth. It asserts that words can only refer to other words and denies being a doctrine or method. Derrida and other scholars like Paul de Man and J. Hillis Miller developed deconstructionism at Yale University between the 1960s and 1980s. Deconstructionism examines the tensions between hierarchical opposites in texts like nature/culture, speech/writing, and mind/body. It calls attention to the role of absence and difference in fundamental philosophical concepts like truth, reality, and being. Despite some criticism of being nihilistic, ahistorical and apolitical,
The document outlines 6 rules of inference used to derive logical conclusions: 1) Conjunctive simplification, 2) Disjunctive amplification, 3) Hypothetical syllogism, 4) Disjunctive syllogism, 5) Modus ponens, and 6) Modus tollens. Each rule is symbolically represented and includes an explanation of its application using truth tables to prove the validity of the logical inferences.
The document provides an overview of propositional logic including:
1. It defines statements, logical connectives, and truth tables. Logical connectives like negation, conjunction, disjunction and others are explained.
2. It discusses various logical concepts like tautology, contradiction, contingency, logical equivalence, and logical implications.
3. It outlines propositional logic rules and properties including commutative, associative, distributive, De Morgan's laws, identity law, idempotent law, and transitive rule.
4. It provides an example of using truth tables to test the validity of an argument about bachelors dying young.
- Greek philosophy began with the Pre-Socratic period, including early philosophers like Thales, Anaximander, and Anaximenes who attempted to explain the universe through singular elements like water or air.
- Additional early philosophers included Heraclitus, who believed in constant change, and the atomists Leucippus and Democritus, who were the first to theorize that matter was made of tiny, indivisible particles called atoms.
- Socrates disagreed with the Sophists' teachings and used questioning to help students discover ethical truths for themselves, influencing both Plato and Aristotle, two of the most important Greek philosophers.
Equality and justice are related but distinct concepts. Equality refers to treating all people the same, while justice considers fairness and individual circumstances and outcomes. True justice cannot be achieved through equality alone. Different cultures understand justice in varying ways based on their shared history and beliefs. Key debates around justice include whether it stems from divine commands, natural law, human design, or a balance of consequences. Theories of justice also consider how to distribute goods fairly in a society.
The document discusses the history of Roman literature in three periods. The first period from 240-80 BC saw the development of drama, epics, satire and prose influenced by Greek models. The golden age from 80 BC to AD 14 represented the highest excellence in prose and poetry, including famous writers like Cicero, Sallust and Caesar. The third period from AD 14 to 180 saw a decline with the loss of freedom and interest in public life, though literature remained popular among some. Major Roman and Greek gods and goddesses are also listed with brief descriptions of their domains.
The document discusses divide and conquer algorithms. It describes divide and conquer as a design strategy that involves dividing a problem into smaller subproblems, solving the subproblems recursively, and combining the solutions. It provides examples of divide and conquer algorithms like merge sort, quicksort, and binary search. Merge sort works by recursively sorting halves of an array until it is fully sorted. Quicksort selects a pivot element and partitions the array into subarrays of smaller and larger elements, recursively sorting the subarrays. Binary search recursively searches half-intervals of a sorted array to find a target value.
The document provides an overview and analysis of Homer's epic poem "The Iliad". It discusses why the poem is studied, defines the epic hero and how Achilles exemplifies this as the central hero of the work. It explores themes of glory, excellence, and the literary device of the "aristeia". Key sections analyze the definition of an aristeia and highlight important details from several books in "The Iliad", including the introduction of Achilles' rage in Book 1 and Helen's review of the champions in Book 3.
Linear search, also called sequential search, is a method for finding a target value within a list by sequentially checking each element until a match is found or all elements are searched. It works by iterating through each element of a data structure (such as an array or list), comparing it to the target value, and returning the index or position of the target if found. The key aspects covered include definitions of data, structure, data structure, and search. Pseudocode and examples of linear search on a phone directory are provided. Advantages are that it is simple and works for small data sets, while disadvantages are that search time increases linearly with the size of the data set.
This is a presentation on Arrays, one of the most important topics on Data Structures and algorithms. Anyone who is new to DSA or wants to have a theoretical understanding of the same can refer to it :D
The document discusses different types of lyric poetry including Italian or Petrarchan sonnets which are divided into an octave stanza and sextet stanza, Shakespearean or Spenserian sonnets which are divided into three quatrains and a couplet, elegies which express grief and death, odes which express noble feelings with no definite structure, psalms which praise God or Mary, awit songs which have 12 syllable measures and are sung to guitar, and corridos which have 8 syllable measures and are recited to a beat. The document was presented by Princess Adelane Gonzales and expresses gratitude at the end.
Insertion sort bubble sort selection sortUmmar Hayat
The document discusses three sorting algorithms: insertion sort, bubble sort, and selection sort. Insertion sort has best-case linear time but worst-case quadratic time, sorting elements in place. Bubble sort repeatedly compares and swaps adjacent elements, having quadratic time in all cases. Selection sort finds the minimum element and exchanges it into the sorted portion of the array in each pass, with quadratic time.
The document describes the quicksort algorithm. It covers the divide and conquer approach, partitioning the array around a pivot element, recursively sorting the subarrays, and combining the results. It provides pseudocode for quicksort and an example. It analyzes the time complexity in the best, worst, and average cases. The best case of O(n log n) occurs when the array is evenly partitioned on each recursive call. The worst case of O(n^2) happens when the partitioning produces very uneven subproblems. The average case is closer to the best case complexity of O(n log n).
The document discusses stacks and queues as data structures. It defines stacks as first-in last-out (LIFO) structures where elements are added and removed from one end of the list. Queues are defined as first-in first-out (FIFO) structures where elements are added to one end and removed from the other. The document provides examples of using stacks and queues in programming and describes the common operations that can be performed on each type of data structure.
A course certificate from University of Michigan and Coursera confirms that Abdullah Al Mamun successfully completed an online Python Data Structures course. The certificate, issued on April 24, 2016 and signed by Charles Severance, a Clinical Associate Professor at the University of Michigan's School of Information, verifies Al Mamun's identity and participation in the non-credit course through Coursera.
This document provides an overview of using Python for data structures and algorithms. It introduces Python, discusses using the Python interpreter, covers basic Python types like numbers, strings and lists, and shows control flow tools like if/else statements and for loops. It also demonstrates defining and calling functions. The goal is to provide attendees with an informal introduction to Python and prepare them for learning about data structures.
The document discusses C# strings and string manipulation. It begins by explaining that a C# string is a sequential collection of Unicode characters represented by a String object which is a collection of Char objects. It then covers key properties of strings like being immutable and reference types. The document also discusses string literals, common string operators, and methods to manipulate strings, as well as the StringBuilder class as a mutable alternative to strings.
This document provides an introduction to LaTeX in 30 minutes. It discusses when to use LaTeX over Microsoft Word, what is needed to get started with LaTeX, and provides examples of basic LaTeX commands. The document demonstrates how to write equations, include images, comment text, handle special characters, create references, and use BibTeX for managing references. It also briefly outlines creating documents like reports that include chapters and covers creating different types of documents like articles, books, and reports.
The document discusses arrays and ArrayLists in C#. It notes that arrays are indexed collections that derive from System.Array, allowing usage of its methods. Arrays are declared with syntax like type[] name and initialized with new. Multidimensional arrays can be declared with commas to specify dimensions. The document also introduces ArrayList as an alternative that dynamically resizes itself as needed.
The document discusses how to effectively read scientific papers. It outlines the typical structure of papers, which usually includes an abstract/summary, introduction, materials and methods, results, discussion, and references sections. The introduction section presents background information on the topic and states the hypothesis being tested. The materials and methods section should provide enough detail for others to replicate the study. Readers may need to refer back to this section while analyzing the results. Taking notes while reading helps retain information and allows breaks between sections.
This document is the preface to a book titled "Data Structures and Algorithms Made Easy" by Narasimha Karumanchi. It introduces the purpose and structure of the book. The book aims to help readers prepare for interviews and exams by focusing on solving problems of varying complexities for each data structures topic. It contains over 700 algorithm problems and their solutions. The preface recommends reading the entire book to fully understand the topics, and provides information on how to contact the author with any corrections or suggestions.
This document is the preface to a book titled "Data Structures and Algorithms Made Easy" by Narasimha Karumanchi. It introduces the objective of the book, which is to provide solutions to algorithmic problems with different complexities rather than focus on theorems and proofs. The book contains approximately 700 problems covering topics relevant for competitive exams and interviews. For each problem, multiple solutions with varying complexities are provided to illustrate different approaches. The preface recommends reading the entire book to gain a full understanding of the topics covered. It also notes that while efforts were made to correct errors, readers should check the author's website for any updates or corrections.
SALIENT FEATURES OF BOOK
Provides insight into what drives the recruitment process and what an interviewer looks for while interviewing an engineering student
Covers concepts, problems, and interview questions for each topic
Covers latest buzzwords like Cloud Computing, Virtualization, Big Data, and many more
All the concepts are discussed in a lucid, easy to understand manner
A reader without any basic knowledge in computers can comfortably follow this book
Coders/Programmers are in demand, but to land the job, you must demonstrate knowledge of those things expected by today's employers. This guide sets you up for success. Not only does it provide the most commonly asked interview questions and answers, but it also offers insight into the interview process in today's marketplace. This book is a comprehensive guide for experienced and first-time programmers alike.
The book is specifically designed for freshers, who despite being brilliant at the technical aspects of the interview, tend to fail when it comes to soft skills and HR interviews. The book provides readers with a relevant blueprint when it comes to planning for pre-interview preparation. It provides candidates with guidelines on the preparation of their resumes and the format that should be followed.
Table of Contents
Organization of Chapters 17
Getting Ready 22
Group Discussions 37
Operating System Concepts 54
C/C++/Java Interview Questions 81
Scripting Languages 157
Bitwise Hacking 194
Concepts of Computer Networking 203
Database Management Systems 256
Brain Teasers 271
Algorithms Introduction 274
Recursion and Backtracking 285
Linked Lists 290
Stacks 322
Queues 336
Trees 345
Priority Queues and Heaps 397
Graph Algorithms 407
Sorting 417
Searching 441
Hashing 466
String Algorithms 473
Algorithms Design Techniques 479
Greedy Algorithms 482
Divide and Conquer Algorithms 486
Dynamic Programming 489
Basics of Design Patterns 496
Non-Technical Help 505
Quantitative Aptitude Concepts 511
Basics of Cloud Computing 524
Miscellaneous Concepts 539
Career Options 559
Elements of Computer Networking: An Integrated Approach (Concepts, Problems a...CareerMonk Publications
Salient Features of Book:
All the concepts are discussed in a lucid, easy to understand manner.
A reader without any basic knowledge in computers can comfortably follow this book.
Helps to build logic in the students which becomes stepping stone for understanding computer networking protocols.
Interview questions collected from the actual interviews of various Software companies (and past compititive examinations like GATE) will help the students to be successful in their campus interviews.
Hundreds of solved problems help the students of various universities do well in their examinations like B.C.A, B.Sc, M.Sc, M.C.A, B.E, B.Tech, M.Tech, etc.
Works like a handy reference to the Software professionals.
Table of Contents (Chapters):
1) Organization of Chapters
2) Introduction
3) Networking Devices
4) OSI and TCP/IP Models
5) LAN Technologies
6) ARP and RARP
7) IP Addressing
8) Network Routing
9) TCP and UDP
10) TCP Error Control
11 )TCP Flow Control
12) TCP Congestion Control
13) Session layer
14) Presentation layer
15) Network Security
16) Application Layer Protocols
17) Miscellaneous Concepts
Data Structures and Algorithms Made Easy in Java ( PDFDrive ).pdfAbdurraufSharifaiGar
This document is the preface of a book titled "Data Structures and Algorithms Made Easy in Java" by Narasimha Karumanchi. It provides acknowledgements to various individuals who helped with the creation and editing of the book. It also contains a brief message from the author encouraging readers to fully read and understand the content of the book in order to help with interview preparation, competitive exams, and more. The book aims to teach data structures and algorithms through problems and solutions rather than just theory.
This document provides an overview of programming basics including:
- Variables are used to store and represent data in programs. Primitive data types define the kind of values variables can hold.
- Data structures are specialized formats for organizing and storing data to allow efficient manipulation. Common data structures include arrays, linked lists, stacks, queues, trees and graphs.
- Abstract data types combine data structures with their associated operations. Common ADTs include linked lists, stacks, queues, trees and graphs.
- Memory is organized as an array of bytes addressed by integers. Variables are allocated contiguous memory and identified by their address.
- Pointers are variables that store the address of other variables. They allow accessing and modifying the data at those
"Peeling Design Patterns: For Beginners and Interviews" by Narasimha Karumanchi and Prof. Sreenivasa Rao Meda is a book that presents design patterns in simple and straightforward manner with a clear-cut explanation. This book will provide an introduction to the basics and covers many real-time design interview questions. It comes handy as an interview and exam guide for computer scientists.
Salient Features of Book:
Readers without any background in software design will be able to understand it easily and completely.
Presents the concepts of design patterns in simple and straightforward manner with a clear-cut explanation.
After reading the book, readers will be in a position to come up with better designs than before and participate in design discussions which happen in their daily office work.
The book provides enough real-time examples so that readers get better understanding of the design patterns and also useful for the interviews. We mean, the book should cover design interview questions.
Table of Contents:
Introduction
UML Basics
Design Patterns Introduction
Creational Patterns
Structural Patterns
Behavioral Patterns
Glossary and Tips
Design Interview Questions
Miscellaneous Concepts
This document provides an introduction to data structures and algorithms. It discusses key concepts like variables, data types, data structures, abstract data types, algorithms, and analysis of algorithms. The goal of algorithm analysis is to compare algorithms in terms of their running time and space usage. Commonly used rates of growth for analyzing running time include constant, logarithmic, linear, quadratic, and exponential time. Algorithm analysis helps determine which solutions are most efficient.
This book provides an introduction to design patterns, which are common solutions to recurring problems in software design. The book aims to help readers learn design patterns to improve their skills and prepare for interviews. It covers fundamental design pattern concepts, categorizes patterns, and explains each pattern with examples. In addition, it includes tips, UML basics, and common design interview questions. The book recommends multiple readings to fully understand patterns and how they can be applied.
Data Structures and Algorithms For GATE: Solutions to all previous GATE quest...CareerMonk Publications
printf("The value of X is: %d\n", *ptr);
This dereferences the pointer and gives us the value stored at the address it points to, which is the value of .
So in summary:
- Pointers hold the address of other variables.
- We assign addresses to pointers using the address-of operator &.
- We access the value at the address a pointer points to using the indirection operator *.
002 One Page Essay What Is The Mla Format For EssAngela Hays
The motherboard connects the central processing unit (CPU) and other vital components like RAM, graphics card, and hard drives. It allows all the systems components to communicate to carry out tasks. Components like fans and the power supply connect to the motherboard to keep it and other parts cool while providing power to run the entire computer system.
Saltstack For DevOps
Extremely fast and simple IT automation and configuration managment
Through this book you will learn how to use one of the most powerful DevOps tools.
Compare And Contrast College And High School EsTony Lisko
This document provides instructions for creating an account and submitting an assignment request on the website HelpWriting.net. It outlines a 5-step process: 1) Create an account with an email and password. 2) Complete a form with assignment details, sources, and deadline. 3) Review bids from writers and choose one. 4) Review the completed paper and authorize payment. 5) Request revisions until satisfied. The website offers original, plagiarism-free assignments and allows customers to ensure their needs and expectations are met.
Rajeev provides guidance to an individual who wants to learn Theory of Constraints (TOC) on their own. He recommends starting with an eBook on basic TOC concepts, then learning about applications to manufacturing, distribution or projects through additional eBooks. Rajeev details eBook titles that explain each application area and provide step-by-step implementation guidance. He offers bundles that provide discounts and presents a learning roadmap. Rajeev notes some eBooks are not comprehensive and recommends additional resources like books, videos and software for more extensive learning.
1. Library staff are required to fill out a form for every patron interaction, selecting from drop-down options for patron type, question type, assistance provided, and whether to include the question in the public knowledge base.
2. Question types include general library questions, print/copy/scan, log-in help, basic or advanced research, citation help, and technology help. Staff must provide details for research and other questions.
3. Assistance options specify if the question was answered alone, with a librarian or other staff, if the patron was referred to a librarian, or circulation desk.
4. The form automatically links
Chapter10InstructionsGoals· Analyze your audience’s expect.docxchristinemaritza
Chapter10
Instructions
Goals
· Analyze your audience’s expectations and the steps required for instructions
· Determine an appropriate format for instructions
· Prepare a clear, concise set of instructions
Terms
· cautions, p. 233
· concurrent testing, p. 241
· explanation, p. 239
· field-test, p. 241
· imperative mood, p. 237
· instructions, p. 231
· online instructions, p. 243
· retrospective testing, p. 242
· step, p. 237
· warnings, p. 233
WRITE TO LEARN
Think of the last time you followed instructions. Maybe the instructions were from an instructor, your parent or guardian, your employer, a textbook, or an online help manual. In a short journal entry, answer these questions: Under what circumstances did you read the instructions? Were the instructions easy or difficult to follow? Explain. How did you handle any problems that resulted from poorly written instructions? If graphics were included, what kind were they? Were they helpful? Why or why not?
Focus on Instructions
Read the sample instructions on the next page and answer these questions:
· How does the writer make it clear what to do first, second, and so on?
· How many actions does each step represent?
· How does each step begin?
· Under “Charge the Battery,” besides telling the reader what to do, what other information is included?
· What is the purpose of the two boxed notes, the graphics, and the two headings?
What If?
· The user had previously owned a BlackBerry® phone?
· The user had never owned a cell phone?
· The user was not a strong native speaker of English?
· The instructions were spoken rather than written?
Sample Instructions
Adapted from BlackBerry® User Guide. Reprinted with permission of BlackBerry®.
[email protected]
Aaron Wartner is an application and database administrator for Accenture, an IT consulting firm in Cincinnati, Ohio. In his role, he is responsible for maintaining a digital planning system used by Accenture’s customers.
Courtesy of Stephen Freas
Aaron’s job creates a link between customers who use Accenture’s technology and the technology itself. He has the tricky task of communicating with both his “techy” colleagues and with clients. “Much of my time is spent e-mailing information to or requesting action from my coworkers. I also write instructions for customers’ scheduled maintenance activities that describe how to perform the work, how to coordinate with other teams, and the timing for each action.”
Writing technical instructions that are easy to follow is not an easy task. “Instructions must be interpreted the same way by every reader to be successful,” says Aaron. “So they must be understandable, and the intent must be clear. Consistency in describing objects and activities and careful use of technical jargon are a few ways I try to meet these goals.”
Aaron begins writing a set of instructions for a certain task by doing that task himself. “I start by walking through the process to understand the flow of steps and to make sure that what I am ...
Tips on answering cv based interview questions [ https://learningpundits.com/module-view/2-cv-based-interview-questions/1-tips-for-answering-cv-based-interview-questions/ ]. LearningPundits helps Job Seekers make great CVs [ https://learningpundits.com/module-view/1-cv-preparation-for-freshers/1-cv-writing-tips-for-freshers/ ] , master English Grammar and Vocabulary [ https://learningpundits.com/course/4-english-grammar/ ] , ace Aptitude Tests [ https://learningpundits.com/course/11-mathematical-aptitude/ ], speak fluently in a Group Discussion [ https://learningpundits.com/module-view/6-group-discussion-questions/1-tips-for-speaking-in-a-group-discussion/ ] and perform well in Interviews [ https://learningpundits.com/course/2-personal-interview/ ] We also conduct weekly online contests on Aptitude and English [ https://learningpundits.com/contest ]. We also allow Job Seekers to apply for Jobs [ https://learningpundits.com/applyForJobs ]
Success key book for:
* Campus Preparation
* Degree/Masters Course Preparation
* Instructor’s
* GATE Preparation
* Big job hunters: Microsoft, Google, Amazon, Yahoo, Flip Kart, Adobe, IBM Labs, Citrix, Mentor Graphics, NetApp, Oracle, Webaroo, De-Shaw, Success Factors, Face book, McAfee and many more
* Reference Manual for working people
This document provides instructions and guidance for students completing a group project on mathematical concepts. Students will be assigned to groups and choose between researching the Pythagorean theorem, quadratic equation, or pi. Each group member is responsible for one section - past, present, future, or example. They will create a PowerPoint presentation covering the history, current applications, future implications, and worked example of their assigned topic. The document provides links and questions to guide their research for the project.
This document provides information about the book "Peeling Design Patterns" by Narasimha Karumanchi. The book covers design patterns in Java and is intended for beginners and interviews. It does not require any prior software design experience. The book also serves as a reference for professionals and can help with degree or masters course preparation. The author Narasimha Karumanchi has a M.Tech from IIT Bombay and works as a Senior Software Developer at Amazon. The book is published by CareerMonk Publications, which is directed by Prof. Meda Sreenivasa Rao from JNTU.
Narasimha Karumanchi is the founder of CareerMonk and has authored several books on data structures, algorithms, and design patterns. He has worked for major tech companies like Amazon, IBM, Microsoft. He holds an M.Tech from IIT Bombay and has taught data structures and algorithms. This book covers common interview questions on data structures and algorithms and provides solutions to problems in C. It is suitable for students and professionals preparing for technical interviews.
Narasimha Karumanchi is a founder of CareerMonk and author of several books on data structures, algorithms, and design patterns. He has worked for major tech companies like Amazon, IBM, Microsoft. He holds an M.Tech from IIT Bombay and has taught data structures and algorithms. His books cover interview questions and have been translated internationally. They are used as references in 58 universities and help prepare students and professionals for interviews and exams.
Table of contents [data structure and algorithmic thinking with python]CareerMonk Publications
This document is the preface of a book on data structures and algorithms by Narasimha Karumanchi. It introduces the book's objectives of helping readers learn fundamental data structures and algorithms through practice problems of varying complexities. The preface emphasizes understanding problem-solving over theory and encourages readers to think of multiple solutions to problems. It also acknowledges those who helped with the book's creation and provides an overview of the book's organization into chapters covering topics like recursion, linked lists, trees and sorting algorithms.
Salient Features of Book:
All the concepts are discussed in a lucid, easy to understand manner.
A reader without any basic knowledge in computers can comfortably follow this book.
Helps to build logic in the students which becomes stepping stone for understanding computer networking protocols.
Interview questions collected from the actual interviews of various Software companies (and past competitive examinations like GATE) will help the students to be successful in their campus interviews.
Hundreds of solved problems help the students of various universities do well in their examinations like B.C.A, B.Sc, M.Sc, M.C.A, B.E, B.Tech, M.Tech, etc.
Works like a handy reference to the Software professionals.
All the concepts are discussed in a lucid, easy to understand manner.
A reader without any basic knowledge in computer s can comfortably follow this book.
Helps to build logic in the students which becomes stepping stone for programming.
Interview questions collected from the actual interviews of various Software companies will help the students to be successful in their campus interviews.
Hundreds of solved programs help the students do well in their examinations like B.C.A, B.Sc, M.Sc, M.C.A, B.E, B.Tech, M.Tech, etc.
Works like a handy reference to the Software professionals in their programming.
Starting at basic level, this book covers advanced topics like Pointers, Data structures, Searching and sorting techniques and Graphics.
A workshop hosted by the South African Journal of Science aimed at postgraduate students and early career researchers with little or no experience in writing and publishing journal articles.
বাংলাদেশের অর্থনৈতিক সমীক্ষা ২০২৪ [Bangladesh Economic Review 2024 Bangla.pdf] কম্পিউটার , ট্যাব ও স্মার্ট ফোন ভার্সন সহ সম্পূর্ণ বাংলা ই-বুক বা pdf বই " সুচিপত্র ...বুকমার্ক মেনু 🔖 ও হাইপার লিংক মেনু 📝👆 যুক্ত ..
আমাদের সবার জন্য খুব খুব গুরুত্বপূর্ণ একটি বই ..বিসিএস, ব্যাংক, ইউনিভার্সিটি ভর্তি ও যে কোন প্রতিযোগিতা মূলক পরীক্ষার জন্য এর খুব ইম্পরট্যান্ট একটি বিষয় ...তাছাড়া বাংলাদেশের সাম্প্রতিক যে কোন ডাটা বা তথ্য এই বইতে পাবেন ...
তাই একজন নাগরিক হিসাবে এই তথ্য গুলো আপনার জানা প্রয়োজন ...।
বিসিএস ও ব্যাংক এর লিখিত পরীক্ষা ...+এছাড়া মাধ্যমিক ও উচ্চমাধ্যমিকের স্টুডেন্টদের জন্য অনেক কাজে আসবে ...
How to Build a Module in Odoo 17 Using the Scaffold MethodCeline George
Odoo provides an option for creating a module by using a single line command. By using this command the user can make a whole structure of a module. It is very easy for a beginner to make a module. There is no need to make each file manually. This slide will show how to create a module using the scaffold method.
How to Setup Warehouse & Location in Odoo 17 InventoryCeline George
In this slide, we'll explore how to set up warehouses and locations in Odoo 17 Inventory. This will help us manage our stock effectively, track inventory levels, and streamline warehouse operations.
The simplified electron and muon model, Oscillating Spacetime: The Foundation...RitikBhardwaj56
Discover the Simplified Electron and Muon Model: A New Wave-Based Approach to Understanding Particles delves into a groundbreaking theory that presents electrons and muons as rotating soliton waves within oscillating spacetime. Geared towards students, researchers, and science buffs, this book breaks down complex ideas into simple explanations. It covers topics such as electron waves, temporal dynamics, and the implications of this model on particle physics. With clear illustrations and easy-to-follow explanations, readers will gain a new outlook on the universe's fundamental nature.
it describes the bony anatomy including the femoral head , acetabulum, labrum . also discusses the capsule , ligaments . muscle that act on the hip joint and the range of motion are outlined. factors affecting hip joint stability and weight transmission through the joint are summarized.
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPRAHUL
This Dissertation explores the particular circumstances of Mirzapur, a region located in the
core of India. Mirzapur, with its varied terrains and abundant biodiversity, offers an optimal
environment for investigating the changes in vegetation cover dynamics. Our study utilizes
advanced technologies such as GIS (Geographic Information Systems) and Remote sensing to
analyze the transformations that have taken place over the course of a decade.
The complex relationship between human activities and the environment has been the focus
of extensive research and worry. As the global community grapples with swift urbanization,
population expansion, and economic progress, the effects on natural ecosystems are becoming
more evident. A crucial element of this impact is the alteration of vegetation cover, which plays a
significant role in maintaining the ecological equilibrium of our planet.Land serves as the foundation for all human activities and provides the necessary materials for
these activities. As the most crucial natural resource, its utilization by humans results in different
'Land uses,' which are determined by both human activities and the physical characteristics of the
land.
The utilization of land is impacted by human needs and environmental factors. In countries
like India, rapid population growth and the emphasis on extensive resource exploitation can lead
to significant land degradation, adversely affecting the region's land cover.
Therefore, human intervention has significantly influenced land use patterns over many
centuries, evolving its structure over time and space. In the present era, these changes have
accelerated due to factors such as agriculture and urbanization. Information regarding land use and
cover is essential for various planning and management tasks related to the Earth's surface,
providing crucial environmental data for scientific, resource management, policy purposes, and
diverse human activities.
Accurate understanding of land use and cover is imperative for the development planning
of any area. Consequently, a wide range of professionals, including earth system scientists, land
and water managers, and urban planners, are interested in obtaining data on land use and cover
changes, conversion trends, and other related patterns. The spatial dimensions of land use and
cover support policymakers and scientists in making well-informed decisions, as alterations in
these patterns indicate shifts in economic and social conditions. Monitoring such changes with the
help of Advanced technologies like Remote Sensing and Geographic Information Systems is
crucial for coordinated efforts across different administrative levels. Advanced technologies like
Remote Sensing and Geographic Information Systems
9
Changes in vegetation cover refer to variations in the distribution, composition, and overall
structure of plant communities across different temporal and spatial scales. These changes can
occur natural.
This slide is special for master students (MIBS & MIFB) in UUM. Also useful for readers who are interested in the topic of contemporary Islamic banking.
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...PECB
Denis is a dynamic and results-driven Chief Information Officer (CIO) with a distinguished career spanning information systems analysis and technical project management. With a proven track record of spearheading the design and delivery of cutting-edge Information Management solutions, he has consistently elevated business operations, streamlined reporting functions, and maximized process efficiency.
Certified as an ISO/IEC 27001: Information Security Management Systems (ISMS) Lead Implementer, Data Protection Officer, and Cyber Risks Analyst, Denis brings a heightened focus on data security, privacy, and cyber resilience to every endeavor.
His expertise extends across a diverse spectrum of reporting, database, and web development applications, underpinned by an exceptional grasp of data storage and virtualization technologies. His proficiency in application testing, database administration, and data cleansing ensures seamless execution of complex projects.
What sets Denis apart is his comprehensive understanding of Business and Systems Analysis technologies, honed through involvement in all phases of the Software Development Lifecycle (SDLC). From meticulous requirements gathering to precise analysis, innovative design, rigorous development, thorough testing, and successful implementation, he has consistently delivered exceptional results.
Throughout his career, he has taken on multifaceted roles, from leading technical project management teams to owning solutions that drive operational excellence. His conscientious and proactive approach is unwavering, whether he is working independently or collaboratively within a team. His ability to connect with colleagues on a personal level underscores his commitment to fostering a harmonious and productive workplace environment.
Date: May 29, 2024
Tags: Information Security, ISO/IEC 27001, ISO/IEC 42001, Artificial Intelligence, GDPR
-------------------------------------------------------------------------------
Find out more about ISO training and certification services
Training: ISO/IEC 27001 Information Security Management System - EN | PECB
ISO/IEC 42001 Artificial Intelligence Management System - EN | PECB
General Data Protection Regulation (GDPR) - Training Courses - EN | PECB
Webinars: https://pecb.com/webinars
Article: https://pecb.com/article
-------------------------------------------------------------------------------
For more information about PECB:
Website: https://pecb.com/
LinkedIn: https://www.linkedin.com/company/pecb/
Facebook: https://www.facebook.com/PECBInternational/
Slideshare: http://www.slideshare.net/PECBCERTIFICATION
A review of the growth of the Israel Genealogy Research Association Database Collection for the last 12 months. Our collection is now passed the 3 million mark and still growing. See which archives have contributed the most. See the different types of records we have, and which years have had records added. You can also see what we have for the future.
Main Java[All of the Base Concepts}.docxadhitya5119
This is part 1 of my Java Learning Journey. This Contains Custom methods, classes, constructors, packages, multithreading , try- catch block, finally block and more.
3. Acknowledgements
𝑀𝑜𝑡ℎ𝑒𝑟 and 𝑓𝑎𝑡ℎ𝑒𝑟, it is impossible to thank you adequately for everything you have done, from loving me
unconditionally to raising me in a stable household, where you persistent efforts traditional values and 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
many people who saw me through this book, to all those 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.
𝑀𝑜ℎ𝑎𝑛 𝑀𝑢𝑙𝑙𝑎𝑝𝑢𝑑𝑖, IIT Bombay, Architect, dataRPM Pvt. Ltd.
𝑁𝑎𝑣𝑖𝑛 𝐾𝑢𝑚𝑎𝑟 𝐽𝑎𝑖𝑠𝑤𝑎𝑙, Senior Consultant, Juniper Networks Inc.
𝐴. 𝑉𝑎𝑚𝑠ℎ𝑖 𝐾𝑟𝑖𝑠ℎ𝑛𝑎, IIT Kanpur, Mentor Graphics Inc.
𝐾𝑜𝑛𝑑𝑟𝑎𝑘𝑢𝑛𝑡𝑎 𝑀𝑢𝑟𝑎𝑙𝑖 𝐾𝑟𝑖𝑠ℎ𝑛𝑎, B-Tech., Technical Lead, HCL
𝑃𝑟𝑜𝑓. 𝐺𝑖𝑟𝑖𝑠ℎ 𝑃. 𝑆𝑎𝑟𝑎𝑝ℎ, 𝐹𝑜𝑢𝑛𝑑𝑒𝑟, 𝑉𝑒𝑔𝑎𝑦𝑎𝑛 𝑆𝑦𝑠𝑡𝑒𝑚𝑠, 𝐼𝐼𝑇 𝐵𝑜𝑚𝑏𝑎𝑦
𝐾𝑖𝑠ℎ𝑜𝑟𝑒 𝐾𝑢𝑚𝑎𝑟 𝐽𝑖𝑛𝑘𝑎, IIT Bombay
𝑃𝑟𝑜𝑓. 𝐻𝑠𝑖𝑛 − 𝑚𝑢 𝑇𝑠𝑎𝑖, 𝑁𝑎𝑡𝑖𝑜𝑛𝑎𝑙 𝑇𝑎𝑖𝑤𝑎𝑛 𝑈𝑛𝑖𝑣𝑒𝑟𝑠𝑖𝑡𝑦, 𝑇𝑎𝑖𝑤𝑎𝑛
𝑃𝑟𝑜𝑓. 𝐶ℎ𝑖𝑛𝑡𝑎𝑝𝑎𝑙𝑙𝑖 𝑆𝑜𝑏ℎ𝑎𝑛 𝐵𝑎𝑏𝑢. 𝐼𝐼𝑇, 𝐻𝑦𝑑𝑒𝑟𝑎𝑏𝑎𝑑
𝑃𝑟𝑜𝑓. 𝑀𝑒𝑑𝑎 𝑆𝑟𝑒𝑒𝑛𝑖𝑣𝑎𝑠𝑎 𝑅𝑎𝑜, 𝐽𝑁𝑇𝑈, 𝐻𝑦𝑑𝑒𝑟𝑎𝑏𝑎𝑑
Last but not least, I would like to thank 𝐷𝑖𝑟𝑒𝑐𝑡𝑜𝑟𝑠 of 𝐺𝑢𝑛𝑡𝑢𝑟 𝑉𝑖𝑘𝑎𝑠 𝐶𝑜𝑙𝑙𝑒𝑔𝑒, 𝑃𝑟𝑜𝑓. 𝑌. 𝑉. 𝐺𝑜𝑝𝑎𝑙𝑎 𝐾𝑟𝑖𝑠ℎ𝑛𝑎 𝑀𝑢𝑟𝑡ℎ𝑦 &
𝑃𝑟𝑜𝑓. 𝐴𝑦𝑢𝑏 𝐾ℎ𝑎𝑛 [𝐴𝐶𝐸 𝐸𝑛𝑔𝑖𝑛𝑒𝑒𝑟𝑖𝑛𝑔 𝐴𝑐𝑎𝑑𝑒𝑚𝑦], 𝑇. 𝑅. 𝐶. 𝐵𝑜𝑠𝑒 [𝐸𝑥. 𝐷𝑖𝑟𝑒𝑐𝑡𝑜𝑟 of 𝐴𝑃𝑇𝑟𝑎𝑛𝑠𝑐𝑜], 𝐶ℎ. 𝑉𝑒𝑛𝑘𝑎𝑡𝑒𝑠𝑤𝑎𝑟𝑎 𝑅𝑎𝑜 𝑉𝑁𝑅
𝑉𝑖𝑔𝑛𝑎𝑛𝑎𝑗𝑦𝑜𝑡ℎ𝑖 [𝐸𝑛𝑔𝑖𝑛𝑒𝑒𝑟𝑖𝑛𝑔 𝐶𝑜𝑙𝑙𝑒𝑔𝑒, 𝐻𝑦𝑑𝑒𝑟𝑎𝑏𝑎𝑑], 𝐶ℎ. 𝑉𝑒𝑛𝑘𝑎𝑡𝑎 𝑁𝑎𝑟𝑎𝑠𝑎𝑖𝑎ℎ [𝐼𝑃𝑆], 𝑌𝑎𝑟𝑎𝑝𝑎𝑡ℎ𝑖𝑛𝑒𝑛𝑖 𝐿𝑎𝑘𝑠ℎ𝑚𝑎𝑖𝑎ℎ
[𝑀𝑎𝑛𝑐ℎ𝑖𝑘𝑎𝑙𝑙𝑢, 𝐺𝑢𝑟𝑎𝑧𝑎𝑙𝑎] & 𝑎𝑙𝑙 𝑜𝑢𝑟 𝑤𝑒𝑙𝑙 − 𝑤𝑖𝑠ℎ𝑒𝑟𝑠 for helping me and my family during our studies.
−𝑁𝑎𝑟𝑎𝑠𝑖𝑚ℎ𝑎 𝐾𝑎𝑟𝑢𝑚𝑎𝑛𝑐ℎ𝑖
M-Tech, 𝐼𝐼𝑇 𝐵𝑜𝑚𝑏𝑎𝑦
Founder, 𝐶𝑎𝑟𝑒𝑒𝑟𝑀𝑜𝑛𝑘. 𝑐𝑜𝑚
4.
5. Preface
Dear Reader,
Please Hold on! I know many people do not read the preface. But I would strongly recommend that you go
through the preface of this book at least. The reason for this is that this preface has 𝑠𝑜𝑚𝑒𝑡ℎ𝑖𝑛𝑔 𝑑𝑖𝑓𝑓𝑒𝑟𝑒𝑛𝑡 to offer.
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.
The main objective of the book is not to give you the theorems and proofs about 𝐷𝑎𝑡𝑎 𝑆𝑡𝑟𝑢𝑐𝑡𝑢𝑟𝑒𝑠 and 𝐴𝑙𝑔𝑜𝑟𝑖𝑡ℎ𝑚𝑠.
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 all possible
solutions for a given problem. This book is very useful for interview preparation, competitive exams preparation,
and campus interview preparations.
In all the chapters you will see more importance given to problems and analyzing them instead of concentrating
more on theory. For each chapter, first you will see the basic required theory and then followed by problems.
For many problems, 𝑚𝑢𝑙𝑡𝑖𝑝𝑙𝑒 solutions are provided with different levels of complexities. We start with
𝑏𝑟𝑢𝑡𝑒 𝑓𝑜𝑟𝑐𝑒 solution and slowly move towards the 𝑏𝑒𝑠𝑡 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛 possible for that problem. For each problem we
will try to understand how much time the algorithm is taking and how much memory the algorithm is taking.
It is 𝑟𝑒𝑐𝑜𝑚𝑚𝑒𝑛𝑑𝑒𝑑 that the reader does at least one complete reading of this book to get full understanding of all
the topics. In the subsequent readings, you can go directly to any chapter and refer. Even though, enough
readings were given for correcting the errors, there could be some minor typos in the book. If any such typos are
found, they will be updated at 𝐶𝑎𝑟𝑒𝑒𝑟𝑀𝑜𝑛𝑘. 𝑐𝑜𝑚. I request you to constantly monitor this site for any corrections,
new problems and solutions. Also, please provide your valuable suggestions at: 𝐼𝑛𝑓𝑜@𝐶𝑎𝑟𝑒𝑒𝑟𝑀𝑜𝑛𝑘. 𝑐𝑜𝑚.
Wish you all the best. I am sure that you will find this book useful.
−𝑁𝑎𝑟𝑎𝑠𝑖𝑚ℎ𝑎 𝐾𝑎𝑟𝑢𝑚𝑎𝑛𝑐ℎ𝑖
M-Tech, 𝐼𝐼𝑇 𝐵𝑜𝑚𝑏𝑎𝑦
Founder, 𝐶𝑎𝑟𝑒𝑒𝑟𝑀𝑜𝑛𝑘. 𝑐𝑜𝑚
6. Other Titles by Narasimha Karumanchi
Data Structures and Algorithms Made Easy
IT Interview Questions
Data Structures and Algorithms for GATE
Data Structures and Algorithms Made Easy in Java
Coding Interview Questions
Peeling Design Patterns
Elements of Computer Networking
7. Table of Contents
0. Organization of Chapters --------------------------------------------------------------------13
0.1 What Is this Book About?-------------------------------------------------------------------------------- 13
0.2 Should I Take this Book? -------------------------------------------------------------------------------- 13
0.3 Organization of Chapters -------------------------------------------------------------------------------- 14
0.4 Some Prerequisites --------------------------------------------------------------------------------------- 17
1. Introduction------------------------------------------------------------------------------------18
1.1 Variables --------------------------------------------------------------------------------------------------- 18
1.2 Data types-------------------------------------------------------------------------------------------------- 18
1.3 Data Structures ------------------------------------------------------------------------------------------- 19
1.4 Abstract Data Types (ADTs) ----------------------------------------------------------------------------- 19
1.5 What is an Algorithm? ----------------------------------------------------------------------------------- 19
1.6 Why Analysis of Algorithms? ---------------------------------------------------------------------------- 20
1.7 Goal of Analysis of Algorithms -------------------------------------------------------------------------- 20
1.8 What is Running Time Analysis? ----------------------------------------------------------------------- 20
1.9 How to Compare Algorithms? --------------------------------------------------------------------------- 20
1.10 What is Rate of Growth? ------------------------------------------------------------------------------- 20
1.11 Commonly used Rate of Growths --------------------------------------------------------------------- 21
1.12 Types of Analysis ---------------------------------------------------------------------------------------- 22
1.13 Asymptotic Notation ------------------------------------------------------------------------------------ 22
1.14 Big-O Notation ------------------------------------------------------------------------------------------- 22
1.15 Omega-Ω Notation--------------------------------------------------------------------------------------- 24
1.16 Theta- Notation ---------------------------------------------------------------------------------------- 24
1.17 Why is it called Asymptotic Analysis?---------------------------------------------------------------- 25
1.18 Guidelines for Asymptotic Analysis------------------------------------------------------------------- 25
1.19 Properties of Notations --------------------------------------------------------------------------------- 27
1.20 Commonly used Logarithms and Summations ----------------------------------------------------- 27
1.21 Master Theorem for Divide and Conquer ------------------------------------------------------------ 27
1.22 Problems on Divide and Conquer Master Theorem ------------------------------------------------ 28
1.23 Master Theorem for Subtract and Conquer Recurrences ----------------------------------------- 29
1.24 Variant of subtraction and conquer master theorem ---------------------------------------------- 29
1.25 Method of Guessing and Confirm--------------------------------------------------------------------- 29
1.26 Amortized Analysis -------------------------------------------------------------------------------------- 30
1.27 Problems on Algorithms Analysis --------------------------------------------------------------------- 31
2. Recursion and Backtracking ---------------------------------------------------------------- 42
2.1 Introduction------------------------------------------------------------------------------------------------ 42
2.2 What is Recursion? --------------------------------------------------------------------------------------- 42
2.3 Why Recursion? ------------------------------------------------------------------------------------------- 42
2.4 Format of a Recursive Function ------------------------------------------------------------------------ 42
2.5 Recursion and Memory (Visualization) ---------------------------------------------------------------- 43
8. 2.6 Recursion versus Iteration ------------------------------------------------------------------------------ 43
2.7 Notes on Recursion --------------------------------------------------------------------------------------- 44
2.8 Example Algorithms of Recursion ---------------------------------------------------------------------- 44
2.9 Problems on Recursion----------------------------------------------------------------------------------- 44
2.10 What is Backtracking?---------------------------------------------------------------------------------- 45
2.11 Example Algorithms of Backtracking ---------------------------------------------------------------- 45
2.12 Problems on Backtracking ----------------------------------------------------------------------------- 45
3. Linked Lists------------------------------------------------------------------------------------ 48
3.1 What is a Linked List?------------------------------------------------------------------------------------ 48
3.2 Linked Lists ADT------------------------------------------------------------------------------------------ 48
3.3 Why Linked Lists? ---------------------------------------------------------------------------------------- 48
3.4 Arrays Overview ------------------------------------------------------------------------------------------- 48
3.5 Comparison of Linked Lists with Arrays and Dynamic Arrays------------------------------------- 50
3.6 Singly Linked Lists---------------------------------------------------------------------------------------- 50
3.7 Doubly Linked Lists -------------------------------------------------------------------------------------- 56
3.8 Circular Linked Lists ------------------------------------------------------------------------------------- 61
3.9 A Memory-Efficient Doubly Linked List --------------------------------------------------------------- 67
3.10 Unrolled Linked Lists ----------------------------------------------------------------------------------- 68
3.11 Skip Lists ------------------------------------------------------------------------------------------------- 72
3.12 Problems on Linked Lists ------------------------------------------------------------------------------ 75
4. Stacks ------------------------------------------------------------------------------------------ 96
4.1 What is a Stack? ------------------------------------------------------------------------------------------ 96
4.2 How Stacks are used?------------------------------------------------------------------------------------ 96
4.3 Stack ADT-------------------------------------------------------------------------------------------------- 97
4.4 Applications------------------------------------------------------------------------------------------------ 97
4.5 Implementation-------------------------------------------------------------------------------------------- 97
4.6 Comparison of Implementations-----------------------------------------------------------------------101
4.7 Problems on Stacks--------------------------------------------------------------------------------------102
5. Queues --------------------------------------------------------------------------------------- 119
5.1 What is a Queue? ----------------------------------------------------------------------------------------119
5.2 How are Queues Used? ---------------------------------------------------------------------------------119
5.3 Queue ADT------------------------------------------------------------------------------------------------119
5.4 Exceptions ------------------------------------------------------------------------------------------------120
5.5 Applications-----------------------------------------------------------------------------------------------120
5.6 Implementation-------------------------------------------------------------------------------------------120
5.7 Problems on Queues-------------------------------------------------------------------------------------125
6. Trees ------------------------------------------------------------------------------------------ 135
6.1 What is a Tree?-------------------------------------------------------------------------------------------135
6.2 Glossary ---------------------------------------------------------------------------------------------------135
6.3 Binary Trees ----------------------------------------------------------------------------------------------136
6.4 Types of Binary Trees -----------------------------------------------------------------------------------137
6.5 Properties of Binary Trees ------------------------------------------------------------------------------137
9. 6.6 Binary Tree Traversals ----------------------------------------------------------------------------------139
6.7 Generic Trees (N-ary Trees)-----------------------------------------------------------------------------159
6.8 Threaded Binary Tree Traversals [Stack or Queue less Traversals]------------------------------166
6.9 Expression Trees -----------------------------------------------------------------------------------------171
6.10 XOR Trees -----------------------------------------------------------------------------------------------174
6.11 Binary Search Trees (BSTs) ---------------------------------------------------------------------------174
6.12 Balanced Binary Search Trees -----------------------------------------------------------------------189
6.13 AVL (Adelson-Velskii and Landis) Trees ------------------------------------------------------------189
6.14 Other Variations in Trees -----------------------------------------------------------------------------206
7. Priority Queues and Heaps---------------------------------------------------------------- 211
7.1 What is a Priority Queue? ------------------------------------------------------------------------------211
7.2 Priority Queue ADT --------------------------------------------------------------------------------------211
7.3 Priority Queue Applications ----------------------------------------------------------------------------212
7.4 Priority Queue Implementations-----------------------------------------------------------------------212
7.5 Heaps and Binary Heap---------------------------------------------------------------------------------213
7.6 Binary Heaps ---------------------------------------------------------------------------------------------214
7.7 Heapsort---------------------------------------------------------------------------------------------------218
7.8 Problems on Priority Queues [Heaps] -----------------------------------------------------------------219
8. Disjoint Sets ADT --------------------------------------------------------------------------- 232
8.1 Introduction-----------------------------------------------------------------------------------------------232
8.2 Equivalence Relations and Equivalence Classes----------------------------------------------------232
8.3 Disjoint Sets ADT ----------------------------------------------------------------------------------------233
8.4 Applications-----------------------------------------------------------------------------------------------233
8.5 Tradeoffs in Implementing Disjoint Sets ADT -------------------------------------------------------233
8.8 Fast UNION implementation (Slow FIND) ------------------------------------------------------------234
8.9 Fast UNION implementations (Quick FIND) ---------------------------------------------------------237
8.10 Summary ------------------------------------------------------------------------------------------------239
8.11 Problems on Disjoint Sets-----------------------------------------------------------------------------239
9. Graph Algorithms --------------------------------------------------------------------------- 241
9.1 Introduction-----------------------------------------------------------------------------------------------241
9.2 Glossary ---------------------------------------------------------------------------------------------------241
9.3 Applications of Graphs----------------------------------------------------------------------------------244
9.4 Graph Representation-----------------------------------------------------------------------------------244
9.5 Graph Traversals-----------------------------------------------------------------------------------------249
9.6 Topological Sort ------------------------------------------------------------------------------------------255
9.7 Shortest Path Algorithms -------------------------------------------------------------------------------257
9.8 Minimal Spanning Tree ---------------------------------------------------------------------------------262
9.9 Problems on Graph Algorithms ------------------------------------------------------------------------266
10.Sorting ---------------------------------------------------------------------------------------- 286
10.1 What is Sorting? ----------------------------------------------------------------------------------------286
10.2 Why is Sorting necessary? ----------------------------------------------------------------------------286
10.3 Classification of Sorting Algorithms -----------------------------------------------------------------286
10. 10.4 Other Classifications-----------------------------------------------------------------------------------287
10.5 Bubble sort ----------------------------------------------------------------------------------------------287
10.6 Selection Sort -------------------------------------------------------------------------------------------288
10.7 Insertion sort--------------------------------------------------------------------------------------------289
10.8 Shell sort-------------------------------------------------------------------------------------------------290
10.9 Merge sort -----------------------------------------------------------------------------------------------291
10.10 Heapsort------------------------------------------------------------------------------------------------293
10.11 Quicksort-----------------------------------------------------------------------------------------------293
10.12 Tree Sort------------------------------------------------------------------------------------------------295
10.13 Comparison of Sorting Algorithms -----------------------------------------------------------------295
10.14 Linear Sorting Algorithms ---------------------------------------------------------------------------296
10.15 Counting Sort------------------------------------------------------------------------------------------296
10.16 Bucket sort [or Bin Sort]-----------------------------------------------------------------------------296
10.17 Radix sort ----------------------------------------------------------------------------------------------297
10.18 Topological Sort ---------------------------------------------------------------------------------------298
10.19 External Sorting---------------------------------------------------------------------------------------298
10.20 Problems on Sorting ----------------------------------------------------------------------------------299
11.Searching ------------------------------------------------------------------------------------ 309
11.1 What is Searching?-------------------------------------------------------------------------------------309
11.2 Why do we need Searching? --------------------------------------------------------------------------309
11.3 Types of Searching -------------------------------------------------------------------------------------309
11.4 Unordered Linear Search------------------------------------------------------------------------------309
11.5 Sorted/Ordered Linear Search -----------------------------------------------------------------------310
11.6 Binary Search-------------------------------------------------------------------------------------------310
11.7 Comparing Basic Searching Algorithms ------------------------------------------------------------311
11.8 Symbol Tables and Hashing --------------------------------------------------------------------------311
11.9 String Searching Algorithms--------------------------------------------------------------------------311
11.10 Problems on Searching-------------------------------------------------------------------------------311
12.Selection Algorithms [Medians] ----------------------------------------------------------- 333
12.1 What are Selection Algorithms? ----------------------------------------------------------------------333
12.2 Selection by Sorting------------------------------------------------------------------------------------333
12.3 Partition-based Selection Algorithm -----------------------------------------------------------------333
12.4 Linear Selection algorithm - Median of Medians algorithm--------------------------------------333
12.5 Finding the K Smallest Elements in Sorted Order ------------------------------------------------334
12.6 Problems on Selection Algorithms -------------------------------------------------------------------334
13.Symbol Tables ------------------------------------------------------------------------------- 343
13.1 Introduction ---------------------------------------------------------------------------------------------343
13.2 What are Symbol Tables? -----------------------------------------------------------------------------343
13.3 Symbol Table Implementations ----------------------------------------------------------------------343
13.4 Comparison of Symbol Table Implementations ----------------------------------------------------344
14.Hashing--------------------------------------------------------------------------------------- 345
14.1 What is Hashing?---------------------------------------------------------------------------------------345
11. 14.2 Why Hashing?-------------------------------------------------------------------------------------------345
14.3 HashTable ADT -----------------------------------------------------------------------------------------345
14.4 Understanding Hashing -------------------------------------------------------------------------------345
14.5 Components of Hashing -------------------------------------------------------------------------------346
14.6 Hash Table ----------------------------------------------------------------------------------------------347
14.7 Hash Function ------------------------------------------------------------------------------------------347
14.8 Load Factor----------------------------------------------------------------------------------------------348
14.9 Collisions ------------------------------------------------------------------------------------------------348
14.10 Collision Resolution Techniques--------------------------------------------------------------------348
14.11 Separate Chaining ------------------------------------------------------------------------------------348
14.12 Open Addressing--------------------------------------------------------------------------------------349
14.13 Comparison of Collision Resolution Techniques -------------------------------------------------350
14.14 How Hashing Gets O(1) Complexity? --------------------------------------------------------------350
14.15 Hashing Techniques----------------------------------------------------------------------------------351
14.16 Problems for which Hash Tables are not suitable -----------------------------------------------351
14.17 Bloom Filters ------------------------------------------------------------------------------------------351
14.18 Problems on Hashing---------------------------------------------------------------------------------353
15.String Algorithms --------------------------------------------------------------------------- 360
15.1 Introduction ---------------------------------------------------------------------------------------------360
15.2 String Matching Algorithms --------------------------------------------------------------------------360
15.3 Brute Force Method ------------------------------------------------------------------------------------360
15.4 Robin-Karp String Matching Algorithm -------------------------------------------------------------361
15.5 String Matching with Finite Automata --------------------------------------------------------------362
15.6 KMP Algorithm------------------------------------------------------------------------------------------363
15.7 Boyce-Moore Algorithm--------------------------------------------------------------------------------366
15.8 Data Structures for Storing Strings -----------------------------------------------------------------367
15.9 Hash Tables for Strings--------------------------------------------------------------------------------367
15.10 Binary Search Trees for Strings --------------------------------------------------------------------367
15.11 Tries-----------------------------------------------------------------------------------------------------367
15.12 Ternary Search Trees---------------------------------------------------------------------------------369
15.13 Comparing BSTs, Tries and TSTs ------------------------------------------------------------------375
15.14 Suffix Trees --------------------------------------------------------------------------------------------375
15.15 Problems on Strings ----------------------------------------------------------------------------------378
16.Algorithms Design Techniques------------------------------------------------------------ 386
16.1 Introduction ---------------------------------------------------------------------------------------------386
16.2 Classification--------------------------------------------------------------------------------------------386
16.3 Classification by Implementation Method ----------------------------------------------------------386
16.4 Classification by Design Method ---------------------------------------------------------------------387
16.5 Other Classifications-----------------------------------------------------------------------------------388
17.Greedy Algorithms -------------------------------------------------------------------------- 389
17.1 Introduction ---------------------------------------------------------------------------------------------389
17.2 Greedy strategy -----------------------------------------------------------------------------------------389
12. 17.3 Elements of Greedy Algorithms ----------------------------------------------------------------------389
17.4 Does Greedy Always Work? ---------------------------------------------------------------------------389
17.5 Advantages and Disadvantages of Greedy Method------------------------------------------------390
17.6 Greedy Applications------------------------------------------------------------------------------------390
17.7 Understanding Greedy Technique -------------------------------------------------------------------390
17.8 Problems on Greedy Algorithms----------------------------------------------------------------------393
18.Divide and Conquer Algorithms ---------------------------------------------------------- 399
18.1 Introduction ---------------------------------------------------------------------------------------------399
18.2 What is Divide and Conquer Strategy?--------------------------------------------------------------399
18.3 Does Divide and Conquer Always Work? -----------------------------------------------------------399
18.4 Divide and Conquer Visualization -------------------------------------------------------------------399
18.5 Understanding Divide and Conquer -----------------------------------------------------------------400
18.6 Advantages of Divide and Conquer ------------------------------------------------------------------400
18.7 Disadvantages of Divide and Conquer --------------------------------------------------------------401
18.8 Master Theorem ----------------------------------------------------------------------------------------401
18.9 Divide and Conquer Applications --------------------------------------------------------------------401
18.10 Problems on Divide and Conquer ------------------------------------------------------------------401
19.Dynamic Programming--------------------------------------------------------------------- 414
19.1 Introduction ---------------------------------------------------------------------------------------------414
19.2 What is Dynamic Programming Strategy?----------------------------------------------------------414
19.3 Properties of Dynamic Programming Strategy -----------------------------------------------------414
19.4 Can Dynamic Programming Solve All Problems?--------------------------------------------------414
19.5 Dynamic Programming Approaches -----------------------------------------------------------------414
19.6 Examples of Dynamic Programming Algorithms --------------------------------------------------415
19.7 Understanding Dynamic Programming -------------------------------------------------------------415
19.8 Longest Common Subsequence ----------------------------------------------------------------------418
19.9 Problems on Dynamic Programming ----------------------------------------------------------------420
20.Complexity Classes ------------------------------------------------------------------------- 451
20.1 Introduction ---------------------------------------------------------------------------------------------451
20.2 Polynomial/Exponential time-------------------------------------------------------------------------451
20.3 What is Decision Problem?----------------------------------------------------------------------------451
20.4 Decision Procedure-------------------------------------------------------------------------------------452
20.5 What is a Complexity Class? -------------------------------------------------------------------------452
20.6 Types of Complexity Classes--------------------------------------------------------------------------452
20.7 Reductions-----------------------------------------------------------------------------------------------454
20.8 Problems on Complexity Classes---------------------------------------------------------------------456
21.Miscellaneous Concepts ------------------------------------------------------------------- 459
21.1 Introduction ---------------------------------------------------------------------------------------------459
21.2 Hacks on Bitwise Programming ----------------------------------------------------------------------459
21.3 Other Programming Questions -----------------------------------------------------------------------463
References---------------------------------------------------------------------------------------- 470
13. Data Structure and Algorithmic Thinking with Python Organization of Chapters
0.1 What Is this Book About? 13
Organization of
Chapters
Chapter
0
0.1 What Is this Book About?
This book is about the fundamentals of data structures and algorithms--the basic elements from which large
and complex software artifacts are built. To develop a good understanding of a data structure requires three
things: First, you must learn how the information is arranged in the memory of the computer. Second, you must
become familiar with the algorithms for manipulating the information contained in the data structure. And
third, you must understand the performance characteristics of the data structure so that when called upon to
select a suitable data structure for a particular application, you are able to make an appropriate decision.
The algorithms and data structures in the book are presented in the Python programming language. A unique
feature of this book that is missing in most of the available books is to offer a balance between theoretical,
practical concepts, problems and interview questions.
𝐶𝑜𝑛𝑐𝑒𝑝𝑡𝑠 + 𝑃𝑟𝑜𝑏𝑙𝑒𝑚𝑠 + 𝐼𝑛𝑡𝑒𝑟𝑣𝑖𝑒𝑤 𝑄𝑢𝑒𝑠𝑡𝑖𝑜𝑛𝑠
The book deals with some of the most important and challenging areas of programming and computer science in
a highly readable manner. It covers both algorithmic theory and programming practice, demonstrating how
theory is reflected in real Python programs. Well-known algorithms and data structures that are built into the
Python language are explained, and the user is shown how to implement and evaluate others.
The book offers a large number of questions to practice each exam objective and will help you assess your
knowledge before you take the real interview. The detailed answers to every question will help reinforce your
knowledge.
Salient features of the book are:
Basic principles of algorithm design
How to represent well-known data structures in Python
How to implement well-known algorithms in Python
How to transform new problems to well-known algorithmic problems with efficient solutions
How to analyze algorithms and Python programs using both mathematical tools and basic experiments
and benchmarks
How to understand several classical algorithms and data structures in depth, and be able to implement
these efficiently in Python
Note that this book does not cover numerical or number-theoretical algorithms, parallel algorithms and
multicore programming.
0.2 Should I Take this Book?
The book is intended for Python programmers who need to learn about algorithmic problem-solving, or who need
a refresher. Data and computational scientists employed to do big data analytic analysis should find this book
useful. Game programmers and financial analysts/engineers may find this book applicable too. And, students of
computer science, or similar programming-related topics, such as bioinformatics, may also find the book to be
14. Data Structure and Algorithmic Thinking with Python Organization of Chapters
0.3 Organization of Chapters 14
quite useful. Although this book is more precise and analytical than many other data structure and algorithm
books, it rarely uses any mathematical concepts that are not taught in high school.
I have made an effort to avoid using any advanced calculus, probability, or stochastic process concepts. The
book is therefore appropriate for undergraduate students for their interview preparation.
0.3 Organization of Chapters
Data Structures and Algorithms are important parts of computer science. They form the fundamental building
blocks of developing logical solutions to problems. They help in creating efficient programs that perform tasks
optimally. This book comprehensively covers the topics required for a thorough understanding of the subjects. It
focuses on concepts like Linked Lists, Stacks, Queues, Trees, Priority Queues, Searching, Sorting, Hashing,
Algorithm Design Techniques, Greedy, Divide and Conquer, Dynamic Programming and Symbol Tables.
The chapters are arranged in the following way:
1. 𝐼𝑛𝑡𝑟𝑜𝑑𝑢𝑐𝑡𝑖𝑜𝑛: This chapter provides an overview of algorithms and their place in modern computing systems.
It considers the general motivations for algorithmic analysis and relationships among various approaches to
studying performance characteristics of algorithms.
2. 𝑅𝑒𝑐𝑢𝑟𝑠𝑖𝑜𝑛 𝐴𝑛𝑑 𝐵𝑎𝑐𝑘𝑇𝑟𝑎𝑐𝑘𝑖𝑛𝑔: 𝑅𝑒𝑐𝑢𝑟𝑠𝑖𝑜𝑛 is a programming technique that allows the programmer to express
operations in terms of themselves. In other words, it is the process of defining a function or calculating a
number by the repeated application of an algorithm.
For many real-world problems, the solution process consists of working your way through a sequence of
decision points in which each choice leads you further along some path (for example problems in Trees and
Graphs domain). If you make the correct set of choices, you end up at the solution. On the other hand, if
you reach a dead end or otherwise discover that you have made an incorrect choice somewhere along the
way, you have to backtrack to a previous decision point and try a different path. Algorithms that use this
approach are called 𝑏𝑎𝑐𝑘𝑡𝑟𝑎𝑐𝑘𝑖𝑛𝑔 algorithms. Backtracking is a form of recursion. Several problems can be
solved by combining recursion with backtracking.
3. 𝐿𝑖𝑛𝑘𝑒𝑑 𝐿𝑖𝑠𝑡𝑠: A 𝑙𝑖𝑛𝑘𝑒𝑑 𝑙𝑖𝑠𝑡 is a dynamic data structure. The number of nodes in a list is not fixed and can
grow and shrink on demand. Any application which has to deal with an unknown number of objects will
need to use a linked list. It is a very common data structure that is used to create other data structures like
trees, graphs, hashing etc...
4. 𝑆𝑡𝑎𝑐𝑘𝑠: A 𝑠𝑡𝑎𝑐𝑘 abstract type is a container of objects that are inserted and removed according to the last-in
first-out (LIFO) principle. There are many applications of stacks, including:
a. Space for function parameters and local variables is created internally using a stack
b. Compiler's syntax check for matching braces is implemented by using stack
c. Support for recursion
d. It can act as an auxiliary data structure for other abstract data types
5. 𝑄𝑢𝑒𝑢𝑒𝑠: 𝑄𝑢𝑒𝑢𝑒 is also an abstract data structure or a linear data structure, in which the first element is
inserted from one end called 𝑟𝑒𝑎𝑟 (also called 𝑡𝑎𝑖𝑙), and the deletion of exisiting element takes place from the
other end called as 𝑓𝑟𝑜𝑛𝑡 (also called ℎ𝑒𝑎𝑑). This makes queue as FIFO data structure, which means that
element inserted first will also be removed first. There are many applications of stacks, including:
a. In operating systems, for controlling access to shared system resources such as printers, files,
communication lines, disks and tapes
b. Computer systems must often provide a ℎ𝑜𝑙𝑑𝑖𝑛𝑔 𝑎𝑟𝑒𝑎 for messages between two processes, two
programs, or even two systems. This holding area is usually called a 𝑏𝑢𝑓𝑓𝑒𝑟 and is often
implemented as a queue.
c. It can act as an auxiliary data structure for other abstract data types
6. 𝑇𝑟𝑒𝑒𝑠: A 𝑡𝑟𝑒𝑒 is a abstract data structure used to organize the data in a tree format so as to make the data
insertion or deletion or search faster. Trees are one of the most useful data structures in computer science.
Some of the common applications of trees are:
a. The library database in a library, a student database in a school or college, an employee database in
a company, a patient database in a hospital or any database for that matter would be implemented
using trees.
b. The file system in your computer i.e. folders and all files, would be stored as a tree.
c. It can act as an auxiliary data structure for other abstract data types
It is an example for non-linear data structure. There are many variants in trees classified by the number of
children and the way of interconnecting them. This chapter focuses on few of those such as Generic Trees,
Binary Trees, Binary Search Trees, and Balanced Binary Trees etc...
7. 𝑃𝑟𝑖𝑜𝑟𝑖𝑡𝑦 𝑄𝑢𝑒𝑢𝑒𝑠: The 𝑃𝑟𝑖𝑜𝑟𝑖𝑡𝑦 𝑄𝑢𝑒𝑢𝑒 abstract data type is designed for systems that maintaining a collection
of prioritized elements, where elements are removed from the collection in order of their priority. Priority
queues turn up in several applications. A simple application comes from processing jobs, where we process
15. Data Structure and Algorithmic Thinking with Python Organization of Chapters
0.3 Organization of Chapters 15
each job based on how urgent it is. For example, operating systems often use a priority queue for the ready
queue of processes to run on the CPU.
8. 𝐺𝑟𝑎𝑝ℎ 𝐴𝑙𝑔𝑜𝑟𝑖𝑡ℎ𝑚𝑠: Graphs are a fundamental data structure in the world of programming. A graph abstract
data type is a collection of nodes called 𝑣𝑒𝑟𝑡𝑖𝑐𝑒𝑠, and the connections between them, called 𝑒𝑑𝑔𝑒𝑠. It is an
example for non-linear data structure. This chapter focuses on representations of graphs (adjacency list and
matrix representations), shortest path algorithms etc... Graphs can be used to model many types of
relations and processes in physical, biological, social and information systems. Many practical problems can
be represented by graphs.
9. 𝐷𝑖𝑠𝑗𝑜𝑖𝑛𝑡 𝑆𝑒𝑡 𝐴𝐷𝑇: A disjoint sets abstract data type represents a collection of sets that are disjoint: that is, no
item is found in more than one set. The collection of disjoint sets is called a partition, because the items are
partitioned among the sets. As an example, suppose the items in our universe are companies that still exist
today or were acquired by other corporations. Our sets are companies that still exist under their own name.
For instance, "𝑀𝑜𝑡𝑜𝑟𝑜𝑙𝑎," "𝑌𝑜𝑢𝑇𝑢𝑏𝑒," and "𝐴𝑛𝑑𝑟𝑜𝑖𝑑" are all members of the "𝐺𝑜𝑜𝑔𝑙𝑒" set.
In this chapter, we will limit ourselves to two operations. The first is called a 𝑢𝑛𝑖𝑜𝑛 operation, in which we
merge two sets into one. The second is called a 𝑓𝑖𝑛𝑑 query, in which we ask a question like, "What
corporation does Android belong to today?" More generally, a 𝑓𝑖𝑛𝑑 query takes an item and tells us which set
it is in. Data structures designed to support these operations are called 𝑢𝑛𝑖𝑜𝑛/𝑓𝑖𝑛𝑑 data structures.
Applications of 𝑢𝑛𝑖𝑜𝑛/𝑓𝑖𝑛𝑑 data structures include maze generation and Kruskal's algorithm for computing
the minimum spanning tree of a graph.
10. 𝑆𝑜𝑟𝑡𝑖𝑛𝑔 𝐴𝑙𝑔𝑜𝑟𝑖𝑡ℎ𝑚𝑠: 𝑆𝑜𝑟𝑡𝑖𝑛𝑔 is an algorithm that arranges the elements of a list in a certain order [either
ascending or descending]. The output is a permutation or reordering of the input. Sorting is one of the
important categories of algorithms in computer science. Sometimes sorting significantly reduces the
complexity of the problem. We can use sorting as a technique to reduce the search complexity. Great
research went into this category of algorithms because of its importance. These algorithms are used in many
computer algorithms [for example, searching elements], database algorithms and many more. In this
chapter, we understand both comparison based sorting algorithms and linear sorting algorithms.
11. 𝑆𝑒𝑎𝑟𝑐ℎ𝑖𝑛𝑔 𝐴𝑙𝑔𝑜𝑟𝑖𝑡ℎ𝑚𝑠: In computer science, 𝑠𝑒𝑎𝑟𝑐ℎ𝑖𝑛𝑔 is the process of finding an item with specified
properties from a collection of items. The items may be stored as records in a database, simple data
elements in arrays, text in files, nodes in trees, vertices and edges in graphs, or may be elements of other
search space.
Searching is one of core computer science algorithms. We know that today’s computers store a lot of
information. To retrieve this information proficiently we need very efficient searching algorithms. There are
certain ways of organizing the data which improves the searching process. That means, if we keep the data
in a proper order, it is easy to search the required element. Sorting is one of the techniques for making the
elements ordered. In this chapter we will see different searching algorithms.
12. 𝑆𝑒𝑙𝑒𝑐𝑡𝑖𝑜𝑛 𝐴𝑙𝑔𝑜𝑟𝑖𝑡ℎ𝑚𝑠: A 𝑠𝑒𝑙𝑒𝑐𝑡𝑖𝑜𝑛 𝑎𝑙𝑔𝑜𝑟𝑖𝑡ℎ𝑚 is an algorithm for finding the 𝑘 𝑡ℎ
smallest/largest number in a
list (also called as 𝑘 𝑡ℎ
order statistic). This includes, finding the minimum, maximum, and median elements.
For finding 𝑘 𝑡ℎ order statistic, there are multiple solutions which provide different complexities and in this
chapter we will enumerate those possibilities. In this chapter we will look a linear algorithm for finding the
𝑘 𝑡ℎ element in a given list.
13. 𝑆𝑦𝑚𝑏𝑜𝑙 𝑇𝑎𝑏𝑙𝑒𝑠 (𝐷𝑖𝑐𝑡𝑖𝑜𝑛𝑎𝑟𝑖𝑒𝑠): Since childhood, we all have used a dictionary, and many of us have a word
processor (say, Microsoft Word), which comes with spell checker. The spell checker is also a dictionary but
limited in scope. There are many real time examples for dictionaries and few of them are:
a. Spelling checker
b. The data dictionary found in database management applications
c. Symbol tables generated by loaders, assemblers, and compilers
d. Routing tables in networking components (DNS lookup)
In computer science, we generally use the term symbol table rather than dictionary, when referring to the
ADT.
14. 𝐻𝑎𝑠ℎ𝑖𝑛𝑔: 𝐻𝑎𝑠ℎ𝑖𝑛𝑔 is a technique used for storing and retrieving information as fast as possible. It is used to
perform optimal search and is useful in implementing symbol tables. From 𝑇𝑟𝑒𝑒𝑠 chapter we understand
that balanced binary search trees support operations such as insert, delete and search in O(𝑙𝑜𝑔𝑛) time. In
applications if we need these operations in O(1), then hashing provides a way. Remember that worst case
complexity of hashing is still O(𝑛), but it gives O(1) on the average. In this chapter, we will take a detailed
look at hashing process and problems which can solved with this technique.
15. 𝑆𝑡𝑟𝑖𝑛𝑔 𝐴𝑙𝑔𝑜𝑟𝑖𝑡ℎ𝑚𝑠: To understand the importance of string algorithms let us consider the case of entering the
URL (Uniform Resource Locator) in any browser (say, Internet Explorer, Firefox, or Google Chrome). You will
observe that after typing the prefix of the URL, a list of all possible URLs is displayed. That means, the
browsers are doing some internal processing and giving us the list of matching URLs. This technique is
sometimes called 𝑎𝑢𝑡𝑜-𝑐𝑜𝑚𝑝𝑙𝑒𝑡𝑖𝑜𝑛. Similarly, consider the case of entering the directory name in command
line interface (in both Windows and UNIX). After typing the prefix of the directory name if we press tab
16. Data Structure and Algorithmic Thinking with Python Organization of Chapters
0.3 Organization of Chapters 16
button, then we get a list of all matched directory names available. This is another example of auto
completion.
In order to support these kind of operations, we need a data structure which stores the string data
efficiently. In this chapter, we will look at the data structures that are useful for implementing string
algorithms. We start our discussion with the basic problem of strings: given a string, how do we search a
substring (pattern)? This is called 𝑠𝑡𝑟𝑖𝑛𝑔 𝑚𝑎𝑡𝑐ℎ𝑖𝑛𝑔 𝑝𝑟𝑜𝑏𝑙𝑒𝑚. After discussing various string matching
algorithms, we will see different data structures for storing strings.
16. 𝐴𝑙𝑔𝑜𝑟𝑖𝑡ℎ𝑚𝑠 𝐷𝑒𝑠𝑖𝑔𝑛 𝑇𝑒𝑐ℎ𝑛𝑖𝑞𝑢𝑒𝑠: In the previous chapters, we see many algorithms for solving different kinds of
problems. Before solving a new problem, the general tendency is to look for the similarity of current problem
with other problems for which we have solutions. This helps us in getting the solution easily. In this
chapter, we will see different ways of classifying the algorithms and in subsequent chapters we will focus on
a few of them (say, Greedy, Divide and Conquer and Dynamic Programming).
17. 𝐺𝑟𝑒𝑒𝑑𝑦 𝐴𝑙𝑔𝑜𝑟𝑖𝑡ℎ𝑚𝑠: A greedy algorithm is also be called a 𝑠𝑖𝑛𝑔𝑙𝑒-𝑚𝑖𝑛𝑑𝑒𝑑 algorithm. A greedy algorithm is a
process that looks for simple, easy-to-implement solutions to complex, multi-step problems by deciding
which next step will provide the most obvious benefit. The idea behind a greedy algorithm is to perform a
single procedure in the recipe over and over again until it can't be done any more and see what kind of
results it will produce. It may not completely solve the problem, or, if it produces a solution, it may not be
the very best one, but it is one way of approaching the problem and sometimes yields very good (or even the
best possible) results. Examples of greedy algorithms include selection sort, Prim's algorithms, Kruskal's
algorithms, Dijkstra algorithm, Huffman coding algorithm etc...
18. 𝐷𝑖𝑣𝑖𝑑𝑒 𝐴𝑛𝑑 𝐶𝑜𝑛𝑞𝑢𝑒𝑟: These algorithms work based on the principles described below.
a. 𝐷𝑖𝑣𝑖𝑑𝑒 - break the problem into several subproblems that are similar to the original problem but
smaller in size
b. 𝐶𝑜𝑛𝑞𝑢𝑒𝑟 - solve the subproblems recursively.
c. 𝐵𝑎𝑠𝑒 𝑐𝑎𝑠𝑒: If the subproblem size is small enough (i.e., the base case has been reached) then solve
the subproblem directly without more recursion.
d. 𝐶𝑜𝑚𝑏𝑖𝑛𝑒 - the solutions to create a solution for the original problem
Examples of divide and conquer algorithms include Binary Search, Merge Sort etc...
19. 𝐷𝑦𝑛𝑎𝑚𝑖𝑐 𝑃𝑟𝑜𝑔𝑟𝑎𝑚𝑚𝑖𝑛𝑔: In this chapter we will try to solve the problems for which we failed to get the optimal
solutions using other techniques (say, Divide & Conquer and Greedy methods). Dynamic Programming (DP)
is a simple technique but it can be difficult to master. One easy way to identify and solve DP problems is by
solving as many problems as possible. The term Programming is not related to coding but it is from
literature, and means filling tables (similar to Linear Programming).
20. 𝐶𝑜𝑚𝑝𝑙𝑒𝑥𝑖𝑡𝑦 𝐶𝑙𝑎𝑠𝑠𝑒𝑠: In previous chapters we solved problems of different complexities. Some algorithms have
lower rates of growth while others have higher rates of growth. The problems with lower rates of growth are
called easy problems (or easy solved problems) and the problems with higher rates of growth are called hard
problems (or hard solved problems). This classification is done based on the running time (or memory) that
an algorithm takes for solving the problem. There are lots of problems for which we do not know the
solutions.
In computer science, in order to understand the problems for which solutions are not there, the problems
are divided into classes and we call them as complexity classes. In complexity theory, a complexity class is a
set of problems with related complexity. It is the branch of theory of computation that studies the resources
required during computation to solve a given problem. The most common resources are time (how much
time the algorithm takes to solve a problem) and space (how much memory it takes). This chapter classifies
the problems into different types based on their complexity class.
21. 𝑀𝑖𝑠𝑐𝑒𝑙𝑙𝑎𝑛𝑒𝑜𝑢𝑠 𝐶𝑜𝑛𝑐𝑒𝑝𝑡𝑠/𝐵𝑖𝑡𝑤𝑖𝑠𝑒 𝐻𝑎𝑐𝑘𝑖𝑛𝑔: The commonality or applicability depends on the problem in hand.
Some real-life projects do benefit from bit-wise operations.
Some examples:
You're setting individual pixels on the screen by directly manipulating the video memory, in
which every pixel's color is represented by 1 or 4 bits. So, in every byte you can have packed 8 or
2 pixels and you need to separate them. Basically, your hardware dictates the use of bit-wise
operations.
You're dealing with some kind of file format (e.g. GIF) or network protocol that uses individual
bits or groups of bits to represent pieces of information.
Your data dictates the use of bit-wise operations. You need to compute some kind of checksum
(possibly, parity or CRC) or hash value and some of the most applicable algorithms do this by
manipulating with bits.
In this chapter, we discuss few tips and tricks with focus on bitwise operators. Also, it covers few other
uncovered and general problems.
17. Data Structure and Algorithmic Thinking with Python Organization of Chapters
0.4 Some Prerequisites 17
At the end of each chapter, a set of problems/questions are provided for you to improve/check your
understanding of the concepts. The examples in this book are kept simple for easy understanding. The objective
is to enhance the explanation of each concept with examples for a better understanding.
0.4 Some Prerequisites
This book is intended for two groups of people: Python programmers, who want to beef up their algorithmics,
and students taking algorithm courses, who want a supplement to algorithms textbook. Even if you belong to
the latter group, I’m assuming you have a familiarity with programming in general and with Python in
particular. If you don’t, the Python web site also has a lot of useful material. Python is a really easy language to
learn. There is some math in the pages ahead, but you don’t have to be a math prodigy to follow the text. We’ll
be dealing with some simple sums and nifty concepts such as polynomials, exponentials, and logarithms, but I’ll
explain it all as we go along.
18. Data Structure and Algorithmic Thinking with Python Introduction
1.1 Variables 18
Introduction
Chapter
1
The objective of this chapter is to explain the importance of analysis of algorithms, their notations, relationships
and solving as many problems as possible. Let us first focus on understanding the basic elements of algorithms,
importance of algorithm analysis and then slowly move toward the other topics as mentioned above. After
completing this chapter you should be able to find the complexity of any given algorithm (especially recursive
functions).
1.1 Variables
Before going to the definition of variables, let us relate them to old mathematical equations. All of us have solved
many mathematical equations since childhood. As an example, consider the below equation:
𝑥2
+ 2𝑦 − 2 = 1
We don’t have to worry about the use of this equation. The important thing that we need to understand is, the
equation has some names (𝑥 and 𝑦), which hold values (data). That means, the 𝑛𝑎𝑚𝑒𝑠 (𝑥 and 𝑦) are placeholders
for representing data. Similarly, in computer science programming we need something for holding data, and
𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒𝑠 is the way to do that.
1.2 Data types
In the above-mentioned equation, the variables 𝑥 and 𝑦 can take any values such as integral numbers (10, 20),
real numbers (0.23, 5.5) or just 0 and 1. To solve the equation, we need to relate them to kind of values they can
take and 𝑑𝑎𝑡𝑎 𝑡𝑦𝑝𝑒 is the name used in computer science programming for this purpose. A 𝑑𝑎𝑡𝑎 𝑡𝑦𝑝𝑒 in a
programming language is a set of data with predefined values. Examples of data types are: integer, floating point
unit number, character, string, etc.
Computer memory is all filled with zeros and ones. If we have a problem and wanted to code it, it’s very difficult
to provide the solution in terms of zeros and ones. To help users, programming languages and compilers provide
us with data types. For example, 𝑖𝑛𝑡𝑒𝑔𝑒𝑟 takes 2 bytes (actual value depends on compiler/interpreter), 𝑓𝑙𝑜𝑎𝑡
takes 4 bytes, etc. This says that, in memory we are combining 2 bytes (16 bits) and calling it as 𝑖𝑛𝑡𝑒𝑔𝑒𝑟.
Similarly, combining 4 bytes (32 bits) and calling it as 𝑓𝑙𝑜𝑎𝑡. A data type reduces the coding effort. At the top
level, there are two types of data types:
System-defined data types (also called 𝑃𝑟𝑖𝑚𝑖𝑡𝑖𝑣𝑒 data types)
User-defined data types
System-defined data types (Primitive data types)
Data types that are defined by system are called 𝑝𝑟𝑖𝑚𝑖𝑡𝑖𝑣𝑒 data types. The primitive data types provided by many
programming languages are: int, float, char, double, bool, etc. The number of bits allocated for each primitive
data type depends on the programming languages, compiler and operating system. For the same primitive data
type, different languages may use different sizes. Depending on the size of the data types the total available
values (domain) will also changes.
19. Data Structure and Algorithmic Thinking with Python Introduction
1.3 Data Structures 19
For example, “𝑖𝑛𝑡” may take 2 bytes or 4 bytes. If it takes 2 bytes (16 bits) then the total possible values are -
32,768 to +32,767 (-215
𝑡𝑜 215
-1). If it takes, 4 bytes (32 bits), then the possible values are between −2,147,483,648
and +2,147,483,647 (-231
𝑡𝑜 231
-1). Same is the case with other data types too.
User defined data types
If the system defined data types are not enough then most programming languages allow the users to define
their own data types called 𝑢𝑠𝑒𝑟 𝑑𝑒𝑓𝑖𝑛𝑒𝑑 𝑑𝑎𝑡𝑎 𝑡𝑦𝑝𝑒𝑠. Good example of user defined data types are: structures in
𝐶/𝐶 + + and classes in 𝐽𝑎𝑣𝑎/𝑃𝑦𝑡ℎ𝑜𝑛. For example, in the snippet below, we are combining many system-defined
data types and call it as user-defined data type with name “𝑛𝑒𝑤𝑇𝑦𝑝𝑒”. This gives more flexibility and comfort in
dealing with computer memory.
class newType (object):
def __init__(self, data1, data2):
self.data1=data1
self.data2=data2
1.3 Data Structures
Based on the discussion above, once we have data in variables, we need some mechanism for manipulating that
data to solve problems. 𝐷𝑎𝑡𝑎 𝑠𝑡𝑟𝑢𝑐𝑡𝑢𝑟𝑒 is a particular way of storing and organizing data in a computer so that it
can be used efficiently. A 𝑑𝑎𝑡𝑎 𝑠𝑡𝑟𝑢𝑐𝑡𝑢𝑟𝑒 is a special format for organizing and storing data. General data
structure types include arrays, files, linked lists, stacks, queues, trees, graphs and so on.
Depending on the organization of the elements, data structures are classified into two types:
1) 𝐿𝑖𝑛𝑒𝑎𝑟 𝑑𝑎𝑡𝑎 𝑠𝑡𝑟𝑢𝑐𝑡𝑢𝑟𝑒𝑠: Elements are accessed in a sequential order but it is not compulsory to store all
elements sequentially. 𝐸𝑥𝑎𝑚𝑝𝑙𝑒𝑠: Linked Lists, Stacks and Queues.
2) 𝑁𝑜𝑛 − 𝑙𝑖𝑛𝑒𝑎𝑟 𝑑𝑎𝑡𝑎 𝑠𝑡𝑟𝑢𝑐𝑡𝑢𝑟𝑒𝑠: Elements of this data structure are stored/accessed in a non-linear order.
𝐸𝑥𝑎𝑚𝑝𝑙𝑒𝑠: Trees and graphs.
1.4 Abstract Data Types (ADTs)
Before defining abstract data types, let us consider the different view of system-defined data types. We all know
that, by default, all primitive data types (int, float, etc.) support basic operations such as addition and
subtraction. The system provides the implementations for the primitive data types. For user-defined data types
also we need to define operations. The implementation for these operations can be done when we want to
actually use them. That means, in general user defined data types are defined along with their operations.
To simplify the process of solving the problems, we combine the data structures along with their operations and
call it 𝐴𝑏𝑠𝑡𝑟𝑎𝑐𝑡 𝐷𝑎𝑡𝑎 𝑇𝑦𝑝𝑒𝑠 (ADTs). An ADT consists of 𝑡𝑤𝑜 parts:
1. Declaration of data
2. Declaration of operations
Commonly used ADTs 𝑖𝑛𝑐𝑙𝑢𝑑𝑒: Linked Lists, Stacks, Queues, Priority Queues, Binary Trees, Dictionaries,
Disjoint Sets (Union and Find), Hash Tables, Graphs, and many other. For example, stack uses LIFO (Last-In-
First-Out) mechanism while storing the data in data structures. The last element inserted into the stack is the
first element that gets deleted. Common operations of it are: creating the stack, pushing an element onto the
stack, popping an element from stack, finding the current top of the stack, finding number of elements in the
stack, etc.
While defining the ADTs do not worry about the implementation details. They come into picture only when we
want to use them. Different kinds of ADTs are suited to different kinds of applications, and some are highly
specialized to specific tasks. By the end of this book, we will go through many of them and you will be in a
position to relate the data structures to the kind of problems they solve.
1.5 What is an Algorithm?
Let us consider the problem of preparing an 𝑜𝑚𝑒𝑙𝑒𝑡𝑡𝑒. For preparing omelette, we follow the steps given below:
1) Get the frying pan.
2) Get the oil.
a. Do we have oil?
i. If yes, put it in the pan.
ii. If no, do we want to buy oil?
20. Data Structure and Algorithmic Thinking with Python Introduction
1.6 Why Analysis of Algorithms? 20
1. If yes, then go out and buy.
2. If no, we can terminate.
3) Turn on the stove, etc...
What we are doing is, for a given problem (preparing an omelet), giving step by step procedure for solving it.
Formal definition of an algorithm can be given as:
An algorithm is the step-by-step instructions to solve a given problem.
Note: we do not have to prove each step of the algorithm.
1.6 Why Analysis of Algorithms?
To go from city “𝐴” to city “𝐵”, there can be many ways of accomplishing this: by flight, by bus, by train and also
by bicycle. Depending on the availability and convenience we choose the one that suits us. Similarly, in
computer science multiple algorithms are available for solving the same problem (for example, sorting problem
has many algorithms like insertion sort, selection sort, quick sort and many more). Algorithm analysis helps us
determining which of them is efficient in terms of time and space consumed.
1.7 Goal of Analysis of Algorithms
The goal of 𝑎𝑛𝑎𝑙𝑦𝑠𝑖𝑠 𝑜𝑓 𝑎𝑙𝑔𝑜𝑟𝑖𝑡ℎ𝑚𝑠 is to compare algorithms (or solutions) mainly in terms of running time but
also in terms of other factors (e.g., memory, developers effort etc.)
1.8 What is Running Time Analysis?
It is the process of determining how processing time increases as the size of the problem (input size) increases.
Input size is the number of elements in the input and depending on the problem type the input may be of
different types. The following are the common types of inputs.
Size of an array
Polynomial degree
Number of elements in a matrix
Number of bits in binary representation of the input
Vertices and edges in a graph
1.9 How to Compare Algorithms?
To compare algorithms, let us define few 𝑜𝑏𝑗𝑒𝑐𝑡𝑖𝑣𝑒 𝑚𝑒𝑎𝑠𝑢𝑟𝑒𝑠:
Execution times? 𝑁𝑜𝑡 𝑎 𝑔𝑜𝑜𝑑 𝑚𝑒𝑎𝑠𝑢𝑟𝑒 as execution times are specific to a particular computer.
Number of statements executed? 𝑁𝑜𝑡 𝑎 𝑔𝑜𝑜𝑑 𝑚𝑒𝑎𝑠𝑢𝑟𝑒, since the number of statements varies with the
programming language as well as the style of the individual programmer.
Ideal Solution? Let us assume that we expressed running time of given algorithm as a function of the input size
𝑛 (i.e., 𝑓(𝑛)) and compare these different functions corresponding to running times. This kind of comparison is
independent of machine time, programming style, etc.
1.10 What is Rate of Growth?
The rate at which the running time increases as a function of input is called 𝑟𝑎𝑡𝑒 𝑜𝑓 𝑔𝑟𝑜𝑤𝑡ℎ. Let us assume that
you went to a shop to buy a car and a cycle. If your friend sees you there and asks what you are buying then in
general you say 𝑏𝑢𝑦𝑖𝑛𝑔 𝑎 𝑐𝑎𝑟. This is because, cost of car is too big compared to cost of cycle (approximating the
cost of cycle to cost of car).
𝑇𝑜𝑡𝑎𝑙 𝐶𝑜𝑠𝑡 = 𝑐𝑜𝑠𝑡_𝑜𝑓_𝑐𝑎𝑟 + 𝑐𝑜𝑠𝑡_𝑜𝑓_𝑐𝑦𝑐𝑙𝑒
𝑇𝑜𝑡𝑎𝑙 𝐶𝑜𝑠𝑡 ≈ 𝑐𝑜𝑠𝑡_𝑜𝑓_𝑐𝑎𝑟 (𝑎𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑖𝑜𝑛)
For the above-mentioned example, we can represent the cost of car and cost of cycle in terms of function and for
a given function ignore the low order terms that are relatively insignificant (for large value of input size, 𝑛). As an
example in the case below, 𝑛4
, 2𝑛2
, 100𝑛 and 500 are the individual costs of some function and approximate it to
𝑛4
. Since, 𝑛4
is the highest rate of growth.
𝑛4
+ 2𝑛2
+ 100𝑛 + 500 ≈ 𝑛4
21. Data Structure and Algorithmic Thinking with Python Introduction
1.11 Commonly used Rate of Growths 21
1.11 Commonly used Rate of Growths
Below diagram shows the relationship between different rates of growth.
Given below is the list of rate of growths which come across in remaining chapters.
Time complexity Name Example
1 Constant Adding an element to the front of a linked list
𝑙𝑜𝑔𝑛 Logarithmic Finding an element in a sorted array
𝑛 Linear Finding an element in an unsorted array
𝑛𝑙𝑜𝑔𝑛 Linear Logarithmic Sorting n items by ‘divide-and-conquer’-Merge sort
𝑛2 Quadratic Shortest path between two nodes in a graph
𝑛3 Cubic Matrix Multiplication
2 𝑛
Exponential The Towers of Hanoi problem
D
e
c
r
e
a
s
i
n
g
R
a
t
e
s
O
f
G
r
o
w
t
h
log log 𝑛
𝑛 log 𝑛
log(𝑛!)
𝑛2
𝑛
2log 𝑛
𝑙𝑜𝑔2 𝑛
√𝑙𝑜𝑔𝑛
1
22 𝑛
𝑛!
4 𝑛
2 𝑛
22. Data Structure and Algorithmic Thinking with Python Introduction
1.12 Types of Analysis 22
1.12 Types of Analysis
To analyze the given algorithm we need to know on what inputs the algorithm takes less time (performing well)
and on what inputs the algorithm takes long time. We have already seen that an algorithm can be represented
in the form of an expression. That means we represent the algorithm with multiple expressions: one for the case
where it takes less time and other for the case where it takes the more time.
In general the first case is called the 𝑏𝑒𝑠𝑡 𝑐𝑎𝑠𝑒 and second case is called the 𝑤𝑜𝑟𝑠𝑡 𝑐𝑎𝑠𝑒 of the algorithm. To
analyze an algorithm we need some kind of syntax and that forms the base for asymptotic analysis/notation.
There are three types of analysis:
Worst case
o Defines the input for which the algorithm takes long time.
o Input is the one for which the algorithm runs the slower.
Best case
o Defines the input for which the algorithm takes lowest time.
o Input is the one for which the algorithm runs the fastest.
Average case
o Provides a prediction about the running time of the algorithm
o Assumes that the input is random
𝐿𝑜𝑤𝑒𝑟 𝐵𝑜𝑢𝑛𝑑 <= 𝐴𝑣𝑒𝑟𝑎𝑔𝑒 𝑇𝑖𝑚𝑒 <= 𝑈𝑝𝑝𝑒𝑟 𝐵𝑜𝑢𝑛𝑑
For a given algorithm, we can represent the best, worst and average cases in the form of expressions. As an
example, let 𝑓(𝑛) be the function which represents the given algorithm.
𝑓(𝑛) = 𝑛2
+ 500, for worst case
𝑓(𝑛) = 𝑛 + 100𝑛 + 500, for best case
Similarly, for average case too. The expression defines the inputs with which the algorithm takes the average
running time (or memory).
1.13 Asymptotic Notation
Having the expressions for the best, average case and worst cases; for all the three cases we need to identify the
upper and lower bounds. To represent these upper and lower bounds we need some kind of syntax and that is
the subject of the following discussion. Let us assume that the given algorithm is represented in the form of
function 𝑓(𝑛).
1.14 Big-O Notation
This notation gives the 𝑡𝑖𝑔ℎ𝑡 upper bound of the given function. Generally, it is represented as 𝑓(𝑛) = O(𝑔(𝑛)).
That means, at larger values of 𝑛, the upper bound of 𝑓(𝑛) is 𝑔(𝑛). For example, if 𝑓(𝑛) = 𝑛4
+ 100𝑛2
+ 10𝑛 + 50
is the given algorithm, then 𝑛4
is 𝑔(𝑛). That means, 𝑔(𝑛) gives the maximum rate of growth for 𝑓(𝑛) at larger
values of 𝑛.
Let us see the O−notation with little more detail. O−notation defined as O(𝑔(𝑛)) = {𝑓(𝑛): there exist positive
constants 𝑐 and 𝑛0 such that 0 ≤ 𝑓(𝑛) ≤ 𝑐𝑔(𝑛) for all 𝑛 ≥ 𝑛0}. 𝑔(𝑛) is an asymptotic tight upper bound for 𝑓(𝑛).
Our objective is to give smallest rate of growth 𝑔(𝑛) which is greater than or equal to given algorithms rate of
growth 𝑓(𝑛).
𝑐𝑔(𝑛)
𝑓(𝑛)
𝑛0
Input Size, 𝑛
Rate of Growth
23. Data Structure and Algorithmic Thinking with Python Introduction
1.14 Big-O Notation 23
Generally we discard lower values of 𝑛. That means the rate of growth at lower values of 𝑛 is not important. In
the figure below, 𝑛0 is the point from which we need to consider the rate of growths for a given algorithm. Below
𝑛0 the rate of growths could be different.
Big-O Visualization
O(𝑔(𝑛)) is the set of functions with smaller or same order of growth as 𝑔(𝑛). For example; O(𝑛2
) includes
O(1), O(𝑛), O(𝑛𝑙𝑜𝑔𝑛) etc.
Note: Analyze the algorithms at larger values of 𝑛 only. What this means is, below 𝑛0 we do not care for rate of
growth.
Big-O Examples
Example-1 Find upper bound for 𝑓(𝑛) = 3𝑛 + 8
Solution: 3𝑛 + 8 ≤ 4𝑛, for all 𝑛 ≥ 8
∴ 3𝑛 + 8 = O(𝑛) with c = 4 and 𝑛0 = 8
Example-2 Find upper bound for 𝑓(𝑛) = 𝑛2 + 1
Solution: 𝑛2 + 1 ≤ 2𝑛2, for all 𝑛 ≥ 1
∴ 𝑛2
+ 1 = O(𝑛2
) with 𝑐 = 2 and 𝑛0 = 1
Example-3 Find upper bound for 𝑓(𝑛) = 𝑛4
+ 100𝑛2
+ 50
Solution: 𝑛4
+ 100𝑛2
+ 50 ≤ 2𝑛4
, for all 𝑛 ≥ 11
∴ 𝑛4 + 100𝑛2 + 50 = O(𝑛4 ) with 𝑐 = 2 and 𝑛0 = 11
Example-4 Find upper bound for 𝑓(𝑛) = 2𝑛3
− 2𝑛2
Solution: 2𝑛3
− 2𝑛2
≤ 2𝑛3
, for all 𝑛 ≥ 1
∴ 2𝑛3
− 2𝑛2
= O(2𝑛3
) with 𝑐 = 2 and 𝑛0 = 1
Example-5 Find upper bound for 𝑓(𝑛) = 𝑛
Solution: 𝑛 ≤ 𝑛, for all 𝑛 ≥ 1
∴ 𝑛 = O(𝑛) with 𝑐 = 1 and 𝑛0 = 1
Example-6 Find upper bound for 𝑓(𝑛) = 410
Solution: 410 ≤ 410, for all 𝑛 ≥ 1
∴ 410 = O(1 ) with 𝑐 = 1 and 𝑛0 = 1
No Uniqueness?
There are no unique set of values for 𝑛0 and 𝑐 in proving the asymptotic bounds. Let us consider, 100𝑛 + 5 =
O(𝑛). For this function there are multiple 𝑛0 and 𝑐 values possible.
Solution1: 100𝑛 + 5 ≤ 100𝑛 + 𝑛 = 101𝑛 ≤ 101𝑛, for all 𝑛 ≥ 5, 𝑛0 = 5 and 𝑐 = 101 is a solution.
Solution2: 100𝑛 + 5 ≤ 100𝑛 + 5𝑛 = 105𝑛 ≤ 105𝑛, for all 𝑛 ≥ 1, 𝑛0 = 1 and 𝑐 = 105 is also a solution.
O(1): 100,1000, 200,1,20, 𝑒𝑡𝑐. O(𝑛):3𝑛 + 100, 100𝑛, 2𝑛 − 1, 3, 𝑒𝑡𝑐.
O(𝑛𝑙𝑜𝑔𝑛): 5𝑛𝑙𝑜𝑔𝑛, 3𝑛 − 100,
2𝑛 − 1, 100, 100𝑛, 𝑒𝑡𝑐.
O(𝑛2):𝑛2
, 5𝑛 − 10, 100, 𝑛2
− 2𝑛 + 1,
5, −200, 𝑒𝑡𝑐.
24. Data Structure and Algorithmic Thinking with Python Introduction
1.15 Omega-Ω Notation 24
1.15 Omega-Ω Notation
Similar to O discussion, this notation gives the tighter lower bound of the given algorithm and we represent it
as 𝑓(𝑛) = W(𝑔(𝑛)). That means, at larger values of 𝑛, the tighter lower bound of 𝑓(𝑛) is 𝑔(𝑛). For example, if
𝑓(𝑛) = 100𝑛2 + 10𝑛 + 50, 𝑔(𝑛) is W(𝑛2).
The Ω notation can be defined as Ω(g(n)) = {f(n): there exist positive constants c and 𝑛0 such that 0 ≤ 𝑐𝑔(𝑛) ≤
𝑓(𝑛) for all n ≥ 𝑛0}. 𝑔(𝑛) is an asymptotic tight lower bound for 𝑓(𝑛). Our objective is to give largest rate of
growth 𝑔(𝑛) which is less than or equal to given algorithms rate of growth 𝑓(𝑛).
Ω Examples
Example-1 Find lower bound for 𝑓(𝑛) = 5𝑛2.
Solution: $ 𝑐, 𝑛0 Such that: 0 £ 𝑐𝑛 £ 5𝑛2
Þ 𝑐𝑛 £ 5 𝑛2
Þ 𝑐 = 1 and 𝑛0 = 1
∴ 5𝑛2 = W(𝑛2) with 𝑐 = 1 and 𝑛0 = 1
Example-2 Prove 𝑓(𝑛) = 100𝑛 + 5 ≠ W(𝑛2).
Solution: $ c, 𝑛0 Such that: 0 £ 𝑐𝑛2
£ 100𝑛 + 5
100𝑛 + 5 £ 100𝑛 + 5𝑛 (" 𝑛 ³ 1) = 105𝑛
𝑐𝑛2 £ 105𝑛 Þ 𝑛(𝑐𝑛 – 105) £ 0
Since 𝑛 is positive Þ 𝑐𝑛 – 105 £ 0 Þ 𝑛 £ 105/𝑐
Þ Contradiction: 𝑛 cannot be smaller than a constant
Example-3 2𝑛 = W(𝑛), 𝑛3 = W(𝑛3), 𝑙𝑜𝑔𝑛 = W(𝑙𝑜𝑔𝑛).
1.16 Theta- Notation
This notation decides whether the upper and lower bounds of a given function (algorithm) are same. The average
running time of algorithm is always between lower bound and upper bound. If the upper bound (O) and lower
Input Size, 𝑛
𝑓(𝑛)
𝑐𝑔(𝑛))
Rate of Growth
𝑛0
𝑓(𝑛)
Rate of Growth
c1 𝑔(𝑛)
Input Size, 𝑛
c2 𝑔(𝑛)
𝑛0
25. Data Structure and Algorithmic Thinking with Python Introduction
1.17 Why is it called Asymptotic Analysis? 25
bound (W) give the same result then notation will also have the same rate of growth. As an example, let us
assume that 𝑓(𝑛) = 10𝑛 + 𝑛 is the expression. Then, its tight upper bound 𝑔(𝑛) is O(𝑛). The rate of growth in best
case is 𝑔(𝑛) = O(𝑛).
In this case, rate of growths in the best case and worst are same. As a result, the average case will also be same.
For a given function (algorithm), if the rate of growths (bounds) for O and W are not same then the rate of growth
case may not be same. In this case, we need to consider all possible time complexities and take average of
those (for example, quick sort average case, refer 𝑆𝑜𝑟𝑡𝑖𝑛𝑔 chapter).
Now consider the definition of notation. It is defined as (𝑔(𝑛)) = {𝑓(𝑛): there exist positive constants 𝑐1, 𝑐2 and 𝑛0
such that 0 ≤ 𝑐1 𝑔(𝑛) ≤ 𝑓(𝑛) ≤ 𝑐2 𝑔(𝑛) for all 𝑛 ≥ 𝑛0}. 𝑔(𝑛) is an asymptotic tight bound for 𝑓(𝑛). (𝑔(𝑛)) is the
set of functions with the same order of growth as 𝑔(𝑛).
Θ Examples
Example-1 Find bound for 𝑓(𝑛) =
𝑛2
2
−
𝑛
2
Solution:
𝑛2
5
≤
𝑛2
2
−
𝑛
2
≤ 𝑛2, for all, 𝑛 ≥ 1
∴
𝑛2
2
−
𝑛
2
= (𝑛2
) with 𝑐1 = 1/5, 𝑐2 = 1 and 𝑛0 = 1
Example-2 Prove 𝑛 ≠ (𝑛2
)
Solution: c1 𝑛2 ≤ 𝑛 ≤ c2 𝑛2Þ only holds for: 𝑛 ≤ 1/c1
∴ 𝑛 ≠ (𝑛2
)
Example-3 Prove 6𝑛3
≠ (𝑛2
)
Solution: 𝑐1 𝑛2
≤ 6𝑛3
≤ c2 𝑛2
Þ only holds for: 𝑛 ≤ c2 /6
∴ 6𝑛3 ≠ (𝑛2)
Example-4 Prove 𝑛 ≠ (𝑙𝑜𝑔𝑛)
Solution: c1 𝑙𝑜𝑔𝑛 ≤ 𝑛 ≤ c2 𝑙𝑜𝑔 𝑛 Þ c2 ≥
𝑛
log 𝑛
, " 𝑛 ≥ 𝑛0 – Impossible
Important Notes
For analysis (best case, worst case and average) we try to give upper bound (O) and lower bound (W) and average
running time (). From the above examples, it should also be clear that, for a given function (algorithm) getting
upper bound (O) and lower bound (W) and average running time () may not be possible always. For example, if
we are discussing the best case of an algorithm, then we try to give upper bound (O) and lower bound (W) and
average running time ().
In the remaining chapters we generally focus on upper bound (O) because knowing lower bound (W) of an
algorithm is of no practical importance and we use notation if upper bound (O) and lower bound (W) are same.
1.17 Why is it called Asymptotic Analysis?
From the discussion above (for all the three notations: worst case, best case, and average case), we can easily
understand that, in every case for a given function 𝑓(𝑛) we are trying to find other function 𝑔(𝑛) which
approximates 𝑓(𝑛) at higher values of 𝑛. That means, 𝑔(𝑛) is also a curve which approximates 𝑓(𝑛) at higher
values of 𝑛.
In mathematics we call such curve 𝑎𝑠𝑦𝑚𝑝𝑡𝑜𝑡𝑖𝑐 𝑐𝑢𝑟𝑣𝑒. In other terms, 𝑔(𝑛) is the asymptotic curve for 𝑓(𝑛). For
this reason, we call algorithm analysis 𝑎𝑠𝑦𝑚𝑝𝑡𝑜𝑡𝑖𝑐 𝑎𝑛𝑎𝑙𝑦𝑠𝑖𝑠.
1.18 Guidelines for Asymptotic Analysis
There are some general rules to help us determine the running time of an algorithm.
1) Loops: The running time of a loop is, at most, the running time of the statements inside the loop
(including tests) multiplied by the number of iterations.
# executes 𝑛 times
for i in range(1,n):
print 'Current Number :', i #constant time
Total time = a constant 𝑐 × 𝑛 = 𝑐 𝑛 = O(𝑛).
26. Data Structure and Algorithmic Thinking with Python Introduction
1.18 Guidelines for Asymptotic Analysis 26
2) Nested loops: Analyze from inside out. Total running time is the product of the sizes of all the loops.
# outer loop executed n times
for i in range(1,n):
# inner loop executes n times
for j in range1,n):
print 'i value %d and j value %d' % (i,j) #constant time
Total time = 𝑐 × 𝑛 × 𝑛 = 𝑐𝑛2 = O(𝑛2).
3) Consecutive statements: Add the time complexities of each statement.
n = 100
# executes 𝑛 times
for i in range(1,n):
print 'Current Number :', i #constant time
# outer loop executed n times
for i in range(1,n):
# inner loop executes n times
for j in range1,n):
print 'i value %d and j value %d' % (i,j) #constant time
Total time = 𝑐0 + 𝑐1 𝑛 + 𝑐2 𝑛2
= O(𝑛2).
4) If-then-else statements: Worst-case running time: the test, plus 𝑒𝑖𝑡ℎ𝑒𝑟 the 𝑡ℎ𝑒𝑛 part or the 𝑒𝑙𝑠𝑒 part
(whichever is the larger).
if n == 1: #constant time
print "Wrong Value"
print n
else:
for i in range(1,n): #n times
print 'Current Number :', i #constant time
Total time = 𝑐0 + 𝑐1 ∗ 𝑛 = O(𝑛).
5) Logarithmic complexity: An algorithm is O(𝑙𝑜𝑔𝑛) if it takes a constant time to cut the problem size by
a fraction (usually by ½). As an example let us consider the following program:
def Logarithms(n):
i = 1
while i <= n:
i= i * 2
print i
Logarithms(100)
If we observe carefully, the value of 𝑖 is doubling every time. Initially 𝑖 = 1, in next step 𝑖 = 2, and in
subsequent steps 𝑖 = 4, 8 and so on. Let us assume that the loop is executing some 𝑘 times. At 𝑘 𝑡ℎ
step 2 𝑘
=
𝑛 and we come out of loop. Taking logarithm on both sides, gives
𝑙𝑜𝑔(2k
) = 𝑙𝑜𝑔𝑛
𝑘𝑙𝑜𝑔2 = 𝑙𝑜𝑔𝑛
𝑘 = 𝑙𝑜𝑔𝑛 //if we assume base-2
Total time = O(𝑙𝑜𝑔𝑛).
Note: Similarly, for the case below also, worst case rate of growth is O(𝑙𝑜𝑔𝑛). The same discussion holds good for
decreasing sequence as well.
def Logarithms(n):
i = n
while i >= 1:
i= i // 2
print i
Logarithms(100)
Another example: binary search (finding a word in a dictionary of 𝑛 pages)
Look at the center point in the dictionary
Is word towards left or right of center?
Repeat process with left or right part of dictionary until the word is found
27. Data Structure and Algorithmic Thinking with Python Introduction
1.19 Properties of Notations 27
1.19 Properties of Notations
Transitivity: 𝑓(𝑛) = (𝑔(𝑛)) and 𝑔(𝑛) = (ℎ(𝑛)) Þ 𝑓(𝑛) = (ℎ(𝑛)). Valid for O and W as well.
Reflexivity: 𝑓(𝑛) = (𝑓(𝑛)). Valid for O and W.
Symmetry: 𝑓(𝑛) = (𝑔(𝑛)) if and only if 𝑔(𝑛) = (𝑓(𝑛)).
Transpose symmetry: 𝑓(𝑛) = O(𝑔(𝑛)) if and only if 𝑔(𝑛) = W(𝑓(𝑛)).
1.20 Commonly used Logarithms and Summations
Logarithms
𝑙𝑜𝑔 𝑥 𝑦
= 𝑦 𝑙𝑜𝑔 𝑥 𝑙𝑜𝑔𝑛 = 𝑙𝑜𝑔10
𝑛
𝑙𝑜𝑔 𝑥𝑦 = 𝑙𝑜𝑔𝑥 + 𝑙𝑜𝑔𝑦 𝑙𝑜𝑔 𝑘
𝑛 = (𝑙𝑜𝑔𝑛)
𝑘
𝑙𝑜𝑔 𝑙𝑜𝑔𝑛 = 𝑙𝑜𝑔(𝑙𝑜𝑔 𝑛) 𝑙𝑜𝑔
𝑥
𝑦
= 𝑙𝑜𝑔𝑥 – 𝑙𝑜𝑔 𝑦
𝑎𝑙𝑜𝑔 𝑏
𝑥
= 𝑥 𝑙𝑜𝑔 𝑏
𝑎
𝑙𝑜𝑔 𝑏
𝑥
=
𝑙𝑜𝑔 𝑎
𝑥
𝑙𝑜𝑔 𝑎
𝑏
Arithmetic series
∑ 𝑘 = 1 + 2 + ⋯ + 𝑛 =
𝑛(𝑛 + 1)
2
𝑛
𝐾=1
Geometric series
∑ 𝑥 𝑘 = 1 + 𝑥 + 𝑥2 … + 𝑥 𝑛 =
𝑥 𝑛+1
− 1
𝑥 − 1
(𝑥 ≠ 1)
𝑛
𝑘=0
Harmonic series
∑
1
𝑘
𝑛
𝑘=1
= 1 +
1
2
+ … +
1
𝑛
≈ 𝑙𝑜𝑔 𝑛
Other important formulae
∑ 𝑙𝑜𝑔 𝑘
𝑛
𝑘=1
≈ 𝑛𝑙𝑜𝑔𝑛
∑ 𝑘 𝑝
𝑛
𝑘=1
= 1 𝑝
+ 2 𝑝
+ ⋯ + 𝑛 𝑝
≈
1
𝑝 + 1
𝑛 𝑝+1
1.21 Master Theorem for Divide and Conquer
All divide and conquer algorithms (Also discussed in detail in the 𝐷𝑖𝑣𝑖𝑑𝑒 𝑎𝑛𝑑 𝐶𝑜𝑛𝑞𝑢𝑒𝑟 chapter) divide the problem
into sub-problems, each of which is part of the original problem, and then perform some additional work to
compute the final answer. As an example, merge sort algorithm [for details, refer 𝑆𝑜𝑟𝑡𝑖𝑛𝑔 chapter] operates on
two sub-problems, each of which is half the size of the original and then performs O( 𝑛) additional work for
merging. This gives the running time equation:
T(𝑛) = 2𝑇 (
𝑛
2
) + O(𝑛)
The following theorem can be used to determine the running time of divide and conquer algorithms. For a given
program (algorithm), first we try to find the recurrence relation for the problem. If the recurrence is of the below
form then we can directly give the answer without fully solving it.
If the recurrence is of the form T(𝑛) = 𝑎𝑇(
𝑛
𝑏
) + (𝑛 𝑘
𝑙𝑜𝑔 𝑝
𝑛), where 𝑎 ≥ 1, 𝑏 > 1, 𝑘 ≥ 0 and 𝑝 is a real number, then:
1) If 𝑎 > 𝑏 𝑘
, then 𝑇(𝑛) = Θ(𝑛𝑙𝑜𝑔 𝑏
𝑎
)
2) If 𝑎 = 𝑏 𝑘
a. If 𝑝 > −1, then 𝑇(𝑛) = Θ(𝑛𝑙𝑜𝑔 𝑏
𝑎
𝑙𝑜𝑔 𝑝+1
𝑛)
b. If 𝑝 = −1, then 𝑇(𝑛) = Θ(𝑛𝑙𝑜𝑔 𝑏
𝑎
𝑙𝑜𝑔𝑙𝑜𝑔𝑛)
c. If 𝑝 < −1, then 𝑇(𝑛) = Θ(𝑛𝑙𝑜𝑔 𝑏
𝑎
)
3) If 𝑎 < 𝑏 𝑘
a. If 𝑝 ≥ 0, then 𝑇(𝑛) = Θ(𝑛 𝑘
𝑙𝑜𝑔 𝑝
𝑛)
b. If 𝑝 < 0, then 𝑇(𝑛) = O(𝑛 𝑘)
28. Data Structure and Algorithmic Thinking with Python Introduction
1.22 Problems on Divide and Conquer Master Theorem 28
1.22 Problems on Divide and Conquer Master Theorem
For each of the following recurrences, give an expression for the runtime 𝑇(𝑛) if the recurrence can be solved
with the Master Theorem. Otherwise, indicate that the Master Theorem does not apply.
Problem-1 𝑇(𝑛) = 3𝑇 (𝑛/2) + 𝑛2
Solution: 𝑇(𝑛) = 3𝑇 (𝑛/2) + 𝑛2
=> 𝑇 (𝑛) =Θ(𝑛2
) (Master Theorem Case 3.a)
Problem-2 𝑇(𝑛) = 4𝑇 (𝑛/2) + 𝑛2
Solution: 𝑇(𝑛) = 4𝑇 (𝑛/2) + 𝑛2
=> 𝑇 (𝑛) = Θ(𝑛2
𝑙𝑜𝑔𝑛) (Master Theorem Case 2.a)
Problem-3 𝑇(𝑛) = 𝑇(𝑛/2) + 𝑛2
Solution: 𝑇(𝑛) = 𝑇(𝑛/2) + 𝑛2
=> Θ(𝑛2
) (Master Theorem Case 3.a)
Problem-4 𝑇(𝑛) = 2 𝑛
𝑇(𝑛/2) + 𝑛 𝑛
Solution: 𝑇(𝑛) = 2 𝑛
𝑇(𝑛/2) + 𝑛 𝑛
=> Does not apply (𝑎 is not constant)
Problem-5 𝑇(𝑛) = 16𝑇(𝑛/4) + 𝑛
Solution: 𝑇(𝑛) = 16𝑇 (𝑛/4) + 𝑛 => 𝑇(𝑛) = Θ(𝑛2
) (Master Theorem Case 1)
Problem-6 𝑇(𝑛) = 2𝑇(𝑛/2) + 𝑛𝑙𝑜𝑔𝑛
Solution: 𝑇(𝑛) = 2𝑇(𝑛/2) + 𝑛𝑙𝑜𝑔𝑛 => 𝑇(𝑛) = Θ(𝑛𝑙𝑜𝑔2
𝑛) (Master Theorem Case 2.a)
Problem-7 𝑇(𝑛) = 2𝑇(𝑛/2) + 𝑛/𝑙𝑜𝑔𝑛
Solution: 𝑇(𝑛) = 2𝑇(𝑛/2) + 𝑛/𝑙𝑜𝑔𝑛 => 𝑇(𝑛) = Θ(𝑛𝑙𝑜𝑔𝑙𝑜𝑔𝑛) (Master Theorem Case 2.b)
Problem-8 𝑇(𝑛) = 2𝑇 (𝑛/4) + 𝑛0.51
Solution: 𝑇(𝑛) = 2𝑇(𝑛/4) + 𝑛0.51 => 𝑇 (𝑛) = Θ(𝑛0.51) (Master Theorem Case 3.b)
Problem-9 𝑇(𝑛) = 0.5𝑇(𝑛/2) + 1/𝑛
Solution: 𝑇(𝑛) = 0.5𝑇(𝑛/2) + 1/𝑛 => Does not apply (𝑎 < 1)
Problem-10 𝑇 (𝑛) = 6𝑇 (𝑛/3) + 𝑛2
𝑙𝑜𝑔𝑛
Solution: 𝑇(𝑛) = 6𝑇(𝑛/3) + 𝑛2
𝑙𝑜𝑔𝑛 => 𝑇(𝑛) = Θ(𝑛2
𝑙𝑜𝑔𝑛) (Master Theorem Case 3.a)
Problem-11 𝑇(𝑛) = 64𝑇(𝑛/8) − 𝑛2
𝑙𝑜𝑔𝑛
Solution: 𝑇(𝑛) = 64𝑇(𝑛/8) − 𝑛2
𝑙𝑜𝑔𝑛 => Does not apply (function is not positive)
Problem-12 𝑇 (𝑛) = 7𝑇(𝑛/3) + 𝑛2
Solution: 𝑇(𝑛) = 7𝑇(𝑛/3) + 𝑛2 => 𝑇(𝑛) = Θ(𝑛2) (Master Theorem Case 3.as)
Problem-13 𝑇 (𝑛) = 4𝑇 (𝑛/2) + 𝑙𝑜𝑔𝑛
Solution: 𝑇(𝑛) = 4𝑇(𝑛/2) + 𝑙𝑜𝑔𝑛 => 𝑇(𝑛) = Θ(𝑛2
) (Master Theorem Case 1)
Problem-14 𝑇 (𝑛) = 16𝑇 (𝑛/4) + 𝑛!
Solution: 𝑇(𝑛) = 16𝑇 (𝑛/4) + 𝑛! => 𝑇(𝑛) = Θ(𝑛!) (Master Theorem Case 3.a)
Problem-15 𝑇 (𝑛) = √2𝑇 (𝑛/2) + 𝑙𝑜𝑔𝑛
Solution: 𝑇(𝑛) = √2𝑇 (𝑛/2) + 𝑙𝑜𝑔𝑛 => 𝑇(𝑛) = Θ(√ 𝑛) (Master Theorem Case 1)
Problem-16 𝑇 (𝑛) = 3𝑇 (𝑛/2) + 𝑛
Solution: 𝑇(𝑛) = 3𝑇 (𝑛/2) + 𝑛 => 𝑇(𝑛) = (𝑛𝑙𝑜𝑔3
) (Master Theorem Case 1)
Problem-17 𝑇(𝑛) = 3𝑇 (𝑛/3) + √ 𝑛
Solution: 𝑇(𝑛) = 3𝑇 (𝑛/3) + √ 𝑛 => 𝑇(𝑛) = Θ(𝑛) (Master Theorem Case 1)
Problem-18 𝑇(𝑛) = 4𝑇 (𝑛/2) + 𝑐𝑛
Solution: 𝑇(𝑛) = 4𝑇 (𝑛/2) + 𝑐𝑛 => 𝑇(𝑛) = (𝑛2) (Master Theorem Case 1)
Problem-19 𝑇(𝑛) = 3𝑇 (𝑛/4) + 𝑛𝑙𝑜𝑔𝑛
Solution: 𝑇(𝑛) = 3𝑇 (𝑛/4) + 𝑛𝑙𝑜𝑔𝑛 => 𝑇(𝑛) = Θ(𝑛𝑙𝑜𝑔𝑛) (Master Theorem Case 3.a)
Problem-20 𝑇 (𝑛) = 3𝑇 (𝑛/3) + 𝑛/2
Solution: 𝑇(𝑛) = 3𝑇 (𝑛/3) + 𝑛/2 => 𝑇 (𝑛) = Θ(𝑛𝑙𝑜𝑔𝑛) (Master Theorem Case 2.a)
29. Data Structure and Algorithmic Thinking with Python Introduction
1.23 Master Theorem for Subtract and Conquer Recurrences 29
1.23 Master Theorem for Subtract and Conquer Recurrences
Let 𝑇(𝑛) be a function defined on positive 𝑛, and having the property
𝑇(𝑛) = {
𝑐, if 𝑛 ≤ 1
𝑎𝑇(𝑛 − 𝑏) + 𝑓(𝑛), if 𝑛 > 1
for some constants 𝑐, 𝑎 > 0, 𝑏 > 0, 𝑘 ≥ 0, and function 𝑓(𝑛). If 𝑓(𝑛) is in O(𝑛 𝑘
), then
𝑇(𝑛) = {
O(𝑛 𝑘), if a < 1
O(𝑛 𝑘+1), if a = 1
O (𝑛 𝑘
𝑎
𝑛
𝑏) , if a > 1
1.24 Variant of subtraction and conquer master theorem
The solution to the equation 𝑇(𝑛) = 𝑇(𝛼 𝑛) + 𝑇((1 − 𝛼)𝑛) + 𝛽𝑛, where 0 < 𝛼 < 1 and 𝛽 > 0 are constants, is
O(𝑛𝑙𝑜𝑔𝑛).
1.25 Method of Guessing and Confirm
Now, let us discuss about a method which can be used to solve any recurrence. The basic idea behind this
method is,
𝑔𝑢𝑒𝑠𝑠 the answer; and then 𝑝𝑟𝑜𝑣𝑒 it correct by induction.
In other words, it addresses the question: What if the given recurrence doesn’t seem to match with any of these
(master theorems) methods? If we guess a solution and then try to verify our guess inductively, usually either
the proof will succeed (in which case we are done), or the proof will fail (in which case the failure will help us
refine our guess).
As an example, consider the recurrence T(𝑛) = √ 𝑛 T(√ 𝑛) + 𝑛. This doesn’t fit into the form required by the Master
Theorems. Carefully observing the recurrence gives us the impression that it is similar to divide and conquer
method (diving the problem into √ 𝑛 subproblems each with size √ 𝑛). As we can see, the size of the subproblems
at the first level of recursion is 𝑛. So, let us guess that T(𝑛) = O(𝑛𝑙𝑜𝑔𝑛), and then try to prove that our guess is
correct.
Let’s start by trying to prove an 𝑢𝑝𝑝𝑒𝑟 bound T(𝑛) ≤ 𝑐𝑛𝑙𝑜𝑔𝑛:
T(𝑛) = √ 𝑛 T(√ 𝑛) + 𝑛
≤ √ 𝑛. 𝑐√ 𝑛 𝑙𝑜𝑔√ 𝑛 + 𝑛
= 𝑛. 𝑐 𝑙𝑜𝑔√ 𝑛 + 𝑛
= 𝑛.c.
1
2
. 𝑙𝑜𝑔𝑛+ 𝑛
≤ 𝑐𝑛𝑙𝑜𝑔𝑛
The last inequality assumes only that 1 ≤ c.
1
2
. 𝑙𝑜𝑔𝑛. This is correct if 𝑛 is sufficiently large and for any constant 𝑐,
no matter how small. From the above proof, we can see that our guess is correct for upper bound. Now, let us
prove the 𝑙𝑜𝑤𝑒𝑟 bound for this recurrence.
T(𝑛) = √ 𝑛 T(√ 𝑛) + 𝑛
≥ √ 𝑛. 𝑘 √ 𝑛 𝑙𝑜𝑔√ 𝑛 + 𝑛
= 𝑛. 𝑘 𝑙𝑜𝑔√ 𝑛 + 𝑛
= 𝑛.𝑘.
1
2
. 𝑙𝑜𝑔𝑛+ 𝑛
≥ 𝑘𝑛𝑙𝑜𝑔𝑛
The last inequality assumes only that 1 ≥ 𝑘.
1
2
. 𝑙𝑜𝑔𝑛. This is incorrect if 𝑛 is sufficiently large and for any constant
𝑘. From the above proof, we can see that our guess is incorrect for lower bound.
From the above discussion, we understood that Θ(𝑛𝑙𝑜𝑔𝑛) is too big. How about Θ(𝑛)? The lower bound is easy to
prove directly:
T(𝑛) = √ 𝑛 T(√ 𝑛) + 𝑛 ≥ 𝑛
Now, let us prove the upper bound for this Θ(𝑛).
T(𝑛) = √ 𝑛 T(√ 𝑛) + 𝑛
≤ √ 𝑛.𝑐. √ 𝑛 + 𝑛
= 𝑛. 𝑐+ 𝑛
30. Data Structure and Algorithmic Thinking with Python Introduction
1.26 Amortized Analysis 30
= 𝑛 (𝑐 + 1)
≰ 𝑐𝑛
From the above induction, we understood that Θ(𝑛) is too small and Θ(𝑛𝑙𝑜𝑔𝑛) is too big. So, we need something
bigger than 𝑛 and smaller than 𝑛𝑙𝑜𝑔𝑛? How about 𝑛√𝑙𝑜𝑔𝑛?
Proving upper bound for 𝑛√𝑙𝑜𝑔𝑛:
T(𝑛) = √ 𝑛 T(√ 𝑛) + 𝑛
≤ √ 𝑛.𝑐. √ 𝑛√ 𝑙𝑜𝑔√ 𝑛 + 𝑛
= 𝑛. 𝑐.
1
√2
𝑙𝑜𝑔√ 𝑛+ 𝑛
≤ 𝑐𝑛𝑙𝑜𝑔√ 𝑛
Proving lower bound for 𝑛√𝑙𝑜𝑔𝑛:
T(𝑛) = √ 𝑛 T(√ 𝑛) + 𝑛
≥ √ 𝑛.𝑘. √ 𝑛√ 𝑙𝑜𝑔√ 𝑛 + 𝑛
= 𝑛. 𝑘.
1
√2
𝑙𝑜𝑔√ 𝑛+ 𝑛
≱ 𝑘𝑛𝑙𝑜𝑔√ 𝑛
The last step doesn’t work. So, Θ(𝑛√𝑙𝑜𝑔𝑛) doesn’t work. What else is between 𝑛 and 𝑛𝑙𝑜𝑔𝑛? How about 𝑛𝑙𝑜𝑔𝑙𝑜𝑔𝑛?
Proving upper bound for 𝑛𝑙𝑜𝑔𝑙𝑜𝑔𝑛:
T(𝑛) = √ 𝑛 T(√ 𝑛) + 𝑛
≤ √ 𝑛.𝑐. √ 𝑛𝑙𝑜𝑔𝑙𝑜𝑔√ 𝑛 + 𝑛
= 𝑛. 𝑐. 𝑙𝑜𝑔𝑙𝑜𝑔𝑛-𝑐. 𝑛 + 𝑛
≤ 𝑐𝑛𝑙𝑜𝑔𝑙𝑜𝑔𝑛, if 𝑐 ≥ 1
Proving lower bound for 𝑛𝑙𝑜𝑔𝑙𝑜𝑔𝑛:
T(𝑛) = √ 𝑛 T(√ 𝑛) + 𝑛
≥ √ 𝑛.𝑘. √ 𝑛𝑙𝑜𝑔𝑙𝑜𝑔√ 𝑛 + 𝑛
= 𝑛. 𝑘. 𝑙𝑜𝑔𝑙𝑜𝑔𝑛-𝑘. 𝑛 + 𝑛
≥ 𝑘𝑛𝑙𝑜𝑔𝑙𝑜𝑔𝑛, if 𝑘 ≤ 1
From the above proofs, we can see that T(𝑛) ≤ 𝑐𝑛𝑙𝑜𝑔𝑙𝑜𝑔𝑛, if 𝑐 ≥ 1 and T(𝑛) ≥ 𝑘𝑛𝑙𝑜𝑔𝑙𝑜𝑔𝑛, if 𝑘 ≤ 1. Technically, we’re
still missing the base cases in both proofs, but we can be fairly confident at this point that T(𝑛) = Θ(𝑛𝑙𝑜𝑔𝑙𝑜𝑔𝑛).
1.26 Amortized Analysis
Amortized analysis refers to determining the time-averaged running time for a sequence of operations. It is
different from average case analysis, because amortized analysis does not make any assumption about the
distribution of the data values, whereas average case analysis assumes the data are not "bad" (e.g., some sorting
algorithms do well on 𝑎𝑣𝑒𝑟𝑎𝑔𝑒 over all input orderings but very badly on certain input orderings). That is,
amortized analysis is a worst-case analysis, but for a sequence of operations, rather than for individual
operations.
The motivation for amortized analysis is to better understand the running time of certain techniques, where
standard worst case analysis provides an overly pessimistic bound. Amortized analysis generally applies to a
method that consists of a sequence of operations, where the vast majority of the operations are cheap, but some
of the operations are expensive. If we can show that the expensive operations are particularly rare we can
𝑐ℎ𝑎𝑟𝑔𝑒 𝑡ℎ𝑒𝑚 to the cheap operations, and only bound the cheap operations.
The general approach is to assign an artificial cost to each operation in the sequence, such that the total of the
artificial costs for the sequence of operations bounds total of the real costs for the sequence. This artificial cost
is called the amortized cost of an operation. To analyze the running time, the amortized cost thus is a correct
way of understanding the overall running time — but note that particular operations can still take longer so it is
not a way of bounding the running time of any individual operation in the sequence.
When one event in a sequence affects the cost of later events:
One particular task may be expensive.
But it may leave data structure in a state that next few operations become easier.
31. Data Structure and Algorithmic Thinking with Python Introduction
1.27 Problems on Algorithms Analysis 31
Example: Let us consider an array of elements from which we want to find 𝑘 𝑡ℎ
smallest element. We can solve
this problem using sorting. After sorting the given array, we just need to return the 𝑘 𝑡ℎ element from it. Cost of
performing sort (assuming comparison based sorting algorithm) is O(𝑛𝑙𝑜𝑔𝑛). If we perform 𝑛 such selections then
the average cost of each selection is O(𝑛𝑙𝑜𝑔𝑛/𝑛) = O(𝑙𝑜𝑔𝑛). This clearly indicates that sorting once is reducing the
complexity of subsequent operations.
1.27 Problems on Algorithms Analysis
Note: From the following problems, try to understand the cases which give different complexities
(O(𝑛), O(𝑙𝑜𝑔𝑛), O(𝑙𝑜𝑔𝑙𝑜𝑔𝑛) etc...).
Problem-21 Find the complexity of the below recurrence:
𝑇(𝑛) = {
3𝑇(𝑛 − 1), 𝑖𝑓 𝑛 > 0,
1, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
Solution: Let us try solving this function with substitution.
𝑇(𝑛) = 3𝑇(𝑛 − 1)
𝑇(𝑛) = 3(3𝑇(𝑛 − 2)) = 32 𝑇(𝑛 − 2)
𝑇(𝑛) = 32(3𝑇(𝑛 − 3))
.
.
𝑇(𝑛) = 3 𝑛
𝑇(𝑛 − 𝑛) = 3 𝑛
𝑇(0) = 3 𝑛
This clearly shows that the complexity of this function is O(3 𝑛).
Note: We can use the 𝑆𝑢𝑏𝑡𝑟𝑎𝑐𝑡𝑖𝑜𝑛 𝑎𝑛𝑑 𝐶𝑜𝑛𝑞𝑢𝑒𝑟 master theorem for this problem.
Problem-22 Find the complexity of the below recurrence:
𝑇(𝑛) = {
2𝑇(𝑛 − 1) − 1, 𝑖𝑓 𝑛 > 0,
1, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
Solution: Let us try solving this function with substitution.
𝑇(𝑛) = 2𝑇(𝑛 − 1) − 1
𝑇(𝑛) = 2(2𝑇(𝑛 − 2) − 1) − 1 = 22 𝑇(𝑛 − 2) − 2 − 1
𝑇(𝑛) = 22(2𝑇(𝑛 − 3) − 2 − 1) − 1 = 23
𝑇(𝑛 − 4) − 22
− 21
− 20
𝑇(𝑛) = 2 𝑛
𝑇(𝑛 − 𝑛) − 2 𝑛−1
− 2 𝑛−2
− 2 𝑛−3
… . 22
− 21
− 20
𝑇(𝑛) = 2 𝑛 − 2 𝑛−1 − 2 𝑛−2 − 2 𝑛−3 … . 22 − 21 − 20
𝑇(𝑛) = 2 𝑛
− (2 𝑛
− 1) [𝑛𝑜𝑡𝑒: 2 𝑛−1
+ 2 𝑛−2
+ ⋯ + 20
= 2 𝑛
]
𝑇(𝑛) = 1
∴ Time Complexity is O(1). Note that while the recurrence relation looks exponential the solution to the
recurrence relation here gives a different result.
Problem-23 What is the running time of the following function?
def Function(n):
i = s = 1
while s < n:
i = i+1
s = s+i
print("*")
Function(20)
Solution: Consider the comments in below function:
def Function(n):
i = s = 1
while s < n: # s is increasing not at rate 1 but i
i = i+1
s = s+i
print("*")
Function(20)
32. Data Structure and Algorithmic Thinking with Python Introduction
1.27 Problems on Algorithms Analysis 32
We can define the terms ‘𝑠’ according to the relation 𝑠𝑖= 𝑠𝑖−1 + 𝑖. The value of ‘𝑖’ increases by 1 for each iteration.
The value contained in ‘𝑠’ at the 𝑖 𝑡ℎ
iteration is the sum of the first ‘𝑖’ positive integers. If 𝑘 is the total number of
iterations taken by the program, then 𝑤ℎ𝑖𝑙𝑒 loop terminates if:
1 + 2 + . . . + 𝑘 =
𝑘(𝑘+1)
2
> 𝑛 ⟹ 𝑘 = O(√ 𝑛).
Problem-24 Find the complexity of the function given below.
def Function(n):
i = 1
count = 0
while i*i <n:
count = count +1
i = i + 1
print(count)
Function(20)
Solution: In the above-mentioned function the loop will end, if 𝑖2
≤ 𝑛 ⟹ 𝑇(𝑛) = O(√ 𝑛). This is similar to
Problem-23.
Problem-25 What is the complexity of the program given below:
def Function(n):
count = 0
for i in range(n/2, n):
j = 1
while j + n/2 <= n:
k = 1
while k <= n:
count = count + 1
k = k * 2
j = j + 1
print (count)
Function(20)
Solution: Observe the comments in the following function.
def Function(n):
count = 0
for i in range(n/2, n): #Outer loop execute n/2 times
j = 1
while j + n/2 <= n: #Middle loop executes n/2 times
k = 1
while k <= n: #Inner loop execute 𝑙𝑜𝑔𝑛 times
count = count + 1
k = k * 2
j = j + 1
print (count)
Function(20)
The complexity of the above function is O(𝑛2
𝑙𝑜𝑔𝑛).
Problem-26 What is the complexity of the program given below:
def Function(n):
count = 0
for i in range(n/2, n):
j = 1
while j + n/2 <= n:
k = 1
while k <= n:
count = count + 1
k = k * 2
j = j * 2
print (count)
Function(20)