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
Question
Chapter 8.1, Problem 4E
Program Plan Intro
To show that lower bounds on the number of comparisons needed to solve the sorting problem is
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
That's enough for you!
def first_preceded_by_smaller(items, k=1):
Find and return the 0irst element of the given list of items that is preceded by at least k smaller elements in the list. These required k smaller elements can be positioned anywhere before the current element, not necessarily consecutively immediately before that element. If no element satisfying this requirement exists in the list, this function should return None.
Since the only operation performed for the individual items is their order comparison, and especially no arithmetic occurs at any point during execution, this function should work for lists of any types of elements, as long as those elements are pairwise comparable with each other.
items
k
Expected result
[4, 4, 5, 6]
2
5
[42, 99, 16, 55, 7, 32, 17, 18, 73]
3
18
[42, 99, 16, 55, 7, 32, 17, 18, 73]
8
None
['bob', 'carol', 'tina', 'alex', 'jack', 'emmy', 'tammy', 'sam', 'ted']…
Suppose you want to find documents that contain at least k of a given set of n keywords. Suppose also you have a keyword index that gives you a (sorted) list of identifiers of documents that contain a specified keyword. Give an efficient algorithm to find the desired set of documents.
Given an array of locations in memory of n dimensions we want to find out what ordering it has in memory.
For example, given the following input array
[[[ 0 4 8 12] [16 20 24 28] [32 36 40 44]]
[[48 52 56 60] [64 68 72 76] [80 84 88 92]]]
your function has to return the tuple: [2,1,0]
As a suggestion:
- Loop through the number of dimensions
- For each dimension generate a list of slices equivalent to the above indexing notation
- Get the first two elements of each axis
- Order the axes by the difference between those two elements
Complete the code:
def get_storage_order(r):
import numpy as np
return
to check the code manually:
r = np.array([[[ 0, 4, 8, 12], [32, 36, 40, 44], [64, 68, 72, 76]], [[16, 20, 24, 28], [48, 52, 56, 60], [80, 84, 88, 92]]])
get_storage_order(r)
Chapter 8 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
- Given an array of locations in memory of n dimensions we want to find out what ordering it has in memory. For example, given the following input array [[[ 0 4 8 12] [16 20 24 28] [32 36 40 44]] [[48 52 56 60] [64 68 72 76] [80 84 88 92]]] your function has to return the tuple: [2,1,0] As a suggestion: - Loop through the number of dimensions - For each dimension generate a list of slices equivalent to the above indexing notation - Get the first two elements of each axis - Order the axes by the difference between those two elements Complete the code: def get_storage_order(r): import numpy as np returnarrow_forwardThe algorithm for finding all occurrences of a sequence in another sequence using the suffix array of the latter sequence can also be implemented in R in a straightforward way, Write R script for given statement.arrow_forwardComputer Science you are given a string X of length n and another string Y of length m ≤n. Say,the indexes p1, p2, p3, p4 and q1, q2, q3, q4 form two sub-sequences, i.e., 0 ≤ p1 < p2 < p3 < p4 < nand 0 ≤q1 < q2 < q3 < q4 < n; then, they are non-overlapping if p4 < q1.The task is to count the maximum number of non-overlapping sub-sequences of X that are thesame as Y . Thus, if X = GAXTYAWBGTAUGBTABGRGTAXB and Y = GTAB, then the answer is3 as shown by the red fonts. We cannot select the underlined GTAB as it overlaps with a red GTAB(i.e., among overlapping sub-sequences, you can select only one of them).Describe an O(m + n) time algorithm to obtain the count. Write a pseudo-code.arrow_forward
- A set of integers 0..MAX may be implemented using an array of boolean values. This particular implementation is called a bit-vector implementation of a Set. Since C doesn’t have the Boolean data type, then substitute TRUE with 1 and FALSE with 0, or define 1 and 0 as TRUE and FALSE, respectively.For example, if the integer 3 is an element of the set, then the array element indexed by 3 is TRUE. On the other hand, if 3 is not an element, then the array element indexed by 3 is FALSE. For example, if the integer 3 is an element of the set, then the array element indexed by 3 is TRUE. On the other hand, if 3 is not an element, then the array element indexed by 3 is FALSE.For example: if s = {3,4,6,8}, the array looks like this: Implement a programmer-defined data type called BitSet to represent a set as follows:typedef int BitSet[MAX];Implement the following functions:void initialize(BitSet s); - set all array elements to FALSEvoid add(int elem,BitSet s);- set the item indexed by elem to…arrow_forwardCreate a bottom-up mergesort that takes advantage of array order by doing the following each time it has to identify two arrays to merge: locate a sorted subarray (by incrementing a pointer until it finds an entry in the array that is smaller than its predecessor), then locate the next, and finally merge them. Examine the algorithm's running time in terms of array size and the number of maximal rising sequences in the array.arrow_forwardGiven a sequence like [1, 2, 3, 4, 5], and an array of sub sequences like [[1,2], [3, 4], [5]], return true or false if the given sequence could be constructed from given sub sequences. The selected sub sequences would concatenate to construct exactly the given sequence. Please write python function to answer the questionarrow_forward
- 1. Consider a problem of computing the prefix average of a sequence of numbers stored in an array P consisting of P integers. We want to compute an array P such that P is the average of P for P . Consider the two algorithms related to the above problem. Answer the questions given belowarrow_forwardillustrates the procedure for Fibonacci search. Here n, thenumber of data elements is such that: i) Fk+1 > (n+1); andii) Fk + m = (n +1) for some m ≥ 0, where Fk+1 and Fk are two consecutiveFibonacci numbers.arrow_forwardWhat is the running time of finding a single number from an unsorted array of p elements. (a) O(n)(b) O(p)(c) O(lgp)(d) O(p^2) What is the running time of finding your name from a sorted list of names.(a) O(n)(b) O(n^3)(c) O(lgn)(d) O(n^2)arrow_forward
- Q1)Suppose you are given an array A of n elements. Your task is to sort n numbers stored in array A by reading the first element of A and placing it on its original position (position after sorting). Then read the second element of A, and place it on its original position. Continue in this manner for the first n-1 elements of A. What type of sorting is this? Write the algorithm and also mention the name of this sorting algorithm. What loop invariant does this algorithm maintain? Give the best-case and worst-case running times of this sorting algorithm. Q2)Consider an array consisting of the following sequence: 1, 4, 9, 16, 25, 49, …, n Suppose a number in the sequence is missing. Write the mathematical process to find the missing number, i.e. some equation. What is the time complexity of finding the missing number in the sequence? Q3)Sort the text “SARHAD UNIVERSITY” in ascending order using the insertion sort algorithm. Discuss also the best, worst and average case complexity of the…arrow_forwardGiven an integer n and an array a of length n, your task is to apply the following mutation to a: Array a mutates into a new array b of length n. For each i from 0 to n - 1, b[i] = a[i - 1] + a[i] + a[i + 1]. If some element in the sum a[i - 1] + a[i] + a[i + 1] does not exist, it should be set to 0. For example, b[0] should be equal to 0 + a[0] + a[1].arrow_forwardWrite a program which will read the elements of a sparse matrix from the keyboard and stores in a two-dimensional array in the form of triplet which saves lots of space. Do the above program in reverse that is triplet is given and your program need to print the corresponding sparse matrix in C language.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License