Introduction to Algorithms
3rd Edition
ISBN: 9780262033848
Author: Thomas H. Cormen, Ronald L. Rivest, Charles E. Leiserson, Clifford Stein
Publisher: MIT Press
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 2.1, Problem 3E
Program Plan Intro
To write the pseudo code for linear search and also discuss the three necessary properties.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Given a non-empty list items of positive integers in strictly ascending order, find and return thelongest arithmetic progression whose all values exist somewhere in that sequence. Return the answer as a tuple (start, stride, n) of the values that define the progression. To ensure unique results to facilitate automated testing, if there exist several progressions of the same length, this function should return the one with the lowest start. If several progressions of equal length emanate from the lowest start, return the progression with the smallest stride. INTRUCTIONS:
Input a comment or a solution on how you solve the problem, write a psuodocode and flow chart about th problem
When the number of elementary events n is small, we could implement row 10 inAlgorithm 2.3 with a simple sequential search. The efficiency of the whole algorithmdepends on how fast this linear search finds the smallest index i for which Si−1 <k ≤ Si. How would you organize the weight sequence W before the prefix sums arecalculated? To verify your solution, implement a program that finds a permutationfor the given weight sequence that minimizes the average number of the requiredsequential steps (i.e. increments of i). Also try out different distribution
Given a sorted array of n comparable items A, and a search value key, return the position (array index) of key in A if it is present, or -1 if it is not present. If key is present in A, your algorithm must run in order O(log k) time, where k is the location of key in A. Otherwise, if key is not present, your algorithm must run in O(log n) time.
Chapter 2 Solutions
Introduction to Algorithms
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- For a string P of length m, define a function ∆P :{1,...,m}→{1,...,m} as follows: ∆P[q] is the length of the shortest actual suffix of Pq which is also a prefix of P. If there is no actual suffix of Pq which is also a prefix of P then ∆P[q]=0. Give an efficient algorithm for calculating ∆P. (The algorithm should output the entire array ∆P .)arrow_forwardFor the 8-queens problem, define a heuristic function, design a Best First Search algorithm in which the search process is guided by f(n) = g(n) + h(n), where g(n) is the depth of node n and h(n) is the heuristic function you define, and give the pseudo code description.arrow_forward2. Let n be a positive integer, and let A be a list of positive integers. We say that the integer n can be factorized by A if there exists a sequence of integers 11, 12,..., ik (with 0 ≤i; < len (A), for j = 1,..., k) such that n is the product of the integers A[i], A[i2],..., A[ik]. Write an efficient algorithm factorizable (n, A) that returns True if n can be factorized by A, and False otherwise. Prove that your algorithm is correct, and bound its running time. Larger scores will be awarded to faster solutions. Example 1: if n = 6 and A [4,2,3], then factorizable (n, A) should return True, L[1] * L[2]. since n == Example 2: if n = 8 and A = L[0] * L[0] * L[0]. [2,5], then factorizable (n, A) should return True, since ,2,3,5,7], then factorizable (n, A) should return Example 3: if n = 13 and A = False since n cannot be factorized by A.arrow_forward
- The algorithm of Euclid computes the greatest common divisor (GCD) of two integer numbers a and b. The following pseudo-code is the original version of this algorithm. Algorithm Euclid(a,b)Require: a, b ≥ 0Ensure: a = GCD(a, b) while b ̸= 0 do t ← b b ← a mod b a ← tend whilereturn a We want to estimate its worst case running time using the big-Oh notation. Answer the following questions: a. Let x be a integer stored on n bits. How many bits will you need to store x/2? b. We note that if a ≥ b, then a mod b < a/2. Assume the values of the input integers a and b are encoded on n bits. How many bits will be used to store the values of a and b at the next iteration of the While loop? c. Deduce from this observation, the maximal number iterations of the While loop the algorithm will do.arrow_forwardThe median m of a sequence of n elements is the element that would fall in the middle if the sequence was sorted. That is, e ≤ m for half the elements, and m ≤ e for the others. Clearly, one can obtain the median by sorting the sequence, but one can do quite a bit better with the following algorithm that finds the kth element of a sequence between a (inclusive) and b (exclusive). (For the median, use k = n/2, a = 0, and b = n.) select(k, a, b)Pick a pivot p in the subsequence between a and b.Partition the subsequence elements into three subsequences: the elements <p, =p, >p Let n1, n2, n3 be the sizes of each of these subsequences.if k < n1 return select(k, 0, n1).else if (k > n1 + n2) return select(k, n1 + n2, n).else return p. c++arrow_forwardConsider a divide-and-conquer algorithm that calculates the sum of all elements in a set of n numbers by dividing the set into two sets of n/2 numbers each, finding the sum of each of the two subsets recursively, and then adding the result. What is the recurrence relation for the number of operations required for this algorithm? Answer is f(n) = 2 f(n/2) + 1. Please show why this is the case.arrow_forward
- You will analyze three algorithms to solve the maximum contiguous subsequence sum problem, and then evaluate the performance of instructor-supplied implementations of those three algorithms. You will compare your theoretical results to your actual results in a written report. What is the maximum contiguous subsequence sum problem? Given a sequence of integers A1, A2, ..., An (where the integers may be positive or negative), find a subsequence Aj, ... , Ak that has the maximum value of all possible subsequences. The maximum contiguous subsequence sum is defined to be zero if all of the integers in the sequence are negative. Consider the sequence shown below. A1: -2 A2: 11 A3: -4 A4: 13 A5: -5 A6: 2 The maximum contiguous subsequence sum is 20, representing the contiguous subsequence in positions 2, 3, and 4 (i.e. 11 + (-4) + 13 = 20). The sum of the values in all other contiguous subsequences is less than or equal to 20. Consider a second sequence, shown below. A1: 1…arrow_forwardGiven a linked list L storing n integers, present an algorithm (either in words or in a pseudocode) that decides whether L contains any 0 or not. The output of your algorithm should be either Yes or No. What is the running time of your algorithm in the worst-case, using O notation?arrow_forwardConsider sorting n numbers stored in array A by first finding the smallest elementof A and exchanging it with the element in A[1]. Then find the second smallestelement of A, and exchange it with A[2]. Continue in this manner for the first n-1elements of A. Write pseudocode for this algorithm, which is known as selectionsort. What loop invariant does this algorithm maintain? Why does it need to run foronly the first n - 1 elements, rather than for all n elements? Give the best-case andworst-case running times of selection sort in Θ -notation.arrow_forward
- An array A[1 . . n] of integers is a mountain if it consists of an increasing sequence followed by a decreasing sequence, or more precisely,If there is an index m ∈ {1, 2, . . . , n} such that• A[i] < A[i + 1] for all 1 ≤ i < m, and• A[i] > A[i + 1] for all m ≤ i < n.In particular, A[m] is the maximum element, and it is the unique “locally maximum” element surrounded by smaller elements (A[m − 1] and A[m + 1]).Give an algorithm to compute the maximum element of a mountain input array A[1 . . n] in O(log(n)) time.arrow_forwardConsidering the search problem, we have a list of ?n integers ?=⟨?1,?2,⋯??⟩A=⟨v1,v2,⋯vn⟩. We want to design an algorithm to check whether an item ?v exists or not such that it should return either the index, ?i, if it was found or −1−1 otherwise, when not found.arrow_forwardIn algorithms, we determine the correctness of an algorithm by providing a loop invariant . A loop invariant is a condition that is true before the beginning of algorithm execution, is true during the execution and is also true at the end of the execution. For this question, consider the Linear Search algorithm whose function is given belodef linearSearch(myList, item): for items in myList: if (item == items): return True; Based above pseudocode: a. The loop invariant for linear search. b. Show the Correctness proof for linear search algorithm determining the correctness of the loop invariant at initialization (before the execution), maintenance (during the execution) and termination (after the execution).arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education