This repository contains the code and problem sets from the Harvard CS50 Artificial Intelligence course (CS50AI). Each folder is a self-contained project implementing foundational AI concepts and algorithms in Python, ranging from logic and inference to deep learning and NLP.
Below is a guide to each project: what it does, what topics it covers, and how you can run or use the code yourself.
- Crossword (Constraint Satisfaction)
- Minesweeper (Logical Inference)
- Degrees (Graph Search)
- Tic-Tac-Toe (Minimax)
- Heredity (Bayesian Networks)
- PageRank (Markov Chains)
- Shopping (Machine Learning)
- Nim (Reinforcement Learning)
- Parser (Context-Free Grammar)
- Traffic (Deep Learning and CNNs)
- Attention (Neural Machine Translation)
- Concepts: Constraint Satisfaction Problem (CSP), AC-3 arc consistency, backtracking search, domain heuristics.
- Description: Generates and solves crossword puzzles by modeling empty slots as variables and filling in words under length, overlap, and uniqueness constraints. Implements variable selection and ordering heuristics for efficient solving.
- Run:
You provide a board structure and word list, and the solver fills the crossword, optionally saving the solution as an image if [outputfile] is provided.
python crossword/generate.py [structure.txt] [words.txt] [outputfile]
- Concepts: Knowledge bases, propositional inference.
- Description: An AI agent maintains a set of logical sentences about the state of the game, inferring which squares are safe or contain mines based on known information and deductive logic.
- Run:
- Text version and Pygame version included.
- For GUI, run:
python minesweeper/runner.py
- Concepts: Breadth-First Search (BFS), shortest paths in graphs.
- Description: Models connections between actors in films as a graph, then computes degrees of separation and shortest connection paths between actors.
- Run:
On startup, it loads the data and asks for two actors' names, outputting the sequence of starring roles that link them.
python degrees/degrees.py [directory]
- Concepts: Minimax algorithm, adversarial search, game AI.
- Description: AI plays Tic-Tac-Toe optimally by building a game tree and recursively choosing moves that maximize the chance of winning or minimize loss/draw.
- Run: Run the main Python file in the tictactoe folder (
python tictactoe/tictactoe.py). The AI will play against a human or itself.
- Concepts: Conditional/joint probability, Bayesian inference, genetic inheritance, mutation probabilities.
- Description: Models family genetic data using Bayesian networks and computes probabilities of having a gene or trait via recursive enumeration of all possible scenarios.
- Run:
Provide a CSV with family data; outputs probabilities for each person.
python heredity/heredity.py [data.csv]
- Concepts: Markov chains, stochastic processes, iterative convergence.
- Description: Computes PageRank scores for a set of webpages using both sampling (random walks) and iterative update methods. Demonstrates the mathematical process underlying Google's original ranking algorithm.
- Run:
Point to a folder of HTML files; outputs estimated PageRank for each file.
python pagerank/pagerank.py [corpus_folder]
- Concepts: K-Nearest Neighbors (KNN) classifier, train/test split, feature engineering, model evaluation (sensitivity and specificity).
- Description: Uses session data from an e-commerce site to predict if a visitor will generate revenue, with the data loaded from CSV and split for training/test.
- Run:
Outputs classification results and performance metrics.
python shopping/shopping.py [data.csv]
- Concepts: Q-learning, temporal-difference updates, epsilon-greedy, self-play agent training.
- Description: Implements an agent that learns optimal strategies for the game of Nim through repeated self-play using Q-learning, then allows you to play against the trained AI.
- Run:
The code first trains the AI for 10,000 games, then lets you play as human.
python nim/play.py
- Concepts: NLTK grammar parsing, context-free grammars (CFG), parse trees, phrase chunking.
- Description: Uses NLTK to parse sentences according to a custom CFG, printing the parse tree(s) and extracting all noun phrases in the sentence.
- Run:
If no file is given, type a sentence at the prompt.
python parser/parser.py [sentence or input file]
- Concepts: Convolutional Neural Networks (CNN), TensorFlow, Keras, multiclass image classification.
- Description: Trains various CNN models to recognize traffic signs from images, documents model architectures and their accuracy in
traffic/README.md. - Run:
python traffic/traffic.py [data_directory] [model_output_file]
- Place images in category-labeled folders, and optionally save the trained network.
- Example model summaries and experimental details are in the README for model performance, design choices, and layer specifics.
- Concepts: Transformers, masked language models (BERT), attention visualization.
- Description: Implements a BERT-based masked language model using TensorFlow and Huggingface Transformers. Visualizes attention weights over tokens, showing which words the model focuses on to predict missing words.
- Run:
python attention/mask.py
- Enter text with the
[MASK]token to see predictions and attention diagrams. - Requires Huggingface Transformers, TensorFlow, and PIL for visualizing attention heads.
- Enter text with the
- Most projects assume Python 3.x and use pip packages such as scikit-learn (
shopping), TensorFlow/Keras (traffic,attention), NLTK (parser), and pygame (minesweeperrunner GUI). - You may need to install project-specific packages:
or install the relevant library for each folder.
pip install -r requirements.txt
This repo provides educational implementations of the official CS50AI assignments. Code and design inspired by Harvard's CS50AI course (cs50.harvard.edu/ai), provided here for study and extension.
Open any project folder for the main Python files, supporting data, and code comments. Many projects come with their sample datasets or structures for ease of experimentation.
Feel free to use, experiment, and learn AI from real, working code!