Dsa 2 PDF
Dsa 2 PDF
10,
08, 15, 12, 13, 07, 09, 17, 20, 18, 04, 05
b) Compare Linear Queue and Circular Queue.
c) Explain Stack and its following operation
I. Push
ii.Pop
OR
Fibonacci numbers are a sequence of numbers where each number is the sum of the
two preceding ones, starting from 0 and 1. The Fibonacci sequence is often used as an
example in computer programming to explain the concept of recursion.
However, the recursive approach to calculating the Fibonacci numbers has exponential
time complexity and is not practical for large input sizes. Dynamic programming is an
efficient technique to solve the Fibonacci problem in a much faster way.
By using dynamic programming, we avoid redundant calculations and reduce the time
complexity from exponential to linear, making it feasible to calculate Fibonacci numbers
for larger input sizes.
b) Construct Binary Tree for following data 10, 25, 2, 4, 7, 13, 11, 22
A collision occurs when two different inputs produce the same hash value. This
can happen because the output space of a hash function is typically much
smaller than the input space, so there are more possible inputs than there are
possible outputs. When two inputs produce the same hash value, it is called a
hash collision.
To minimize the risk of collisions, hash functions are designed to distribute the
input data evenly across the output space, using techniques such as modular
arithmetic, bit shifting, and XOR operations. Additionally, modern hash
functions often incorporate "hashing algorithms" that help to reduce the
likelihood of collisions by introducing additional randomness into the output.
However, even with these techniques, it is still possible for collisions to occur,
especially when the input data is very large or the hash function is poorly
designed.
Input:- Height 14, 2, 0, 3, 2, 51. Draw the figure and find solution.
A priority queue is a data structure that allows you to store a collection of elements and retrieve
them in order of priority. The priority of each element is determined by a priority function that
you define.
The use of priority queue are:
• Dijkstra’s Shortest Path Algorithm using priority queue: When the
graph is stored in the form of adjacency list or matrix, priority queue
can be used to extract minimum efficiently when implementing
Dijkstra’s algorithm.
• Prim’s algorithm: It is used to implement Prim’s Algorithm to store keys
of nodes and extract minimum key node at every step.
• Heap Sort : Heap sort is typically implemented using Heap which is an
implementation of Priority Queue.
• Operating systems: It is also used in Operating System for load
balancing (load balancing on server), interrupt handling.
• Optimization problems: Priority Queue is used in optimization
problems such as Huffman coding, Kruskal’s Algorithm and Prim’s
Algorithm
• Robotics: Priority Queue is used in robotics to plan and execute tasks
in a priority-based manner.
• Event-driven simulations: Priority queues are used in event-driven
simulations, such as network simulations, to determine which events
should be processed next.
• Medical systems: Priority queues are used in medical systems, such
as triage systems in emergency departments, to prioritize patients
based on the urgency of their condition.
OR
Binary search is a search algorithm that operates on a sorted array or list of elements. It
works by repeatedly dividing in half the portion of the array that could contain the
target value, until the target value is found or until it is determined that the target value
is not in the array.
The algorithm begins by comparing the target value to the middle element of the array.
If the middle element is equal to the target value, the search is complete. If the middle
element is greater than the target value, the algorithm repeats the search on the lower
half of the array. If the middle element is less than the target value, the algorithm
repeats the search on the upper half of the array. This process continues until the target
value is found or until the search has narrowed down to a range of indices where the
target value cannot exist.
Binary search is a very efficient algorithm for searching sorted arrays, with a time
complexity of O(log n), where n is the number of elements in the array. This makes it
much faster than linear search, which has a time complexity of O(n), where n is the
number of elements in the array.
However, binary search requires that the array is sorted beforehand, and if the array is
frequently modified, it may be more efficient to use a different search algorithm.
b) Find the Hamiltonian cycle from following graph.
The basic idea of the Jump Game Algorithm is to keep track of the farthest index that
can be reached from the current position. At each step, the algorithm checks if the
current position can reach the end of the array, if not, it updates the farthest index that
can be reached from the current position. The algorithm continues until the farthest
index that can be reached is greater than or equal to the end of the array.
1. Initialize the farthest index that can be reached to 0 and the number of jumps to 0.
2. Loop through the array from the first index to the second-to-last index:
a. If the current index plus its value is greater than the farthest index that can be
reached:
i. Update the farthest index that can be reached to the current index plus its value.
b. If the farthest index that can be reached is greater than or equal to the end of the
array, return the number of jumps.
3. If the farthest index that can be reached is less than the end of the array, return -1.
The time complexity of the Jump Game Algorithm is O(n), where n is the length of the
array.
b) Sort the following data using merge sort algorithm (38, 27, 43,3,9,82,10)
OR
a) Illustrate the stages, in finding the minimum cost spanning tree for givengraph
using Prim's algorithm..
b) Explain Rules for Tower of Hanoi with an suitable example.
m 20. p (25, 24, 15), w (18, 15, 10) using dynamic programming. Determine the
optimal profit and the solution vector.
b) Write an algorithm delete element from linked list whose sum is equal to zero.
Step 1: Let sum = 0
Step 2: Let current_node = head
Step 3:Repeat step 4 to step 6 until current_node !=null:
Step 4:sum = sum +current_node.data
Step 5:if sum == 0:
delete entire linked list
end of if
Step 6: current_node = current_node.next
Step 7:End