DAA Unit 2 OBS
DAA Unit 2 OBS
1. Disjoint Sets
A disjoint-set (or union-find) data structure is used to keep track of a set of elements
partitioned into a number of disjoint (non-overlapping) subsets.
Union-Find Algorithms:
Union by Rank: This optimizes the union operation by attaching the smaller tree
under the root of the larger tree.
Path Compression: This optimizes the find operation by making nodes point
directly to the root, flattening the structure of the tree.
Time Complexity:
With union by rank and path compression, the amortized time complexity for each
operation (Find and Union) is O(α(n)), where α(n) is the inverse Ackermann
function, which grows very slowly, making the operations almost constant in
practice.
2. Priority Queue
A priority queue is an abstract data type in which each element has a "priority"
associated with it. Elements with higher priority are dequeued before elements with
lower priority.
Heaps:
A heap is a specialized tree-based data structure that satisfies the heap property:
Max-Heap Property: The key at a node is greater than or equal to the keys of its
children.
Min-Heap Property: The key at a node is less than or equal to the keys of its
children.
Common Operations in Heaps:
Heapsort:
Steps in Heapsort:
Time Complexity:
3. Backtracking
Backtracking is a general algorithmic technique used for solving problems
incrementally, by building candidates for solutions and abandoning candidates
("backtracking") as soon as it is determined that they cannot possibly lead to a valid
solution.
General Method:
1. Define a solution space.
2. Explore the solution space by traversing nodes, which represent partial solutions.
3. At each node, check if it is a valid solution.
4. If not, backtrack by moving to the previous node and trying a different path.
Applications of Backtracking
1. n-Queens Problem
Problem: Place n queens on an n x n chessboard so that no two queens
threaten each other (i.e., no two queens share the same row, column, or
diagonal).
Approach: Place queens row by row. If placing a queen on a row leads to no valid
position for the next queen, backtrack and try a different placement.
Sudo Algorithm:
return True
2. Sum of Subsets Problem
Problem: Given a set of non-negative integers and a value S , find all subsets
whose elements sum to S .
Approach: Explore all subsets by including/excluding each element. Backtrack
when the partial sum exceeds S .
Sudo Algorithm:
3. Graph Coloring
Problem: Assign colors to the vertices of a graph such that no two adjacent
vertices have the same color using the minimum number of colors.
Approach: Explore color assignments for each vertex and backtrack when two
adjacent vertices have the same color.
Sudo Algorithm:
C
4. Hamiltonian Cycle
Problem: A Hamiltonian cycle is a cycle in a graph that visits each vertex exactly
once and returns to the starting vertex.
Approach: Explore different paths in the graph, and backtrack if a valid cycle
cannot be formed.
Sudo Algorithm:
C
return True
return True
return False