0% found this document useful (0 votes)
177 views

Dsa 2 PDF

The document discusses Prim's

Uploaded by

Lol Telr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
177 views

Dsa 2 PDF

The document discusses Prim's

Uploaded by

Lol Telr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Q2) a) Apply the algorithm to draw Binary search tree for the following data.

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

a) Explain Fibonacci Number with respect to dynamic programming

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.

In dynamic programming, we break down a problem into smaller subproblems and


solve each subproblem only once, storing the solution in memory for future use. To
calculate the nth Fibonacci number, we can use dynamic programming by first
initializing an array to store the values of the Fibonacci sequence. We start by setting the
first two elements of the array to 0 and 1, respectively. We then iterate through the
array, calculating the value of each element as the sum of the previous two elements in
the array.

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

anddetermine inorder. postorder & preorder?

c) Define Hash function 2 collision.

A hash function is a mathematical function that takes in an input (often called


the "key") and outputs a fixed-size value, known as the "hash" or "digest." The
purpose of a hash function is to map arbitrary-sized data to a fixed-size value,
which can then be used as a unique identifier or fingerprint for that data.

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.

Hash collisions can be problematic because many applications rely on hash


functions to provide a unique identifier for data, such as in data indexing,
cryptography, and password storage. If two inputs produce the same hash
value, it can lead to data corruption, security vulnerabilities, and other issues.

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.

03) a) Apply Rain Terrace algorithm to the following problem.

Input:- Height 14, 2, 0, 3, 2, 51. Draw the figure and find solution.

b) Explain power set with example.

c) Discuss use of priority queue.

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

a)What is Binary Search?

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.

c) Write an algorithm to count number of nodes in singly linked list.


04) a) What is Jump Game algorithm?
Jump Game Algorithm is a popular algorithm used in Data Structures and Algorithms. It
is used to solve a problem where given an array of non-negative integers, you are
initially positioned at the first index of the array. Each element in the array represents
your maximum jump length at that position. Your goal is to reach the last index of the
array using the minimum number of jumps.

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.

The algorithm for the Jump Game Algorithm:

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.

ii. Increment the number of jumps by 1.

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)

c) Explain need of circular queue.

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.

c) What is the purpose of linked list"

05) a) Consider the instance of 0/1 knapsack problem n = 3.

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 to reverse the nodes of a linked list.


OR a) Find the longest common subsequence for following string using

dynamicprogramming. X={A, B, C, D, B, A, C,D,F} Y={C,B,A,F}

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy