DSA_Unit_05
DSA_Unit_05
3. Quick Sort
2. Bubble Sort
Definition:
Definition: Quick Sort is a Divide and
Bubble Sort repeatedly swaps Conquer sorting algorithm that
adjacent elements if they are in the selects a pivot element, partitions
wrong order, pushing the largest the array around the pivot, and
element to the right in each pass. recursively sorts the subarrays.
Bellman-Ford Algorithm
The Bellman-Ford Algorithm finds
Single Source Shortest Path the shortest path from a source
Algorithms vertex to all other vertices, even if
Single-source shortest path the graph has negative weight edges
algorithms find the shortest paths (but no negative weight cycles).
from a single source vertex to all
other vertices in a weighted graph. Steps in Bellman-Ford Algorithm:
1. Initialize:
Dijkstra’s Algorithm o Set the distance to the
Dijkstra’s algorithm finds the source as 0 and all other
shortest path from a source vertex to vertices as infinity.
all other vertices in a graph with 2. Relax all edges V−1V-1
non-negative edge weights. times:
o For each edge (u, v)
Steps in Dijkstra's Algorithm: with weight w, update
1. Initialize: distance if
o Assign distance 0 to the extdistance[u]+w<extdi
source vertex and stance[v].
infinity to all others. 3. Detect negative weight
o Create a priority queue cycles:
and insert the source o If a shorter path is
vertex. found after V−1V-1
2. Process the closest vertex: iterations, a negative
o Extract the vertex with cycle exists.
the minimum distance
from the queue. Time Complexity: O(VE)O(VE),
o Update the distance to making it slower than Dijkstra’s
its adjacent vertices if a algorithm but useful for graphs with
shorter path is found. negative weights.
o Insert updated vertices
into the queue.
3. Repeat until all vertices are
processed. Dynamic Programming (DP)
Dynamic Programming is an o Trace back through
optimization technique used in stored values to build
computer science and mathematics the final solution.
to solve complex problems by
breaking them down into simpler Types of Dynamic Programming
subproblems. It is mainly used for Approaches:
problems with overlapping • Top-Down Approach
subproblems and optimal (Memoization): Solve the
substructure properties. problem recursively and store
intermediate results to avoid
General Method of Dynamic redundant computations.
Programming • Bottom-Up Approach
1. Characterize the Structure (Tabulation): Solve the
of an Optimal Solution: problem iteratively by
o Identify the computing solutions to
subproblems and how smaller subproblems first and
they relate to the building up to the final
original problem. solution.
o Recognize the optimal
substructure property
(i.e., the optimal
solution to a problem Knapsack Problem
contains optimal
solutions to its The Knapsack Problem is a
subproblems). combinatorial optimization problem
2. Define the Recursive where we try to maximize the value
Solution: of items placed in a knapsack of
o Express the solution limited capacity.
recursively in terms of
solutions to smaller Types of Knapsack Problems:
subproblems. 1. 0/1 Knapsack Problem:
3. Compute the Solution in a Items cannot be divided; we
Bottom-Up Manner: either take an item or leave it.
o Avoid redundant 2. Fractional Knapsack
calculations by storing Problem: Items can be
results in a table divided, allowing us to take
(memoization or fractional values of an item.
tabulation).
o Use iterative 0/1 Knapsack Problem (Using
approaches to fill the Dynamic Programming)
table.
4. Construct the Optimal Problem Statement: Given n items,
Solution (if required): each with a weight w[i] and a value
v[i], and a knapsack of capacity W, Instead of finding the shortest path
determine the maximum value that from a single source, this algorithm
can be obtained by selecting items finds the shortest path between
without exceeding the weight limit. every pair of nodes in a graph.
Algorithm:
1. Place a queen in the first row.
2. Move to the next row and
place a queen in a non-
attacking position.
3. If no valid position exists,
backtrack and try a different
position in the previous row.
4. Repeat until all queens are
placed.
Time Complexity:
• In the worst case, O(N!), as it
explores all permutations.
• Pruning (constraint checking)
reduces the number of
possible solutions.
Applications of Backtracking:
• Sudoku Solver: Fills empty
cells while ensuring valid