📑 Contents

Chapter 18: Artificial Intelligence (AI)

9618 Computer Science

Input Hidden Output AI 🧠
📚 Learning Objectives
📋 Prior Knowledge Required
🌟 Did You Know?

Artificial Intelligence is defined as the capability of a computer or a robot controlled by a computer to perform tasks that typically require human intelligence. These tasks include reasoning, discovering meaning, generalizing, or learning from past experiences!

1. AI & Graphs

In Artificial Intelligence, graphs are fundamental data structures used to model relationships, networks, and decision-making problems. A graph is a set of vertices/nodes connected by edges/arcs.

1.1 What Can Graphs Represent in AI?

📖 Graph Applications in AI
Undirected Directed 5 3 7 Weighted

1.2 Types of Graphs

Graph Type Use in AI
Directed Graph Models one-way relationships, such as state transitions in search algorithms (e.g. A*)
Undirected Graph Useful for bidirectional relationships, such as undirected social connections
Weighted Graph Represents costs or heuristics, commonly used in shortest path algorithms like Dijkstra's or A*

1.3 Graph Representation

Graphs can be represented using two main methods:

📝 Graph Representation Methods
💡 Exam Tip

Remember: Adjacency Matrix = O(n²) space but O(1) edge lookup. Adjacency List = O(V+E) space, better for sparse graphs!

2. Undirected, Unweighted Graphs

An undirected, unweighted graph is a type of graph where edges have no direction (connections go both ways) and all edges are equal (there are no weights or costs).

📖 Key Characteristics
A B C A B C A B C 0 1 1 1 0 1 1 1 0 Symmetric Matrix!

2.1 AI Relevance

Undirected, unweighted graphs are useful in AI for:

Use Case Description
Exploring environments Mapping rooms in a building with equal-cost connections
Clustering in ML Presence of a connection implies similarity
Social network analysis Mutual connections (friendships, links) are undirected
Graph traversal Depth-first or breadth-first search to explore all reachable nodes
Example: Each node represents a town (e.g., Bolton, Dunkirk, Teesside). A 1 in the matrix shows a connection exists between two towns. A 0 means no direct connection. The matrix is symmetric, confirming that the graph is undirected.

3. Directed Graphs & Weighted Graphs

3.1 Directed Graph (Digraph)

A directed graph is one where edges have a direction. This means connections go from one node to another. Connections cannot be traversed in reverse unless another directed edge exists.

⚠️ Key Points

3.2 Directed Graph AI Applications

Use Case How the Graph is Used
Search algorithms Used in A*, BFS, DFS where state transitions are directional
Expert systems/rule engines Representing dependencies between logical conditions or actions
Planning and scheduling Modelling prerequisites and directed workflows
Navigation Handling one-way streets or directed transport systems

3.3 Undirected, Weighted Graphs

An undirected, weighted graph is a graph where edges go both ways (no direction) but each edge has a weight, which can represent cost, distance, time, or risk.

📖 Weight Representation

3.4 Directed, Weighted Graphs

A directed, weighted graph combines both direction and weights. Edges have a direction (e.g., A → B but not B → A) and each edge has a weight such as distance, cost, time, or probability.

A B C D E 5 3 4 2 6 Weights = distance/cost Arrows = direction
Use Case Purpose in AI
A* and Dijkstra algorithms Choosing the shortest path where direction and cost matter
Route planning in robotics Modelling direction-sensitive travel (e.g., traffic rules)
Reinforcement learning Modelling rewards/costs for state transitions in decision environments
Game AI Movement systems with terrain costs or restrictions

4. Dijkstra's Shortest Path Algorithm

In Computer Science, an optimisation problem involves finding the most efficient solution to a given problem. This could mean minimising cost, time, or resource usage, or maximising output, efficiency, or value.

4.1 What is Dijkstra's Algorithm?

Dijkstra's shortest path algorithm is a classic optimisation algorithm that calculates the shortest path from a starting node to all other nodes in a weighted graph.

📖 How Dijkstra's Works

4.2 Algorithm Steps

📝 Dijkstra's Algorithm Procedure
  1. Set the start node's path weight to 0 and all other path weights to infinity
  2. Mark all nodes as unvisited
  3. Visit the node with the lowest path weight
  4. For each neighbour, calculate the new path weight through current node
  5. If new weight is less than current weight, update it
  6. Mark current node as visited
  7. Repeat steps 3-6 until all nodes visited or goal reached
  8. Backtrack from goal to find the shortest path
1. Set start=0 2. Mark unvisited 3. Visit lowest 4. Update neighbours A 0 B C D G 2 5 1 2 3 Start node (A) has weight 0, others have ∞
Real-World Example: In a city road map (graph where intersections are nodes), Dijkstra's algorithm helps to find the shortest path from home (source node) to a destination like a coffee shop.

4. Dijkstra's Algorithm (Continued)

4.3 Pseudocode

FUNCTION Dijkstra(graph, start, goal) // Initialise distances FOR EACH node FROM graph SET distance[node] TO infinity NEXT node SET distance[start] TO 0 DECLARE previousNode AS DICTIONARY DECLARE visited AS LIST DECLARE unvisited AS LIST FOR EACH node FROM graph ADD node TO unvisited NEXT node WHILE LENGTH(unvisited) > 0 // Find unvisited node with shortest distance SET min TO null FOR EACH node FROM unvisited IF min = null THEN SET min TO node ELSEIF distance[node] < distance[min] THEN SET min TO node ENDIF NEXT node // Exit if goal reached IF min = goal THEN EXIT WHILE ENDIF // Update distances to neighbours FOR EACH neighbour FROM graph[min] SET cost TO weight(min, neighbour) SET alt TO distance[min] + cost IF alt < distance[neighbour] THEN SET distance[neighbour] TO alt SET previousNode[neighbour] TO min ENDIF NEXT neighbour REMOVE min FROM unvisited ADD min TO visited ENDWHILE // Build path from goal to start DECLARE path AS LIST SET node TO goal WHILE node != start ADD node TO path SET node TO previousNode[node] ENDWHILE ADD start TO path REVERSE path RETURN path ENDFUNCTION

4.4 Python Implementation

def dijkstra(graph, start, goal): # Initialise distances and previous node map distance = {node: float('inf') for node in graph} distance[start] = 0 previous_node = {} visited = set() unvisited = set(graph.keys()) while unvisited: # Find the unvisited node with smallest distance min_node = None for node in unvisited: if min_node is None or distance[node] < distance[min_node]: min_node = node if min_node == goal: break for neighbour, weight in graph[min_node]: if neighbour in visited: continue alt = distance[min_node] + weight if alt < distance[neighbour]: distance[neighbour] = alt previous_node[neighbour] = min_node visited.add(min_node) unvisited.remove(min_node) # Reconstruct path from goal to start path = [] current = goal while current != start: path.append(current) current = previous_node[current] path.append(start) path.reverse() return path
💡 Exam Tip

Key assumptions in pseudocode: graph[node] returns a list of neighbour nodes, weight(x, y) returns the edge weight between nodes x and y. Lists support ADD and REMOVE operations.

5. A* Algorithm

The A* (A-star) algorithm is a pathfinding algorithm that builds upon Dijkstra's algorithm by introducing a heuristic function to improve efficiency.

5.1 How A* Improves on Dijkstra's

📖 Key Improvements

5.2 The A* Formula

f(x) = g(x) + h(x)
Term Meaning
g(x) The actual cost from the start node to the current node
h(x) The heuristic estimate to the goal node (e.g., straight-line distance)
f(x) The total estimated cost of the cheapest solution through node x
Start x g=3 h=5 Goal cost=3 estimated f(x) = g(x) + h(x) f(x) = 3 + 5 = 8 Total estimated cost

5.3 Heuristics in A*

⚠️ Important: Heuristic Rules
Example: For a GPS navigation system (using a graph where locations are nodes), A* might use straight-line distance to the destination as a heuristic to suggest quicker routes.

5. A* Algorithm (Continued)

5.4 A* vs Dijkstra's Comparison

Feature Dijkstra's A* Search
Goal-aware? No Yes
Cost function g(x) only g(x) + h(x)
Speed Slower (explores more nodes) Faster (more direct toward goal)
Use of heuristic None Yes, estimates distance to goal
Best for... Full shortest path map Fastest route to a specific destination

5.5 A* Pseudocode

FUNCTION AStarSearch(graph, start, goal) // Initialise distances and f-scores FOR EACH node FROM graph SET g[node] TO infinity SET f[node] TO infinity NEXT node SET g[start] TO 0 SET f[start] TO h[start] DECLARE openSet AS LIST ADD start TO openSet WHILE openSet IS NOT EMPTY // Find node in openSet with the lowest f value SET min TO null FOR EACH node FROM openSet IF min = null THEN SET min TO node ELSEIF f[node] < f[min] THEN SET min TO node ENDIF NEXT node IF min = goal THEN EXIT WHILE ENDIF REMOVE min FROM openSet // Check all neighbours of current node FOR EACH neighbour FROM graph[min] SET tentativeG TO g[min] + cost(min, neighbour) IF tentativeG < g[neighbour] THEN SET g[neighbour] TO tentativeG SET f[neighbour] TO g[neighbour] + h[neighbour] SET previousNode[neighbour] TO min IF neighbour NOT IN openSet THEN ADD neighbour TO openSet ENDIF ENDIF NEXT neighbour ENDWHILE // Build path from goal to start DECLARE path AS LIST SET node TO goal WHILE node != start ADD node TO path SET node TO previousNode[node] ENDWHILE ADD start TO path REVERSE path RETURN path ENDFUNCTION
💡 Exam Tip

Key assumptions: graph[node] returns adjacency lists, h[node] is your heuristic table, g[] and f[] are maps for real cost and estimated total cost.

🧠 Memory Trick

Remember the A* formula: f(x) = g(x) + h(x)

6. Machine Learning

Machine learning is a subset of AI where algorithms are trained to learn from and make predictions or decisions based on past data (experience).

📖 What is Machine Learning?
Data Learning Algorithm Model Predictions Decisions Feedback / Training

6.1 Categories of Machine Learning

Machine learning algorithms are categorised based on how they learn from data:

Category Description Example
Supervised Learning Trained on labelled data with known outputs Spam filter, face recognition
Unsupervised Learning Works with unlabelled data to find patterns Customer segmentation
Reinforcement Learning Learns by trial and error with rewards/penalties Game AI, robotics
Example: A machine learning model may learn to identify fruits from images by analyzing many examples of fruits, similar to how a baby learns to recognize objects by looking at them repeatedly.

7. Types of Machine Learning

7.1 Supervised Learning

Supervised learning involves training a model on a labelled dataset, where the desired outcomes are known. The model learns to map input data to known outputs, effectively learning by example.

📖 Supervised Learning Characteristics
Example: A spam filter is trained with emails labelled as 'spam' or 'not spam'. The program uses this training to improve its ability to classify new emails correctly.

7.2 Unsupervised Learning

Unsupervised learning works with datasets without labelled outcomes. The goal is to find patterns or intrinsic structures within the data.

📖 Unsupervised Learning Characteristics
Supervised Learning 📧 A spam 📧 B not spam 📧 C spam Model Unsupervised Learning 👤 A 👤 B 👤 C 👤 D Group 1 Group 2
Example: Market segmentation where customer data are grouped into segments without pre-assigned labels, helping a business tailor its strategies to different customer groups.

7. Types of Machine Learning (Continued)

7.3 Reinforcement Learning

Reinforcement learning is a type of machine learning where an agent learns by interacting with an environment. It receives rewards for good actions and penalties for poor actions.

📖 Reinforcement Learning Characteristics
Agent (Robot/AI) Environment Action State Reward/Penalty
Application Description
Robotics Robots learn to navigate and perform tasks
Self-driving cars Learn to drive safely in various conditions
Game-playing AI Like AlphaGo - learns optimal game strategies
Industrial automation Optimizes manufacturing processes
Example: A robotic vacuum cleaner learns to navigate a room by trying different paths and getting positive feedback when it picks up dirt or negative feedback when it bumps into walls.
💡 Exam Tip - Key Differences

8. Artificial Neural Networks (ANNs)

Artificial Neural Networks (ANNs) are algorithms inspired by the structure of the human brain. A neural network is made up of layers of nodes (neurons) connected by weighted links.

📖 How ANNs Work
Input Layer Hidden Layer 1 Hidden Layer 2 Output Layer Deep Learning = Multiple Hidden Layers
⚠️ Why Use ANNs?

9. Deep Learning

Deep learning is a specialized subset of machine learning that uses deep (multi-layered) neural networks. The more layers a network has, the more complex patterns it can learn.

📖 Key Characteristics of Deep Learning

9.1 Why Multiple Hidden Layers?

📝 Reasons for Multiple Hidden Layers
Example - Image Recognition: Early layers might detect edges, while deeper layers identify more complex features like shapes or objects. In facial recognition, early layers detect features like eyes and noses, deeper layers recognize complete faces.
Application How Deep Learning Helps
Facial Recognition Learns features like eyes, noses, then full faces through many layers
Self-driving Cars Interprets sensor data and makes driving decisions autonomously
Medical Diagnosis Identifies subtle patterns in medical images indicative of diseases
Language Translation Understands context and nuance in language
🌟 Deep Learning vs Machine Learning

While machine learning may require manual feature identification and relies on smaller datasets, deep learning leverages multiple layers to autonomously learn features directly from large amounts of data, often resulting in more accurate and robust models.

10. Back Propagation

Back propagation is a fundamental process in training neural networks. It's an iterative process where the network's performance is constantly evaluated and improved.

📖 How Back Propagation Works

10.1 Back Propagation Steps

📝 The Four Stages of Back Propagation
  1. Forward Pass: Input data passes through the network layer by layer. The network produces an output (prediction).
  2. Error Calculation: The output is compared to the actual target value. The difference is called the error.
  3. Backward Pass: The error is propagated backwards through the network. Each layer calculates its contribution to the error.
  4. Weight Adjustment: Weights are updated using an algorithm (e.g., gradient descent) to reduce future errors.
Input Hidden Layers Output (Predicted) Forward Pass → Error Calc Target (Actual) ← Backward Pass (Error) Weights Updated!
⚠️ Why Use Back Propagation?
🧠 Memory Trick

Think of back propagation like learning from mistakes:

11. Regression

Regression is a type of supervised learning used to predict continuous values (rather than categories). The aim is to find the relationship between input features and a numerical output.

📖 What is Regression Analysis?

11.1 Types of Regression

Type Description Example
Linear Regression Predicts output using a straight-line relationship (y = mx + c) Predicting house prices from size
Multiple Linear Regression Uses multiple input features to predict one continuous output Predicting price from size, location, age
Logistic Regression Predicts binary outcomes (yes/no), despite the name "regression" Will customer buy? (Yes/No)
House Size (m²) Price ($) y = mx + c Best Fit Line
Example: Predicting house prices based on square footage - Input: size of house, Output: estimated price. The model learns a line that best fits the data points.

11.2 Why Use Regression?

📝 Applications of Regression

12. Exam-Style Questions

1. Supervised and unsupervised learning are two categories of machine learning. Describe supervised learning and unsupervised learning. [4 marks]

Answer:

Supervised learning (Max 3 marks):

  • Supervised learning allows data to be collected, or a data output produced, from previous experience
  • In supervised learning, known input and associated outputs are given OR uses sample data with known outputs (in training) OR uses labelled input data
  • Able to predict future outcomes based on past data

Unsupervised learning (Max 3 marks):

  • Unsupervised machine learning helps all kinds of unknown patterns in data to be found
  • Unsupervised learning only requires input data to be given
  • Uses any data OR not trained on the right output OR uses unlabelled input data

Additional points for deeper understanding:

  • Supervised: Used for classification and regression tasks
  • Unsupervised: Used for clustering and pattern recognition
2. Explain the difference between Dijkstra's algorithm and the A* algorithm. [4 marks]

Answer:

  • Dijkstra's algorithm considers only the actual cost g(x) from the start node
  • A* uses f(x) = g(x) + h(x) where h(x) is a heuristic estimate to the goal
  • Dijkstra's is not goal-aware - explores all nodes equally
  • A* is goal-aware - uses heuristic to move toward goal more directly
  • A* is typically faster as it explores fewer nodes
  • Dijkstra's is best for finding shortest paths to all nodes
  • A* is best for finding the fastest route to a specific destination
3. Describe the purpose of the heuristic function in the A* algorithm. [4 marks]

Answer:

  • The heuristic function h(x) estimates the remaining distance/cost to the goal
  • It helps the algorithm prioritize paths that appear to lead most directly to the destination
  • The heuristic should never overestimate the true cost to ensure optimality
  • Common heuristic is straight-line (Euclidean) distance to the goal
  • The closer h(x) is to the true cost, the fewer nodes A* needs to explore
  • It makes the algorithm more goal-oriented and efficient
4. Explain what is meant by back propagation in neural networks. [5 marks]

Answer:

  • Back propagation is a training method used to improve neural network accuracy
  • It works by adjusting the weights of connections between neurons
  • Errors from predictions are propagated backward through the network layers
  • Each layer calculates its contribution to the error
  • Weights are updated using algorithms like gradient descent
  • The process repeats until predictions reach acceptable accuracy
  • It allows the network to learn from its mistakes
5. Describe how Dijkstra's algorithm finds the shortest path. [5 marks]

Answer:

  • Set the start node's distance to 0 and all other nodes to infinity
  • Mark all nodes as unvisited
  • Select the unvisited node with the lowest distance
  • For each neighbour, calculate new distance through current node
  • If new distance is smaller than current, update it
  • Mark current node as visited
  • Repeat until all nodes visited or goal reached
  • Backtrack from goal to find the shortest path

12. Exam-Style Questions (Continued)

6. Describe the characteristics of an undirected, weighted graph and give an example of its use in AI. [4 marks]

Answer:

  • Edges go both ways (no direction)
  • Each edge has a weight representing cost, distance, time, or risk
  • If no connection exists, the matrix contains ∞ (infinity)
  • The adjacency matrix is symmetric
  • Example: Pathfinding algorithms like A* or Dijkstra finding lowest cost path
  • Example: Clustering with similarity weights between data points
7. Explain the difference between deep learning and traditional machine learning. [5 marks]

Answer:

  • Deep learning uses multiple hidden layers in neural networks
  • Traditional ML may require manual feature identification
  • Deep learning autonomously learns features from large amounts of data
  • Deep learning excels at complex patterns in images, text, audio, video
  • Deep learning typically requires more data and computational power
  • Traditional ML works well with smaller datasets
  • Deep learning often results in more accurate and robust models
8. Describe how graphs can be represented in computer systems. [4 marks]

Answer:

  • Adjacency Matrix: 2D array where matrix[i][j] = 1 if edge exists between nodes i and j
  • Ideal for dense graphs with fast edge lookup
  • Uses O(n²) space where n is number of nodes
  • Adjacency List: Stores list of neighbours for each node
  • More space-efficient for sparse graphs
  • Uses O(V+E) space (vertices + edges)
  • Often used in AI search algorithms
9. Explain what is meant by reinforcement learning and give an example application. [4 marks]

Answer:

  • Agent learns to make decisions by taking actions in an environment
  • Aims to maximize cumulative reward
  • Learns through trial and error with feedback
  • Receives rewards for good actions and penalties for poor actions
  • Over time, learns an optimal strategy called a policy
  • Example: Self-driving cars learning to navigate safely
  • Example: Game-playing AI like AlphaGo learning optimal strategies
  • Example: Robotic vacuum cleaner learning room navigation
10. Describe the role of hidden layers in artificial neural networks. [5 marks]

Answer:

  • Hidden layers are layers between input and output layers
  • They transform data received from input layers through computational processes
  • Progressively extract higher-level features from raw input
  • Early layers may detect simple features (edges), deeper layers detect complex features (objects)
  • Multiple hidden layers enable deep learning
  • Allow network to build a deep hierarchy of concepts
  • More layers = ability to learn more complex patterns
  • Results from hidden layers are used to make decisions at the output layer

13. Glossary

📖 Key Terms

14. Exam Success Tips (Part 1)

💡 Graph Types - Quick Reference
💡 Dijkstra's Algorithm Steps
💡 A* Formula - Must Remember!
f(x) = g(x) + h(x)
🧠 Memory Trick: A* Formula

14. Exam Success Tips (Part 2)

💡 Machine Learning Types - Key Differences
💡 Neural Networks Key Points
❌ Common Mistakes to Avoid
💡 Back Propagation Steps
  1. Forward Pass: Data through network → prediction
  2. Error Calculation: Compare prediction to actual
  3. Backward Pass: Propagate error backward
  4. Weight Adjustment: Update weights to reduce error

15. Key Takeaways

📌 Summary Points

AI & Graphs

Pathfinding Algorithms

Machine Learning

Neural Networks

Regression

🌟 Final Thought

Artificial Intelligence continues to transform our world. From pathfinding algorithms powering GPS navigation to deep learning enabling facial recognition and self-driving cars, these concepts form the foundation of modern AI systems. Understanding these fundamentals is essential for any computer scientist!