DAA Unit 1: Introduction, Performance Analysis, and Graph Traversals
Introduction to Algorithms
Definition: An algorithm is a finite sequence of well-defined instructions that, when executed, accomplishes a specific task or solves a particular problem.
Formal Definition: A finite set of instructions that, if followed, accomplishes a particular task.
Essential Characteristics of an Algorithm:
Input: The algorithm accepts zero or more externally supplied values.
Output: It produces at least one meaningful result.
Definiteness: Every instruction is precise, clear, and unambiguous.
Finiteness: The process terminates after a finite number of steps.
Effectiveness: Each operation is simple, feasible, and executable.
Algorithm Specification and Pseudocode Conventions
Pseudocode: This is an informal, language-independent notation used to describe algorithm steps.
Pseudocode Conventions:
Comments: Used to improve readability. Written after
//or enclosed within comments.Compound Statements: Multiple statements grouped between
BEGINandENDkeywords.Assignment Statement: Assigns a value to a variable using the symbol
←. Example:.Conditional Statements: Used for decision making with keywords
IF,THEN, andELSE.Repeat-Until Loop: Executes at least once and stops when the condition becomes true.
For Loop: Used when the exact number of iterations is known.
Case Statement: Used for multi-way selection, similar to a switch statement. Format:
CASE choice OF 1 : Add 2 : Delete 3 : Search ENDCASE.Procedure/Function Call: Invokes another module using
CALL, e.g.,CALL Sort(A, n).Input/Output Statements: Used for accepting and displaying data with
READ(n)andWRITE(sum).
Recursion and the Towers of Hanoi
Recursion: A technique where a function or algorithm calls itself to solve a problem.
Direct Recursion: An algorithm calls itself directly.
Indirect Recursion: An algorithm calls another algorithm which eventually calls the original.
Towers of Hanoi Example:
Description: A classic puzzle with three towers (, , and ) and disks of different sizes stacked on tower in decreasing order.
Rules: Move only one top disk at a time; a larger disk cannot be placed on a smaller disk.
Objective: Move all disks from Tower (Source) to Tower (Destination) using Tower (Auxiliary).
Performance Analysis
Objective Measures: Execution time is not ideal because it is computer-specific. Counting statements is not ideal as it varies by language and programmer style.
Phases of Evaluation:
A priori estimates: Theoretical calculations.
A posteriori testing: Practical performance testing.
Space Complexity: The amount of memory an algorithm needs to run to completion.
Formula: , where is a constant fixed part (instruction space, simple variables, constants) and is the variable part (size dependent on problem instance).
Time Complexity: The amount of computer time needed to run to completion.
Total time .
Compile time is independent of instance characteristics.
Program Step: A meaningful statement or group of statements whose execution time is constant regardless of input size.
Time Complexity Calculation Examples
Summation (RSum):
An algorithm that recursively sums elements of an array.
Step count for : .
Step count for n > 0: , where .
Matrix Addition (Add):
Iterates through rows and columns .
for i = 1 to m doexecutes times.for j = 1 to n doexecutes times.Inner addition executes times.
Total steps: .
Fibonacci Calculation:
Calculates the Fibonacci number using a loop.
if (n ≤ 1): step.for i := 2 to n do: steps.Inner loop assignments and additions: steps.
Total steps for n > 1: .
Asymptotic Notations and Growth Rates
Purpose: To compare algorithms by looking at their rate of growth as approach infinity.
Common Notations:
Big-OH (O): Upper bound.
Big-OMEGA (Ω): Lower bound.
Big-THETA (Θ): Tight bound; if and only if and .
Rate of Growth Concept: Low-order terms are insignificant for large . For example, .
Comparison of Growth Orders (at N = 1,000,000):
: .
: .
: .
: .
: .
: .
: Practically infinite.
Mathematical Properties:
Transitivity: If and , then .
Reflexivity: .
Symmetry: .
Transpose Symmetry: .
Analysis of Linear Search
Input: Sequence of numbers and a key.
Operations:
i ← 1: 1 time.while i ≤ n and A[i] != key: executes times.do i++: executes times.
Performance Cases:
Best Case: Key found at index 1. Execution steps: . Complexity: .
Worst Case: Key not in list. Iterates through all elements. Execution steps: . Complexity: .
Average Case: Assume search for random item (executes times). Steps: . Complexity: .
AVL Trees
Definition: Height-balanced binary search trees (BST).
Balance Factor (BF): .
Requirements: For every node, the heights of the left and right subtrees can differ by no more than 1. A node is balanced if .
Left-heavy: (depending on terminology, some systems use for left-heavy; the transcript notes as left-heavy and as right-heavy).
Rotations (Rebalancing):
LL Rotation: Single right rotation at node A (where A has and its left child has or ).
RR Rotation: Single left rotation at node A (where A has and its right child has or ).
LR Rotation: Double rotation (left rotation at child B, then right rotation at A).
RL Rotation: Double rotation (right rotation at child B, then left rotation at A).
m-Way Search Trees
Definition: Multi-way trees where each node contains multiple elements. In a tree of order , each node has a maximum of elements and children.
Complexity: Operation access is , where height .
Insertion Cases:
Non-full leaf: Insert in sorted position.
Full leaf: Split leaf, promote middle key to parent.
Full parent: Continue splitting and promoting upward.
Full root: Split root, create new root, increasing tree height.
Deletion Cases:
Leaf key: Simple delete.
Key with one subtree: Replace with largest from left or smallest from right, then delete replacing node.
Key with both subtrees: Replace with largest from LST or smallest from RST, then delete replacement element.
Disjoint Sets and Union-Find (DSU)
Concept: Collections of non-overlapping sets. Represented as a forest where children point to parents.
Operations:
Find: Determines the representative/root of the set containing an element.
Union: Combines two disjoint sets. Make the root of one tree the child of the root of the other.
Array Representation: Array
p[1:n]wherep[i]is the parent. Ifp[i] = -1, index is a root.Optimization Rules:
Weighting Rule for Union: Link the root of the smaller tree to the root of the larger tree.
Collapsing Rule for Find: Update the parent links of all nodes on the path from to the root to point directly to the root, improving future efficiency.
AND/OR Graphs and Problem Reduction
Representation: Directed graphs where nodes represent problems and edges represent subproblems.
Node Types:
AND Node: All child subproblems must be solved to solve the parent.
OR Node: Solving any one child solves the parent.
Terminal Node: primitive, solvable problems.
Key Traits: AND/OR graphs are not necessarily trees; they can share subproblems or contain cycles. A "Solution Graph" is the subgraph proving solvability.
Graph Traversals
Reachability Problem: Determines if a path exists from vertex to .
Breadth First Search (BFS):
Starts at source, marks as visited, explores all neighbors using a FIFO Queue.
Complexity for identifying connected components: .
Depth First Search (DFS):
Explores a path as deeply as possible before backtracking using recursion or a stack.
Assigns a Depth First Number (DFN) based on visit order.
Tree Edge: Edge in the DFS spanning tree.
Back Edge: Edge connecting a vertex to its ancestor in the DFS tree.
Connected and Biconnected Components
Connected Graph: All vertices reached in one BFS/DFS traversal.
Spanning Tree: Sub-graph connecting all vertices without cycles (using edges).
Articulation Point (Cut Vertex): A vertex whose removal disconnects the graph. Examples in the lesson include vertices 2, 3, and 5.
Biconnected Graph: Contains no articulation points; provides high fault tolerance.
Biconnected Component: A maximal biconnected subgraph.
DFS in Biconnectivity: Used to compute . An articulation point exists if .