To My Parents Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers


 Laurence Flowers
 2 years ago
 Views:
Transcription
1
2 To My Parents Laxmi and Modaiah To My Family Members To My Friends To IIT Bombay To All Hard Workers
3
4 Copyright 2010 by CareerMonk.com All rights reserved. Designed by Narasimha Karumanchi Printed in India
5
6 Acknowledgements I would like to express my gratitude to the 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. I would like to thank h for encouraging me when I was at IIT Bombay. He is the first person who taught me the importance of h and its. From that day I keep on updating myself. I would like to thank h h [ h ] and [ ] for spending time in reviewing this book and providing me the valuable suggestions almost every day. I would like to thank h [, ] for spending his valuable time in reviewing the book and suggestions. His review gave me the confidence in the quality of the book. I would like to thank and [Founder s of h. ] for approaching me for teaching Data Structures and Algorithms at their training centers. They are the primary reason for initiation of this book. My and have contributed greatly to the quality of this book. I thank all of you for your help and suggestions. Special thanks should go to my wife, for her encouragement and help during writing of this book. Last but not least, I would like to thank Director s of, h h [Director of ], [Former Director of ] and [ h, ] for helping me and my family during our studies. Narasimha Karumanchi MTech, IIT Bombay Founder of CareerMonk.com
7
8 Preface Dear Reader, Please Hold on! I know many people do not read preface. But I would like to strongly recommend reading preface of this book at least. This preface has h from regular prefaces. As a if you read complete book with good understanding, I am sure you will challenge the interviewer s and that is the objective of this book. If you read as an, you will give better lectures with easy go approach and as a result your students will feel proud for selecting Computer Science / Information Technology as their degree. This book is very much useful for the of and during their academic preparations. All the chapters of this book contain theory and their related problems as many as possible. There a total of approximately 700 algorithmic puzzles and all of them are with solutions. If you read as a preparing for competition exams for Computer Science/Information Technology], the content of this book covers the topics in full details. While writing the book, an intense care has been taken to help students who are preparing for these kinds of exams. 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 of the problems, solutions are provided with different 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, at least one complete reading of this book is required to get full understanding of all the topics. In the subsequent readings, readers can directly go to any chapter and refer. Even though, enough readings were given for correcting the errors, due to human tendency there could be some minor typos in the book. If any such typos found, they will be updated at... I request readers to constantly monitor this site for any corrections, new problems and solutions. Also, please provide your valuable suggestions Wish you all the best. Have a nice reading. Narasimha Karumanchi MTech, IIT Bombay Founder of CareerMonk.com
9
10 Table of Contents Chapter 1 Introduction Variables Data types System defined data types (Primitive data types) User defined data types Data Structure Abstract Data Types (ADT s) Memory and Variables Size of a Variable Address of a Variable Pointers Declaration of Pointers Pointers Usage Pointer Manipulation Arrays and Pointers Dynamic Memory Allocation Function Pointers Parameter Passing Techniques Actual and Formal Parameters Semantics of Parameter Passing Language Support for Parameter Passing Techniques Pass by Value Pass by Result Pass by ValueResult Pass by Reference (aliasing) Pass by Name Binding Binding Times Static Binding (Early binding) Dynamic Binding (Late binding) Scope... 44
11 Static Scope Dynamic Scope Storage Classes Auto Storage Class Extern storage class Register Storage Class Static Storage Class Storage Organization Static Segment Stack Segment Heap Segment Shallow Copy versus Deep Copy Chapter 2 Analysis of Algorithms Introduction What is an Algorithm? Why Analysis of Algorithms? Goal of Analysis of Algorithms? What is Running Time Analysis? How to Compare Algorithms? What is Rate of Growth? Commonly used Rate of Growths Types of Analysis Asymptotic Notation? BigO Notation BigO Visualization BigO Examples No Uniqueness? OmegaΩ Notation Ω Examples Thetaθ Notation Θ Examples Important Notes Why is it called Asymptotic Analysis?... 67
12 Guidelines for Asymptotic Analysis? Properties of Notations Commonly used Logarithms and Summations Master Theorem for Divide and Conquer Problems Divide and Conquer Master Theorem Master Theorem for Subtract and Conquer Recurrences Variant of subtraction and conquer master theorem Problems on Algorithms Analysis Chapter 3 Recursion and Backtracking Introduction What is Recursion? Why Recursion? Format of a Recursive Function Recursion and Memory (Visualization) Recursion versus Iteration Recursion Iteration Notes on Recursion Example Algorithms of Recursion Problems on Recursion What is Backtracking? Example Algorithms Of Backtracking Problems On Backtracking Chapter 4 Linked Lists What is a Linked List? Linked Lists ADT Why Linked Lists? Arrays Overview Why Constant Time for Accessing Array Elements? Advantages of Arrays Disadvantages of Arrays Dynamic Arrays Advantages of Linked Lists... 97
13 Issues with Linked Lists (Disadvantages) Comparison of Linked Lists with Arrays and Dynamic Arrays Singly Linked Lists Basic Operations on a List Traversing the Linked List Singly Linked List Insertion Inserting a Node in Singly Linked List at the Beginning Inserting a Node in Singly Linked List at the Ending Inserting a Node in Singly Linked List in the Middle Singly Linked List Deletion Deleting the First Node in Singly Linked List Deleting the last node in Singly Linked List Deleting an Intermediate Node in Singly Linked List Deleting Singly Linked List Doubly Linked Lists Doubly Linked List Insertion Inserting a Node in Doubly Linked List at the Beginning Inserting a Node in Doubly Linked List at the Ending Inserting a Node in Doubly Linked List in the Middle Doubly Linked List Deletion Deleting the First Node in Doubly Linked List Deleting the Last Node in Doubly Linked List Deleting an Intermediate Node in Doubly Linked List Circular Linked Lists Counting Nodes in a Circular List Printing the contents of a circular list Inserting a Node at the End of a Circular Linked List Inserting a Node at Front of a Circular Linked List Deleting the Last Node in a Circular List Deleting the First Node in a Circular List Applications of Circular List A MemoryEfficient Doubly Linked List Problems on Linked Lists
14 Chapter 5 Stacks What is a Stack? How are Stacks Used? Stack ADT Main stack operations Auxiliary stack operations Exceptions Applications Implementation Simple Array Implementation Dynamic Array Implementation Performance Linked List Implementation Performance Comparison of Implementations Comparing Incremental Strategy and Doubling Strategy Comparing Array Implementation and Linked List Implementation Problems on Stacks Chapter 6 Queues What is a Queue? How are Queues Used? Queue ADT Main queue operations Auxiliary queue operations Exceptions Applications Direct applications Indirect applications Implementation Why Circular Arrays? Simple Circular Array Implementation Performance & Limitations Dynamic Circular Array Implementation
15 Performance Linked List Implementation Performance Comparison of Implementations Problems on Queues Chapter 7 Trees What is a Tree? Glossary Binary Trees Types of Binary Trees Properties of Binary Trees Structure of Binary Trees Operations on Binary Trees Applications of Binary Trees Binary Tree Traversals Traversal Possibilities Classifying the Traversals PreOrder Traversal InOrder Traversal PostOrder Traversal Level Order Traversal Problems on Binary Trees Generic Trees (Nary Trees) Representation of Generic Trees Problems on Generic Trees Threaded Binary Tree Traversals [Stack or Queue less Traversals] Issues with Regular Binary Trees Motivation for Threaded Binary Trees Classifying Threaded Binary Trees Types of Threaded Binary Trees Threaded Binary Tree structure Difference between Binary Tree and Threaded Binary Tree Structures Finding Inorder Successor in Inorder Threaded Binary Tree
16 Inorder Traversal in Inorder Threaded Binary Tree Finding PreOrder Successor in InOrder Threaded Binary Tree PreOrder Traversal of InOrder Threaded Binary Tree Insertion of Nodes in InOrder Threaded Binary Trees Problems on Threaded binary Trees Expression Trees Algorithm for Building Expression Tree from Postfix Expression Example XOR Trees Binary Search Trees (BSTs) Why Binary Search Trees? Binary Search Tree Property Binary Search Tree Declaration Operations on Binary Search Trees Important Notes on Binary Search Trees Finding an Element in Binary Search Trees Finding an Minimum Element in Binary Search Trees Finding an Maximum Element in Binary Search Trees Where is Inorder Predecessor and Successor? Inserting an Element from Binary Search Tree Deleting an Element from Binary Search Tree Problems on Binary Search Trees Balanced Binary Search Trees Complete Balanced Binary Search Trees AVL (AdelsonVelskii and Landis) trees Properties of AVL Trees Minimum/Maximum Number of Nodes in AVL Tree AVL Tree Declaration Finding Height of an AVL tree Rotations Observation Types of Violations Single Rotations
17 Double Rotations Insertion into an AVL tree Problems on AVL Trees Other Variations in Trees RedBlack Trees Splay Trees Augmented Trees Interval Trees Chapter 8 Priority Queue and Heaps What is a Priority Queue? Priority Queue ADT Main Priority Queues Operations Auxiliary Priority Queues Operations Priority Queue Applications Priority Queue Implementations Unordered Array Implementation Unordered List Implementation Ordered Array Implementation Ordered List Implementation Binary Search Trees Implementation Balanced Binary Search Trees Implementation Binary Heap Implementation Comparing Implementations Heaps and Binary Heap What is a Heap? Types of Heaps? Binary Heaps Representing Heaps Declaration of Heap Creating Heap Parent of a Node Children of a Node Getting the Maximum Element
18 Heapifying an Element Deleting an Element Inserting an Element Destroying Heap Heapifying the Array Heapsort Problems on Priority Queues [Heaps] Chapter 9 Disjoint Sets ADT Introduction Equivalence Relations and Equivalence Classes Disjoint Sets ADT Applications Tradeoffs in Implementing Disjoint Sets ADT Fast FIND Implementation (Quick FIND) Fast UNION Implementation (Quick UNION) Fast UNION implementation (Slow FIND) Fast UNION implementations (Quick FIND) UNION by Size UNION by Height (UNION by Rank) Comparing UNION by Size and UNION by Height Path Compression Summary Problems on Disjoint Sets Chapter 10 Graph Algorithms Introduction Glossary Applications of Graphs Graph Representation Adjacency Matrix Adjacency List Adjacency Set Comparison of Graph Representations Graph Traversals
19 Depth First Search [DFS] Breadth First Search [BFS] Comparing DFS and BFS Topological Sort Applications of Topological Sorting Shortest Path Algorithms Shortest Path in Unweighted Graph Shortest path in Weighted Graph [Dijkstra s] BellmanFord Algorithm Overview of Shortest Path Algorithms Minimal Spanning Tree Prim's Algorithm Kruskal s Algorithm Problems on Graph Algorithms Chapter 11 Sorting What is Sorting? Why Sorting? Classification By Number of Comparisons By Number of Swaps By Memory Usage By Recursion By Stability By Adaptability Other Classifications Internal Sort External Sort Bubble sort Implementation Performance Selection Sort Algorithm Implementation
20 Performance Insertion sort Advantages Algorithm Implementation Example Analysis Performance Comparisons to Other Sorting Algorithms Shell sort Implementation Analysis Performance Merge sort Important Notes Implementation Analysis Performance Heapsort Performance Quicksort Algorithm Implementation Analysis Performance Randomized Quick sort Tree Sort Performance Comparison of Sorting Algorithms Linear Sorting Algorithms Counting Sort Bucket sort [or Bin Sort] Radix sort
21 Topological Sort External Sorting Problems on Sorting Chapter 12 Searching What is Searching? Why Searching? Types of Searching Unordered Linear Search Sorted/Ordered Linear Search Binary Search Comparing Basic Searching Algorithms Symbol Tables and Hashing String Searching Algorithms Problems on Searching Chapter 13 Selection Algorithms [Medians] What are Selection Algorithms? Selection by Sorting Partitionbased Selection Algorithm Linear Selection algorithm  Median of Medians algorithm Finding the k Smallest Elements in Sorted Order Problems on Selection Algorithms Chapter 14 Symbol Tables Introduction What are Symbol Tables? Symbol Table Implementations Unordered Array Implementation Ordered [Sorted] Array Implementation Unordered Linked List Implementation Ordered Linked List Implementation Binary Search Trees Implementation Balanced Binary Search Trees Implementation Ternary Search Implementation Hashing Implementation
22 Comparison of Symbol Table Implementations Chapter 15 Hashing What is Hashing? Why Hashing? HashTable ADT Understanding Hashing If Arrays Are There Why Hashing? Components in Hashing Hash Table Hash Function How to Choose Hash Function? Characteristics of Good Hash Functions Load Factor Collisions Collision Resolution Techniques Separate Chaining Open Addressing Linear Probing Quadratic Probing Double Hashing Comparison of Collision Resolution Techniques Comparisons: Linear Probing vs. Double Hashing Comparisons: Open Addressing vs. Separate Chaining Comparisons: Open Addressing methods How Hashing Gets O(1) Complexity? Hashing Techniques Static Hashing Dynamic Hashing Problems for which Hash Tables are not Suitable Problems on Hashing Chapter 16 String Algorithms Introduction String Matching Algorithms
23 Brute Force Method RobinKarp String Matching Algorithm Selecting Hash Function Step by Step explanation String Matching with Finite Automata Finite Automata How Finite Automata Works? Important Notes for Constructing the Finite Automata Matching Algorithm KMP Algorithm Filling Prefix Table Matching Algorithm BoyceMoore Algorithm Data structures for Storing Strings Hash Tables for Strings Binary Search Trees for Strings Issues with Binary Search Tree Representation Tries What is a Trie? Why Tries? Inserting a String in Trie Searching a String in Trie Issues with Tries Representation Ternary Search Trees Ternary Search Trees Declaration Inserting strings in Ternary Search Tree Searching in Ternary Search Tree Displaying All Words of Ternary Search Tree Finding Length of Largest Word in TST Comparing BSTs, Tries and TSTs Suffix Trees Prefix and Suffix Observation
24 What is a Suffix Tree? The Construction of Suffix Trees Applications of Suffix Trees Problems on Strings Chapter 17 Algorithms Design Techniques Introduction Classification Classification by Implementation Method Recursion or Iteration Procedural or Declarative (NonProcedural) Serial or Parallel or Distributed Deterministic or NonDeterministic Exact or Approximate Classification by Design Method Greedy Method Divide and Conquer Dynamic Programming Linear Programming Reduction [Transform and Conquer] Other Classifications Classification by Research Area Classification by Complexity Randomized Algorithms Branch and Bound Enumeration and Backtracking Chapter 18 Greedy Algorithms Introduction Greedy strategy Elements of Greedy Algorithms Greedy choice property Optimal substructure Does Greedy Works Always? Advantages and Disadvantages of Greedy Method Greedy Applications
25 Understanding Greedy Technique Huffman coding algorithm Problems on greedy algorithms Chapter 19 Divide and Conquer Algorithms Introduction What is Divide and Conquer Strategy? Does Divide and Conquer Work Always? Divide and Conquer Visualization Understanding Divide and Conquer Advantages of Divide and Conquer Disadvantages of Divide and Conquer Master Theorem Divide and Conquer Applications Problems on Divide and Conquer Chapter 20 Dynamic Programming Introduction What is Dynamic Programming Strategy? Can Dynamic Programming Solve Any Problem? Dynamic Programming Approaches Bottomup Dynamic Programming Topdown Dynamic Programming Bottomup versus Topdown Programming Examples of Dynamic Programming Algorithms Understanding Dynamic Programming Fibonacci Series Observations Factorial of a Number Problems on Dynamic Programming Chapter 21 Complexity Classes Introduction Polynomial/exponential time What is Decision Problem? Decision Procedure
26 What is a Complexity Class? Types of Complexity Classes P Class NP Class CoNP Class Relationship between P, NP and CoNP NPhard Class NPcomplete Class Relationship between P, NP CoNP, NPHard and NPComplete Does P==NP? Reductions Important NPComplete Problems (Reductions) Problems on Complexity Classes Chapter 22 Miscellaneous Concepts Introduction Hacks on Bitwise Programming Bitwise AND Bitwise OR Bitwise ExclusiveOR Bitwise Left Shift Bitwise Right Shift Bitwise Complement Checking whether Kth bit is set or not Setting Kth bit Clearing Kth bit Toggling Kth bit Toggling Rightmost One bit Isolating Rightmost One bit Isolating Rightmost Zero bit Checking Whether Number is Power of 2 not Multiplying Number by Power of Dividing Number by Power of Finding Modulo of a Given Number
27 Reversing the Binary Number Counting Number of One s in number Creating for Mask for Trailing Zero s
28 DATA STRUCTURES AND ALGORITHMS MADE EASY
29
30 Chapter 2 ANALYSIS OF ALGORITHMS Introduction The objective of this chapter is to explain the importance of analysis of algorithms, their notations, relationships and solving as many problems as possible. We first concentrate on understanding the importance of analysis and then slowly move towards analyzing the algorithms with different notations and finally, the problems. After completion of this chapter you should be able to find the complexity of any given algorithm (especially recursive functions). What is an Algorithm? Just to understand better, let us consider the problem of preparing an omelet. For preparing omelet, general steps which we follow are: 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? 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 stepbystep instructions to a given problem. One important note to remember while writing the algorithms is: we do not have to prove each step of the algorithm. Why Analysis of Algorithms? If we want to go from city to city There can be many ways of doing this: by flight, by bus, by train and also by cycle. Depending on the availability and convenience we choose the one which suits us. Similarly, in computer science there can be multiple algorithms exist for solving the same problem (for 57 Analysis of Algorithms Introduction
31 example, sorting problem has lot of 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. 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, developer's effort etc.) 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 number of elements in the input and depending on the problem type the input may be of different types. In general, we encounter the following 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 How to Compare Algorithms? To compare algorithms, let us define some. Execution times? Number of statements executed? as execution times are specific to a particular computer. 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., ). We can compare these different functions corresponding to running times and this kind of comparison is independent of machine time, programming style, etc.. 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 for buying a car and a cycle. If your friend sees you there and asks what you are buying then in general we say This is because cost of car is too big compared to cost of cycle ( approximating the cost of cycle to cost of car). Total Cost = cost_of_car + cost_of_cycle Total Cost cost_of_car (approximation) 58 Analysis of Algorithms Goal of Analysis of Algorithms?
32 For the above example, we can represent the cost of car and cost of cycle in terms of function and for a given function we ignore the low order terms that are relatively insignificant (for large value of input size, ). As an example in the below case,,, and are the individual costs of some function and we approximate it to. Since, is the highest rate of growth. Commonly used Rate of Growths Below diagram shows the relationship between different rates of growth. D e c r e a s i n g R a t e s O f G r o w t h 59 Analysis of Algorithms Commonly used Rate of Growths
33 Below is the list of rate of growths which come across in remaining chapters. Time complexity Name Example 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 divideandconquer Mergesort Quadratic Shortest path between two nodes in a graph Cubic Matrix Multiplication Exponential The Towers of Hanoi problem Types of Analysis If we have an algorithm for a problem and want to know on what inputs the algorithm is taking less time (performing well) and on what inputs the algorithm is taking huge 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 case where it is taking the less time and other for case where it is taking the more time. In general the first case is called the best case and second case is called the worst case for 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 huge 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 best case, worst case, and average case analysis in the form of expressions. As an example, let be the function which represents the given algorithm., for worst case for best case 60 Analysis of Algorithms Types of Analysis
34 Similarly, for average case too. The expression defines the inputs with which the algorithm takes the average running time (or memory). Asymptotic Notation? Having the expressions for best case, average case and worst case, for all the three cases we need to identify the upper bound, lower bounds. In order to represent these upper bound and lower bounds we need some syntax and that is the subject of following discussion. Let us assume that the given algorithm is represented in the form of function. BigO Notation This notation gives the upper bound of the given function. Generally we represent it as. That means, at larger values of, the upper bound of is. For example, if is the given algorithm, then is. That means gives the maximum rate of growth for at larger values of. Let us see the notation with little more detail. notation defined as there exist positive constants and such that for all. 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. In general, we discard lower values of. That means the rate of growth at lower values of is not important. In the below figure, is the point from which we need to consider the rate of growths for a given algorithm. Below the rate of growths could be different. Rate of Growth BigO Visualization Input Size, is the set of functions with smaller or same order of growth as For example, includes etc.. Note: Analyze the algorithms at larger values of only. What this means is, below we do not care for rate of growth. 61 Analysis of Algorithms Asymptotic Notation?
35 .,., BigO Examples Example1 Find upper bound for Solution: for all = with c = 4 and Example2 Find upper bound for Solution: for all = with and Example3 Find upper bound for Solution: for all = with and Example4 Find upper bound for Solution: for all = with and Example5 Find upper bound for Solution: for all = with and Example6 Find upper bound for Solution: for all with and 62 Analysis of Algorithms BigO Notation
36 No Uniqueness? There is no unique set of values for and in proving the asymptotic bounds. Let us consider, For this function there are multiple and values possible. Solution1: for all and is a solution. Solution2: for all and is also a solution. OmegaΩ Notation Similar to discussion, this notation gives the tighter lower bound of the given algorithm and we represent it as. That means, at larger values of, the tighter lower bound of is For example, if, is The notation can be defined as there exist positive constants and such that for all 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. Rate of Growth Ω Examples Input Size, Example1 Find lower bound for Solution: Such that: and = 1 with and = 1 Example2 Prove Solution: Such that: 63 Analysis of Algorithms OmegaΩ Notation
37 Example3 Since is positive Contradiction: cannot be smaller than a constant Thetaθ Notation This notation decides whether the upper and lower bounds of a given function (algorithm) are same or not. The average running time of algorithm is always between lower bound and upper bound. If the upper bound ( ) and lower bound ( ) gives the same result then notation will also have the same rate of growth. As an example, let us assume that is the expression. Then, its tight upper bound is. The rate of growth in best case is. In this case, rate of growths in 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 and are not same then the rate of growth case may not be same. Rate of Growth Input Size, Now consider the definition of notation. It is defined as there exist positive constants and such that for all. is an asymptotic tight bound for is the set of functions with the same order of growth as Θ Examples Example1 Find θ bound for Solution:, for all, with and = 1 Example2 Prove Solution: only holds for: 64 Analysis of Algorithms Thetaθ Notation
38 Example3 Prove Solution: 6 only holds for: Example4 Prove Solution: Impossible Important Notes For analysis (best case, worst case and average) we try to give upper bound ( ) and lower bound ( ) and average running time. From the above examples, it should also be clear that, for a given function (algorithm) getting upper bound ( ) and lower bound ( ) 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 ( ) and lower bound ( ) and average running time ( ). In the remaining chapters we generally concentrate on upper bound ( ) because knowing lower bound ( ) of an algorithm is of no practical importance and we use notation if upper bound ( ) and lower bound ( ) are same. Why is it called Asymptotic Analysis? From the above discussion (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 as. In other terms, is the asymptotic curve for For this reason, we call algorithm analysis as. Guidelines for Asymptotic Analysis? There are some general rules to help us in determining the running time of an algorithm. Below are few of them. 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=1; i<=n; i++) m = m + 2; // constant time, c 65 Analysis of Algorithms Why is it called Asymptotic Analysis?
39 Total time = a constant 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=1; i<=n; i++) // inner loop executed n times for (j=1; j<=n; j++) k = k+1; //constant time Total time 3) Consecutive statements: Add the time complexities of each statement. x = x +1; //constant time // executed n times for (i=1; i<=n; i++) m = m + 2; //constant time //outer loop executed n times for (i=1; i<=n; i++) //inner loop executed n times for (j=1; j<=n; j++) k = k+1; //constant time Total time 4) Ifthenelse statements: Worstcase running time: the test, plus the part or the part (whichever is the larger). //test: constant if (length ( )!= otherstack. length ( ) ) return false; //then part: constant 66 Analysis of Algorithms Guidelines for Asymptotic Analysis?
40 else // else part: (constant + constant) * n for (int n = 0; n < length( ); n++) // another if : constant + constant (no else part) if (!list[n].equals(otherstack.list[n])) //constant return false; Total time 5) Logarithmic complexity: An algorithm is 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: for (i=1; i<=n;) i = i*2; If we observe carefully, the value of i is doubling every time. Initially in next step and in subsequent steps and so on. Let us assume that the loop is executing some times. That means at step and we come out of loop. So, if we take logarithm on both sides, ( ) So the total time //if we assume base2 Note: Similarly, for the below case also, worst case rate of growth is discussion holds good for decreasing sequence also. That means, the same for (i=n; i<=1;) i = i/2; 67 Analysis of Algorithms Guidelines for Asymptotic Analysis?
41 Another example algorithm is binary search: finding a word in a dictionary of pages Look at the centre point in the dictionary Is word towards left or right of centre? Repeat process with left or right part of dictionary until the word is found Properties of Notations Transitivity: and Valid for and also. Reflexivity: Valid for and also. Symmetry: if and only if Transpose symmetry: if and only if Commonly used Logarithms and Summations Logarithms Arithmetic series Geometric series Harmonic series Other important formulae Master Theorem for Divide and Conquer 68 Analysis of Algorithms Properties of Notations
42 In all divide and conquer algorithms we divide the problem into subproblems, each of which is some part of the original problem, and then perform some additional work to compute the final answer. As an example, if we consider merge sort [for details, refer chapter], it operates on two subproblems, each of which is half the size of the original, and then uses additional work for merging. This gives the running time equation: ( ) 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 directly give the answer without fully solving it. If the recurrence is of the form where and is a real number, then: 1), then ( ) 2) 3) a. If, then ( ) b. If, then ( ) c. If, then ( ) a. If, then ( ) b. If, then ( ) Problems 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. Problem1 Solution: (Master Theorem Case 3.a) Problem2 Solution: (Master Theorem Case 2.a) Problem3 Solution: (Master Theorem Case 3.a) Problem4 Solution: => Does not apply ( is not constant) Problem5 69 Analysis of Algorithms Problems Divide and Conquer Master Theorem
43 Solution: (Master Theorem Case 1) Problem6 Solution: (Master Theorem Case 2.a) Problem7 Solution: (Master Theorem Case 2.b) Problem8 Solution: (Master Theorem Case 3.b) Problem9 Solution: Does not apply ( ) Problem10 Solution: (Master Theorem Case 3.a) Problem11 Solution: Does not apply (function is not positive) Problem12 Solution: (Master Theorem Case 3.as) Problem13 Solution: (Master Theorem Case 1) Problem14 Solution: (Master Theorem Case 3.a) Problem15 Solution: (Master Theorem Case 1) Problem16 Solution: (Master Theorem Case 1) Problem17 Solution: (Master Theorem Case 1) Problem18 Solution: (Master Theorem Case 1) 70 Analysis of Algorithms Problems Divide and Conquer Master Theorem
44 Problem19 Solution: (Master Theorem Case 3.a) Problem20 Solution: (Master Theorem Case 2.a) Master Theorem for Subtract and Conquer Recurrences Let be a function defined on positive, and having the property for some constants, and function If is in then ( ) ( ) ( ) Variant of subtraction and conquer master theorem The solution to the equation, where and are constants, is Problems on Algorithms Analysis Note: From the following problems, try to understand in what cases we get different complexities ( etc..). Problem21 Find the complexity of the below recurrence: Solution: Let us try to solve this function with substitution. ( ) 71 Analysis of Algorithms Master Theorem for Subtract and Conquer Recurrences
45 This clearly shows that the complexity of this function is. Note: We can use the master theorem for this problem. Problem22 Find the complexity of the below recurrence: Solution: Let us try to solve this function with substitution. Complexity is Note that while the recurrence relation looks exponential the solution to the recurrence relation here gives a different result. Problem23 What is the running time of the following function (specified as a function of the input value ) void Function(int n) int i=1 ; int s=1 ; while( s <= n) i++ ; s= s+i ; print(\*"); Solution: Consider the comments in below function: void Function (int n) int i=1 ; int s=1 ; // s is increasing not at rate 1 but i while( s <= n) i++ ; 72 Analysis of Algorithms Problems on Algorithms Analysis
46 s= s+i ; print( *"); We can define the terms according to the relation = The value of increases by one for each iteration. So 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 the while loop terminates once. Problem24 Find the complexity of the function given below. void Function(int n) int i, count =0;; for(i=1; i*i<=n; i++) count++; Solution: Consider the comments in below function: void Function(int n) int i, count =0;; for(i=1; i*i<=n; i++) count++; In the above function the loop will end, if Problem23.. The reasoning is same as that of Problem25 What is the complexity of the below program: void function(int n) int i, j, k, count =0; for(i=n/2; i<=n; i++) for(j=1; j + n/2<=n; j= j++) for(k=1; k<=n; k= k * 2) count++; Solution: Consider the comments in the following function. void function(int n) int i, j, k, count =0; 73 Analysis of Algorithms Problems on Algorithms Analysis
47 //outer loop execute n/2 times for(i=n/2; i<=n; i++) //Middle loop executes n/2 times for(j=1; j + n/2<=n; j= j++) //outer loop execute logn times for(k=1; k<=n; k= k * 2) count++; The complexity of the above function is Problem26 What is the complexity of the below program: void function(int n) int i, j, k, count =0; for(i=n/2; i<=n; i++) for(j=1; j<=n; j= 2 * j) for(k=1; k<=n; k= k * 2) count++; Solution: Consider the comments in the following function. void function(int n) int i, j, k, count =0; //outer loop execute n/2 times for(i=n/2; i<=n; i++) //Middle loop executes logn times for(j=1; j<=n; j= 2 * j) //outer loop execute logn times for(k=1; k<=n; k= k*2) count++; The complexity of the above function is Problem27 Find the complexity of the below program. function( int n ) if (n == 1) return; for( i = 1 ; i <= n ; i + + ) for( j= 1 ; j <= n ; j + + ) print( *" ) ; 74 Analysis of Algorithms Problems on Algorithms Analysis
48 break; Solution: Consider the comments in the following function. function( int n ) //constant time if ( n == 1 ) return; //outer loop execute times for( i = 1 ; i <= n ; i + + ) // inner loop executes only time due to statement. for( j= 1 ; j <= n ; j + + ) print( *" ) ; break; The complexity of the above function is break statement it is executing only once.. Even though the inner loop is bounded by, but due to the Problem28 Write a recursive function for the running time of the function function, whose code is below. Prove using the iterative method that function( int n ) if ( n == 1 ) return ; for( i = 1 ; i <= n ; i + + ) for( j = 1 ; j <= n ; j + + ) print( *" ) ; function( n3 ); Solution: Consider the comments in below function: function (int n) //constant time if ( n == 1 ) return ; //outer loop execute times for( i = 1 ; i <= n ; i + + ) 75 Analysis of Algorithms Problems on Algorithms Analysis
49 //inner loop executes n times for( j = 1 ; j <= n ; j + + ) //constant time print( *" ) ; function( n3 ); The recurrence for this code is clearly T for some constant since each call prints out asterisks and calls itself recursively on n  3. Using the iterative method we get: Using the master theorem, we get Problem29 Determine bounds for the recurrence relation: ( ) Solution: Using Divide and Conquer master theorem, we get. Problem30 Determine bounds for the recurrence: Solution: Substituting in the recurrence equation, we get:, where is a constant. Problem31 Determine bounds for the recurrence relation: Solution: Using Master Theorem we get Problem32 Prove that the running time of the code below is Read(int n); int k = 1 ; while( k < n ) k = 3k; Solution: The while loop will terminate once the value of is greater than or equal to the value of Since each loop the value of is being multiplied by, if i is the number of iterations, then has the value of 3i after i iterations. That is the loop is terminated upon reaching i iterations when n i, which shows that Problem33 Solve the following recurrence. 76 Analysis of Algorithms Problems on Algorithms Analysis
50 Solution: By iteration: Note: We can use the master theorem for this problem. Problem34 Consider the following program: Fib[n] if (n==0) then return 0 else if (n==1) then return 1 else return Fib[n1]+Fib[n2] Solution: The recurrence relation for running time of this program is Notice has two recurrence calls indicating a binary tree. Each step recursively calls the program for reduced by and, so the depth of the recurrence tree is The number of leaves at depth is since this is a full binary tree, and each leaf takes at least computation for the constant factor. Running time is clearly exponential in. Problem35 Running time of following program? function(n) for( i = 1 ; i <= n ; i + + ) for( j = 1 ; j <= n ; j+ = i ) print( * ) ; Solution: Consider the comments in below function: function (n) //this loop executes n times for( i = 1 ; i <= n ; i + + ) 77 Analysis of Algorithms Problems on Algorithms Analysis
51 //this loop executes j times with j increase by the rate of i for( j = 1 ; j <= n ; j+ = i ) print( * ) ; Its running time is since the inner loop is same as that of Problem23]. Problem36 What is the complexity of? Solution: Using the logarithmic property, equivalent to we can see that this problem is This shows that that the time complexity. Problem37 What is the running time of the following recursive function (specified as a function of the input value )? First write the recurrence formula and then find its complexity. function(int n) if (n <= 1) return ; for (int i=1 ; i <= 3; i++ ) f( ); Solution: Consider the comments in below function: function (int n) //constant time if (n <= 1) return ; //this loop executes with recursive loop of value for (int i=1 ; i <= 3; i++ ) f( ); 78 Analysis of Algorithms Problems on Algorithms Analysis
52 We can assume that for asymptotical analysis for every integer. The recurrence for this code is. Using master theorem, we get Problem38 What is the running time of the following recursive function (specified as a function of the input value? First write a recurrence formula, and show its solution using induction. function(int n) if (n <= 1) return; for (i=1 ; i <= 3 ; i++ ) function (n 1). Solution: Consider the comments in below function: function (int n) //constant time if (n <= 1) return; //this loop executes 3 times with recursive call of n1 value for (i=1 ; i <= 3 ; i++ ) function (n 1). The requires constant time ( ). With the, we neglect the loop overhead and only count the three times that the function is called recursively. This implies a time complexity recurrence: Now we use repeated substitution to guess at the solution when we substitute times: Using the master theorem, we get Problem39 Write a recursion formula for the running time of the function, whose code is below. What is the running time of, as a function of? function (int n) if (n <= 1) return; 79 Analysis of Algorithms Problems on Algorithms Analysis
53 int i = 1 ; for(i = 1; i < n; i + +) print( * ); function ( 0.8n ) ; Solution: Consider the comments in below function: function (int n) //constant time if (n <= 1) return; //constant time int i = 1 ; // this loop executes times with constant time loop for(i = 1; i < n; i + +) print( * ); //recursive call with 0.8n function ( 0.8n ) ; The recurrence for this piece of code is Applying master theorem we get Problem40 Find the complexity of the recurrence: Solution: The given recurrence is not in the master theorem form. Let try to convert this master theorem format. For that let use assume that Applying logarithm on both sides gives, Now, the given function becomes, ( ) ( ). To make it simple we assume ( ) Applying the master theorem would result If we substitute back,. Problem41 Find the complexity of the recurrence: 80 Analysis of Algorithms Problems on Algorithms Analysis
54 Solution: We apply the same logic as that of Problem40 and we get ( ) Applying the master theorem would result. Substituting, gives. Problem42 Find the complexity of the recurrence: Solution: Applying the logic of Problem40, gives: ( ) Using the master theorem results ( ). Substituting gives. Problem43 Find the complexity of the below function. int Function (int n) if (n <= 2) return 1; else return (Function (floor(sqrt(n))) + 1); Solution: Consider the comments in below function: int Function (int n) //constant time if (n <= 2) return 1; else // executes + 1 times return (Function (floor(sqrt(n))) + 1); For the above function, the recurrence function can be given as:. And, this is same as that of Problem41. Problem44 Analyze the running time of the following recursive procedure as a function of. void function(int n) if ( n < 2 ) return; else 81 Analysis of Algorithms Problems on Algorithms Analysis
55 counter = 0; for i = 1 to 8 do function ( ); for I =1 to do counter = counter + 1; Solution: Consider the comments in below function and let us refer to the running time of function (n) as. void function(int n) //constant time if ( n < 2 ) return; else counter = 0; // this loop executes 8 times with n value half in every call for i = 1 to 8 do function ( ); // this loop executes times with constant time loop for I =1 to do counter = counter + 1; can be defined as follows: Using the master theorem gives,. Problem45 Find the complexity of the below function. temp = 1 repeat for i = 1 to n temp = temp + 1; n = until n <= 1 Solution: Consider the comments in below function: //const time temp = 1 repeat 82 Analysis of Algorithms Problems on Algorithms Analysis
56 // this loops executes n times for i = 1 to n temp = temp + 1; //recursive call with n = until n <= 1 The recurrence for this function is. Using master theorem, we get, Problem46 Running time of following program? function(int n) for( i = 1 ; i <= n ; i + + ) for( j = 1 ; j <= n ; j * = 2 ) print( * ) ; Solution: Consider the comments in below function: function(int n) // this loops executes n times for( i = 1 ; i <= n ; i + + ) // this loops executes logn times from our logarithms //guideline for( j = 1 ; j <= n ; j * = 2 ) print( * ) ; Complexity of above program is : Problem47 Running time of following program? function(int n) for( i = 1 ; i <= n/3 ; i + + ) for( j = 1 ; j <= n ; j += 4 ) print( * ) ; Solution: Consider the comments in below function: function(int n) 83 Analysis of Algorithms Problems on Algorithms Analysis
AP Computer Science AB Syllabus 1
AP Computer Science AB Syllabus 1 Course Resources Java Software Solutions for AP Computer Science, J. Lewis, W. Loftus, and C. Cocking, First Edition, 2004, Prentice Hall. Video: Sorting Out Sorting,
More informationCSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Today Registration should be done. Homework 1 due 11:59 pm next Wednesday, January 14 Review math essential
More informationAlgorithms and Data Structures
Algorithm Analysis Page 1 BFHTI: Softwareschule Schweiz Algorithm Analysis Dr. CAS SD01 Algorithm Analysis Page 2 Outline Course and Textbook Overview Analysis of Algorithm PseudoCode and Primitive Operations
More informationWhy? A central concept in Computer Science. Algorithms are ubiquitous.
Analysis of Algorithms: A Brief Introduction Why? A central concept in Computer Science. Algorithms are ubiquitous. Using the Internet (sending email, transferring files, use of search engines, online
More informationJava Software Structures
INTERNATIONAL EDITION Java Software Structures Designing and Using Data Structures FOURTH EDITION John Lewis Joseph Chase This page is intentionally left blank. Java Software Structures,International Edition
More informationMathematics for Algorithm and System Analysis
Mathematics for Algorithm and System Analysis for students of computer and computational science Edward A. Bender S. Gill Williamson c Edward A. Bender & S. Gill Williamson 2005. All rights reserved. Preface
More informationBinary Heap Algorithms
CS Data Structures and Algorithms Lecture Slides Wednesday, April 5, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009 Glenn G. Chappell
More informationData Structures. Algorithm Performance and Big O Analysis
Data Structures Algorithm Performance and Big O Analysis What s an Algorithm? a clearly specified set of instructions to be followed to solve a problem. In essence: A computer program. In detail: Defined
More informationPseudo code Tutorial and Exercises Teacher s Version
Pseudo code Tutorial and Exercises Teacher s Version Pseudocode is an informal way to express the design of a computer program or an algorithm in 1.45. The aim is to get the idea quickly and also easy
More informationChapter 13: Query Processing. Basic Steps in Query Processing
Chapter 13: Query Processing! Overview! Measures of Query Cost! Selection Operation! Sorting! Join Operation! Other Operations! Evaluation of Expressions 13.1 Basic Steps in Query Processing 1. Parsing
More informationThe UnionFind Problem Kruskal s algorithm for finding an MST presented us with a problem in datastructure design. As we looked at each edge,
The UnionFind Problem Kruskal s algorithm for finding an MST presented us with a problem in datastructure design. As we looked at each edge, cheapest first, we had to determine whether its two endpoints
More informationBinary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *
Binary Heaps A binary heap is another data structure. It implements a priority queue. Priority Queue has the following operations: isempty add (with priority) remove (highest priority) peek (at highest
More informationEfficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.
Algorithms Efficiency of algorithms Computational resources: time and space Best, worst and average case performance How to compare algorithms: machineindependent measure of efficiency Growth rate Complexity
More informationHome Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit
Data Structures Page 1 of 24 A.1. Arrays (Vectors) nelement vector start address + ielementsize 0 +1 +2 +3 +4... +n1 start address continuous memory block static, if size is known at compile time dynamic,
More informationThe following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).
CHAPTER 5 The Tree Data Model There are many situations in which information has a hierarchical or nested structure like that found in family trees or organization charts. The abstraction that models hierarchical
More informationIMPROVING PERFORMANCE OF RANDOMIZED SIGNATURE SORT USING HASHING AND BITWISE OPERATORS
Volume 2, No. 3, March 2011 Journal of Global Research in Computer Science RESEARCH PAPER Available Online at www.jgrcs.info IMPROVING PERFORMANCE OF RANDOMIZED SIGNATURE SORT USING HASHING AND BITWISE
More informationBasic Programming and PC Skills: Basic Programming and PC Skills:
Texas University Interscholastic League Contest Event: Computer Science The contest challenges high school students to gain an understanding of the significance of computation as well as the details of
More informationInternational Journal of Software and Web Sciences (IJSWS) www.iasir.net
International Association of Scientific Innovation and Research (IASIR) (An Association Unifying the Sciences, Engineering, and Applied Research) ISSN (Print): 22790063 ISSN (Online): 22790071 International
More informationCS 2302 Data Structures Spring 2015
1. General Information Instructor: CS 2302 Data Structures Spring 2015 Olac Fuentes Email: ofuentes@utep.edu Web: www.cs.utep.edu/ofuentes Office hours: Tuesdays and Thursdays 2:003:30, or by appointment,
More informationCS/COE 1501 http://cs.pitt.edu/~bill/1501/
CS/COE 1501 http://cs.pitt.edu/~bill/1501/ Lecture 01 Course Introduction Metanotes These notes are intended for use by students in CS1501 at the University of Pittsburgh. They are provided free of charge
More informationBinary Trees and Huffman Encoding Binary Search Trees
Binary Trees and Huffman Encoding Binary Search Trees Computer Science E119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Motivation: Maintaining a Sorted Collection of Data A data dictionary
More informationComplexity Theory. IE 661: Scheduling Theory Fall 2003 Satyaki Ghosh Dastidar
Complexity Theory IE 661: Scheduling Theory Fall 2003 Satyaki Ghosh Dastidar Outline Goals Computation of Problems Concepts and Definitions Complexity Classes and Problems Polynomial Time Reductions Examples
More informationI PUC  Computer Science. Practical s Syllabus. Contents
I PUC  Computer Science Practical s Syllabus Contents Topics 1 Overview Of a Computer 1.1 Introduction 1.2 Functional Components of a computer (Working of each unit) 1.3 Evolution Of Computers 1.4 Generations
More informationSorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)
Sorting revisited How did we use a binary search tree to sort an array of elements? Tree Sort Algorithm Given: An array of elements to sort 1. Build a binary search tree out of the elements 2. Traverse
More informationLearning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees
Learning Outcomes COMP202 Complexity of Algorithms Binary Search Trees and Other Search Trees [See relevant sections in chapters 2 and 3 in Goodrich and Tamassia.] At the conclusion of this set of lecture
More informationNear Optimal Solutions
Near Optimal Solutions Many important optimization problems are lacking efficient solutions. NPComplete problems unlikely to have polynomial time solutions. Good heuristics important for such problems.
More informationCOMPUTER SCIENCE (5651) Test at a Glance
COMPUTER SCIENCE (5651) Test at a Glance Test Name Computer Science Test Code 5651 Time Number of Questions Test Delivery 3 hours 100 selectedresponse questions Computer delivered Content Categories Approximate
More informationA TOOL FOR DATA STRUCTURE VISUALIZATION AND USERDEFINED ALGORITHM ANIMATION
A TOOL FOR DATA STRUCTURE VISUALIZATION AND USERDEFINED ALGORITHM ANIMATION Tao Chen 1, Tarek Sobh 2 Abstract  In this paper, a software application that features the visualization of commonly used
More informationCost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:
CSE341T 08/31/2015 Lecture 3 Cost Model: Work, Span and Parallelism In this lecture, we will look at how one analyze a parallel program written using Cilk Plus. When we analyze the cost of an algorithm
More informationCOMPUTER SCIENCE TRIPOS
CST.98.5.1 COMPUTER SCIENCE TRIPOS Part IB Wednesday 3 June 1998 1.30 to 4.30 Paper 5 Answer five questions. No more than two questions from any one section are to be answered. Submit the answers in five
More informationChapter 6 Quantum Computing Based Software Testing Strategy (QCSTS)
Chapter 6 Quantum Computing Based Software Testing Strategy (QCSTS) 6.1 Introduction Software testing is a dual purpose process that reveals defects and is used to evaluate quality attributes of the software,
More information6 March 2007 1. Array Implementation of Binary Trees
Heaps CSE 0 Winter 00 March 00 1 Array Implementation of Binary Trees Each node v is stored at index i defined as follows: If v is the root, i = 1 The left child of v is in position i The right child of
More informationPRACTICE BOOK COMPUTER SCIENCE TEST. Graduate Record Examinations. This practice book contains. Become familiar with. Visit GRE Online at www.gre.
This book is provided FREE with test registration by the Graduate Record Examinations Board. Graduate Record Examinations This practice book contains one actual fulllength GRE Computer Science Test testtaking
More informationA Fast Algorithm For Finding Hamilton Cycles
A Fast Algorithm For Finding Hamilton Cycles by Andrew Chalaturnyk A thesis presented to the University of Manitoba in partial fulfillment of the requirements for the degree of Masters of Science in Computer
More informationCSC148 Lecture 8. Algorithm Analysis Binary Search Sorting
CSC148 Lecture 8 Algorithm Analysis Binary Search Sorting Algorithm Analysis Recall definition of Big Oh: We say a function f(n) is O(g(n)) if there exists positive constants c,b such that f(n)
More informationThe Goldberg Rao Algorithm for the Maximum Flow Problem
The Goldberg Rao Algorithm for the Maximum Flow Problem COS 528 class notes October 18, 2006 Scribe: Dávid Papp Main idea: use of the blocking flow paradigm to achieve essentially O(min{m 2/3, n 1/2 }
More informationComputer Science III
Computer Science III Dr. Chris Bourke Department of Computer Science & Engineering University of Nebraska Lincoln Lincoln, NE 68588, USA http://cse.unl.edu/~cbourke cbourke@cse.unl.edu 2015/08/13 14:38:26
More informationCS711008Z Algorithm Design and Analysis
CS711008Z Algorithm Design and Analysis Lecture 7 Binary heap, binomial heap, and Fibonacci heap 1 Dongbo Bu Institute of Computing Technology Chinese Academy of Sciences, Beijing, China 1 The slides were
More informationTesting LTL Formula Translation into Büchi Automata
Testing LTL Formula Translation into Büchi Automata Heikki Tauriainen and Keijo Heljanko Helsinki University of Technology, Laboratory for Theoretical Computer Science, P. O. Box 5400, FIN02015 HUT, Finland
More informationCpt S 223. School of EECS, WSU
Priority Queues (Heaps) 1 Motivation Queues are a standard mechanism for ordering tasks on a firstcome, firstserved basis However, some tasks may be more important or timely than others (higher priority)
More informationMathematical Induction
Mathematical Induction (Handout March 8, 01) The Principle of Mathematical Induction provides a means to prove infinitely many statements all at once The principle is logical rather than strictly mathematical,
More informationApproximation Algorithms
Approximation Algorithms or: How I Learned to Stop Worrying and Deal with NPCompleteness Ong Jit Sheng, Jonathan (A0073924B) March, 2012 Overview Key Results (I) General techniques: Greedy algorithms
More informationLecture 2 February 12, 2003
6.897: Advanced Data Structures Spring 003 Prof. Erik Demaine Lecture February, 003 Scribe: Jeff Lindy Overview In the last lecture we considered the successor problem for a bounded universe of size u.
More informationMany algorithms, particularly divide and conquer algorithms, have time complexities which are naturally
Recurrence Relations Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally modeled by recurrence relations. A recurrence relation is an equation which
More informationCPSC 211 Data Structures & Implementations (c) Texas A&M University [ 313]
CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 313] File Structures A file is a collection of data stored on mass storage (e.g., disk or tape) Why on mass storage? too big to fit
More informationDecision Trees from large Databases: SLIQ
Decision Trees from large Databases: SLIQ C4.5 often iterates over the training set How often? If the training set does not fit into main memory, swapping makes C4.5 unpractical! SLIQ: Sort the values
More informationData Structure with C
Subject: Data Structure with C Topic : Tree Tree A tree is a set of nodes that either:is empty or has a designated node, called the root, from which hierarchically descend zero or more subtrees, which
More informationChapter 14 The Binary Search Tree
Chapter 14 The Binary Search Tree In Chapter 5 we discussed the binary search algorithm, which depends on a sorted vector. Although the binary search, being in O(lg(n)), is very efficient, inserting a
More informationAnalysis of Algorithms I: Binary Search Trees
Analysis of Algorithms I: Binary Search Trees Xi Chen Columbia University Hash table: A data structure that maintains a subset of keys from a universe set U = {0, 1,..., p 1} and supports all three dictionary
More informationTables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)
Hash Tables Tables so far set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n) Worst O(lg n) O(lg n) O(lg n) Table naïve array implementation
More informationClassification  Examples
Lecture 2 Scheduling 1 Classification  Examples 1 r j C max given: n jobs with processing times p 1,...,p n and release dates r 1,...,r n jobs have to be scheduled without preemption on one machine taking
More informationCS473  Algorithms I
CS473  Algorithms I Lecture 9 Sorting in Linear Time View in slideshow mode 1 How Fast Can We Sort? The algorithms we have seen so far: Based on comparison of elements We only care about the relative
More informationEfficient Data Structures for Decision Diagrams
Artificial Intelligence Laboratory Efficient Data Structures for Decision Diagrams Master Thesis Nacereddine Ouaret Professor: Supervisors: Boi Faltings Thomas Léauté Radoslaw Szymanek Contents Introduction...
More informationSIMS 255 Foundations of Software Design. Complexity and NPcompleteness
SIMS 255 Foundations of Software Design Complexity and NPcompleteness Matt Welsh November 29, 2001 mdw@cs.berkeley.edu 1 Outline Complexity of algorithms Space and time complexity ``Big O'' notation Complexity
More informationInformation Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay
Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay Lecture  17 ShannonFanoElias Coding and Introduction to Arithmetic Coding
More informationMemory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation
Dynamic Storage Allocation CS 44 Operating Systems Fall 5 Presented By Vibha Prasad Memory Allocation Static Allocation (fixed in size) Sometimes we create data structures that are fixed and don t need
More informationAUTOMATED TEST GENERATION FOR SOFTWARE COMPONENTS
TKK Reports in Information and Computer Science Espoo 2009 TKKICSR26 AUTOMATED TEST GENERATION FOR SOFTWARE COMPONENTS Kari Kähkönen ABTEKNILLINEN KORKEAKOULU TEKNISKA HÖGSKOLAN HELSINKI UNIVERSITY OF
More informationA binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heaporder property
CmSc 250 Intro to Algorithms Chapter 6. Transform and Conquer Binary Heaps 1. Definition A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called
More informationTHIS CHAPTER studies several important methods for sorting lists, both contiguous
Sorting 8 THIS CHAPTER studies several important methods for sorting lists, both contiguous lists and linked lists. At the same time, we shall develop further tools that help with the analysis of algorithms
More information1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier.
Study Group 1 Variables and Types 1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier. 2. What does the byte 00100110 represent? 3. What is the purpose of the declarations
More informationPARALLEL PROGRAMMING
PARALLEL PROGRAMMING TECHNIQUES AND APPLICATIONS USING NETWORKED WORKSTATIONS AND PARALLEL COMPUTERS 2nd Edition BARRY WILKINSON University of North Carolina at Charlotte Western Carolina University MICHAEL
More informationChapter Objectives. Chapter 9. Sequential Search. Search Algorithms. Search Algorithms. Binary Search
Chapter Objectives Chapter 9 Search Algorithms Data Structures Using C++ 1 Learn the various search algorithms Explore how to implement the sequential and binary search algorithms Discover how the sequential
More informationIntroduction to Learning & Decision Trees
Artificial Intelligence: Representation and Problem Solving 538 April 0, 2007 Introduction to Learning & Decision Trees Learning and Decision Trees to learning What is learning?  more than just memorizing
More informationSmall Maximal Independent Sets and Faster Exact Graph Coloring
Small Maximal Independent Sets and Faster Exact Graph Coloring David Eppstein Univ. of California, Irvine Dept. of Information and Computer Science The Exact Graph Coloring Problem: Given an undirected
More informationA binary search tree is a binary tree with a special property called the BSTproperty, which is given as follows:
Chapter 12: Binary Search Trees A binary search tree is a binary tree with a special property called the BSTproperty, which is given as follows: For all nodes x and y, if y belongs to the left subtree
More informationThnkwell s Homeschool Precalculus Course Lesson Plan: 36 weeks
Thnkwell s Homeschool Precalculus Course Lesson Plan: 36 weeks Welcome to Thinkwell s Homeschool Precalculus! We re thrilled that you ve decided to make us part of your homeschool curriculum. This lesson
More informationBinary search algorithm
Binary search algorithm Definition Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than
More informationSoftware Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.
Software Testing Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program. Testing can only reveal the presence of errors and not the
More informationGlossary of Object Oriented Terms
Appendix E Glossary of Object Oriented Terms abstract class: A class primarily intended to define an instance, but can not be instantiated without additional methods. abstract data type: An abstraction
More information1/1 7/4 2/2 12/7 10/30 12/25
Binary Heaps A binary heap is dened to be a binary tree with a key in each node such that: 1. All leaves are on, at most, two adjacent levels. 2. All leaves on the lowest level occur to the left, and all
More informationSQL Query Evaluation. Winter 20062007 Lecture 23
SQL Query Evaluation Winter 20062007 Lecture 23 SQL Query Processing Databases go through three steps: Parse SQL into an execution plan Optimize the execution plan Evaluate the optimized plan Execution
More informationApplied Algorithm Design Lecture 5
Applied Algorithm Design Lecture 5 Pietro Michiardi Eurecom Pietro Michiardi (Eurecom) Applied Algorithm Design Lecture 5 1 / 86 Approximation Algorithms Pietro Michiardi (Eurecom) Applied Algorithm Design
More informationTheoretical Computer Science (Bridging Course) Complexity
Theoretical Computer Science (Bridging Course) Complexity Gian Diego Tipaldi A scenario You are a programmer working for a logistics company Your boss asks you to implement a program that optimizes the
More informationAn Introduction to Programming and Computer Science
An Introduction to Programming and Computer Science Maria Litvin Phillips Academy, Andover, Massachusetts Gary Litvin Skylight Software, Inc. Skylight Publishing Andover, Massachusetts Copyright 1998 by
More informationCollege Algebra. Barnett, Raymond A., Michael R. Ziegler, and Karl E. Byleen. College Algebra, 8th edition, McGrawHill, 2008, ISBN: 9780072867381
College Algebra Course Text Barnett, Raymond A., Michael R. Ziegler, and Karl E. Byleen. College Algebra, 8th edition, McGrawHill, 2008, ISBN: 9780072867381 Course Description This course provides
More informationData storage Tree indexes
Data storage Tree indexes Rasmus Pagh February 7 lecture 1 Access paths For many database queries and updates, only a small fraction of the data needs to be accessed. Extreme examples are looking or updating
More informationArithmetic Coding: Introduction
Data Compression Arithmetic coding Arithmetic Coding: Introduction Allows using fractional parts of bits!! Used in PPM, JPEG/MPEG (as option), Bzip More time costly than Huffman, but integer implementation
More informationAnalysis of Algorithms I: Optimal Binary Search Trees
Analysis of Algorithms I: Optimal Binary Search Trees Xi Chen Columbia University Given a set of n keys K = {k 1,..., k n } in sorted order: k 1 < k 2 < < k n we wish to build an optimal binary search
More informationroot node level: internal node edge leaf node CS@VT Data Structures & Algorithms 20002009 McQuain
inary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from each
More informationInstruction Set Architecture (ISA)
Instruction Set Architecture (ISA) * Instruction set architecture of a machine fills the semantic gap between the user and the machine. * ISA serves as the starting point for the design of a new machine
More informationSummit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A
Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A Developed By Brian Weinfeld Course Description: AP Computer
More informationUnionFind Algorithms. network connectivity quick find quick union improvements applications
UnionFind Algorithms network connectivity quick find quick union improvements applications 1 Subtext of today s lecture (and this course) Steps to developing a usable algorithm. Define the problem. Find
More informationDivision of Mathematical Sciences
Division of Mathematical Sciences Chair: Mohammad Ladan, Ph.D. The Division of Mathematical Sciences at Haigazian University includes Computer Science and Mathematics. The Bachelor of Science (B.S.) degree
More informationCatalan Numbers. Thomas A. Dowling, Department of Mathematics, Ohio State Uni versity.
7 Catalan Numbers Thomas A. Dowling, Department of Mathematics, Ohio State Uni Author: versity. Prerequisites: The prerequisites for this chapter are recursive definitions, basic counting principles,
More informationOPTIMAL BINARY SEARCH TREES
OPTIMAL BINARY SEARCH TREES 1. PREPARATION BEFORE LAB DATA STRUCTURES An optimal binary search tree is a binary search tree for which the nodes are arranged on levels such that the tree cost is minimum.
More informationAlgorithms Chapter 12 Binary Search Trees
Algorithms Chapter 1 Binary Search Trees Outline Assistant Professor: Ching Chi Lin 林 清 池 助 理 教 授 chingchi.lin@gmail.com Department of Computer Science and Engineering National Taiwan Ocean University
More informationLecture 6: Binary Search Trees CSCI 700  Algorithms I. Andrew Rosenberg
Lecture 6: Binary Search Trees CSCI 700  Algorithms I Andrew Rosenberg Last Time Linear Time Sorting Counting Sort Radix Sort Bucket Sort Today Binary Search Trees Data Structures Data structure is a
More informationComputer Algorithms. NPComplete Problems. CISC 4080 Yanjun Li
Computer Algorithms NPComplete Problems NPcompleteness The quest for efficient algorithms is about finding clever ways to bypass the process of exhaustive search, using clues from the input in order
More informationQuestions and Answers in Computer Science. By Yonas Tesfazghi Weldeselassie http://www.ictp.trieste.it/ weldesel
Questions and Answers in Computer Science By Yonas Tesfazghi Weldeselassie http://www.ictp.trieste.it/ weldesel The Questions in this material are collected from Computer Science Subject Graduate Record
More informationQuiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s
Quiz 4 Solutions Q1: What value does function mystery return when called with a value of 4? int mystery ( int number ) { if ( number
More informationIntroduction to Logic in Computer Science: Autumn 2006
Introduction to Logic in Computer Science: Autumn 2006 Ulle Endriss Institute for Logic, Language and Computation University of Amsterdam Ulle Endriss 1 Plan for Today Now that we have a basic understanding
More informationOptimizing Description Logic Subsumption
Topics in Knowledge Representation and Reasoning Optimizing Description Logic Subsumption Maryam FazelZarandi Company Department of Computer Science University of Toronto Outline Introduction Optimization
More informationMemory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example
Announcements Memory management Assignment 2 posted, due Friday Do two of the three problems Assignment 1 graded see grades on CMS Lecture 7 CS 113 Spring 2008 2 Safe user input If you use scanf(), include
More informationAlgorithm Design and Recursion
Chapter 13 Algorithm Design and Recursion Objectives To understand basic techniques for analyzing the efficiency of algorithms. To know what searching is and understand the algorithms for linear and binary
More informationAlgorithms and Data Structures
Algorithms and Data Structures CMPSC 465 LECTURES 2021 Priority Queues and Binary Heaps Adam Smith S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Trees Rooted Tree: collection
More informationBinary Search Trees > = Binary Search Trees 1. 2004 Goodrich, Tamassia
Binary Search Trees < 6 2 > = 1 4 8 9 Binary Search Trees 1 Binary Search Trees A binary search tree is a binary tree storing keyvalue entries at its internal nodes and satisfying the following property:
More informationFlorida Math for College Readiness
Core Florida Math for College Readiness Florida Math for College Readiness provides a fourthyear math curriculum focused on developing the mastery of skills identified as critical to postsecondary readiness
More informationCOLLEGE ALGEBRA. Paul Dawkins
COLLEGE ALGEBRA Paul Dawkins Table of Contents Preface... iii Outline... iv Preliminaries... Introduction... Integer Exponents... Rational Exponents... 9 Real Exponents...5 Radicals...6 Polynomials...5
More informationThe Running Time of Programs
CHAPTER 3 The Running Time of Programs In Chapter 2, we saw two radically different algorithms for sorting: selection sort and merge sort. There are, in fact, scores of algorithms for sorting. This situation
More informationAssessment Plan for CS and CIS Degree Programs Computer Science Dept. Texas A&M University  Commerce
Assessment Plan for CS and CIS Degree Programs Computer Science Dept. Texas A&M University  Commerce Program Objective #1 (PO1):Students will be able to demonstrate a broad knowledge of Computer Science
More information