I. Foundations of Algorithm Design and Analysis
Algorithm design develops finite, precise procedures for solving computational problems, while algorithm analysis predicts the resources those procedures require independently of a particular implementation or machine.
- Algorithm: A finite sequence of unambiguous steps that maps each valid input instance to the required output.
- Correctness: An algorithm must terminate and produce an output satisfying the problem specification for every valid input.
- Input size: Resource use is expressed as a function of a size parameter , such as the number of array elements, graph vertices, or input bits.
- Time complexity: Running time is estimated by counting elementary operations under a stated computational model.
- Space complexity: Memory use includes input storage, auxiliary data structures, and—in recursive algorithms—the call stack.
- Abstraction: Machine-dependent constants are usually suppressed so that growth rates can be compared as becomes large.
- Design–analysis relationship: Data structures determine available operations, computational models assign their costs, and asymptotic analysis compares alternative algorithms.
II. Elementary Data Structures — Organizing and Accessing Data
A. Elementary data structures
Elementary data structures arrange values so that algorithms can store, retrieve, update, and traverse them efficiently.
- Array: A contiguous sequence supporting indexed access.
- Reading or writing takes time.
- Searching an unsorted array takes time in the worst case.
- Inserting at index may require shifting as many as elements.
- Linked list: A sequence of nodes in which each node stores a value and a link to the next node.
- Accessing the -th node takes , hence in the worst case.
- Insertion after a known node takes .
- A doubly linked list also stores a predecessor link, enabling movement in both directions.
- Stack: A last-in, first-out structure with
push,pop, andtopoperations, each normally .- Function calls use a call stack containing parameters, local variables, and return addresses.
- Queue: A first-in, first-out structure using
enqueueanddequeue, normally in time.- A circular array avoids shifting elements after each removal.
- Tree: A hierarchical structure consisting of nodes and edges, with one root and no cycles.
- In a balanced binary search tree, search, insertion, and deletion take .
- In a highly skewed tree, the same operations can deteriorate to .
- Hash table: An array indexed through a hash function , where is a key.
- Search and update take expected time under suitable hashing assumptions.
- Collisions require techniques such as chaining or open addressing.
- Representation choice: The best structure depends on the dominant operation; arrays favor random access, while linked structures favor local insertion and deletion.
III. Computational Models — Assigning Costs to Operations
A. Basic computational models
A computational model specifies which operations are elementary and how their time and storage costs are measured.
- Random Access Machine model: The RAM model represents computation as instructions operating on registers and randomly addressable memory.
- Arithmetic, comparison, assignment, branching, and memory access are usually assigned unit cost.
- A loop executing one constant-time statement times therefore has cost , where and are constants.
- Word-RAM qualification: A machine word contains bits, commonly assuming so an input index fits in one word.
- Operations on one word cost .
- Arbitrarily large integers cannot realistically be treated as constant-cost values.
- Comparison model: Algorithms gain information about keys only through comparisons such as .
- Comparison sorting requires comparisons in the worst case.
- Counting sort escapes this bound because it uses integer values as indices rather than relying only on comparisons.
- Pointer model: Data is accessed through references between records rather than unrestricted address arithmetic.
- It naturally represents linked lists, trees, and graphs.
- Cost conventions: The model must remain consistent when algorithms are compared; treating multiplication as in one analysis and bit-dependent in another can invalidate the comparison.
- Model limitation: Unit-cost analysis abstracts away cache behavior, instruction pipelines, networks, and storage latency, so empirical performance can differ even when asymptotic bounds agree.
IV. Analysis of Algorithms — Behaviour Across Inputs
A. Analysis of algorithms: best-case, average-case, and worst-case behaviour
Case analysis distinguishes the minimum, expected, and maximum resources used among inputs of the same size.
- Cost function: Let be the operation count for input , and let be the set of inputs of size .
- Best-case behaviour: The minimum cost is
TextT_best(n) = min { T(x) : x ∈ I_n }.- Linear search has comparison when the target is the first element.
- This bound may describe unusually favorable inputs rather than typical performance.
- Worst-case behaviour: The maximum cost is
TextT_worst(n) = max { T(x) : x ∈ I_n }.- Linear search uses comparisons when the target is absent or occurs last.
- Worst-case bounds provide guarantees valuable in real-time and safety-critical systems.
- Average-case behaviour: Given probability for each , expected cost is
TextT_avg(n) = Σ T(x)P(x), for all x ∈ I_n.- If a successful linear-search target is equally likely to occupy any position, the expected comparisons are
- An average-case result is meaningful only when its input distribution is stated.
- Amortized distinction: Amortized analysis averages costs across an operation sequence, not across random inputs; dynamic-array append is amortized despite occasional resizing.
- Resource selection: Analyses may count comparisons, assignments, arithmetic operations, memory cells, or communication, depending on the computational setting.
V. Asymptotic Growth — Comparing Long-Run Efficiency
A. Asymptotic notations: big O notation
Asymptotic notation classifies resource functions by growth rate while ignoring constant factors and lower-order terms.
- Big —upper bound: For nonnegative functions and ,
Textf(n) ∈ O(g(n))
when constants and exist such that Here is input size, is a constant multiplier, and is the threshold beyond which the bound holds. - Worked bound: For , when , so using and .
- Big —lower bound: if constants exist such that for all .
- Big —tight bound: when it is both and ; thus .
- Growth hierarchy: Common rates increase in the order
- Interpretation: Big does not automatically mean worst case; it can upper-bound best-, average-, or worst-case functions.
- Simplification rules: Constant factors are removed, lower-order terms are discarded, and sequential costs are added while nested independent loops generally multiply.
VI. Recursion — Solving Problems Through Smaller Instances
A. Recursion
Recursion defines a solution in terms of solutions to smaller instances of the same problem.
- Essential components:
- Base case: Stops further calls and directly returns a result.
- Recursive case: Reduces the input and combines the smaller result.
- Progress condition: Ensures each call approaches a base case.
- Factorial example:
TextFACTORIAL(n) if n = 0 return 1 return n × FACTORIAL(n - 1)
For integer , this implements with . - Call stack:
FACTORIAL(4)creates calls for ; suspended calls return in reverse order, producing . - Resource use: Factorial makes calls, takes time, and uses stack space.
- Divide and conquer: Algorithms such as merge sort recursively solve multiple smaller subproblems and combine their results.
- Limitations: Missing base cases cause nontermination, insufficient input reduction causes infinite recursion, and deep recursion can overflow the call stack.
- Iteration comparison: Tail-recursive processes can often be rewritten as loops, reducing auxiliary stack space from to where tail-call optimization is unavailable.
VII. Recurrence Analysis — Measuring Recursive Algorithms
A. Recurrence relations to analyse recursive algorithms
A recurrence expresses the cost of a recursive algorithm using the costs of its smaller calls plus nonrecursive work.
- General divide-and-conquer form: where is the number of subproblems, is each subproblem’s size, and is division and combination work.
- Base condition: A recurrence must specify a constant-size case, commonly .
- Substitution method: Guess a bound and prove it by induction after replacing smaller terms with the inductive hypothesis.
- Iteration method: Repeatedly expand the recurrence until reaching its base case.
- For , expansion gives .
- Recursion-tree method: Represent recursive calls by levels and sum the work at each level.
- Merge sort has ; every level costs , and there are levels, giving .
- Master theorem: For , compare with .
- If is polynomially smaller, then .
- If the terms have equal order up to logarithmic factors, the result gains an additional logarithmic factor.
- If is polynomially larger and a regularity condition holds, then .
- Applicability limits: The basic Master theorem does not directly handle unequal subproblem sizes, forms such as , or irregular non-polynomial relationships; substitution or recursion trees may still apply.
Nearly through. Did unit 1 of ECAP538 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 1 of ECAP538 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill; the rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks.