Ada
Ada
The best-case time complexity of Quick Sort is O(n log n), which
occurs when the pivot element is chosen such that it divides the
array into two sub-arrays of roughly equal size. In this case, the
recursion depth is only log n, and each level of the recursion
performs O(n) work.
The worst-case time complexity of Quick Sort is O(n^2), which
occurs when the pivot element is chosen to be the largest or
smallest element in the array. In this case, the partitioning step will
only create one sub-array with all the elements except the pivot,
and the recursion will continue on this sub-array until it has only
one element. This results in n recursive calls, each of which
performs O(n) work.
The average-case time complexity of Quick Sort is also O(n log n),
and this is typically the case for randomly chosen pivot elements.
However, there are certain types of input that can cause Quick Sort
to run in O(n^2) time even on average.
Binary search is a search algorithm that finds an element in a sorted list or array
by repeatedly dividing the search space in half. It is much faster than linear
search, especially for large datasets.
• It is much faster than linear search for large datasets. In the best case,
binary search can find the target value in just one comparison!
• It is relatively simple to implement.
• It can be used to search for any element in a sorted list or array.
❖ multiply 981 and 1234 using the divide-and-conquer method.
Difference between Greedy Approach and
Dynamic Programming
2 In this technique, there is no In this technique, you can get the assurance
assurance of obtaining the optimal of an optimal solution.
solution.
3 Greedy approach is used to get the Dynamic programming is also used to get
optimal solution. the optimal solution.
4 The greedy method never alters the This technique prefers memorization due to
earlier choices, thus making it more which the memory complexity increases,
efficient in terms of memory. making it less efficient.
❖
Back Edge: It is an edge (u, v) such that v is an ancestor of node u but not part
of the DFS Traversal of the tree. Edge from 5 to 4 is a back edge. The
presence of a back edge indicates a cycle in a directed graph.
Acyclic Directed Graph: - It is directed graph that contain no cycle. This type of
graph is used in compliers for identifying the common subexpressions
Step 2: Check for adjacent Nodes, Now we have to choices (Either choose
Node1 with distance 2 or either choose Node 2 with distance 6 ) and choose
Node with minimum distance. In this step Node 1 is Minimum distance
adjacent Node, so marked it as visited and add up the distance.
Distance: Node 0 -> Node 1 = 2
Dijkstra’s Algorithm
Step 3: Then Move Forward and check for adjacent Node which is Node 3,
so marked it as visited and add up the distance, Now the distance will be:
Distance: Node 0 -> Node 1 -> Node 3 = 2 + 5 = 7
Dijkstra’s Algorithm
Step 4: Again we have two choices for adjacent Nodes (Either we can
choose Node 4 with distance 10 or either we can choose Node 5 with
distance 15) so choose Node with minimum distance. In this step Node 4 is
Minimum distance adjacent Node, so marked it as visited and add up the
distance.
Distance: Node 0 -> Node 1 -> Node 3 -> Node 4 = 2 + 5 + 10 = 17
Dijkstra’s Algorithm
Step 5: Again, Move Forward and check for adjacent Node which is Node 6,
so marked it as visited and add up the distance, Now the distance will be:
Distance: Node 0 -> Node 1 -> Node 3 -> Node 4 -> Node 6 = 2 + 5 + 10
+ 2 = 19
Dijkstra’s Algorithm
So, the Shortest Distance from the Source Vertex is 19 which is optimal
one
Why it is This is used to solve the decision-based This is used to solve the
used? problem. optimization problem.
Nodes This is a state space tree where the This explored the optimization
node explored the depth-first search. problem.
Solves Backtracking can solve the game of It doesn’t solve any game
chess and sudoku. problem.
Application This application is used to solve the N- This type of application is used to
Used queen problem, the Hamilton cycle, and solve the problem based on the
the problem based on graph coloring. Travelling salesman problem.
❖ Explain Minimax Principle
NP-complete:
• These are the "toughest" problems in NP. Any problem in NP can be
reduced to an NP-complete problem in polynomial time.
• Finding a solution to an NP-complete problem is like finding the master
key that unlocks all other NP problems.
• If we could efficiently solve any NP-complete problem, we could
efficiently solve all NP problems, which would be a major breakthrough
in computer science.
• Examples:
o 3SAT: A specific version of SAT where each clause contains
exactly 3 variables.
o Knapsack problem: Given a set of items with weights and values,
find the subset with the maximum total value that doesn't exceed a
weight limit.
NP-Hard:
❖ The Rabin-Karp-Algorithm
The Rabin-Karp string matching algorithm calculates a hash value for
the pattern, as well as for each M-character sub sequences of text to be
compared. If the hash values are unequal, the algorithm will determine
the hash value for next M-character sequence. If the hash values are
equal, the algorithm will analyze the pattern and the M-character
sequence. In this way, there is only one comparison per text
subsequence, and character matching is only required when the hash
values match.
Example: For string matching, working module q = 11, how many spurious hits does
the Rabin-Karp matcher encounters in Text T = 31415926535.......
1. T = 31415926535.......
2. P = 26
3. Here T. Length =11 so Q = 11
4. And P mod Q = 26 mod 11 = 4
5. Now find the exact match of P mod Q... Solution: