Explanation of Solution
The following
Pseudocode:
Input: Two integer arrays “a[]” and “b[]” of size “n”.
Size of Input: The size specifies the number “n” of array entries.
Output: The condition is “true” of each element of “a[]” is also an element of “b[]” otherwise, it display “false”.
The following is the pseudocode for given data,
Boolean Contains =true
int i=0
While Contains && i<n do
Contains=Linearsearch(a[i],b)
i++;
End while
Print Contains
Explanation:
- In the given code, set the Boolean variable “Contains” is equal to “true”.
- Execute the while loop if the condition is true,
- The condition must be “i” less than “n” and the variable “Contains” must be “true”.
- If it is true, then it calls for Linearsearch() function.
- Increment the value of “i”.
- Thus, the while loop is executed until the condition satisfies and then finally it print the Boolean value either “True” or “False”.
Pseudocode for LinearSearch:
Input: An integer “X” and an array “b[]” of size “n”.
Size of input: The size specifies the number “n” of array entries.
Output: It returns true if “X” contains in “b[]”, otherwise “false”.
The following is the pseudocode for linear search,
Boolean LinSearch(int x, int[] b)
Begin
int k=0;
Boolean found =false;
While !found && k<n do
if(X==b[k])
found =true;
else
K++
End If
End While
Return found
End LinSearch
Explanation:
- The given code performs the linear search algorithm concept which process to find an item in an array...
Want to see the full answer?
Check out a sample textbook solutionChapter 16 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
- I need help with this pleasearrow_forwardWe have two input arrays, an array A with n elements, and an array B with m elements, where m > n. There may be duplicate elements. We want to decide if every element of B is an element of A. a. Describe a brute-force algorithm. What is the worst-case time complexity? b. Describe an algorithm to solve this problem in O(m log n) worst case time. (Hint: You may apply instance simplification.)arrow_forwardGiven is a strictly increasing function, f(x). Strictly increasing meaning: f(x)< f(x+1). (Refer to the example graph of functions for a visualization.) Now, define an algorithm that finds the smallest positive integer, n, at which the function, f(n), becomes positive. The things left to do is to: Describe the algorithm you came up with and make it O(log n).arrow_forward
- Given 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_forwardWireless sensor networks (WSNs) refer to a domain of communication networks. In a WSN, small devices called sensor nodes are used for data transfer among each other and a base station. One limitation of a sensor node is that it has a very limited processing capability and very small memory. Consider a scenario in which you have an algorithm that can be subdivided into smaller problems. If you have to run these sub-problems in different sensor nodes, would you prefer to use divide & conquer approach or dynamic programming approach? Write briefly and precisely.arrow_forwardIn this question you will explore Graph Colouring algorithms. Given a graph G, we say that G is k-colourable if every vertex of G can be assigned one of k colours so that for every pair u, v of adjacent vertices, u and v are assigned different colours. The chromatic number of a graph G, denoted by χ(G), is the smallest integer k for which graph G is k-colorable. To show that χ(G) = k, you must show that the graph is k-colourable and that the graph is not (k − 1)-colourable. Question: It is NP-complete to determine whether an arbitrary graph has chromatic number k, where k ≥ 3. However, determining whether an arbitrary graph has chromatic number 2 is in P. Given a graph G on n vertices, create an algorithm that will return TRUE if χ(G) = 2 and FALSE if χ(G) 6= 2. Clearly explain how your algorithm works, why it guarantees the correct output, and determine the running time of your algorithm.arrow_forward
- Consider the problem of finding a maximum weight spanning tree of a given weighted connected undirected graph. Another words, you would like to have an algorithm that finds a spanning tree with the largest possible total edge weight. Describe such algorithm in pseudocode, give a justification of correctness of your algorithm, and discuss the running time. For this use “reduce to known” technique: assume that you can call Prim’s algorithm as described in class, but you can not modify the algorithm to adjust it to your problem (however, you a free to modify the given input graph).arrow_forwardAnswer the given question with a proper explanation and step-by-step solution. You are asked to pick up a project on building highways to connect all cities in the country. The cost of building a highway between two cites i and j is c(i, j) > 0. If you were in charge from the beginning, this would have been a minimum spanning tree problem and could be solve easily with the algorithms covered in class. Since you pick it up halfway, however, some suboptimal choices have already been made by your predecessor. In other words, highways were already built between some pairs of cities. Design an algorithm to find a cost minimizing set of highways to built subject to the choices already made. Do not copy others.arrow_forwardFor part A, how what would the pseudo-code visually be when trying to visualize an efficient algorithm? As for Part B, what would determine the space and complexity?arrow_forward
- Consider array A of n numbers. We want to design a dynamic programming algorithm to find the maximum sum of consecutive numbers with at least one number. Clearly, if all numbers are positive, the maximum will be the sum of all the numbers. On the other hand, if all of them are negative, the maximum will be the largest negative number. The complexity of your dynamic programming algorithm must be O(n2). However, the running time of the most efficient algorithm is O(n). Design the most efficient algorithm you can and answer the following questions based on it. To get the full points you should design the O(n) algorithm. However, if you cannot do that, still answer the following questions based on your algorithm and you will get partial credit. Write the recursion that computes the optimal solution recursively based on the solu- (a) tion(s) to subproblem(s). Briefly explain how it computes the solution. Do not forget the base case(s) of your recursion.arrow_forwardPlease help me Josephus Problem is a theoretical problem related to a certain counting-out game. On thiscase, people are standing in a circle waiting to be executed. After a specified number ofpeople are skipped, the next person is executed. The procedure is repeated with theremaining people, starting with the next person, going in the same direction and skippingthe same number of people, until one person remains, and is freed.Arrange the numbers 1 , 2, 3 , ... consecutively (say, clockwise) in a circle. Now removenumber 2 and proceed clockwise by removing every other number, among those thatremain, until one number is left. (a) Let denote the final number which remains. Find formula for .(b) If there are 70 people, what is the safe number (the number that remains)?arrow_forwardAnalyze the Print Shortest Path algorithm (Algorithm 3.5) and show that it has a linear-time complexity.arrow_forward
- 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