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 2.1, Problem 3E
Program Plan Intro
To write the pseudo code for linear search and also discuss the three necessary properties.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Question # 01:
Consider the searching problem:
Input: A sequence of n numbers A = (a1, a2, a3,
an ) a value V.
Output: An index i such that v = A[i] or the special value NIL if v does not appear in A.
Write pseudocode for linear search, which scans through the sequence, looking for v. Using a
loop invariant, prove that your algorithm is correct. Make sure that your loop invariant fulfills
the three necessary properties.
The intersection of two sets contains the list of elements that are common to both, without
repetition. As such, you are required to write an algorithm that finds and returns the intersection
of two sets A and B. The sets A and B and the resulting intersection set are all lists. Assume
the size of A is n and that of B is m.
(a) Write an iterative algorithm that finds the intersection.
(b) Prove the correctness of your algorithm; i.e. state its loop invariant and prove it by induc-
tion.
(c) Analyze the time complexity of your algorithm.
(d) Think of a way to enhance the complexity of your algorithm by sorting both lists A and
B before calculating their intersection. Write the enhanced version and prove that it has a
better time complexity.
2.3-1
Using Figure 2.4 as a model, illustrate the operation of merge sort on the array
A = (3, 41, 52, 26, 38, 57, 9, 49).
5
2
2
5
merge
2
1
4
2
5
merge
4
4
2
7
sorted sequence
3
4
7
merge
merge
1
7
initial sequence
5
1
3
merge
3
6
7
2 3
merge
2
2
6
6
merge
6
Chapter 2 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
- Below are a number of statements about linear and binary search. A. Linear search systematically searches from beginning to end. B. Linear search as well as binary search works on both sorted and unsorted data sets. C. For linear search, T (n) = O (n) applies in the worst case. D. For linear search, T (n) = 0 (n) applies in the worst case, if the quantity is unsorted. E. For binary search, T (n) = O (log n) applies in the worst case. Indicate which of these is correct. a,b,d,e true a,b,c,d true a,c,d,e true a,e truearrow_forward1. Analyze the word finder with wild card characters scenario to form the algorithm and compute the time complexity. You are given a list of characters with size n, m. You need to read character data provided as input into this list. There is also a list of p words that you need to find in the given problem. You can only search in left, right, up and down direction from a position. There are two wild characters *' and '+' ,if there is any instance of wild card characters for example '+' appears you can match whatever character in your word that you are searching at this position (only one character). Similarly, if you got wildcard *' you can skip any number of characters ( A sequence of characters not necessary same in between the characters) but the last character should be matched in the given data. This means you are not allowed to match a first and last character with *' wildeard. Input: The first line of the input will give you two integers n and m. From the next line you will get…arrow_forwardGiven an array of 1000 integers: a. In the best-case scenario, what is the fewest possible comparisons needed to find a number using linear search? b. In the worst-case scenario, what is the most number of comparisons needed to find a number using linear search? c. How many comparisons will be done if we attempt to search for a number that does not exist in the list using linear search?arrow_forward
- The SparseVector class should be extended to include a method sum() that accepts a SparseVector as an argument and returns a SparseVector that represents the term-by-term total of the input and output vectors. Note: To handle the situation where an entry becomes 0, you must use delete() (and pay close mind to precision).use java to codearrow_forwardLet's say you're going to invite some people to a party. You're considering n friends, but you know that they will have a good time only if each of them knows at least k others at the party. (Assume that if A knows B, then B automatically knows A.) Solve your problem by designing an algorithm for finding the largest possible subset of your friends where everyone knows at least k of the others, if such a subset exists. Please comment on code and show all work so I can better understand the problem and please use python thank you, To help you get started l've made a graph of "my friends": G={'Rachel':['Ross Monica'], 'Ross':['Rachel', 'Monica'], 'Monica':['Rachel', 'Ross'], 'Jon Snow':['Daenerys' "Sansa' "Arya'), 'Daenerys':['Jon Snow "Sansa "Arya' Khal Drogo'), 'Sansa':['Jon Snow DaenerysArya'), 'Arya':['Jon Snow "Daenerys' Sansa'), 'Khal Drogo':['Daenerys'], 'Cersei':['Jaime'], 'Jaime':['Cersei'], 'Bart':['Milhouse'], 'Milhouse':['Bart','Lisa'], 'Lisa':['Milhouse'],…arrow_forwardApply the Binary Search method to find a target using Recursive method.arrow_forward
- An n x n matrix is called a positive Markov matrix if eachelement is positive and the sum of the elements in each column is 1. Write thefollowing method to check whether a matrix is a Markov matrix:public static boolean isMarkovMatrix(double[][] m) Write a test program that prompts the user to enter a 3 x 3 matrix of doublevalues and tests whether it is a Markov matrix. Here are sample runs: Enter a 3−by−3 matrix row by row:0.15 0.875 0.375 ↵Enter0.55 0.005 0.225 ↵Enter0.30 0.12 0.4 ↵EnterIt is a Markov matrix Enter a 3−by−3 matrix row by row:0.95 −0.875 0.375 ↵Enter0.65 0.005 0.225 ↵Enter0.30 0.22 −0.4 ↵EnterIt is not a Markov matrixarrow_forwardIN JAVA, USING RECURSION PLEASE Create a method int[][] generateMatrix(int row, int col, int boundary1, int boundary2, int iteration) that generates a random matrix with random numbers between [min(boundary1, boundary2), max(boundary1, boundary2)). The sum of the diagonal and the sub-diagonal should be the same. If not, regenerate it again, until a matrix that satisfies the condition is generated (return that matrix). If you try iteration times and none of the matrixes satisfy the condition, return null.arrow_forwardWrite a program that inputs a matrix and displays the trans- pose of that matrix. A transpose of a matrix is obtained by converting all the rows of a given matrix into columns and vice versa.arrow_forward
- Write and implement a recursive version of the binary search algorithm. Also, write a version of the sequential search algorithm that can be applied to sorted lists. Add this operation to the class orderedArrayListType for array-based lists. Moreover, write a test program to test your algorithm.arrow_forwardAnalysis of Linear search (Worst, Average and Best Cases)#include <stdio.h>// Linearly search x in arr[]. If x is present then return the index,// otherwise return -1int search(int arr[], int n, int x){ int i; for (i=0; i<n; i++) { if (arr[i] == x) return i; } return -1;}arrow_forward4. Consider the function IndexEqual(A,i.j) that returns true if there exists an index x (i sx sj) such that A[x] = x; otherwise, retums false. You may assume A is a sorted integer array in which every element is unique. a. Write an efficient recursive algorithm for IndexEqual(A,i.j). b. What is the situation resulting in the best-case running time of your function, and give an expression for that running time? c. What is the situation resulting in the worst-case running time of your function, and give an expression for that running time in terms of n, where n=j-i+1?arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- 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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education