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 11.1, Problem 2E
Program Plan Intro
To describe the application of bit vector to represent a dynamic set of distinct element.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
#!/usr/bin/env python 3
Suppose we have very large sparse vectors, which contains a lot of
zeros and double.
find a data structure to store them
get the dot product of them
def vector_to_index_value_list(vector):
return [(i, v) for i,v in enumerate(vector) if v != 0.0]
def dot product (iv_list1, iv_list2):
product = 0
p1=len (iv_list1)-1
p2 = len (iv_list2) - 1
while p1 >= 0 and p2 >= 0:
i1, v1 = iv_list1 [p1]
i2, v2 = iv_list2 [p2]
if i1
In PYTHON
Define a vector of integers with 5 elements
Write the following function
Given a vector and two indexes, returns a vector where the values at the provided indexes are swapped
Example Input: my_vector = [40,51,62,73,84,95]
Invokefunction:swap(my_vector,2,4)
Output:[40,51,84,73,62,95]
Write the following data structures in R: Vectors, Matrices, Arrays, Lists, and Data Frames.
Access R Studio. Then, demonstrate how to work with each data structure as outlined below.
Vectors
Create a vector as a sequence of number 1-15.
Create a vector as a sequence of numbers 1-15 that increment by 0.1.
Demonstrate how missing data is represented as NA in vectors (use the na() and anyNA() functions).
Utilize the conversion between modes "coercion" in R to perform an "implicit coercion" on the following: a) xx ß p(2.5, "g"), b) xx ß p(TRUE, 4) and c) xx ß p("g", TRUE).
Control how vectors are coerced explicitly using the as.<class_name>() functions (numeric and character).
Matrices
Create a column-wise 5 x 8 matrix.
Check that the matrices are vectors with a class attribute of matrix by using class() and tyepof().
Create a matrix by transforming a 5 x 8 vector into a matrix.
Arrays
Create an array of movies that contains the Top 10 movies of 2020.
Lists
Construct a list…
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
- Write a recursive function named binarySearch that accepts a reference to a sorted vector of integers and an integer target value and uses a recursive binary search algorithm to find and return an index at which that target value is found in the vector. If the target value is not found in the vector, return -1. The following code shows some example calls and their expected return values: // index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16vector<int>v {-4, 2, 7, 10, 15, 20, 22, 25, 30, 36, 42, 50, 56, 68, 85, 92, 103};intindex=binarySearch(v, 42); // 10intindex=binarySearch(v, 66); // -1 You should assume that the vector's elements are already sorted; you do not need to handle the case of an unsorted vector. Your function must be recursive and must use a binary search algorithm. Do not use loops or auxiliary data structures.arrow_forwardlanguage is python 3arrow_forwardlanguage is python 3arrow_forward
- Python Numpy function to complete: def t19(N, s, X, y): Inputs: N: An integer s: A floating-point number - x: A floating-point number - y: A floating-point number Returns: A numpy array I of shape (N, N) such that I[i, j] exp(-||(j, i) –- (x, y)||^2 / s^2) Par: 3 lines Instructor: 2 lines return Nonearrow_forwardfunction Sum(A,left,right) if left > right: return 0else if left = right: return A[left] mid = floor(N/2) lsum = Sum(A,left,mid) rsum = Sum(A,mid+1,right) return lsum + rsum function CreateB(A,N)B = new Array of length 1 B[0] = Sum(A,0,N-1) return B Building on the above, in a new scenario, given an array A of non-negative integers of length N, additionally a second array B is created; each element B[j] stores the value A[2*j]+A[2*j+1]. This works straightforwardly if N is even. If N is odd then the final element of B just stores A[N-1] as we can see in the figure below: (added in image) The second array B is now introducing redundancy, which allows us to detect if there has been a hardware failure: in our setup, such a failure will mean the values in the arrays are altered unintentionally. The hope is that if there is an error in A which changes the integer values then the sums in B are no longer correct and the algorithm says there has been an error; if there were an error in B…arrow_forwardsolution in python languagearrow_forward
- UAA: [109, 105, 114, 122, 97, 98, 101]Use C++ if programmed <-- not required to. Using your UAA, apply the Lomuto’s Partioning Algorithm. Write the initial array and trace the content of the array and the positions of the indices (s, i) for each iteration in i, and for the final swap.arrow_forwardCoding in python, one the fist question please.arrow_forwardPython code Screenshot and output is mustarrow_forward
- Python numpy library functions to perform basic matrix operations are as follows.• C=numpy.add(A, B): Add two matrices A and B and store result in C.• C=numpy.subtract(A, B): Subtract matrix B from matrix A and store result in C.• C=numpy.divide(A, B): Divide matrix A by matrix B and store result in C.• C=numpy.multiply(A, B): Multiply matrix A by matrix B and store result in C.• C=numpy.sum(A): Form the sum of elements of matrix A and store result in c ∈ R.• C=numpy.sum(A, axis = 0): Form the column wise summation of matrix A andstore result in vector C.• C=numpy.sum(A, axis = 1): Form the row wise summation of matrix A, storeresult in vector C.Python code to show the implementation of these methods in a sample matrix.arrow_forwardHow its gonna look without vector in cpp, recursivearrow_forwardPython data structures: write a function that takes in a list (L) as input, and returns the number of inversions. An inversion in L is a pair of elements x and y such that x appears before y in L, but x > y. Your function must run in -place and implement the best possible algoritm of time complexity. def inversions(L: List) -> int:'''finds and returns number of inversions'''arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- New Perspectives on HTML5, CSS3, and JavaScriptComputer ScienceISBN:9781305503922Author:Patrick M. CareyPublisher:Cengage Learning
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning