Assume you are given two linear lists of size n each; consider the problem of determining whether any element of one list is an element of the other (not value, element). Derive a lower bound for this problem and design an algorithm for this problem. Derive its time complexity. It should be as close the lower bound as possible.
My work so far:
//Copy list n1 into n3 int n3[n];
for(int I = 0; I < n; i++)n3[i] = n1[i];
//Modify n2 by decrementing each element by 1
for(int = 0; i < n; i++) n2[i] -= 1;
//Check that n1 and n3 bool is_equal = true;
for(int I = 0; I < n; i++)
if(n1[i] != n3[i]) is_equal = false;
Break;
//Restore n2 back to its original state
for(int I = 0; I < n; i++) n2[i] +=1;
if(is_equal)
printf(“%s”, “The two lists don’t have any common element”);
else
printf(“%s”,”The two lists have 1 or more common elements”);
This requires 4 linear loops. So, the order is O(4n) which simplifies to O(n)
The algorithm posted by you looks almost correct, however it needs some minor corrections.
NOTE: Since you need to check if two lists contains the same element reference, it is important to modify the memory reference for the algorithm to work.
ALGORITHM
//Copy list n1 into n3, MAKE SURE THAT THE COPY IS BY VALUE, AND
NOT BY REFERENCE, OTHERWISE THE ALGORITHM WON'T WORK
int n3[n];
for(int i = 0; i < n; i++) { // this step takes O(n) time
n3[i] = n1[i];
}
//Modify n2 by decrementing each element by 1
for(int i = 0; i < n; i++) { // this step takes O(n) time
n2[i] -= 1;
}
//Check that n1 and n3 bool has_same = false;
for(int i = 0; i < n; i++) { // this step takes O(n) time in the
worst case
if(n1[i] != n3[i]) {
has_same = true;
break;
}
}
//Restore n2 back to its original state
for(int i = 0; i < n; i++) { // this step takes O(n) time
n2[i] +=1;
}
if(has_same) // this step takes O(1) time
printf(“%s”,”The two lists have 1 or more common elements”);
else
printf(“%s”, “The two lists don’t have any common element”);
Hence, the total running time of the algorithm is: 4 * O(n) + O(1) = O(n)
Assume you are given two linear lists of size n each; consider the problem of determining...
In need of help to solve this problem.
Theory 1. Assume you are given two linear lists LI and L2 with nl, n2 elements, respectively. Consider the problem of determining whether any element of L is an element of L2 (not value, element!) (a) Derive a lower bound for this problem. (b) Design an algorithm for this problem. Derive its time complexity. It should be as close to your lower bound as possible.
I am having trouble with my C++ program.... Directions: In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node), but it need not be sorted. The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the...
C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below you will find a linear search function with early stop. A linear search is just a naive search - you go through each of the elements of a list one by one. Early stop works only on sorted list. Early stop means, instead of going through whole list, we will stop when your number to search can no longer be possibly found in the...
3. (20 pts.) You are given two sorted lists of numbers with size m and n. Give an O(logn+ logm) time algorithm for computing the k-th smallest element in the union of the two lists. 4. (20 pts.) Solve the following recurrence relations and give a bound for each of them. CMPSC 465, Fall 2019, HW 2 (a) T(n) = 117(n/5)+13n!.3 (b) T(n) = 2T (n/4)+nlogn (c) T(n) = 5T (n/3) +log-n (d) T(n) = T(n/2) +1.5" (e) T(n) =...
A simple linear regression model is given as follows Yi = Bo + B1Xi+ €i, for i = 1, ...,n, where are i.i.d. following N (0, o2) distribution. It is known that x4 n, and x = 0, otherwise. Denote by n2 = n - ni, Ji = 1 yi, and j2 = 1 1. for i = 1, ... ,n1 < n2 Lizn1+1 Yi. n1 Zi=1 1. Find the least squares estimators of Bo and 31, in terms of...
This is a Physical Chemistry problem. Thank you in advance!
1. Consider a system composed of a collection of N molecules that can distribute so that there are ni molecules in the state with energy E, n2 molecules in the state with energy E2, n3 molecules in the state with energy E..etc. The number of ways, W, in which molecules can distribute this way is N! N! W П "! n,in In,.. II Suppose that this system is undergoing the...
Match each problem to the time complexity class it likely belongs to: 1. O(1): Constant 2. O(n): Linear 3. O(n!): Factorial 4. O(logn): Logarithmic 5.O(n2): Quadratic 6. O(n3): Cubic OPTIONS: a. Find an element in an unsorted array b. Shortest path between two nodes in a graph c. Matrix multiplication d. Generate permutations of a string e. Add an element to the front of a linked list f. Find an element in a binary search tree
C++
You're given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. The lists are equal only if they have the same number of nodes and corresponding nodes contain the same data. Either head pointer given may be null meaning that the corresponding list is empty. Input Format You have to complete the int CompareLists (Node headA, Node* head B) method which takes...
5. (2 points) In this problem, consider the following variant of linear search, called search: static int search(int[] a, int x) { for (int i = 0; i <= a.length / 2; i++) { if (a[i] == x) return i; if (a[a. length - i - 1] == x) return a.length - i - 1; return -1; (a) (1 point) Which of the following describes the worst-case input for search? A. An array of size N in which x is...
Here are some common orders of growth, ranked from no growth to
fastest growth:
Θ(1) — constant time takes the same amount of time regardless
of input size
Θ(log n) — logarithmic time
Θ(n) — linear time
Θ(n log n) — linearithmic time
Θ(n2 ) — quadratic time
Θ(n3 ), etc. — polynomial time
Θ(2n), Θ(3n), etc. — exponential time
(considered “intractable”; these are really, really horrible)
In addition, some programs will never terminate if they get
stuck in an...