COS 102 - Note-1
COS 102 - Note-1
Learning Outcomes
Course Contents
1
The core concept of computing revolves around the automatic processing of data to solve
problems using machines (computers). It is built on several fundamental principles,
including:
1. Computation
3. Algorithms
4. Programming
7. Abstraction
2
Hiding complex details to focus on high-level operations i.e. hiding complex
implementation details and presenting a simpler interface to the user or programmer.
Used in programming, hardware design, and problem-solving.
8. Efficiency
These concepts are the foundation of fields like software engineering, data science, artificial
intelligence, and computer systems.
Computer science is all about solving problems with computers. The problems that we want
to solve can come from any real-world problem or perhaps even from the abstract world. We
need to have a standard systematic approach to solving problems. Since we will be using
computers to solve problems, it is important to first understand the computer’s information
processing model. The model shown in the diagram below assumes a single CPU (Central
Processing Unit). Many computers today have multiple CPUs, so you can imagine the above
model duplicated multiple times within the computer.
3
A typical single CPU computer processes information as shown in the diagram. Problems are
solved using a computer by obtaining some kind of user input (e.g., keyboard/mouse
information or game control movements), then processing the input and producing some kind
of output (e.g., images, text or sound). Sometimes the incoming and outgoing data may be in
the form of hard drives or network devices.
4
In regards to problem solving, we will apply the above model such that we will assume that
we are given some kind of input information that we need to work with in order to produce
some desired output solution. However, the above model is quite simplified. For larger and
more complex problems, we need to iterate (i.e., repeat) the input/process/output stages
multiple times in sequence, producing intermediate results along the way that solve part of
our problem, but not necessarily the whole problem. For simple computations, the above
model is sufficient.
It is the “problem solving” part of the process that is the interesting part, so we’ll break this
down a little. There are many definitions for “problem solving”. Here is one:
2. Formulate a Model
3. Develop an Algorithm
1. Input: get all the grades … perhaps by typing them in via the keyboard or by reading them
from a USB flash drive or hard disk.
As you can see, the problem is easily solved by simply getting the input, computing
something and producing the output. Let us now examine the 6 steps to problems solving
within the context of the above example.
STEP 1: Understand the Problem: It sounds strange, but the first step to solving any
problem is to make sure that you understand the problem that you are trying to solve. You
need to know:
In our example, we well understand that the input is a bunch of grades. But we need to
understand the format of the grades. Each grade might be a number from 0 to 100 or it may
be a letter grade from A to F. If it is a number, the grade might be a whole integer like 73
or it may be a real number like 73.42. We need to understand the format of the grades in
order to solve the problem.
We also need to consider missing grades. What if we do not have the grade for every student
(e.g., some were away during the test)? Do we want to be able to include that person in our
average (i.e., they received 0) or ignore them when computing the average?
We also need to understand what the output should be. Again, there is a formatting issue.
Should we output a whole or real number or a letter grade? Maybe we want to display a pie
chart with the average grade. It is our choice.
6
Finally, we should understand the kind of processing that needs to be performed on the data.
This leads to the next step.
Now we need to understand the processing part of the problem. Many problems break down
into smaller problems that require some kind of simple mathematical computations in order
to process the data. In our example, we are going to compute the average of the incoming
grades. So, we need to know the model (or formula) for computing the average of a bunch of
numbers. If there is no such “formula”, we need to develop one. Often, however, the problem
breaks down into simple computations that we well understand. Sometimes, we can look up
certain formulas in a book or online if we get stuck.
In order to come up with a model, we need to fully understand the information available to us
by:
Now that we understand the problem and have formulated a model, it is time to come up with
a precise plan of what we want the computer to do.
7
Some of the more complex algorithms may be considered “randomized algorithms” or
“non-deterministic algorithms” where the instructions are not necessarily in sequence and
in may not even have a finite number of instructions. However, the above definition will
apply for all algorithms that we will discuss in this course.
pseudo code
flow charts.
Consider the following example (from Wikipedia) of solving the problem of a broken lamp.
To the right is an example of a flow chart, while to the left is an example of pseudocode for
solving the same problem:
Notice that:
Although flowcharts can be visually appealing, pseudocode is often the preferred choice for
algorithm development because:
It can be difficult to draw a flowchart neatly, especially when mistakes are made.
Pseudocode fits more easily on a page of paper.
8
Pseudocode can be written in a way that is very close to real program code, making it
easier later to write the program (i.e., in step 4).
Pseudocode takes less time to write than drawing a flowchart.
Pseudocode will vary according to whoever writes it. That is, one person’s pseudocode is
often quite different from that of another person. However, there are some common control
structures (i.e., features) that appear whenever we write pseudocode.
9
10
11
STEP 4: Write the Program:
Now that we have a precise set of steps for solving the problem, most of the hard work has
been done. We now have to transform the algorithm from step 3 into a set of instructions that
can be understood by the computer.
Writing a program is often called "writing code" or “implementing an algorithm”. So the
code (or source code) is actually the program itself.
For now, we will not discuss the details of how to produce the above source code. In fact, the
source code would vary depending on the programming language that was used. Learning a
programming language may seem difficult at first, but it will become easier with practice.
The computer requires precise instructions in order to understand what you are asking it to
do. For example, if you removed one of the semi-colon characters ( ; ) from the program
above, the computer would become confused as to what you are doing because the ( ; ) is
what it understands to be the end of an instruction. Leaving one of them off will cause your
program to generate what is known as a compile error.
Compiling is the process of converting a program into instructions that can be understood by
the computer.
The longer your program becomes, the more likely you will have multiple compile errors.
You need to fix all such compile errors before continuing on to the next step.
12
STEP 5: Test the Program:
Once you have a program written that compiles, you need to make sure that it solves the
problem that it was intended to solve and that the solutions are correct.
Running a program is the process of telling the computer to evaluate the compiled
instructions.
When you run your program, if all is well, you should see the correct output. It is possible
however, that your program works correctly for some set of data input but not for all. If the
output of your program is incorrect, it is possible that you did not convert your algorithm
properly into a proper program. It is also possible that you did not produce a proper algorithm
back in step 3 that handles all situations that could arise. Maybe you performed some
instructions out of sequence. Whatever happened, such problems with your program are
known as bugs.
Bugs are problems/errors with a program that cause it to stop working or produce incorrect or
undesirable results.
You should fix as many bugs in your program as you can find. To find bugs effectively, you
should test your program with many test cases (called a test suite). It is also a good idea to
have others test your program because they may think up situations or input data that you
may never have thought of.
The process of finding and fixing errors in your code is called debugging and it is often a
very time-consuming “chore” when it comes to being a programmer. If you take your time to
carefully follow problem solving steps 1 through 3, this should greatly reduce the amount of
bugs in your programs and it should make debugging much easier.
Once your program produces a result that seems correct, you need to re-consider the original
problem and make sure that the answer is formatted into a proper solution to the problem. It
is often the case that you realize that your program solution does not solve the problem the
way that you wanted it to. You may realize that more steps are involved.
For example, if the result of your program is a long list of numbers, but your intent was to
determine a pattern in the numbers or to identify some feature from the data, then simply
producing a list of numbers may not suffice. There may be a need to display the information
13
in a way that helps you visualize or interpret the results with respect to the problem. Perhaps
a chart or graph is needed.
It is also possible that when you examine your results, you realize that you need additional
data to fully solve the problem. Or, perhaps you need to adjust the results to solve the
problem more efficiently (e.g., your game is too slow).
It is important to remember that the computer will only do what you told it to do. It is up to
you to interpret the results in a meaningful way and determine whether or not it solves the
original problem. It may be necessary to re-do some of the steps again, perhaps going as far
back as step 1 again, if data was missing.
So there you have it. Those are the 6 steps that you should follow in order to solve problems
using computers. Throughout the course, you should try to use this approach for all of your
assignments. It is a good idea to practice problem solving to make sure that you understand
the process. Below are some practice exercises that will help you practice the first 3 steps of
the problem solving process. Later, you will gain experience with steps 4 through 6.
TYPES OF PROBLEMS
There are basically two types of problems in Computing and General Problem Solving;
routine and non-routine problems
Routine Problems
These are problems that have clear, known procedures or methods for solving them. They are
familiar, often repetitive, and require applying previously learned techniques.
Key Features:
Well-defined steps to follow.
Predictable outcome.
Can often be solved using formulas, algorithms, or known procedures.
Typically requires lower-order thinking (recall, application).
Examples:
Converting binary to decimal.
Calculating the average of a set of numbers.
Sorting a list using a known algorithm (e.g., bubble sort).
Writing code to check if a number is even or odd.
14
Non-Routine Problems
Problems that are unfamiliar, complex, or do not have a straightforward solution method.
They require creativity, critical thinking, and sometimes trial-and-error.
Key Features:
No clear or single method of solution.
Requires deeper thinking, analysis, or innovation.
Often open-ended or involve multiple steps or strategies.
May involve unknowns, constraints, or abstract reasoning.
Examples:
Designing a new algorithm to optimize delivery routes.
Debugging a software program with an unknown error.
Creating a mobile app to solve a unique user problem.
Developing a cybersecurity strategy for a new type of threat.
Comparison Table:
PRACTICE EXERCISES
Formulate a model and then develop an algorithm for each of the following problems. In each
case, start with a simple algorithm and then try to think about situations that can realistically
go wrong and make appropriate adjustments to the algorithm. Keep in mind that there is no
“right” answer to these problems. Everyone will have a unique solution.
PHY:
b. Ascending to heaven
CHM:
15
a. Producing 3 kg of cooking salt
Characteristics:
In today's digital age, the ubiquity of technology and the explosion of data have led to an
increasing reliance on algorithms in a variety of fields. These intricate sets of instructions and
rules define the core of artificial intelligence and machine learning, driving significant
advances in complex problem solving and automated decision-making. In this vast
algorithmic landscape, a fascinating variety of approaches unfolds, each designed to tackle
different types of problems.
Here, we will dive into the fascinating world of algorithms, exploring their different
categories and highlighting those that have emerged as the most prominent and widely used
16
today. From classic algorithms that have withstood the test of time to more recent innovations
that harness modern computational power, we will examine how these algorithms have
radically transformed the way we process information and make decisions in an increasingly
digitised world. Join us on this exploratory journey as we unravel the mysteries of algorithms,
from their foundations to their most cutting-edge applications.
The truth is that there are many, many types of algorithms and it is impossible to be familiar
with all of them. However, here are a few of the most commonly used ones.
1. Sorting algorithm
Quicksort: An efficient sorting algorithm that uses a "divide and conquer" strategy. It
divides the data set into smaller subsets, sorts each subset and then combines the
results.
Mergesort: A sorting algorithm that also employs the "divide and conquer" strategy.
It divides the set into halves, sorts each half, and then combines the sorted halves to
obtain the final sorted set.
17
2. Search algorithms
A search algorithm is a set of instructions designed to find the location or determine the
existence of a specific item within a data set. There are several search strategies, and the
choice of algorithm depends on factors such as the structure of the data and the efficiency
required. However, the two most commonly used search algorithms are:
Linear Search: Examines each element in the dataset in sequence until the desired
element is found or the end is reached. It is simple but can be inefficient on large
datasets.
18
Binary Search: Applicable only on ordered data sets. Repeatedly divides the set in
half and compares the desired element with the element in the middle, thus reducing
the search space.
Both sorting and search algorithms are fundamental and widely used in programming and
efficiently solving problems related to data organisation and search. The choice of the
appropriate algorithm depends on several factors, such as the size of the data set, the
complexity of the problem and efficiency requirements.
3. Graph algorithms
A graph algorithm is based on rules designed to solve problems involving data structures
called graphs. Graphs are abstract representations of relationships between objects and
consist of nodes (or vertices) and edges (or arcs) connecting these nodes.
Graph algorithms are used to analyse and manipulate these structures in order to solve
practical problems in various fields. Again, within the range of graph algorithms, there are
many types of algorithms, the most important of which are:
Breadth-first search (BFS): An algorithm that explores all nodes at the same depth
before advancing to nodes at the next depth. It is useful for finding the shortest path in
un-weighted graphs.
Dijkstra algorithm: Used to find the shortest path between a source node and all
other nodes in a weighted graph with non-negative weights.
19
Bellman-Ford algorithm: Similar to Dijkstra but can handle graphs with negative
weighted edges. It is used to find the shortest path in graphs with variable costs.
Kruskal algorithm: Used to find a minimum spanning tree in a weighted graph. This
tree spans all the nodes of the graph and has the minimum sum of the edge weights.
4. Classification algorithm
The classification process involves training a model using a training dataset, where
instances are provided along with their respective class labels. The model uses this
information to learn patterns and relationships between features and classes. Once
trained, the model can be used to classify new instances whose class labels are unknown.
Logistic Regression: Despite its name, it is used for binary classification problems,
assigning instances to one of two possible classes.
Support Vector Machines (SVM): Searches for a hyper-plane that maximises the
margin between classes in a feature space.
Decision Trees (explored in more depth below): Represent decisions in the form of
a tree, iteratively dividing the data set into subsets.
K-Nearest Neighbours (KNN): Classifies an instance according to the most classes
of its k nearest neighbours in the feature space.
Naive Bayes: Based on Bayes' theorem, assumes independence between features and
assigns probabilities to classes.
Neural Networks: Models inspired by the functioning of the brain, used for complex
classification problems.
Random Forest: An ensemble of decision trees that vote to determine the class of an
instance.
Gradient Boosting: Combines multiple weaker models to improve overall model
accuracy.
20
The choice of classification algorithm depends on the nature of the problem, the size and
quality of the data, as well as the specific requirements of the application context. Each
algorithm has its own characteristics and performance in different situations, so proper
selection is essential to achieve optimal results.
5. Regression algorithms
Regression algorithms are a set of techniques in machine learning and statistics used to
model and analyse the relationship between variables. These algorithms are mainly
applied in prediction problems for predictive analytics, where the objective is to predict the
value of an output variable (or dependent variable) based on one or more input variables (or
independent variables).
The main task of a regression model is to find a mathematical function that describes the
relationship between the input variables and the output variable. The resulting function
can be used to make predictions about future or unknown values of the output variable.
Some common regression algorithms include:
Simple and Multiple Linear Regression: Simple linear regression models the
relationship between two variables, while multiple linear regression models the
relationship between more than two variables, considering multiple predictors.
Polynomial Regression: This technique uses a polynomial equation to model non-
linear relationships.
K-Nearest Neighbors Regression (KNN): The algorithm predicts the output value
based on the output values of the k nearest neighbors in the feature space.
Support Vector Machines (SVM) for Regression: This algorithm finds a hyper-
plane in the feature space that best fits the data.
Regression Trees: By using decision trees, this algorithm models non-linear
relationships and segments the feature space into regions.
Ridge and Lasso Regression: These are regularization techniques that help control
model over-fitting.
Neural Networks for Regression: This algorithm employs neural network
architectures to model complex relationships.
21
A decision tree algorithm is a supervised learning method used in the field of machine
learning and artificial intelligence. It is designed to make decisions based on multiple
conditions or features. Decision trees model decisions in the form of a tree, where each
internal node represents a test on a feature, each branch represents the outcome of that test,
and each leaf represents the final decision.
Recursive Division: The tree is built recursively, partitioning the dataset into smaller
subsets based on relevant features.
Decision Criteria: At each node, a test is conducted on a specific feature, and the
decision on which feature to choose and how to divide is made using a criterion that
maximizes the purity of the resulting leaves.
Classification or Regression: Decision trees can be utilized for both classification
and regression problems. In classification, the leaves represent classes or categories,
while in regression, the leaves contain numerical values.
Interpretability: A key advantage of decision trees is their ability to be easily
interpreted by humans. The tree provides a logical structure that can be visually
understood.
7. Greedy' algorithm:
A greedy algorithm is an approach to algorithm design that makes local decisions at each
step in the hope of arriving at a globally optimal solution. In other words, at each step, the
algorithm selects the option that appears to be the best at the time, without considering
the long-term consequences. The idea is to make decisions that appear to be the most
beneficial at the present moment, without worrying about their future impact.
Greedy Choice: At each step, the algorithm makes the decision that appears to be the
best at the time.
No Backtracking: Once a decision is made, it is not reviewed or changed at later
stages.
22
Global Optimisation Expectancy: Despite making local decisions, the sequence of
decisions is expected to lead to a globally optimal solution or at least an acceptable
solution.
Greedy algorithms are effective in situations where the locally optimal choice also leads
to a globally optimal or near-optimal solution. However, they do not always guarantee the
best solution in all cases, so their applicability depends on the specific problem being
addressed.
8. Brute-force algorithm
While brute-force algorithms can be effective for small problems or when there is no
known more efficient solution, their execution time can become impractical (time
complexity) as the problem size increases. In many cases, more specialised and efficient
algorithms are sought to address specific problems.
9. Backtracking algorithm
23
A backtracking algorithm tries to search for all possible solutions to a problem by
systematically exploring all available options. This approach is based on the principle of
trial and error, where the algorithm moves forward to try a solution, but backtracks when it
realises that the partial solution cannot lead to a valid solution.
24
Problem Definition: Formulate the problem in terms of smaller subproblems.
Subproblem Identification: Identify subproblems to be solved and combined to
solve the overall problem.
Recursive Equation Definition: Develop a recursive equation that relates the
solution of the original problem to the solutions of its sub-problems.
Order of Solving: Determine the order in which the sub-problems are solved to
ensure that the necessary solutions are available when required.
Results Storage: Storing the results of solved sub-problems in a data structure (often
a table or matrix) for reuse.
Encryption algorithms
Beyond the algorithms mentioned above, it is also essential to know about encryption
algorithms, which convert the data in a file in such a way that it cannot be retrieved
without the corresponding encryption key. These algorithms are the basis of online
security.
Conclusion
In this blog post, we have explored a variety of algorithms used in machine learning and
artificial intelligence. We have learned about algorithms such as Polynomial Regression,
KNN, SVM, Regression Trees, Ridge and Lasso Regression, Neural Networks, as well as the
Decision Tree Algorithm, Greedy Algorithms, Brute Force Algorithms, Backtracking
Algorithms, and Dynamic Programming Algorithms. Each of these algorithms has its own
characteristics and specific applications. Additionally, we have mentioned the importance of
encryption algorithms in online security. If you are interested in learning more about these
algorithms and how they are applied in the real world, I encourage you to continue
researching and delving into each of them. The world of artificial intelligence and machine
learning is fascinating and constantly evolving, so don't get left behind!
25
A heuristic is another type of problem solving strategy. While an algorithm must follow lay
procedure exactly to produce a correct result, a heuristic is a general problem-solving
framework which does not follow that order.
In problem-solving, a heuristic is a mental shortcut or rule of thumb that uses prior
experience and familiar knowledge to quickly find a "good enough" solution, though it
doesn't guarantee a correct or optimal one. Heuristics are practical, experience-based
strategies used when a complex problem is difficult, time-consuming, or impossible to solve
perfectly, allowing for rapid decision-making and a reduction in mental effort.
Such a rule saves the person time and energy when making a decision, but despite its time-
saving characteristics, it is not always the best method for making a rational decision.
Different types of heuristics are used in different types of situations, but the impulse to use a
heuristic occurs when one of five conditions is met:
When one is faced with too much information
When the time to make a decision is limited
When the decision to be made is unimportant
When there is access to very little information to use in making the decision
When an appropriate heuristic happens to come to mind in the same moment, working
backwards is a useful heuristic in which you begin solving the problem by focusing
on the end result. It is common to use the working backwards heuristic to plan the
events of your day on a regular basis, probably without even thinking about it.
Another useful heuristic is the practice of accomplishing a large goal or task by breaking it
into a series of smaller steps. Students often use this common method to complete a large
research project or long essay for school. For example, students typically brainstorm, develop
a thesis or main topic, research the chosen topic, organize their information into an outline,
write a rough draft, revise and edit the rough draft, develop a final draft, organize the
references list, and proofread their work before turning in the project. The large task becomes
less overwhelming when it is broken down into a series of small steps.
Key Characteristics of Heuristics
Mental Shortcuts:
They are action-oriented mental shortcuts and "rules of thumb" that simplify complex
choices and speed up the problem-solving process.
Experience-Based:
26
Heuristics are often derived from past experiences and what is familiar to the
problem-solver.
"Good Enough" Solutions:
They are pragmatic and aim for satisfactory solutions rather than perfect or fully
optimized ones.
No Guarantee of Success:
Unlike algorithms, which are step-by-step procedures that guarantee a solution,
heuristics do not promise a correct or optimal outcome.
Simplification: They help simplify complex factors and the cognitive load of making
decisions.
Time-Saving: They save time by avoiding complex procedures for routine tasks.
Analogy and Induction: They often rely on induction (finding patterns and applying
them) and analogy (comparing the current problem to a similar, familiar one).
Examples of Heuristics
27