Photo AI
Last Updated Sep 27, 2025
Revision notes with simplified explanations to understand Graphs quickly and effectively.
381+ students studying
A graph is a data structure used to model relationships between objects. It consists of nodes (also called vertices) and edges (connections between nodes). Graphs are widely used in various fields, such as networking (e.g., the Internet), social networks, and pathfinding algorithms in maps.
Graphs can be:
matrix[i][j]
indicates whether there is an edge from node i
to node j
.matrix[i][j]
is the weight of the edge.Example for an undirected graph with 3 nodes:
0 1 1
1 0 0
1 0 0
Example:
0: [1, 2]
1: [0]
2: [0]
Two primary traversal methods:
# Create a graph with 3 nodes using an adjacency matrix
graph = [
[0, 1, 1],
[1, 0, 0],
[1, 0, 0]
]
def add_edge(matrix, u, v):
matrix[u][v] = 1
matrix[v][u] = 1 # For undirected graphs
def remove_edge(matrix, u, v):
matrix[u][v] = 0
matrix[v][u] = 0 # For undirected graphs
def traverse_matrix(matrix):
for i, row in enumerate(matrix):
print(f"Node {i} is connected to: {', '.join(str(j) for j in range(len(row)) if row[j] == 1)}")
# Graph using an adjacency list
graph = {
0: [1, 2],
1: [0],
2: [0]
}
def add_edge(adj_list, u, v):
adj_list[u].append(v)
adj_list[v].append(u) # For undirected graphs
def remove_edge(adj_list, u, v):
adj_list[u].remove(v)
adj_list[v].remove(u)
def traverse_list(adj_list):
for node in adj_list:
print(f"Node {node} is connected to: {adj_list[node]}")
Example in Python:
class Graph:
def __init__(self):
self.adj_list = {}
def add_node(self, node):
if node not in self.adj_list:
self.adj_list[node] = []
def add_edge(self, u, v):
if u in self.adj_list and v in self.adj_list:
self.adj_list[u].append(v)
self.adj_list[v].append(u) # For undirected graphs
def remove_edge(self, u, v):
if u in self.adj_list and v in self.adj_list:
self.adj_list[u].remove(v)
self.adj_list[v].remove(u)
def dfs(self, start, visited=None):
if visited is None:
visited = set()
visited.add(start)
print(start, end=" ")
for neighbor in self.adj_list[start]:
if neighbor not in visited:
self.dfs(neighbor, visited)
def bfs(self, start):
visited = set()
queue = [start]
visited.add(start)
while queue:
current = queue.pop(0)
print(current, end=" ")
for neighbor in self.adj_list[current]:
if neighbor not in visited:
queue.append(neighbor)
visited.add(neighbor)
DFS on an Undirected Graph:
g = Graph()
g.add_node(0)
g.add_node(1)
g.add_node(2)
g.add_edge(0, 1)
g.add_edge(0, 2)
g.dfs(0) # Output: 0 1 2
BFS on the Same Graph:
g.bfs(0) # Output: 0 1 2
Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!
90 flashcards
Flashcards on Graphs
Revise key concepts with interactive flashcards.
Try Computer Science Flashcards9 quizzes
Quizzes on Graphs
Test your knowledge with fun and engaging quizzes.
Try Computer Science Quizzes29 questions
Exam questions on Graphs
Boost your confidence with real exam questions.
Try Computer Science Questions27 exams created
Exam Builder on Graphs
Create custom exams across topics for better practice!
Try Computer Science exam builder12 papers
Past Papers on Graphs
Practice past papers to reinforce exam experience.
Try Computer Science Past PapersDiscover More Revision Notes Related to Graphs to Deepen Your Understanding and Improve Your Mastery
Join 500,000+ A-Level students using SimpleStudy...
Join Thousands of A-Level Students Using SimpleStudy to Learn Smarter, Stay Organized, and Boost Their Grades with Confidence!
Report Improved Results
Recommend to friends
Students Supported
Questions answered