PART 5 • INTELLIGENT SYSTEMS & CAREER • LEVEL 23

Classical AI, Search & Reasoning

Turn goals into state spaces, choose search strategies deliberately, reason through adversarial games and constraints, and represent knowledge that an intelligent system can query and explain.

⏱️ 380–480 min🎯 Beginner → Interview Ready🧪 2 Computational Labs💼 Search & Reasoning Focus
SABCDEFG
SEARCH CONTRACTstate • action • cost • heuristic • goal testf(n) = g(n) + h(n)

By the End of This Level, You Can

01Formulate a real task as states, actions, goals and path costs.
02Compare BFS, DFS, uniform-cost, greedy and A* search.
03Implement A* with a priority queue, parent map and stale-entry control.
04Calculate minimax values and explain alpha–beta pruning.
05Solve constraint problems with ordering and propagation heuristics.
06Separate symbolic entailment from probabilistic belief and causality.

Six Building Blocks of Classical AI

Classical AI makes the decision process explicit: what the system knows, what it may do and why one choice is preferred.

STATEA complete decision snapshot

Contains the information required to choose a valid next action.

ACTIONA permitted transition

Transforms one state into a successor according to the model.

PATH COSTThe price of a sequence

Adds distance, time, risk or another measurable objective.

HEURISTICAn estimate of remaining cost

Guides informed search without replacing the true accumulated cost.

CONSTRAINTA rule valid solutions obey

Prunes assignments that cannot belong to a complete solution.

INFERENCEA justified new conclusion

Derives consequences from facts, rules or probabilistic dependencies.

A Search Problem Begins with a Precise Model

Before selecting an algorithm, define what a state means, which actions are legal and what counts as success.

INITIAL STATEWhere reasoning begins

The current board, city, machine configuration or partial assignment.

ACTIONS & TRANSITIONSHow successors are generated

Each action has a precondition and a deterministic or stochastic result.

GOAL TESTWhat terminates search

A predicate checks whether the current state satisfies the task.

PATH COSTWhat should be minimized

The sum of edge costs may represent distance, latency, money or danger.

DETAILED EXPLANATION

A state must contain enough information to predict valid successors, but no irrelevant history. The state space is the directed graph induced by the transition model; a search tree is the algorithm’s evolving view of that graph. Repeated-state detection matters because different action sequences can reach the same state. The solution is not merely a goal state: it is a sequence of actions whose total cost can be evaluated. A weak formulation creates an enormous graph or accepts invalid plans, so formulation quality often matters more than a clever implementation.

WORKED INTUITION

In route planning, a state can be the current city, an action is a road, the transition reaches its destination, the goal test compares cities and edge cost is travel time.

PLACEMENT CONNECTION

State whether the environment is fully observable, deterministic, static, discrete and single-agent before choosing a method.

COMMON MISCONCEPTION

The search tree and state graph are not identical. One graph state may appear through many tree paths unless duplicates are controlled.

Uninformed Strategies Differ in Expansion Order

BFS, DFS and uniform-cost search use no estimate of distance to the goal, yet their guarantees differ sharply.

BREADTH-FIRSTShallowest first

FIFO queue. Complete with finite branching and optimal only when step costs are equal.

frontier = deque([start])
DEPTH-FIRSTDeepest first

Stack or recursion. Memory-efficient, but can follow an infinite or unhelpful branch.

frontier = [start]
UNIFORM-COSTLowest g(n) first

Priority queue. Complete and optimal when action costs have a positive lower bound.

priority = path_cost
DETAILED EXPLANATION

BFS expands every node at depth d before depth d+1, so its time and space can grow as O(b^d). DFS stores roughly one path plus unexplored siblings, often O(bm), but its answer depends strongly on successor order. Uniform-cost search generalizes BFS to unequal non-negative edge costs and may revisit a state when a cheaper path is discovered. A visited set should be applied at the correct time: for optimal cost search, maintain the best known cost and ignore stale queue entries rather than permanently closing a state too early.

SELECTION RULE

Use BFS for minimum moves with equal costs, UCS for minimum total cost, and DFS for bounded exploration where memory is tight and optimality is unnecessary.

COMPLEXITY LENS

Report branching factor b, shallowest solution depth d, maximum depth m and whether costs can be zero or negative.

COMMON MISCONCEPTION

Fewest edges is not necessarily cheapest. BFS can return an expensive two-edge route while UCS finds a cheaper three-edge route.

Heuristics Trade Search Effort for Informed Direction

Greedy best-first uses the estimated distance alone; A* balances distance already paid with distance believed to remain.

GREEDYf(n) = h(n)

Often reaches a goal quickly, but can choose a cheap-looking route with a high true cost.

A*f(n) = g(n) + h(n)

Balances accumulated evidence and a future estimate.

ADMISSIBLEh(n) ≤ h*(n)

Never overestimates the true optimal remaining cost.

CONSISTENTh(n) ≤ c(n,n′)+h(n′)

Satisfies a triangle inequality so f-values do not decrease along a path.

DETAILED EXPLANATION

A heuristic injects domain knowledge while preserving a general search procedure. With non-negative costs and an admissible heuristic, tree-search A* is optimal; graph-search A* is simplest when the heuristic is consistent, though reopening states also supports broader cases. A zero heuristic reduces A* to UCS. A more accurate admissible heuristic usually expands fewer nodes, but computing it may be expensive. Heuristics often come from relaxed problems—for example, Manhattan distance ignores obstacles while retaining the minimum grid movement required.

DOMINANCE

If two admissible heuristics satisfy h₂(n) ≥ h₁(n) everywhere, h₂ is more informed and A* generally expands no more nodes under comparable conditions.

PLACEMENT CONNECTION

Explain admissibility, consistency, reopen logic and the termination condition; do not only quote f=g+h.

COMMON MISCONCEPTION

An admissible heuristic estimates remaining cost; it does not need to be close, and a larger arbitrary value can destroy optimality.

A Correct A* Implementation Manages Competing Paths

The priority queue may hold multiple entries for one state, so best costs and parent pointers must be authoritative.

1Push start

Store f=h(start), g=0 and no parent.

2Pop minimum

Remove the smallest priority entry.

3Skip stale

Ignore entries whose g exceeds best_g[state].

4Relax edges

Update a successor only when tentative g is lower.

5Reconstruct

Follow parents from goal back to start.

DETAILED EXPLANATION

Priority queues commonly lack an efficient decrease-key operation, so implementations push a new entry whenever a lower cost is found. Old entries remain but become stale. The best_g map prevents them from corrupting the result. Parent pointers must change together with best_g so reconstruction follows the cheapest discovered predecessor. With a consistent heuristic, the first time the goal is popped its path is optimal. Stopping when the goal is merely generated is unsafe because another frontier node may still lead to a cheaper goal path.

INVARIANT

best_g[s] is the lowest discovered cost to state s; every accepted relaxation strictly improves it.

ENGINEERING CHECK

Add a deterministic tie-break counter when states are not comparable or reproducible expansion order matters.

COMMON BUG

Marking a node permanently visited when first inserted can discard a later, cheaper path.

Adversarial Search Reasons About an Opponent

Minimax selects an action whose worst rational response is as strong as possible; alpha–beta proves that some branches cannot change that choice.

MAX NODEChoose the largest child value

Represents the decision maker whose utility is being optimized.

MIN NODEChoose the smallest child value

Represents an opponent assumed to minimize MAX’s outcome.

α BOUNDBest value MAX can force

Travels down the current path as a lower bound.

β BOUNDBest value MIN can force

Acts as an upper bound; prune when α ≥ β.

DETAILED EXPLANATION

Terminal utilities describe outcomes from MAX’s perspective. Minimax backs leaf values upward by alternating maximum and minimum. For large games, depth-limited search applies an evaluation function at the cutoff, so better evaluation and deeper search improve decisions. Alpha–beta returns the same move as full minimax while avoiding branches already proven irrelevant. Good move ordering tightens α and β early and can approach O(b^(d/2)) effective work in ideal cases, compared with O(b^d) minimax.

WORKED INTUITION

If MAX already has an option worth 6 and a MIN branch reveals a child worth 4, MIN can force at most 4 there; the rest of that branch cannot attract MAX.

PLACEMENT CONNECTION

State whose perspective the evaluation uses, whether chance nodes exist and how the cutoff evaluator is validated.

COMMON MISCONCEPTION

Alpha–beta does not approximate minimax. With the same depth and evaluator, it returns the identical result using fewer evaluations.

Constraint Satisfaction Searches Assignments, Not Paths

A CSP separates variables, domains and constraints, enabling heuristics that exploit structure before exploring every combination.

MRVMost constrained variable first

Choose the unassigned variable with the fewest legal values to fail early.

DEGREEBreak ties by influence

Prefer the variable constraining the largest number of unassigned neighbours.

LCVLeast constraining value first

Try the value that removes the fewest options from neighbours.

PROPAGATIONDetect consequences immediately

Forward checking or arc consistency removes unsupported values before recursion.

DETAILED EXPLANATION

Backtracking assigns one variable at a time and reverses a choice after detecting inconsistency. MRV reduces wasted branching by exposing a likely contradiction early. Forward checking updates neighbouring domains after each assignment; AC-3 repeatedly enforces that every value has support across each binary constraint. Constraint graphs reveal independent components and tree structure that can be solved more efficiently. These techniques preserve completeness because they remove only values that cannot participate in a consistent extension.

EXAMPLE

In exam scheduling, variables are exams, domains are time slots and constraints forbid clashes for shared students or rooms.

DESIGN QUESTION

Ask whether a global constraint such as AllDifferent is more expressive and efficient than many pairwise inequalities.

COMMON MISCONCEPTION

Greedily picking the first legal value is not backtracking unless the system can undo the assignment and explore alternatives.

Knowledge Representation Makes Assumptions Queryable

Facts, predicates and rules support explainable deductions when their semantics and limits are explicit.

PROPOSITIONAL LOGICWhole statements are true or false

Useful for finite Boolean relationships and SAT-style reasoning.

FIRST-ORDER LOGICObjects, relations and quantifiers

Represents reusable claims such as every enrolled student has an identifier.

FORWARD CHAININGFacts drive conclusions

Repeatedly fire rules whose premises are known until no new fact appears.

BACKWARD CHAININGA query drives subgoals

Start from the desired conclusion and search for rules that could prove it.

DETAILED EXPLANATION

A knowledge base entails a statement when that statement is true in every model satisfying the knowledge base. Sound inference derives only entailed conclusions; complete inference can derive every entailed conclusion in the chosen language. Horn clauses permit efficient chaining and underlie many rule systems. Real systems must also define whether an absent fact means false, unknown or inaccessible. Provenance—recording which facts and rules supported a result—turns symbolic reasoning into an inspectable decision trail.

EXAMPLE RULE

eligible(x) ∧ registered(x) → may_interview(x). A backward query asks which premises must be proved for a particular student.

AI CONNECTION

Knowledge graphs, policy engines and neuro-symbolic systems still rely on careful entity identity, relation semantics and provenance.

COMMON MISCONCEPTION

Failure to prove a claim does not automatically prove its negation unless the system explicitly adopts a closed-world assumption.

Bayesian Networks Reason Under Uncertainty

A directed acyclic graph encodes conditional dependencies and factorizes a joint probability distribution.

STRUCTUREX₁ → X₂ → X₃

Edges encode direct conditional dependence assumptions, not automatically causal truth.

FACTORIZATIONP(x₁,…,xₙ)=∏P(xᵢ|paᵢ)

Each variable depends directly on its parents rather than the entire history.

INFERENCEP(H|E) ∝ P(E|H)P(H)

Evidence updates belief through exact elimination or approximate sampling.

DETAILED EXPLANATION

Conditional independence makes a large joint distribution compact. Variable elimination multiplies relevant factors and sums out hidden variables, but its cost depends on graph structure and elimination order. Sampling methods approximate posterior probabilities when exact inference is too expensive. Explaining-away occurs when two independent causes become dependent after observing their common effect. A Bayesian network can express observational dependence; causal interpretation needs additional assumptions about how the graph was generated and how interventions differ from observations.

WORKED INTUITION

If rain and a sprinkler can both cause wet grass, seeing wet grass raises belief in both. Learning that it rained can reduce the need to explain the grass using the sprinkler.

PLACEMENT CONNECTION

Distinguish marginal, conditional and posterior probability, then identify which variables are observed, queried and marginalized.

COMMON MISCONCEPTION

A directed edge is not proof of causation; observational data can support several equivalent dependency structures.

PREMIUM COMPUTATIONAL VISUALIZER

🧭 Graph Search & A* Laboratory

Run BFS, uniform-cost, greedy or A* on the same graph. Inspect every expansion, frontier priority, best-known cost and reconstructed path.

CodeBhavya • Expand, Relax, Reconstruct

Changing a control rebuilds the event trace. The graph uses positive edge costs and deterministic tie-breaking.

PHASEReady
EXPANDED0
FRONTIER1
PATH COST
CURRENTStart
ADVERSARIAL REASONING VISUALIZER

♟️ Minimax & Alpha–Beta Laboratory

Evaluate a real game tree event by event. Compare complete minimax with alpha–beta, change move order and see exactly which branches become irrelevant.

CodeBhavya • Evaluate, Bound, Prune

The same terminal utilities are preserved. Ordering changes work performed, not the minimax decision.

PHASEReady
LEAVES READ0
PRUNED0
ROOT VALUE
BEST MOVE
PROGRAM TRACING • TRUE LOOP EXECUTION

Trace A* Search from First Principles

Follow every heap pop, stale-entry test, neighbour relaxation, priority push and parent lookup. The cursor returns through the while and for loops exactly as Python executes.

💻 Classical AI & Search Challenges

Attempt each program independently. Workspaces, hints and model programs remain collapsed initially.

0 / 5Solved independently0 / 500Best score

Test Your Search & Reasoning

Select one answer per question. Results show your choice, the correct answer and a clear explanation.

Not checked yet

Diagnose Search Systems Like an AI Engineer

State the guarantee you need, verify assumptions and inspect intermediate decisions before replacing the algorithm.

ROUTE TOO EXPENSIVE?

Check whether BFS was used with unequal costs, whether relaxations reopen states and whether termination happened on generation.

A* EXPANDS TOO MUCH?

Measure heuristic accuracy and computation cost; compare against the zero-heuristic UCS baseline.

GAME MOVE IS WEAK?

Check utility perspective, cutoff depth, evaluation calibration, horizon effects and move ordering.

CSP THRASHES?

Add MRV, propagation and structure-aware constraints; inspect which assignment first empties a domain.

RULE ANSWER MISSING?

Separate absent facts, unmatched entities, rule direction, inference incompleteness and access restrictions.

POSTERIOR SURPRISING?

Verify conditional tables, observed evidence, normalization, independence assumptions and causal interpretation.

CodeBhavya interview pattern:Formulate states and goals → Specify costs and guarantees → Select expansion rule → State assumptions → Trace frontier or recursion → Control repeated work → Prove stopping condition → Reconstruct or explain result → Measure time and space → Describe failure modes and alternatives.

🎤 Classical AI, Search & Reasoning — Interview Questions

Answer aloud before selecting Show Answer for each detailed explanation.

Intelligence Begins with an Explicit Decision Model

1Formulate

Define state, action, goal and cost.

2Prioritize

Choose the next state or move deliberately.

3Update

Record better paths, bounds or domains.

4Terminate

Stop only when the guarantee is satisfied.

5Explain

Return the path, proof or decision trail.

A strong AI solution is not only a final answer—it is a justified sequence of choices whose assumptions, costs and stopping rule can be inspected.

Eight Practical Search & Reasoning Habits

01

Write the state representation and goal predicate before writing the frontier loop.

02

Use BFS only when step costs are equal and minimum depth is the objective.

03

Maintain best costs and skip stale priority-queue entries.

04

Test A* with h=0 to obtain a uniform-cost correctness baseline.

05

Reconstruct paths using parents instead of copying complete paths into every queue item.

06

Order promising game moves first to expose alpha–beta pruning.

07

Use MRV and propagation to reveal CSP contradictions before deep recursion.

08

Record provenance for every rule-based or probabilistic conclusion.

Strengthen Search & Reasoning

Calculate intermediate values and defend every expansion, pruning and inference decision.

  1. 01

    Formulate the 8-puzzle as a search problem.

  2. 02

    Trace BFS and DFS on the same cyclic graph.

  3. 03

    Explain why UCS handles unequal edge costs.

  4. 04

    Calculate A* priorities for five frontier states.

  5. 05

    Design an admissible heuristic for grid routing.

  6. 06

    Construct an inconsistent but admissible heuristic.

  7. 07

    Explain stale heap entries and reopen logic.

  8. 08

    Back up a three-ply minimax tree by hand.

  9. 09

    Mark alpha–beta cutoffs for two move orders.

  10. 10

    Model exam scheduling as a CSP.

  11. 11

    Apply MRV, LCV and forward checking to map coloring.

  12. 12

    Compare forward and backward chaining.

  13. 13

    Identify an explaining-away pattern.

  14. 14

    Separate probabilistic association from intervention.