Topic 1 - Data Modelling
Topic 1 - Data Modelling
Graph Theory
A large chunk of time in Algorithmics is devoted to the study of graphs, as they are a very
useful way of representing data. Below is an example of a graph, you can drag it around and
zoom in and out (all graph examples in Algorithmics 24 are interactive like this one).
e 15
d
2
b 7
a
8
Nodes (also sometimes called a vertices) represent some point of data (maybe a city, house,
computer, or road intersection). To provide information about how two nodes are related we can
connect them together with an edge.
10
a b
Edges represent a connection between two things that themselves are represented as nodes (for
example: roads connecting cities or network cables connecting computers). Notice that, in the
above graph, there is a "10" over the edge? That’s the edge’s weight, it could be used to
represent the distance between two places, or the number of changes between two states. The
weight of an edge basically represents a characteristic of that connection between nodes.
Edges can also be directed, meaning that they have a direction by which the connection goes.
This is represented by the arrow on the end of the edge. We call the node at the end of the
arrow the target (or terminal) node, and the one at the start the source node.
✦ Algorithmics 24
10
a b
Circuits
A graph contains a circuit if there is more than one path that connects two nodes. This pops
up when there is a "loop" or "circle" in the edges of a graph. Circuits themselves
specifically contain no repeated edges (but they can contain repeated nodes) and start and end
on the same node. Note that, in the graph below, there is a circuit following the path of "a",
"b", "c" then "a" again. If there wasn’t an edge between "c" and "a", then there wouldn’t be a
circuit in this graph.
c
d
a
Graphs
A Graph is a collection of nodes and edges that may or may not be joining those nodes. We’ve
already explored what a graph looks like before, but here are a few specific categories of
graph:
✦ Algorithmics 24
Connected Graphs: These graphs are ones where every node is connected to every other node,
either directly or indirectly. That is, starting from any node, you can reach any other node
by traversing one or more edges.
Complete Graphs: A complete graph is a type of graph in which every pair of distinct
vertices is connected by a unique edge. In other words, there is a direct edge between every
pair of nodes, meaning each node is directly connected to every other node, without needing
to traverse through a different node. This makes it a form of a fully connected network.
Directed Graphs: Directed graphs feature exclusively edges that are directed.
Directed Acyclic Graphs: On the other hand, a directed acyclic graph (DAG) is a directed
graph with no directed cycles. That is, it consists of vertices and edges, with each edge
directed from one vertex to another, such that following those directions will never form a
closed loop. The term "acyclic" refers to the fact that there’s no way to start at any
vertex v and follow a consistently-directed sequence of edges that eventually loops back to
v again.
Weighted Graphs: A Weighted graph is one that has weights on all of its edges.
Trees: A tree is a kind of graph that is connected, but does not contain any circuits.
Subgraphs: A Subgraph is a part of a larger graph. All edges and nodes in a subgraph must
be in the original graph, but not all edges and nodes in the original graph have to be in
the subgraph.
Transitive Closure
The transitive closure of a graph is a concept in graph theory, which involves determining if
there is a directed path between all pairs of vertices. In other words, it tells you which
vertices are reachable from other vertices.
In a connected graph, there needs to be at least one path between every pair of vertices. This
does not mean every pair of vertices needs to be directly connected with a unique edge.
Instead, it just requires the possibility of reaching any vertex from another, possibly
through other vertices.
On the other hand, in a complete graph, every pair of distinct vertices must be connected by a
unique edge. This signifies that each vertex/node is directly connected to every other vertex
in the graph. This requires many more edges than a connected graph.
In simple terms, all complete graphs are connected, but not all connected graphs are complete.
✦ Algorithmics 24
A cycle is a path that starts and ends on the same node (that start and end node is the one
exception to the rule of paths that there can be no repeated nodes).
This is a path because it starts at node A, ends at node E, and there are no repeated edges or
nodes along the way. Every node and edge is unique and traversed only once.
e
This forms a cycle because it is a path that starts and ends at the same node, A. Note that in
the definition of a cycle, the starting/ending node is allowed to be the same, which makes
this sequence a valid cycle. All other nodes (B, C, D, E) and edges are unique and not
repeated, satisfying the path criteria except for the allowance of the start and end node to
repeat in the case of a cycle.
c
e
b
a
Decision Trees
A Decision Tree is a tree whose branches represent the outcomes of a series of decisions.
Decision Trees can be used to compare outcomes and choose decisions that lead to good results.
✦ Algorithmics 24
State Graphs
State graphs are like maps that illustrate various conditions, or "states," for a system and
the pathways, or transitions, between these states. Each state represents a specific behavior
or condition of the system. The transitions indicate how the system shifts from one state to
another, usually triggered by events or specific conditions. The graph begins at a starting,
or "initial," state, and follows the paths from state to state according to the specific
rules, or "transitions, until it reaches a final state. By following the paths in a state
graph, you can understand and predict how the system will behave based on given scenarios or
conditions.
Each ADT has a specification associated with it, which informs the possible operations that
can be performed involving the ADT. Note that the specification is not specific to any
programming language, and does not tell us about how the ADT is implemented in code. For
Algorithmics you do not need to know how individual ADTs are implemented.
There is one unique type of operator, called a constructor. Constructors, as indicated by its
name, initializes the ADT with specific sets of data. This is shown using the 'create'
✦ Algorithmics 24
specification - Lists for example, does not take any values within its constructor, and hence
just returns itself. Arrays however, take an integer and then returns itself. The reasons for
these values are different per ADT but this is beyond the scope of the study design.
KEY TO REMEMBER
Programming is subject to zero-based numbering, meaning that to get the first element of a
list, you would get the '0’th index, the second element, the '1’st index, etc.
Arrays
Arrays are a method of storing elements in a collection that is of a fixed length. Elements in
an array can only be accessed by their index in the array. You can think of it like a book: it
has a fixed length, and you can get a page’s information by turning to that page number.
Arrays can be used to store a fixed number of elements, such as the days of the week, the
seats in a cinema hall, or temperature readings over a month.
Arrays are suitable when you know the number of elements beforehand, and when you need fast
access to elements at specific positions.
NAME: Array
OPERATORS:
Create (Constructor): Integer -> Array
Set: Array × Integer × Element -> Array
Get: Array × Integer -> Element
Lists
A list is similar to an array, but instead of having one unchangeable length, it is flexible
and can have any length. Elements can be inserted to the list at any point and can be deleted
at any point too. The list ADT is very similar to an actual list like you might have in your
notes app: you can read and edit information at any point in the list, and add new elements to
it in any way that you’d like.
Lists are used for inventories, to-do lists, and any scenario where the number of items can
change over time.
Lists are suitable for cases where you need dynamic storage, and when the number of elements
can change frequently.
NAME: List
OPERATORS:
Create (Constructor): -> List
IsEmpty: List -> Boolean
Get: List × Integer -> Element
Set: List × Integer × Element -> List
✦ Algorithmics 24
Insert: List × Integer × Element -> list
Delete: List × Integer -> List
Stacks
A Stack is an ordered collection of items that are retrieved in a last in, first out (LIFO)
basis. That is, new elements are added to the top of the stack, and items are taken from the
top of the stack also. You can only take items from the top of the stack, and cannot get them
from anywhere else in the stack.
Stacks are used in undo operations in software applications, browser history (last page
visited is the first one to go back to), and for parsing expressions (e.g., bracket matching).
Stacks are suitable for scenarios where you need to track actions in a reverse order, such as
navigating backward or tracking recursive function calls.
NAME: Stack
OPERATORS:
Create (Constructor): -> Stack
IsEmpty: Stack -> Boolean
Push: Stack × Element -> Stack
Peek: Stack -> Element
Pop: Stack -> Stack
Queues
A Queue is like a stack, in that it is an ordered collection of items, but unlike a stack, the
items in a queue are retrieved in a first in, first out (FIFO) order. New elements are added
to the end of the queue, and elements can only be retrieved if they are at the front of the
queue.
Queues are used for scheduling jobs in printers, call center systems, and managing requests on
a web server.
Queues are suitable for handling tasks in the order they arrive, such as processing customer
requests.
NAME: Queue
OPERATORS:
Create (Constructor): -> Queue
IsEmpty: Queue -> Boolean
Append: Queue × Element -> Queue
Peek: Queue -> Element
Serve: Queue -> Queue
Priority queues are used in operating systems for process scheduling, emergency services where
more critical cases are attended first, and in pathfinding algorithms (like Dijkstra’s and
A*).
Priority queues are suitable when order of removal depends on specific criteria than simple
arrival time, like urgency or importance.
Dictionaries (Maps)
A Dictionary is an unordered collection of key-value pairs. That is, it contains a bunch of
keys, where each key represents a specific value. In an actual real-life dictionary, the keys
would be words, and the values would be their definitions. We call that connection between a
key and a value a "key-value pair".
In some programming languages, dictionaries are called Maps, however for the purposes of the
Study Design we only use the term dictionary.
Dictionaries are used for caches, database indexing, and associative arrays where you want to
retrieve values based on specific keys.
NAME: Dictionary
OPERATORS:
Create (Constructor): -> Dictionary
HasKey: Dictionary × Element -> Boolean
Add: Dictionary × Element × Element -> Dictionary
Update: Dictionary × Element × Element -> Dictionary
✦ Algorithmics 24
Remove: Dictionary × Element -> Dictionary
Get: Dictionary × Element -> Element
Graphs
All of the following relate to the previously mentioned Graph Theory, note that the specifics
of these DO NOT need to be remembered;
Social networks use graphs to represent users and their friendships, roads and the
intersections can be modeled as a graph for route planning, and recommendation systems use
graphs to understand and predict relationships between entities.
NAME: UndirectedGraph
OPERATORS:
Create (Constructor): -> UndirectedGraph
AddNode: UndirectedGraph × Element -> UndirectedGraph
AddEdge: UndirectedGraph × Element × Element -> UndirectedGraph
RemoveEdge: UndirectedGraph × Element × Element -> UndirectedGraph
Adjacent: UndirectedGraph × Element × Element -> Boolean
Neighbours: UndirectedGraph × Element -> List
NAME: DirectedGraph
OPERATORS:
Create (Constructor): -> DirectedGraph
AddNode: DirectedGraph × Element -> DirectedGraph
AddEdge: DirectedGraph × Element × Element -> DirectedGraph
RemoveEdge: DirectedGraph × Element × Element -> DirectedGraph
Adjacent: DirectedGraph × Element × Element -> Boolean
Neighbours: DirectedGraph × Element -> List
Predecessors: DirectedGraph × Element -> List
Successors: DirectedGraph × Element -> List
Unweighted Graph ADT (inherits from UndirectedGraph and DirectedGraph since an unweighted
graph can be either)
NAME: UnweightedGraph
OPERATORS: (Same as either UndirectedGraph or DirectedGraph)
✦ Algorithmics 24
NAME: WeightedGraph
OPERATORS:
Create: -> WeightedGraph
AddNode: WeightedGraph × Element -> WeightedGraph
AddEdge: WeightedGraph × Element × Element × Number -> WeightedGraph
UpdateEdgeWeight: WeightedGraph × Element × Element × Number -> WeightedGraph
RemoveEdge: WeightedGraph × Element × Element -> WeightedGraph
Adjacent: WeightedGraph × Element × Element -> Boolean
Neighbours: WeightedGraph × Element -> List
GetEdgeWeight: WeightedGraph × Element × Element -> Number
A. stack → stack
B. stack → element
Answer
A.
✦ Algorithmics 24
B.
C.
D.
Answer