Question

In: Computer Science

DATA STRUCTURES: For each algorithm, always explain how and why they work. If not by a...

DATA STRUCTURES:

For each algorithm, always explain how and why they work. If not by a proof, at least by a clear explanation. ALWAYS, analyze the running time complexity of your algorithms. Do not write a program. Write pseudo codes or explain in words

Q1: We want to maintain both a Queue and a Priority Queue. When you do Enqueue you also add the item to the Priority Queue and when you do Dequeue you also remove the item from the Priority Queue. And vise-versa. Show how to do it.

Q2: Give a data structure that implements a Min-Heap and the command Max(S) that returns the maximum in the heap. Try and make the last operation as fast as possible.

Q3: (a)Show how to implement a Queue by a Priority Queue. What is the run time per operation?

(b Show how to implement a Stack using Priority Queue. What is the run time per operation?

Solutions

Expert Solution

SOLUTION:

CONSIDERING THE CONDITIONS AND REQUIREMENTS FROM THE QUESTION

THUS THE PSEUDO CODES FOR SET OF QUESTIONS ARE:

QUESTION 1:

Say that we want to maintain both a Queue and a Priority Queue. This means that when you do Enqueue you also add the item to the Priority Queue and when you do Dequeue you also remove the item from the Priority Queue. And vise-versa. Show how to do it.

ans:

it is just an extension of queue

It properties incluude:

Every item has its own priority
first of all low priority element is dequeued and then high priority element will be dequeued
If two elements have the same priority, it will be treated according to order of sequence
insert(item, priority): This adds an item
getHighestPriority(): it shows item which has highest priority
deleteHighestPriority(): it deletes an item which has highest priority

QUESTION 2:

Give a data structure that implements a Min-Heap and the command Max(S) that returns the maximum in the heap. Try and make the last operation as fast as possible.

ans:

Input : 15
/ \
20 22
/ \ / \
44 31 59 41
Output : 59

Input : 10
/ \
30 18
Output : 30
ans:

QUESTION 3:

1. Show how to implement a Queue by a Priority Queue. What is the run time per operation?

2. Show how to implement a Stack using Priority Queue. What is the run time per operation?

ans:It can be implemented using many common data structures

1)Using Array:

struct item {
int item;
int priority;
}
2)here , elements are assigned with a priority and then pushed. A stack requires elements to be processed it follows LIFO rule. The idea is to associate a count that determines when it was pushed. it just work as a key for the priority queue.So the implementation of stack uses a priority queue of pairs, 1st element is considered to be key.

NOTE: PLEASE UPVOTE ITS VERY MUCH NECESSARY FOR ME A LOT. PLZZZZ.....


Related Solutions

For each algorithm, always explain how and why they work. If not by a proof, at...
For each algorithm, always explain how and why they work. If not by a proof, at least by a clear explanation. ALWAYS, analyze the running time complexity of your algorithms. Do not write a program. Write pseudo codes or explain in words Q2: Give a data structure that implements a Min-Heap and the command Max(S) that returns the maximum in the heap. Try and make the last operation as fast as possible. Q3: (a)Show how to implement a Queue by...
In all algorithm, always explain how and why they work. ALWAYS, analyze the complexity of your...
In all algorithm, always explain how and why they work. ALWAYS, analyze the complexity of your algorithms. In all algorithms, always try to get the fastest possible. A correct algorithm with slow running time may not get full credit. In all data structures, try to minimize as much as possible the running time of any operation. 1.Show that if a binary tree has a vertex with a single child, then this can not be an optimal Huffman tree. 2. Question...
Q1. In all algorithm, always explain how and why they work. If not by a proof,...
Q1. In all algorithm, always explain how and why they work. If not by a proof, at least by a clear explanation. ALWAYS, analyze the running time complexity of your algorithms. Do not write a program. Write pseudo codes or explain in words. (A) Find an efficient data structure supporting the following operations. Insert(S, x), Delete−Max(S), and Delete−100'th(S) which deletes from H the 100 largest element in the structure. Assume that the number of elements is more than 100. Also...
A) What is the Production Possibilities Curve and how does it work? Explain why each of...
A) What is the Production Possibilities Curve and how does it work? Explain why each of the following would cause the Production Possibilities Curve for a country to move outward or inward: B) The average work week increases. C) There is an increase in the retirement age. D) Immigration decreases. E) There are decreases in smoking, alcohol, and drug abuse.
Data Structures and Algorithm Analysis in Java by Weiss: Exercise 6.8 Show the following regarding the...
Data Structures and Algorithm Analysis in Java by Weiss: Exercise 6.8 Show the following regarding the maximum item in the heap: A) It must be at least one of the leaves. B) There are exactly [N/2] leaves. C) Every leaf must be examined to find it.
Explain why prices are always flexible.
Explain why prices are always flexible.
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each algorithm is applied to a group of 52 sample problems. The new algorithm completes the sample problems with a mean time of 16.34 hours. The current algorithm completes the sample problems with a mean time of 16.93 hours. The standard deviation is found to be 3.913 hours for the new algorithm, and 3.6243.624 hours for the current algorithm. Conduct a hypothesis test at the...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each algorithm is applied to a group of 41sample problems. The new algorithm completes the sample problems with a mean time of 23.42 hours. The current algorithm completes the sample problems with a mean time of 26.39 hours. Assume the population standard deviation for the new algorithm is 3.442 hours, while the current algorithm has a population standard deviation of 5.364 hours. Conduct a hypothesis...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each algorithm is applied to a group of 5454 sample problems. The new algorithm completes the sample problems with a mean time of 11.9011.90 hours. The current algorithm completes the sample problems with a mean time of 14.0714.07 hours. Assume the population standard deviation for the new algorithm is 5.1155.115 hours, while the current algorithm has a population standard deviation of 5.7365.736 hours. Conduct a...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each algorithm is applied to a group of 51 51 sample problems. The new algorithm completes the sample problems with a mean time of 24.43 24.43 hours. The current algorithm completes the sample problems with a mean time of 24.68 24.68 hours. The standard deviation is found to be 3.481 3.481 hours for the new algorithm, and 3.420 3.420 hours for the current algorithm. Conduct...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT