Introduction to Computational Problems
Introduction to Computational Problems
Solving
Module 1: Computational Problems
Example 2: Amazon
•Problem: Recommend products based on your search.
•Input: Purchase history, browsing.
•Output: Suggested items (Recommendation System).
• Structure:
• 1. Input → What is given
• 2. Output → What we want
• 3. Method → Steps/Algorithm to solve
Types of Computational Problems
• Numerical → Sorting, GCD, Fibonacci
• Graph → Shortest path, MST
• Optimization → Knapsack, Job Scheduling
• String → Pattern Matching, LCS
• Decision → Primality, 3-SAT
Sorting
Sorting refers to rearrangement of a given array or list of elements according
to a comparison operator on the elements. The comparison operator is used to
decide the new order of elements in the respective data structure
Sorting
Sorting algorithms are essential in Computer Science as they simplify complex
problems and improve efficiency. They are widely used in searching, databases,
divide and conquer strategies, and data structures.
Key Applications:
For example, the GCD of 20 and 15 is 5, because 5 is the largest number that divides both 20 and 15 evenly.
The concept is fundamental in number theory and has applications in simplifying fractions, modular
arithmetic, and encryption algorithms.
A factorial, denoted by an exclamation point (!), is the product of all positive integers from 1 up to a given
non-negative integer.
Definition: A prime number is a whole number greater than 1 that can only be divided evenly by 1 and
itself.
Examples: 2, 3, 5, 7, 11, 13, and 97 are prime numbers.
Non-examples:1 is not prime: It is specifically excluded from the definition of prime numbers.
Composite numbers: Numbers that have more than two factors (e.g., 4, 6, 8, 9, 10) are called composite
numbers.
Integer factorization is the process of breaking down a composite positive integer into its prime factors,
which are prime numbers that multiply together to equal the original number. For example, the prime
factorization of 60 is 2 × 2 × 3 × 5
Polynomial Identity Testing
Polynomial Identity Testing (PIT) is a problem in theoretical computer science and mathematics that asks
whether a given arithmetic circuit computes the zero polynomial (a polynomial that is identically zero for all
variable assignments). While a simple randomized algorithm exists by evaluating the polynomial at a
random point, a deterministic algorithm remains an open challenge and its solution has significant
implications for understanding the hardness of computations.
To apply Polynomial Identity Testing, one would typically rearrange the equation to test if the difference of
the two sides is identically zero.
Let P = (a+b)(a-b) and Q = a2 – b2
We want to test if P – Q = 0
Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. we traverse all
adjacent vertices one by one. When we traverse an adjacent vertex, we completely finish the traversal of all
vertices reachable through that adjacent vertex. Extra memory, usually a stack, is needed to keep track of the
nodes discovered so far along a specified branch which helps in backtracking of the graph.
The depth first search traversal order of the above graph is-
A, B, E, F, C, D
Shortest Path
Depth First Search:
Shortest Path
Depth First Search:
Shortest Path
Depth First Search:
Breadth-first search (BFS) is an algorithm for searching a tree data structure for a node that satisfies a given
property. It starts at the tree root and explores all nodes at the present depth prior to moving on to the nodes at
the next depth level. Extra memory, usually a queue, is needed to keep track of the child nodes that were
encountered but not yet explored.
The breadth first search traversal order of the above graph is-
A, B, C, D, E, F
Shortest Path
Breadth First Search:
Shortest Path
Breadth First Search:
Shortest Path
Breadth First Search:
Shortest Path
Dijkstra's Algorithm
In Dijkstra's Algorithm, the goal is to find the shortest distance from a given source node to all other nodes in the
graph. As the source node is the starting point, its distance is initialized to zero. From there, we iteratively pick
the unprocessed node with the minimum distance from the source, this is where a min-heap (priority queue) or a
set is typically used for efficiency. For each picked node u, we update the distance to its neighbors v using the
formula: dist[v] = dist[u] + weight[u][v], but only if this new path offers a shorter distance than the current
known one. This process continues until all nodes have been processed.
5
A 3 B C
Shortest Path
Dijkstra's Algorithm
1
B D Find the shortest path from A to D
10
3
A 9 6 DBCA
2
5
C E
2
A B C D E
A 0
C 10 5
E 8 14 7
B 8 13
D 9
Shortest Path
Dijkstra's Algorithm
Limitation of Dijkstra's Algorithm: Since, we need to find the single source shortest path, we might initially think of
using Dijkstra's algorithm. However, Dijkstra is not suitable when the graph consists of negative edges. The reason is, it doesn't
revisit those nodes which have already been marked as visited. If a shorter path exists through a longer route with negative
edges, Dijkstra's algorithm will fail to handle it.
Shortest Path
Dijkstra's Algorithm
Shortest Path
Dijkstra's Algorithm
Shortest Path
Bellman-Ford Algorithm
Bellman-Ford is a single source shortest path algorithm. It effectively works in the cases of negative edges and is
able to detect negative cycles as well. It works on the principle of relaxation of the edges.
Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed
edge u-v, vertex u comes before v in the ordering.
Shortest Path
Topological Sort
Shortest Path
Topological Sort
For the given graph, following 2 different topological orderings are possible-
•A B C D E
•A B D C E
Shortest Path
Topological Sort
For the given graph, following 4 different topological orderings are possible:
•1 2 3 4 5 6
•1 2 3 4 6 5
•1 3 2 4 5 6
•1 3 2 4 6 5
Shortest Path
A* Search Algorithm
To approximate the shortest path in real-life situations, like- in maps, games where there can be many
hindrances. We can consider a 2D Grid having several obstacles and we start from a source cell to reach towards
a goal cell.
A* Search algorithm is one of the best and popular technique used in path-finding and graph traversals
Shortest Path
A* Search Algorithm
S Edge costs:
1 4 S-A=1, S-B=4, A-B=2, A-G=5, B-G=1
2
A B Heuristic h(n):
h(S)=7, h(A)=6, h(B)=2, h(G)=0
5 1
G
Neighbours:
• Neighbors: A (via B): g=6, h=6 → f=12 (worse,
At S: • A: g=1, h=6 → f=7 ignore)
g(S) = 0 G: g=5, h=0 → f=5
• B: g=4, h=2 → f=6 Path found: S → B → G
f(S) = g + h = 0 + 7 = 7
• Open = {A(f=7), B(f=6)} Total cost = 5
Open = {S} Open = {A(f=7), G(f=5)}
• Pick B (lowest f). Pick G (lowest f).
Shortest Path
Floyd – Warshall Algorithm
(Uses Dijkstra’s and Bellman Ford to compute. Less complex compare to Floyd-warshall)
H(a) = (s,a) = 0
H(b) = (s,b) = s, d, c, b = -1
H(c) = (s,c) = s, d, c = -5
H(d) = (s,d) = s, d = 0
H(e) = (s,e) = s, a, e = -4
Given an undirected graph, our task is to determine whether the graph contains a Hamiltonian cycle or not.
For example, in a graph with vertices A, B, C, and D, a Hamiltonian cycle could be A -> B -> C -> D -> A.
Given an undirected graph, our task is to determine whether the graph contains a Hamiltonian cycle or not.
For example, in a graph with vertices A, B, C, and D, a Hamiltonian cycle could be A -> B -> C -> D -> A.
Integer Programming
Integer programming (IP) is an optimization technique where some or all of the decision variables are restricted
to integer values, such as "whole numbers".
This mathematical approach is used to solve problems involving discrete quantities or yes/no decisions, aiming
to minimize or maximize an objective function subject to certain constraints.
An example of an Integer Programming (IP) problem is the Knapsack Problem. A hiker wants to fill their
knapsack with items to maximize the total value, subject to a weight capacity constraint. Each item has a specific
weight and a specific value. The hiker cannot take fractions of items; they must take the whole item or none at
all.
Knapsack Problem
A thief is robbing a store and can carry a maximal
weight of W into his knapsack. There are n items
available in the store, and the weight of the i-th item is
wi, and its profit is pi. What items should the thief
take?
Based on the nature of the items, Knapsack problems
are categorized as
Knapsack Problem
Objective of the Knapsack problem:
We have some objects, and every object has some
weights. We are provided with a bag, which is known as a
Knapsack
We have to fill the maximum objects in the bag according
to their weights and profit so that the profit we get is
maximum.
Constraints:
1 2 3
Profit 25 24 15
Weight 18 15 10
Knapsack Problem
Knapsack Problem
It’s a Dynamic Programming concept which falls in optimization category.
It derives its name from a scenario where, given a set of items with specific weights and assigned values, the goal is
to maximize the value in a knapsack while remaining within the weight constraint. Each item can only be selected
once, as we don’t have multiple quantities of any item.
Example
Let’s take the example of Mary, who wants to carry some fruits in her knapsack and maximize the profit she makes.
She should pick them such that she minimizes weight and maximizes value.
Here are the weights and profits associated with the different fruits:
Items: { Apple, Orange, Banana, Melon }
Weights: { 2, 3, 1, 4 }
Profits: { 4, 5, 3, 7 }
Knapsack Capacity: 5
Fruits Picked by Mary:
Banana and Melon is the best combination, as it gives us the maximum profit (10) and the total weight does not
exceed the knapsack’s capacity (5).
Knapsack Problem
Knapsack Problem
120 Because from there, changes started occurring. So 120 – 70 = 50, so next wherever it is happening, which means item 1
3-SAT
SAT stands for Boolean Satisfiability Problem.
Imagine you’re given a logical expression made up of variables (like A, B, C), and each variable can be either
true or false.
Your job is to figure out: Is there any way to assign true/false values to these variables so that the whole
expression becomes true?
Example:
(A OR NOT B) AND (B OR C)
3-SAT
3SAT is a special version of SAT where: The expression is written in Conjunctive Normal Form (CNF): a bunch
of clauses joined by ANDs.
Each clause has exactly three literals (variables or their negations) joined by ORs.
Your task is the same: Find a way to assign true/false values to all variables so that every clause is true.
4-Node Cliques
Check if any 4 nodes are fully connected:
•{1, 3, 4, 5} → All connected
•{2, 3, 4, 5} → All connected
So, 2 cliques of size 4.
5-Node Clique?
Check if all 5 nodes are mutually connected. Node 1 is not connected to node 5, so no 5-node clique.
Clique
Vertex Cover
A vertex cover is a set of vertices in a graph such that every edge in the graph is connected to at least one vertex
in this set. In simple terms, You’re choosing nodes so that every edge is “touched” by at least one of them.
Edges:
A—B—C
•A–B, A–D
| | |
•B–C, B–E
D—E—F
•C–F
|
•D–E
G
•E–F, E–G
Prim’s algorithm is a Greedy algorithm. This algorithm always starts with a single node and moves through
several adjacent nodes, in order to explore all of the connected edges along the way.
Minimum Spanning Tree (MST)
Minimum Spanning Tree (MST)
Minimum Spanning Tree (MST)
Minimum Spanning Tree (MST)
Undirected S-T Reachability
Undirected S-T Reachability, also known as Undirected S-T Connectivity (USTCON), is the problem of
determining if there is a path between two specified vertices, s and t, in an undirected graph. Standard
graph traversal algorithms like Breadth-First Search (BFS) and Depth-First Search (DFS) can solve this
problem, but they require more space than is ideal.
Pattern Matching
Pattern matching is the process of searching for a specific sequence of characters, called a “Pattern,” within
a larger piece of text or data.
Longest Common Subsequence (LCS)
The Longest Common Subsequence (LCS) problem is where you're asked to find the longest sequence of
characters present in two strings. Variations of this problem are commonly found in real-world applications
such as bioinformatics, natural language processing, and text comparison. This problem can be solved using
dynamic programming techniques, which involve breaking down the problem into smaller subproblems and
then solving them iteratively.
Example 1
Input: s1 = "abccba", s2 = "abddba" Output: "abba"
Example 2
Input: s1 = "zfadeg", s2 = "cdfsdg" Output: "fdg"
Example 3
Input: s1 = "abd", s2 = "badc" Output: "ad" (or "bd")
Longest Common Subsequence (LCS)
The following steps are followed for finding the longest common subsequence.
3. If the character corresponding to the current row and current column are matching, then fill the current cell by adding one to
the diagonal element. Point an arrow to the diagonal cell.
4. Else take the maximum value from the previous column and previous row element for filling the current cell. Point an arrow to
the cell with maximum value. If they are equal, point to any of them.
The problem lies in finding a minimal path passing from all vertices once.
For example the path Path1 {A, B, C, D, E, A} and the path Path2 {A, B, C, E, D, A} pass all the vertices but Path1
has a total length of 24 and Path2 has a total length of 31.
Travelling Salesman Problem
How the Hamiltonian Cycle and the Traveling Salesman Problem differ? The Hamiltonian Cycle problem is
to find out if there exists a tour that visits each city exactly once. Here, we know that the Hamiltonian Tour
exists (due to the graph being complete), and there are indeed many such tours. The problem is to find a
minimum weight Hamiltonian Cycle.
Maximum Flow
The Maximum Flow problem is about finding the maximum flow through a directed graph, from one place in the
graph to another.
More specifically, the flow comes from a source vertex s, and ends up in a sink vertex t, and each edge in the graph
is defined with a flow and a capacity, where the capacity is the maximum flow that edge can have.
SADT 2
SBCT 3
SBACT 3
Total 8
Recommendation System
E-commerce and retail companies are utilizing the power of data to boost sales with the help of
recommender systems implemented on their websites. The use cases of these systems have been increasing
consistently.
• Collection: Data collected can be explicit (ratings and comments on products) or implicit (page views,
order history, etc.).
• Storing: The type of data used to create recommendations can help you decide the kind of storage you
should use- NoSQL database, object storage, or standard SQL database.
• Analyzing: The recommender system finds items with similar user engagement data after analysis.
• Filtering: This is the last step where data gets filtered to access the relevant information required to
provide recommendations to the user. To enable this, you will need to choose an algorithm suiting the
recommendation system.
Recommendation System
Types of Recommendation System:
1. Collaborative Filtering: The collaborative filtering method is based on gathering and analyzing data on
user’s behavior. This includes the user’s online activities and predicting what they will like based on the
similarity with other users.
For example, if user A likes Apple, Banana, and Mango while user B likes Apple, Banana, and Jackfruit, they
have similar interests. So, it is highly likely that A would like Jackfruit and B would enjoy Mango. This is how
collaborative filtering takes place.
2. Content-Based Filtering
Content-based filtering methods are based on the description of a product and a profile of the user’s
preferred choices. In this recommendation system, products are described using keywords, and a user profile
is built to express the kind of item this user likes.
For instance, if a user likes to watch movies such as Iron Man, the recommender system recommends movies
of the superhero genre or films describing Tony Stark. The central assumption of content-based filtering is
that you will also like a similar item if you like a particular item.
Recommendation System
Types of Recommendation System:
By recommending such shows/movies that share similar traits with those rated highly by the user, Netflix uses
content-based filtering.
Job Scheduling
Job scheduling algorithm is applied to schedule the jobs on a single processor to maximize the profits. The
greedy approach of the job scheduling algorithm states that, Given n number of jobs with a starting time and
ending time, they need to be scheduled in such a way that maximum profit is received within the maximum
deadline.
Algorithm:
Step1 − Find the maximum deadline value from the input set of jobs.
Step2 − Once, the deadline is decided, arrange the jobs in descending order of their profits.
Step3 − Selects the jobs with highest profits, their time periods not exceeding the maximum deadline.
Step4 − The selected set of jobs are the output.
Job Scheduling
Problem: Solve the following job scheduling with deadlines problem using the greedy method. Number of
jobs N = 4. Profits associated with Jobs : (P1, P2, P3, P4) = (100, 10, 15, 27). Deadlines associated with jobs
(d1, d2, d3, d4) = (2, 1, 2, 1)
Given an array, a range sum query asks: “What is the sum of elements between index L and R?”
Naive Approach: Loop through the array for each query → O(n) per query Not ideal when you have
many queries.
Efficient Approach: Prefix Sum: Precompute cumulative sums so each query takes O(1) time.
Efficient Range Sum Queries
Let’s say we have:
arr = [2, 4, 6, 8, 10]
• Flowcharts are graphical representations of data, algorithms, or processes, providing a visual approach to
understanding code.
• Flowcharts illustrate step-by-step solutions to problems, making them useful for beginner programmers.
• Flowcharts help in debugging and troubleshooting issues.
• Flowchart consists of sequentially arranged boxes that depict the process flow.
• Since it visually represents an algorithm or workflow, it is easier to interpret and understand. However, to
create an effective Flowchart, certain standardised rules must be followed, ensuring clarity and
consistency across different professionals worldwide.
Flowcharts and Algorithms
Start / End Data or Input/Output
Process
Data Flow
[Link] Flowchart: This type of Flowchart shows all the activities that are involved in making a product. It
provides a pathway to analyze the product to be built. It is most commonly used in process engineering to
illustrate the relation between the major as well as minor components present in the product. It is used in
business product modelling to help understand employees about the project requirements and gain some insight
into the project.
[Link] Flowchart: It is used to analyze the data, specifically it helps in analyzing the structural details related to
the project. Using this Flowchart, one can easily understand the data inflow and outflow from the system. It is
most commonly used to manage data or to analyze information to and fro from the system.
[Link] Process Modelling Diagram: Using this Flowchart or diagram, one can analytically represent the
business process and help simplify the concepts needed to understand business activities and the flow of
information. This Flowchart illustrates the business process and models graphically which paves the way for
process improvement.
Flowcharts and Algorithms
Draw a Flowchart to find the greatest number among the 2 numbers.
Algorithm:
[Link]
[Link] 2 variables from user
[Link] check the condition If a > b, go to step 4, else go to step
5.
[Link] a is greater, go to step 6
[Link] b is greater
[Link]
Flowcharts and Algorithms
Draw a Flowchart to find the sum of 2 numbers.
Algorithm:
[Link]
[Link] A, B
3.C = A + B
[Link] C
[Link]