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
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write in C Language
Description
A sparse matrix is a matrix in which most of the elements are zero. Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the cases. So, we can compress the sparse matrix by only store non-zero elements. This means storing non-zero elements with triples- (Row, Column, value).
0 0 0 0 0 0
0 3 0 0 0 0
0 0 0 6 0 0
0 0 9 0 0 0
0 0 0 0 12 0
Here is a 5X6 matrix, there are 4 element non-zero, we using three number represent it
5 6 4
After the three number are (Row, Column, value):
row col val
1 1 3
2 3 6
3 2 9
4 4 12
Write a program to read the compressed matrix and print out the original matrix.
Input
First line consists 3 integers. First and second integers shows the dimensions of original matrix. Third integer k shows number of non-zero elements. Followed k lines are content of the sparse matrix. Each line consists 3 integers, row number, column number, and data.
Output
Output…
Write in C Language
Description
A sparse matrix is a matrix in which most of the elements are zero. Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the cases. So, we can compress the sparse matrix by only store non-zero elements. This means storing non-zero elements with triples- (Row, Column, value).
0 0 0 0 0 0
0 3 0 0 0 0
0 0 0 6 0 0
0 0 9 0 0 0
0 0 0 0 12 0
Here is a 5X6 matrix, there are 4 element non-zero, we using three number represent it
5 6 4
After the three number are (Row, Column, value):
row col val
1 1 3
2 3 6
3 2 9
4 4 12
Write a program to read the compressed matrix and print out the original matrix.
Input
First line consists 3 integers. First and second integers shows the dimensions of original matrix. Third integer k shows number of non-zero elements. Followed k lines are content of the sparse matrix. Each line consists 3 integers, row number, column number, and data.
Output
Output…
#!/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
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_forwardPython 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_forwardWrite 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…arrow_forward
- function 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_forwardin c++ In a 2D integer array TD, assume that the row indices range from -3 to 7 and column indices range from 6 to 14. An element TD [-3, 6] stored at address 3220. Find out the dimension of TD and address of an element TD [2, 10], if TD stores the elements in column major order.arrow_forwardIntroduction For this assignment, you are to write a program which implements a Sorted List data structure using a circular array-based implementation and a driver program that will test this implementation. The Sorted List ADT is a linear collection of data in which all elements are stored in sorted order. Your implementation has to store a single int value as each element of the list and support the following operations: 1. add(x) – adds the integer x to the list. The resulting list should remain sorted in increasing order. The time complexity of this operation should be 0(N), where N is the size of the list. 2. removefirst() - deletes the first integer from the list and returns its value. The remaining list should remain sorted. Time complexity of this operation should be 0(1). 3. removelast() – deletes the last integer from the list and returns its value. The remaining list should remain sorted. Time complexity of this operation should be 0(1). 4. exists(x) – returns true if the…arrow_forward
- in c++ 6. Consider a 2D array A[m][m], each element takes 4 bytes of storage. If the base address at A[1][1] is 1500 and the address of A[4][5] is 1608, determine the order of the matrix when it is stored in Column Major Wise.arrow_forwardWrite a function in C swapRows(): which takes two 4x4 array as an argument and swap the values of its even rows with odd rows . Expected input: 5 2 3 1 4 8 9 0 -1 3 8 -2 99 102 33 56 Expected Output: 4 8 9 0 5 2 3 1 99 102 33 56 -1 3 8 -2arrow_forwardplease convert the code to C language //C++ program to check if two arrays //are equal or not #include <bits/stdc++.h>using namespace std; bool similar_array(vector<int> arr1, vector<int> arr2){ //create teo different hash table where for each key //the hash function is h(arr[i])=arr[i] //we will use stl map as hash table and //will keep frequency stored //so say two keys arr[0] and arr[5] are mapping to //the same location, then the location will have value 2 //instead of the keys itself //if two hash tables are exactly same then //we can say that our arrays are similar map<int, int> hash1; map<int, int> hash2; //for each number for (int i = 0, j = 0; i < arr1.size(); i++, j++) { hash1[arr1[i]]++; hash2[arr2[i]]++; } //now check whether hash tables are exactly same or not for (auto it = hash1.begin(), ij = hash2.begin(); it != hash1.end() && ij != hash2.end(); it++, ij++) {…arrow_forward
- The median of a set of numbers is when the numbers are ordered from smallest to largest. is the number. For example: The median of the set 3, 4, 4, 5, 6, 8, 8, 8, 10 is 6 The median of 5, 5, 7, 9, 11, 12, 15, 18 is (9+11)/2 = 10. Calculates the median of N numbers as a subprogram with a dynamic array and sends it to the main program. Write the Fortran code of a program that rotates and prints the result to the screen. Function name will be MEDIA and defined as inner functionarrow_forwardWrite the implementations of functionscopy1 and copy2 for copying strings. copy1 uses array index notation. copy2 uses Pointers andPointer Arithmetic.#include <stdio.h>#define SIZE 10void copy1(char* s1, const char* s2); // prototypevoid copy2(char* s1, const char* s2); // prototypeint main(void){char string1[SIZE]; // create array string1char *string2 = "Hello"; // create a pointer to a stringcopy1(string1, string2);printf("string1 = %s\n", string1);char string3[SIZE]; // create array string3char string4[] = "Good Bye"; // create an array containing a stringcopy2(string3, string4);printf("string3 = %s\n", string3);}void copy1(char* s1, const char* s2){//Implement copying string using array index notation}void copy2(char* s1, const char* s2){//Implement copying string using Pointers and Pointer Arithmetic }arrow_forwardStudy the function matrix_mult that takes two 2D arrays and performs a matrix multiplication and returns a new two-dimensional array. Each array should be represented as a list of lists. For example, A = [[1, 2, 3], [-2, 3, 7]] B = [[1,0,0],[0,1,O],[0,0,1]] matrix_mult(A, B) [[1, 2, 3], [-2, 3, 7]] Identify all lines that are in error - that is, select all answers that are correct. def matrix_mult(A, B): A_n = len(A) A_m = len(A[0]) B_n = len(B) B_m = len(B[0]) assert A_m = B_n R_n = A_n 1. 2. 3. 4. 5. 6. 7. 8. R_m = B_m R = [[0 for j in range (R_m)] fori in range(R_n)] def row(M, r): return v[c] for v in M def col(M, c): return M[r] def dot(v1, v2): return sum([x*y for x, y in zip(v1, v2)]) for i in range(R_n): for j in range (R_m): R[i][j] - dot(row(A,i), col(8,j)) return R 9. 10. 11. 12. 13. 14.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