Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 2: Complexity of Concrete Algorithms — Sorting, Searching, Graphs

Lesson 8 of 15 in the free Complexity Theory notes on Siksha Sarovar, written by Rohit Jangra.

4.1 Searching

AlgorithmRequirementTimeSpace
Linear searchnoneO(n)O(1)
Binary searchsorted arrayO(log n)O(1)
Hash table lookuphash functionO(1) expected, O(n) worstO(n)
BST search (balanced)AVL/Red-Black treeO(log n)O(n)

A genuine lower bound: any comparison-based search of a sorted array must distinguish n+1 outcomes; each comparison has ≤ 2 (3) results, so Ω(log n) comparisons are necessary — binary search is optimal. This is the simplest example of complexity theory's signature move: a bound on every possible algorithm in a model, via an information/adversary argument.

4.2 Sorting — and the Ω(n log n) theorem

AlgorithmBestAverageWorstSpaceStable?
Bubble sortO(n)O(n²)O(n²)O(1)yes
Insertion sortO(n)O(n²)O(n²)O(1)yes
Merge sortO(n log n)O(n log n)O(n log n)O(n)yes
QuicksortO(n log n)O(n log n)O(n²)O(log n)no
HeapsortO(n log n)O(n log n)O(n log n)O(1)no
Counting/RadixO(n + k) / O(d(n + k))O(n + k)yes
Decision-tree lower bound (exam favourite — reproduce the proof): a comparison sort on n keys must produce any of n! permutations. Its computation is a binary decision tree; a tree of height h has ≤ 2ʰ leaves, so 2ʰ ≥ n! ⟹ h ≥ log₂(n!) = Θ(n log n) (by Stirling). ∎

Merge sort and heapsort match the lower bound — sorting's comparison complexity is closed: Θ(n log n). Counting/radix sort beat it only by stepping outside the comparison model (they inspect digits) — a perfect illustration that lower bounds are statements about a model.

4.3 Graph algorithms — the polynomial side

For G = (V, E), n = |V|, m = |E|:

ProblemAlgorithmComplexity
Traversal / connectivityBFS, DFSO(n + m)
Shortest path (unweighted)BFSO(n + m)
Shortest path (non-negative weights)Dijkstra + binary heapO((n + m) log n)
Shortest path (negative weights)Bellman–FordO(n·m)
All-pairs shortest pathsFloyd–WarshallO(n³)
Minimum spanning treeKruskal / PrimO(m log n)
Topological sortDFSO(n + m)
Strongly connected componentsKosaraju / TarjanO(n + m)
Maximum bipartite matchingHopcroft–KarpO(m√n)
2-SATimplication graph + SCCO(n + m)

All polynomial — these problems are comfortably inside P.

4.4 The cliff edge — innocent-looking twins

The deepest lesson of this unit: problems that look like siblings sit on opposite sides of the P / NP-complete divide.

In P 😊NP-complete 🔥
Shortest path between two verticesLongest simple path
Euler circuit (use every edge once) — O(m)Hamiltonian cycle (every vertex once)
2-colourability (bipartiteness) — O(n+m)3-colourability
2-SAT — O(n+m)3-SAT
Minimum spanning treeMinimum Steiner tree
Matching (even non-bipartite, Edmonds)3-dimensional matching
Min cut (= max flow)Max cut

Why the asymmetry? Loosely: the easy versions have local certificates of optimality / matroid or convex structure (greedy and augmenting-path arguments work), while the hard twins require globally coordinated choices, where every local fix can break something far away — the combinatorial explosion NP captures.

Practical moral: when you meet a new problem, locate it on this table first. Recognising "this is longest-path in disguise" before coding saves the doomed week — and articulating that is exactly what Unit 3 trains: reductions from known hard problems.

4.5 Solving the recurrences behind the table (show your work)

  • Binary search: T(n) = T(n/2) + c. Unroll: T(n) = c·log₂ n + T(1) → Θ(log n).
  • Merge sort: T(n) = 2T(n/2) + cn. Level k of the recursion tree has 2ᵏ subproblems of size n/2ᵏ, costing cn per level; log₂ n levels → Θ(n log n).
  • Quicksort, worst case: the pivot is the min/max every time → T(n) = T(n−1) + cn → c(n + (n−1) + ... + 1) = Θ(n²). The average-case recurrence solves to Θ(n log n) — quote it, derive it only if explicitly asked.

4.6 The adversary argument — your second lower-bound technique

Claim: finding the maximum of n elements needs at least n−1 comparisons. Adversary view: every element except the announced maximum must lose at least one comparison — an element that never lost could secretly be the maximum (the adversary would assign it a huge value and contradict the algorithm's answer). Each comparison creates at most one new loser, and n−1 losers are required. ∎

The same technique shows max and min together need ⌈3n/2⌉ − 2 comparisons, and merging two sorted n-element lists needs 2n − 1 in the worst case. Together with the decision-tree method (§4.2) you now own both standard lower-bound tools of the comparison world — and lower bounds are what make this subject complexity theory rather than benchmarking.

4.7 Worked classification drill (do these cold before the exam)

Problem as posedClassification and one-line reason
"Is there a path from s to t with ≤ 10 edges?"P — BFS by layers
"Is there a simple path from s to t with ≥ k edges?"NP-complete — longest path in disguise
"Can n exam slots be 2-coloured so conflicting courses differ?"P — bipartiteness check
"...with 3 colours?"NP-complete — 3-COLOURING
"Does a route cross every bridge (edge) exactly once?"P — Euler: check degrees + connectivity
"Does a route visit every city (vertex) exactly once?"NP-complete — HAM-CYCLE
"Pick the maximum number of non-overlapping intervals"P — greedy by finishing time
"Pick k tasks pairwise compatible by an arbitrary table"NP-complete — IND-SET in disguise

The skill being drilled: strip the story, identify the underlying formal problem, then recall its class — never classify by how the story "feels".

Exam pointer

① "Prove the Ω(n log n) sorting lower bound" — decision tree, 2ʰ ≥ n!, Stirling; add that merge/heap sort make it tight. ② "Why doesn't counting/radix sort contradict it?" — different model: digits inspected, not comparisons. ③ "Compare sorting algorithms" — §4.2's table; know which are stable and which are in-place. ④ "Classify these problems with reasons" — §4.7's drill, verbatim format.

Self-check

  1. Solve T(n) = 4T(n/2) + n by the Master theorem. (a = 4, b = 2 → leaves n²; n grows slower → Θ(n²).)
  2. State why a comparison sort's decision tree must have at least n! leaves.
  3. Euler circuit vs Hamiltonian cycle: which is in P, and what is the P-side characterisation?
  4. Why does Dijkstra fail with negative edge weights, and which polynomial algorithm takes over?
  5. Give the one-sentence adversary argument proving max needs n−1 comparisons.