Lecture#1
Lecture#1
Hina Ali
Lecture # 1
Discussion Of Course outline
The Algorithm What is your take on it
• Designing an Algorithm ?
• For example, lets see you need to add 3 numbers
• A problem can be written in C, python etc
• Before writing code for problem a blueprint is required • prerequisites
this is termed as algorithm 1. The problem that is to be solved by this algorithm:
• we can devise multiple algorithm for one problem Add 3 numbers and print their sum.
Properties of Algorithm: 2. The constraints of the problem that must be
1. It should terminate after a finite time. considered while solving the problem:
2. It should produce at least one output. The numbers must contain only digits and no other
characters.
3. It should take zero or more input.
3. The input to be taken to solve the problem:
4. It should be deterministic means giving the same
output for the same input case. The three numbers to be added.
5. Every step in the algorithm must be effective i.e. 4. The output to be expected when the problem is solved:
every step should do some work. The sum of the three i.e., a single integer value.
A detailed explanation is provided on referenced page for 5. The solution to this problem, in the given constraints:
interested students
The solution consists of adding the 3 numbers. It can be
done with the help of the ‘+’ operator, or bit-wise, or any
• Source = https://www.geeksforgeeks.org/introduction-to-algorithms/
other method.
• Useful Link : https://aofa.cs.princeton.edu/80strings/
How to write Algorithm
Natural Language
Pseudocode
Flowchart
Natural Language
As described in designing algorithm example
Pseudocode
High-level description of an algorithm
More structured than English prose
Less detailed than a program
Preferred notation for describing algorithms
Constraints:
•2 <= nums.length <= 104
•-109 <= nums[i] <= 109
•-109 <= target <= 109
•Only one valid answer exists.
Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?
Notation for Analyzing Algorithm(Asymptotic
Notation, for comparing algorithms )
1)Big Oh (O) notation (Worst)
• Big O notation is used to explain the complexity of algorithm in form of algebraic terms.
• “f(n) is O(g(n))” iff for some constants c and n ₀,
0 ≤ f(n) ≤ cg(n) for all n > n₀
1. if f(n) = n2 +1 and g(n) = n3 for n₀>1 we can say f(n) = O(g(n))
2. If g(n) =c n2 can we say that f(n) = O(g(n))? If no, why if yes why? What will be value of c and n₀
3. What about O(n4), O(n4 +1 ), O(n5), O(n6)…….. ?
4. What if we create a graph of f(n)=n3 and g(n)=n4 ?