LOADING
1322 words
7 minutes
My Notes from CS50 AI
2026-04-06

Personal Notes of the CS50 AI course

Search

Agent: entity perceives its environment and acts upon that environment

State: config of the agent and its environment

Actions: choices that can be made in a state

ACTIONS(s) returns the set of actions that can be executed in state s

transition model: description of what state results from performing any applicable action in any state

RESULT(s, a) returns the state resulting from performing action a in state s

state space: the set of all states reachable from the inital state by any sequence of actions (sometimes we use a Graph to represent the state space)

goal test: way to determine whether a given state is a goal state

path cost: numerical cost associated with a given path

Search Problems: init state, actions, transition model, goal test, path cost function

Solution: sequence of actions that leads to the goal state

Optimal Solution: the solution with the lowest path cost

Node: data structure that keeps track of: a state, a parent, an action, a path cost (from init to node).

Search ALGOS: (most examples are in traversing a maze)

  • Frontier:
    • Depth-First Search (uses a stack) DFS
    • Breadth-First Search (uses a queue) BFS

Informed search: search startegy that uses problem-specific knowledge to find solutions more efficiently

Greedy Best-First search: search algo that expands the node is closest to the goal, esitmated by a heuristic function h(n)

A* Search: search algo that expands the node with lowest value of g(n) + h(n)

g(n) = cost to reach node

h(n) = estimated cost to goal

this algo is the optimal under these conditions:

  • h(n) is admissible (never overestimates the true cost) and
  • h(n) is consistent (for every node n and successor n’ with step cost c, h(n) <= h(n’) + c)

like playing a game against opponent (tic tac toe, )

Minimax:

  • MAX: (X) aims to maximize score
  • MIN: (O) aims to minimize score

S0: init state

Player/s: returns which player to move in state s

ACTIONS(s) returns the set of actions that can be executed in state s

RESULT(s, a) returns the state resulting from performing action a in state s

TERMINAL(s): checks if state s is a terminal state (game over)

UTILITY(s): final numerical value for the terminal state s (score of the state)

The algo process: Given a state s:

  • MAX picks action a in ACTIONS(s) that produces the highest value of MIN-VALUE(RESULT(s, a))
  • MIN picks action a in ACTIONS(s) that produces the lowest value of MAX-VALUE(RESULT(s, a))
function MAX-VALUE(state):
if TERMINAL(state):
return UTILITY(state)
v = -∞
for action in ACTIONS(state):
v = MAX(v, MIN-VALUE(RESULT(state, action)))
return v
function MIN-VALUE(state):
if TERMINAL(state):
return UTILITY(state)
v = ∞
for action in ACTIONS(state):
v = MIN(v, MAX-VALUE(RESULT(state, action)))
return v

Alpha-Beta Pruning: I can ignore the rest of the nodes of already there is a choice for the other player that is less than we have already to optimize the minimax algo

Depth-Limited Minimax: after certain number of moves I won’t consider extra ones, this algo is important for complex games like Chess that got billions of possibilites that cannot be calculated in a reasonable time

evaluation function: function that estimates the expected utility of the game from a given state (the better this function is in estimating, the more intelligent the AI is)

Knowledge

knowledge-based agents: agents that reason by operating on internal representations of knowledge

Sentence: an assertion about the world in a knowledge representation language

Propositional Logic, Proposition Symbols: P & Q, Truth Tables are the essential way to represent logic and deductions for AI

Model: assignment of truth value to every propositional symbol (a “possible world”)

Knowledge base: a set of sentences known by a knowledge-based agent

Entailment: αβ\alpha \vdash \beta in every model in which sentence α\alpha is true, sentence β\beta is also true

Inference: the process of deriving new sentences from old ones

KB is knowledge base Inference Algo: does KBαKB \vdash \alpha ?

Model Checking:

  • To Determine if KBαKB \vdash \alpha:
    • Enumerate all possible models.
    • if in every model where KBKB is true, α\alpha is true, then KBKB entails α\alpha

Inference rules:

Modus Ponens, De Morgan’s Law, And elimination, implication elimination, Double Negation elimination, bi-conditional elimination, Distributive Property

Theorem Proving

works a bit like search problems

State: config of the agent and its knowledge base

Actions: inference rules

ACTIONS(s) returns the set of actions that can be executed in state s

transition model: new knowledge base after inference

RESULT(s, a) returns the state resulting from performing action a in state s

state space: the set of all states reachable from the inital state by any sequence of actions (sometimes we use a Graph to represent the state space)

goal test: way to determine whether a given state is a goal state (check statement we are trying to prove)

path cost: number of steps in proof

Theorem Proving: init state, actions, transition model, goal test, path cost function

Conversion To CNF (Conjuctive Normal Form)

disjunction: literals connected with \lor conjunction: literals connected with \land

clause: a disjunction of literals

Conjuctive Normal Form: logical sentence that is a conjuction of clauses e.g. (ABC)(D¬E)(FG)(A \lor B \lor C) \land (D \lor \neg E) \land (F \lor G)

Steps to Convert to CNF

  • Eliminate biconditonals
    • turn (αβ)(\alpha \leftrightarrow \beta) into (αβ)(βα)(\alpha \rightarrow \beta) \land (\beta \rightarrow \alpha)
  • Eliminate implications
    • turn (αβ)(\alpha \rightarrow \beta) into ¬αβ\neg \alpha \lor \beta
  • Move ¬\neg inwards using De Morgan’s Laws
    • e.g. turn ¬(αβ)\neg(\alpha \land \beta) into ¬α¬β\neg \alpha \lor \neg \beta
  • Use distributive law to distribute \lor wherever possible

Inference by Resolution

  • To Determine if KBαKB \vdash \alpha:

    • Check if (KB¬αKB \land \neg \alpha) is a contradiction
      • if so, then KBαKB \vdash \alpha
      • Otherwise, no entailment
  • To Determine if KBαKB \vdash \alpha:

    • Convert (KB¬αKB \land \neg \alpha) to CNF (Conjuctive Normal Form)
    • Keep checking to see if we can use resolution to produce a new clause
      • if ever we produce the empty clause (contradiction in computer represented as false), then we have a contradiction, and KBαKB \vdash \alpha
      • Otherwise, if we can’t add new clauses, no entailment.

First-Order Logic

Constant Symbols (Names, Objects, etc), Predicate Symbols (Classes) (Person, House, BelongsTo)

Universal Quantification: x\forall x Existential Quantification: x\exists x and so on of Quantifications

Probability

Possible World: ω\omega Probability of a possible world: P(ω)P(\omega)

1 = Certain 0 = Impossible

0P(ω)10 \leq P(\omega) \leq 1

The sum of all possible worlds ω\omega in the set of all worlds Ω\Omega is 1 ωΩP(ω)=1\sum_{\omega \in \Omega} P(\omega) = 1

unconditional probability

degree of belief in a proposition in the absence of any other evidence.

conditional probability

degree of belief in a proposition given some evidence that has already been revealed

the probability of “a” given “b” P(ab)P(a|b)

to know the probability of a given b, we take the probability of both and divide it by the probability of the second (b) to get rid of its own probability that happens when (a) doesn’t

P(ab)=P(ab)P(b)P(a|b) = \frac{P(a \land b)}{P(b)}

random variable

a variable in probability theory with a domain of possible values it can take on can be encoded as an array to distribute the probabilities

Probability Distribution

P(Flight=ontime)=0.6P(Flight = on time) = 0.6

P(Flight=delayed)=0.3P(Flight = delayed) = 0.3

P(Flight=cancelled)=0.1P(Flight = cancelled) = 0.1

or as a vector/array

P(Flight)=<0.6,0.3,0.1>P(Flight) = <0.6, 0.3, 0.1>

independence

the knowledge that one event occurs doesn’t affect the probability of the other event

then P(ba)=P(b)P(b|a) = P(b) because (b) is independent of (a) anyways

P(ab)=P(a)P(ba)=P(a)P(b)P(a \land b) = P(a)P(b|a) = P(a)P(b)

dependence

the knowledge that one event occurs affects the probability of the other event

Bayes’ Rule

Note

\because P(ab)=P(b)P(ab)P(a \land b) = P(b)P(a|b)

P(ab)=P(a)P(ba)P(a \land b) = P(a)P(b|a)

\therefore P(b)P(ab)=P(a)P(ba)P(b)P(a|b) = P(a)P(b|a)

\therefore

P(ba)=P(b)P(ab)P(a)P(b|a) = \frac{P(b) P(a | b)}{P(a)}

knowing P(a | b) we can calculate P(b | a)

knowing P(visible effect | unknown cause) we can calculate P(unknown cause | visible effect)

knowing P(medical test result | disease) we can calculate P(disease | medical test result)

Joint Probability

P(ab)=P(ab)P(b)=αP(ab)P(a|b) = \frac{P(a \land b)}{P(b)} = \alpha P(a \land b)
α=1P(b)\alpha = \frac{1}{P(b)}

the conditional probability of (a) given (b) is proportional to a factor α\alpha multiplied by the joint probability of (a) and (b) P(ab)P(a \land b)

Probability Rules

Negation

P(¬a)=1P(a)P(\neg a) = 1 - P(a)

Inclusion-Exclusion

P(ab)=P(a)+P(b)P(ab)P(a \lor b) = P(a) + P(b) - P(a \land b)

Marginalization

P(a)=P(ab)+P(a¬b)P(a) = P(a \land b) + P(a \land \neg b)
P(X=xi)=jP(X=xiY=yj)P( X = x_{i}) = \sum_{j} P(X = x_{i} \land Y = y_{j})

Conditioning

P(a)=P(ab)P(b)+P(a¬b)P(¬b)P(a) = P(a|b)P(b) + P(a| \neg b)P(\neg b)
P(X=xi)=jP(X=xiY=yj)P(Y=yj)P( X = x_{i}) = \sum_{j} P(X = x_{i} | Y = y_{j})P( Y = y_{j})

Probability Models

Bayesian Network

data structure that represents the dependencies among random variables.

  • Directed Graph
  • Each node represents a random variable
  • arrow from X to Y means X is a parent of Y
  • each node X has probability distribution P(XParents(X))P(X | Parents(X))

Some information may be outdated