How to assess runtime of recursive algorithms

Assignment Help Data Structure & Algorithms
Reference no: EM13934179

Data structures Assignment

1 Union of sorted data collections

1.1 Union of sorted arrays

Let A and B be sorted arrays with all elements of A distinct and all elements of B distinct (though elements can occur in both A and B). Design an O(n) algorithm that produces a sorted array C containing all elements of A and B without repetitions. For instance, if A = [1; 2; 5; 7] and B = [2; 5; 10], C = [1; 2; 5; 7; 10].

1.2 Union of sorted linked lists

Solve the same exercise for the case where A and B are linked lists.

2 Intersection of sorted data collections

2.1 Intersection of sorted arrays

With A and B as in exercise 1, design an O(n) algorithm that produces a sorted array containing those elements that occur in both A and B. In the example as above C = [2; 5]

2.2 Intersection of linked lists

Solve the same exercise for the case where A and B are linked lists.

3 Reversing a linked list

In this exercise you are asked to design an algorithm that reverses a linked list. This algorithm takes as input a linked list and returns a linked list where the elements are in the reverse order. For example, if the linked list given as input is (10; 9; 23; 47; 15) then the resulting linked list should be (15; 47; 23; 9; 10).

3.1 Reversing a linked list using a stack

Reverse a linked list using the following approach:


- Traverse the list from the beginning to the end and push each element onto a stack that is initially empty.

- Remove (pop) the elements from the stack one by one and insert them into a new linked list so that each new element is inserted as the last one.

3.2 Reversing a linked list using constant memory

The previous task uses O(n) additional memory (the stack plus extra list). Reverse a linked list in O(n) time using only a constant amount of additional memory. That is, you are not allowed any additional data collections (e.g. arrays, lists, stacks, queues) and can only have several single variables.

Hint: consider moving along the list and reversing the pointers. That is, if the current pointer is from a to b (a:next = b), change it into b:next = a. Make sure that the pointer to the new rst element is correctly set.

4 Recursive algorithms for nding a speci ed pair of elements in an array

In this task, you are asked to design algorithms that use recursion instead of loops. That is, you are not allowed to use for or while loops and have to use the recursion instead.

4.1 Unsorted array: nd two elements di ering by 20

Design a recursive algorithm that checks whether the given array contains two elements whose di erence is 20.

4.2 Sorted array: nd two equal elements in linear time

Design a recursive O(n) algorithm that checks whether the given sorted array contains two equal elements. Remark. In this module we do not systematically learn how to assess runtime of recursive algorithms. One easy way to justify correctness of this particular algorithm is to design rst a non-recursive algorithm and then to transform it into recursive form.

4.3 Sorted array: nd two elements di ering by 20

Design a recursive O(n) algorithm that checks whether the given sorted array contains two elements whose di erence is 20. The remark for the previous exercise applies.

Reference no: EM13934179

Questions Cloud

Companies that sell groceries over the internet : Feasibility Study: Companies that sell groceries over the Internet are called e-grocers. Customers enter their orders, pay by credit card and receive delivery by truck. To determine whether an e-grocery would be profitable in one large city, a pot..
What do you think the outcome should have been : What do you think the outcome should have been for the Phoenix Four?
Determine the required pressure on the diameter piston : Determine the force in hydraulic cylinder AB for the position shown if the tree weighs 6000 lb. Determine the required pressure on the 4.72-in. diameter piston of the cylinder.
Explain the general purpose of the firewall : Explain the general purpose of the firewall above. Your explanation should include a description of the networks the gateway machine is connected to and how it is connected
How to assess runtime of recursive algorithms : Design a recursive O(n) algorithm that checks whether the given sorted array contains two equal elements. Remark. In this module we do not systematically learn how to assess runtime of recursive algorithms.
Critical value from normal distribution : Using the above RSAC and RSPAC, calculate the corresponding krs , krt , kk rs , kk rt k = 1, 2, 3, 4 5, 6, to identify a model describing ηt .Explain your choice of model. Please also provide the new model for STAT S802F - Multivariate and Time Se..
Substantial economic and social impact in australia : Topic:The drug "ice" is currently having a substantial economic and social impact in Australia. Outline the main problems caused, and suggest some measures to reduce the influence of this illicit drug in Australia.
Explain way that group members responded to initial stage : Post by Day 3 a description of the leadership style you observed. Explain how the leader creates safety establishes the norms of the group including confidentiality, and engages members.
We have to bring this new drug to market : We have to bring this new drug to market if we are going to be a player in this industry.

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  Karatsuba''s divide-and-conquer algorithm

In class we discussed Karatsuba's divide-and-conquer algorithm for integer multiplication, which multiplies n-bit numbers by recursively multiplying n bit numbers. We take two numbers X and Y and split them each into their most significant half a..

  What is the difference between syntax and semantics

Explain the distinction between an ambiguity in a proposed algorithm and an ambiguity in the representation of an algorithm.

  Question about character array

The 2-most important design issues that are specific to character string types are the given, Should strings be simply a special kind of character array or a primitive type?

  Give an algorithm that takes a sequence of points

Give an algorithm that takes a sequence of points in the plane (x1, y1), (x2, y2), ...., (xn, yn) and an integer k as input and returns the best piecewise linear function f consisting of at most k pieces that minimizes the sum squared error.

  Questionneural and tree learning on continuous attributesa

questionneural and tree learning on continuous attributesa in general feedforward neural networks multi-layer

  Design a linear-time algorithm that works directly with

suppose a cs program consists of n courses. the prerequisite graph g has a vertex for each course and an edge from

  Structural and behavioral models

Your analysis phase of the SRS project went well and your team feels good about their Functional, Structural, and Behavioral models. You also discussed the result of your analysis with the School of Prosperity (SoP) administration and they seem to be..

  Devise a linear-time algorithm to count the parallel edges

Devise a linear-time algorithm to count the parallel edges in a graph. Write the algorithm in pseudocode.

  Finding majority element

Let A be an array of n elements. An element x is said to be a majority element in A if it occurs in A more than n/2 times.

  Create an algorithm to produce list of customers

Create an algorithm to produce list of customers from Glad Rags Clothing Company's customer master file. Each record on customer master file contains the customer's number

  Measure the execution time of the three sorting algorithms

The program should display the array values before sorting and then after invoking each sorting method. For this case, consider SIZE value 100 and MAXRNG value 9999.

  What items do we require for a small business wlan what

imagine that you work at a small company with 75 employees in a modern office building. some of the employees are eager

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd