Skip to content

Design & Analysis of Algorithms (DAA)

DSSSB TGT CS — Section B P2 (Rank ~11). Focus: notations, paradigm ideas, complexity table, graphs.


1. Algorithm Properties

An algorithm should be:

PropertyMeaning
InputZero or more inputs
OutputAt least one output
DefinitenessClear, unambiguous steps
FinitenessTerminates after finite steps
EffectivenessSteps doable / basic enough

Also: correctness, efficiency (time/space).


2. Asymptotic Notations

NotationMeaning
O (Big-O)Upper bound (grows no faster than)
Ω (Omega)Lower bound
Θ (Theta)Tight bound (both upper & lower)
  • Ignore constants and lower-order terms: (3n^2+2n+5 = O(n^2)).
  • Trap: (O) is not always “worst case” — it is an upper bound; people often use it for worst-case runtime informally.

3. Best / Average / Worst Case

CaseMeaning
BestMinimum time over inputs of size n
AverageExpected time over input distribution
WorstMaximum time — common exam focus

Example: Linear search — best O(1), worst O(n).


4. Algorithm Design Paradigms

ParadigmIdeaExamples
Divide & ConquerSplit → solve → combineMerge sort, binary search, quicksort
GreedyLocally optimal choice nowKruskal, Prim, activity selection, Dijkstra*
Dynamic Programming (DP)Optimal substructure + overlapping subproblems; store resultsFibonacci DP, 0/1 knapsack, LCS

*Dijkstra is greedy-style shortest path (non-negative weights).

  • Backtracking / branch-and-bound — awareness for exams.

5. Sorting Complexities (must memorize)

AlgorithmBestAverageWorstSpace (extra)Stable?
BubbleO(n)O(n²)O(n²)O(1)Yes
SelectionO(n²)O(n²)O(n²)O(1)No
InsertionO(n)O(n²)O(n²)O(1)Yes
MergeO(n log n)O(n log n)O(n log n)O(n)Yes
QuickO(n log n)O(n log n)O(n²)O(log n) avgNo*
HeapO(n log n)O(n log n)O(n log n)O(1)No
CountingO(n+k)O(n+k)O(n+k)O(k)Yes

*Typical in-place quicksort not stable.

  • Comparison sort lower bound: Ω(n log n) worst-case for general comparison sorts.

6. Searching

MethodIdeaTime
LinearScan sequentiallyO(n)
BinarySorted array; halve search spaceO(log n)
  • Binary search requires sorted data.

7. Graph Traversals

BFSDFS
StructureQueueStack / recursion
ExploresLevel by levelDeep path first
UseShortest path in unweighted graphCycle detect, topo sort, connectivity

8. Shortest Path & MST

AlgorithmIdea
DijkstraGreedy; grow set of finalized distances; non-negative weights
Bellman-FordHandles negative edges; detect negative cycles (awareness)
Kruskal MSTSort edges; add if no cycle (Union-Find)
Prim MSTGrow tree from a start vertex by cheapest edge out
  • MST = Minimum Spanning Tree — connect all vertices, min total weight, no cycle.
  • Trap: Dijkstra fails with negative edge weights.

9. NP-Hard (light)

| Class (informal) | Idea | | --- | --- | --- | | P | Solvable in polynomial time | | NP | Verifiable in polynomial time (exam-level slogan) | | NP-complete / NP-hard | “Hard” problems; no known poly-time algorithm |

  • Exam: TSP, some scheduling — often cited as hard; don’t over-formalize.

10. Space–Time Tradeoff

  • Use more memory to save time (e.g. hashing, DP tables, indexes).
  • Or compress/recompute to save space at cost of time.
  • Classic: lookup tables vs recalculation.

Quick Revision Traps

  1. Θ = tight; O = upper; Ω = lower.
  2. Quicksort worst O(n²); merge always O(n log n).
  3. Binary search needs sorted input.
  4. BFS → queue; DFS → stack.
  5. Dijkstra → non-negative weights.
  6. Kruskal sorts edges; Prim grows from a vertex.
  7. Greedy ≠ always optimal for every problem; DP when overlapping subproblems.