0% found this document useful (0 votes)
29 views10 pages

Focp Ut 1 Notes

The document provides a comprehensive overview of algorithms, defining them as finite sets of rules for problem-solving and highlighting their applications in fields like computer science, mathematics, and artificial intelligence. It outlines the characteristics, properties, types, advantages, and disadvantages of algorithms, as well as methods for designing and expressing them, including flowcharts and pseudocode. Additionally, it includes examples of algorithms for calculating factorials and summing digits, discussing their time and space complexities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views10 pages

Focp Ut 1 Notes

The document provides a comprehensive overview of algorithms, defining them as finite sets of rules for problem-solving and highlighting their applications in fields like computer science, mathematics, and artificial intelligence. It outlines the characteristics, properties, types, advantages, and disadvantages of algorithms, as well as methods for designing and expressing them, including flowcharts and pseudocode. Additionally, it includes examples of algorithms for calculating factorials and summing digits, discussing their time and space complexities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Unit 1

What is Algorithm | Introduction to Algorithms


Definition of Algorithm
The word Algorithm means ” A set of finite rules or instructions to be followed in calculations or other problem-
solving operations ”
Or
” A procedure for solving a mathematical problem in a finite number of steps that frequently involves recursive
operations”.
Therefore Algorithm refers to a sequence of finite steps to solve a particular problem.

Use of the Algorithms:


Algorithms play a crucial role in various fields and have many applications. Some of the key areas where algorithms
are used include:
1. Computer Science: Algorithms form the basis of computer programming and are used to solve problems
ranging from simple sorting and searching to complex tasks such as artificial intelligence and machine learning.
2. Mathematics: Algorithms are used to solve mathematical problems, such as finding the optimal solution to a
system of linear equations or finding the shortest path in a graph.
3. Operations Research: Algorithms are used to optimize and make decisions in fields such as transportation,
logistics, and resource allocation.
4. Artificial Intelligence: Algorithms are the foundation of artificial intelligence and machine learning, and are
used to develop intelligent systems that can perform tasks such as image recognition, natural language
processing, and decision-making.
5. Data Science: Algorithms are used to analyze, process, and extract insights from large amounts of data in fields
such as marketing, finance, and healthcare.
These are just a few examples of the many applications of algorithms. The use of algorithms is continually
expanding as new technologies and fields emerge, making it a vital component of modern society.

What is the need for algorithms?


1. Algorithms are necessary for solving complex problems efficiently and effectively.
2. They help to automate processes and make them more reliable, faster, and easier to perform.
3. Algorithms also enable computers to perform tasks that would be difficult or impossible for humans to do
manually.
4. They are used in various fields such as mathematics, computer science, engineering, finance, and many others
to optimize processes, analyze data, make predictions, and provide solutions to problems.
What are the Characteristics of an Algorithm?

 Clear and Unambiguous: The algorithm should be unambiguous. Each of its steps should be clear in all
aspects and must lead to only one meaning.
 Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs. It may or may not
take input.
 Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it should be well-
defined as well. It should produce at least 1 output.
 Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time.
 Feasible: The algorithm must be simple, generic, and practical, such that it can be executed with the available
resources. It must not contain some future technology or anything.
 Language Independent: The Algorithm designed must be language-independent, i.e. it must be just plain
instructions that can be implemented in any language, and yet the output will be the same, as expected.
 Input: An algorithm has zero or more inputs. Each that contains a fundamental operator must accept zero or
more inputs.
 Output: An algorithm produces at least one output. Every instruction that contains a fundamental operator
must accept zero or more inputs.
 Definiteness: All instructions in an algorithm must be unambiguous, precise, and easy to interpret. By referring
to any of the instructions in an algorithm one can clearly understand what is to be done. Every fundamental
operator in instruction must be defined without any ambiguity.
 Finiteness: An algorithm must terminate after a finite number of steps in all test cases. Every instruction which
contains a fundamental operator must be terminated within a finite amount of time. Infinite loops or recursive
functions without base conditions do not possess finiteness.
 Effectiveness: An algorithm must be developed by using very basic, simple, and feasible operations so that one
can trace it out by using just paper and pencil.

Properties of Algorithm:
 It should terminate after a finite time.
 It should produce at least one output.
 It should take zero or more input.
 It should be deterministic means giving the same output for the same input case.
 Every step in the algorithm must be effective i.e. every step should do some work.

Types of Algorithms:
There are several types of algorithms available. Some important algorithms are:
1. Brute Force Algorithm:
It is the simplest approach to a problem. A brute force algorithm is the first approach that comes to finding when we
see a problem.
2. Recursive Algorithm:
A recursive algorithm is based on recursion. In this case, a problem is broken into several sub-parts and called the
same function again and again.
3. Backtracking Algorithm:
The backtracking algorithm builds the solution by searching among all possible solutions. Using this algorithm, we
keep on building the solution following criteria. Whenever a solution fails we trace back to the failure point build on
the next solution and continue this process till we find the solution or all possible solutions are looked after.

4. Searching Algorithm:
Searching algorithms are the ones that are used for searching elements or groups of elements from a particular data
structure. They can be of different types based on their approach or the data structure in which the element should be
found.
5. Sorting Algorithm:
Sorting is arranging a group of data in a particular manner according to the requirement. The algorithms which help
in performing this function are called sorting algorithms. Generally sorting algorithms are used to sort groups of data
in an increasing or decreasing manner.
6. Hashing Algorithm:
Hashing algorithms work similarly to the searching algorithm. But they contain an index with a key ID. In hashing,
a key is assigned to specific data.
7. Divide and Conquer Algorithm:
This algorithm breaks a problem into sub-problems, solves a single sub-problem, and merges the solutions to get the
final solution. It consists of the following three steps:
 Divide
 Solve
 Combine
8. Greedy Algorithm:
In this type of algorithm, the solution is built part by part. The solution for the next part is built based on the
immediate benefit of the next part. The one solution that gives the most benefit will be chosen as the solution for the
next part.
9. Dynamic Programming Algorithm:
This algorithm uses the concept of using the already found solution to avoid repetitive calculation of the same part
of the problem. It divides the problem into smaller overlapping subproblems and solves them.
10. Randomized Algorithm:
In the randomized algorithm, we use a random number so it gives immediate benefit. The random number helps in
deciding the expected outcome.
To learn more about the types of algorithms refer to the article about “Types of Algorithms“.

Advantages of Algorithms:
 It is easy to understand.
 An algorithm is a step-wise representation of a solution to a given problem.
 In an Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer
to convert it into an actual program.

Disadvantages of Algorithms:
 Writing an algorithm takes a long time so it is time-consuming.
 Understanding complex logic through algorithms can be very difficult.
 Branching and Looping statements are difficult to show in Algorithms(imp).

How to Design an Algorithm?


To write an algorithm, the following things are needed as a pre-requisite:
1. The problem that is to be solved by this algorithm i.e. clear problem definition.
2. The constraints of the problem must be considered while solving the problem.
3. The input to be taken to solve the problem.
4. The output is to be expected when the problem is solved.
5. The solution to this problem is within the given constraints.
Then the algorithm is written with the help of the above parameters such that it solves the problem.

How to express an Algorithm?


1. Natural Language:- Here we express the Algorithm in the natural English language. It is too hard to
understand the algorithm from it.
2. Flowchart:- Here we express the Algorithm by making a graphical/pictorial representation of it. It is easier to
understand than Natural Language.
3. Pseudo Code:- Here we express the Algorithm in the form of annotations and informative text written in plain
English which is very much similar to the real code but as it has no syntax like any of the programming
languages, it can’t be compiled or interpreted by the computer. It is the best way to express an algorithm
because it can be understood by even a layman with some school-level knowledge.

Flow chart:
Flowcharts are the visual representations of an algorithm or a process. Flowcharts use symbols/shapes like
arrows, rectangles, and diamonds to properly explain the sequence of steps involved in the algorithm or process.

Why use Flowcharts?


Flowcharts are used due to the numerous amount of benefits they provide. Below are some of the important
reasons to use flowcharts:
 They provide clarity and simplification to the complex processes and algorithms
 Flowcharts provide a universal visual language and helps reduce miscommunications.
 It makes it easier to train new employees.
 Flowcharts help in increasing the visualization of the problem being solved which enables more informed
and data-driven choices.

1. Terminal/Terminator
The oval symbol indicates Start, Stop and Halt in a program’s logic flow. A pause/halt is generally used in a
program logic under some error conditions. Terminal is the first and last symbols in the flowchart.

2. Input/Output
A parallelogram denotes any function of input/output type. Program instructions that take input from input
devices and display output on output devices are indicated with parallelogram in a flowchart.

3. Action/Process
A box represents arithmetic instructions, specific action or operation that occurs as a part of the process. All
arithmetic processes such as adding, subtracting, multiplication and division are indicated by action/process
symbol.

4. Decision
Diamond symbol represents a decision point. Decision based operations such as yes/no question or true/false are
indicated by diamond in flowchart.
5. On-Page Connector/Reference
Whenever flowchart becomes complex or it spreads over more than one page, it is useful to use connectors to
avoid any confusions. connectors are used to indicate a jump from one part of the flowchart to another without
drawing long or complicated lines. On-Page Connector is represented by a small circle

6. Off-Page Connector/Reference
Whenever flowchart becomes complex or it spreads over more than one page, it is useful to use connectors to
avoid any confusions. connectors are used to indicate a jump from one part of the flowchart to another without
drawing long or complicated lines. Off-Page Connector is represented by a pentagon.

7. Flow lines
Flow lines indicate the exact sequence in which instructions are executed. Arrows represent the direction of flow
of control and relationship among different symbols of flowchart.

Rules For Creating a Flowchart


A flowchart is a graphical representation of an algorithm. It should follow some rules while creating a flowchart
 Rule 1: Flowchart opening statement must be ‘start’ keyword.
 Rule 2: Flowchart ending statement must be ‘end’ keyword.
 Rule 3: All symbols in the flowchart must be connected with an arrow line.
 Rule 4: Each decision point should have two or more distinct outcomes.
 Rule 5: Flow should generally move from top to bottom or left to right.

Example of a Flowchart
Draw a flowchart to input two numbers from the user and display the largest of two numbers.

Advantages of using a Flowchart


 Flowcharts are a better way of communicating the logic of the system.
 Flowcharts act as a guide for blueprint during program designed.
 Flowcharts help in debugging process.
 With the help of flowcharts programs can be easily analyzed.
 They provide better documentation.
 Flowcharts serve as a good proper documentation.

Disadvantages of using a Flowchart


 It is difficult to draw flowcharts for large and complex programs.
 There is no standard to determine the amount of detail.
 It is very difficult to modify the Flowchart.
 Making a flowchart is costly.
 If changes are done in software, then the flowchart must be redrawn

Write an algorithm (in pseudocode) to calculate the factorial of a number. Discuss its time complexity and
space complexity. How can the algorithm be optimized to reduce space usage?

1 . Algorithm: Calculate Factorial of a Number


Pseudocode:
Algorithm Factorial(n)
Input: n (a non-negative integer)
Output: Factorial of n

if n == 0 or n == 1 then
return 1
else
result ← 1
for i ← 2 to n do
result ← result * i
end for
return result
end if
End Algorithm

Time Complexity:
The time complexity of this algorithm is O(n) because the loop iterates from 2 to n, performing one multiplication
per iteration.
Space Complexity:
The space complexity is O(1) because the algorithm uses a constant amount of space for variables (result and i),
regardless of the input size.

Optimization to Reduce Space Usage:


The above algorithm is already space-efficient (O(1)) for iterative implementation. However, for comparison:
1. Recursive Implementation: A recursive factorial algorithm has a space complexity of O(n) due to the
function call stack required for nn recursive calls. For example:
Algorithm FactorialRecursive(n)
if n == 0 or n == 1 then
return 1
else
return n * FactorialRecursive(n-1)
end if
End Algorithm
o This uses O(n) space for recursion, which can cause a stack overflow for large n.

2. Iterative Implementation: Switching to an iterative approach (as provided above) reduces space
complexity to O(1), as it avoids recursion and uses only a single variable to store intermediate results.

2. Algorithm to Add Digits of a Number


Pseudocode:
Algorithm SumOfDigits(n)
Input: n (a non-negative integer)
Output: Sum of all digits in n

sum ← 0
while n > 0 do
digit ← n mod 10 // Get the last digit
sum ← sum + digit // Add the digit to the sum
n ← n div 10 // Remove the last digit from n
end while
return sum
End Algorithm

Example for n=345n = 345:


1. Start: sum=0sum = 0, n=345n = 345
2. Iteration 1: digit=345mod 10=5digit = 345 \mod 10 = 5, sum=0+5=5sum = 0 + 5 = 5, n=345÷10=34n =
345 \div 10 = 34
3. Iteration 2: digit=34mod 10=4digit = 34 \mod 10 = 4, sum=5+4=9sum = 5 + 4 = 9, n=34÷10=3n = 34 \div
10 = 3
4. Iteration 3: digit=3mod 10=3digit = 3 \mod 10 = 3, sum=9+3=12sum = 9 + 3 = 12, n=3÷10=0n = 3 \div 10
=0
5. Return sum=12sum = 12

Time Complexity Analysis:


The time complexity is O(d), where dd is the number of digits in nn. This is because the algorithm processes one
digit per iteration, and the number of digits is proportional to log⁡10(n)\log_{10}(n).
 For example:
o A number with 3 digits (e.g., 345) requires 3 iterations.
o A number with 10 digits (e.g., 12345678901234567890) requires 10 iterations.
If the number has more digits, the number of iterations increases linearly with the number of digits, but it is still
efficient because d=⌈log⁡10(n)⌉d = \lceil \log_{10}(n) \rceil.
Space Complexity Analysis:
The space complexity is O(1), as the algorithm uses a constant amount of space regardless of the size of the input.
The variables sum, digit, and n are updated in-place, so no additional space is required.

Conclusion:
 Time Complexity: O(d), where dd is the number of digits.
 Space Complexity: O(1).
The time complexity increases linearly with the number of digits dd, but the space complexity remains constant,
making the algorithm highly efficient in terms of memory usage.

Case study:

1. Factorial of a Number
 Input: Take a positive integer 'n' as input.
 Initialize: Set a variable 'factorial' to 1.
 Loop: Iterate through numbers from 1 to 'n'.
 Multiply: In each iteration, multiply the current 'factorial' value by the current number.
 Output: After the loop, the final value of 'factorial' is the factorial of 'n'.

Algorithm Steps:
1. Start
2. Read input: Get the positive integer 'n' from the user.
3. Initialize: Set 'factorial' variable to 1.
4. Loop:
For 'i' from 1 to 'n':
'factorial' = 'factorial' * 'i'
5. Print result: Display the calculated 'factorial' value.
6. End

2. A Fibonacci sequence algorithm can be written as:


Algorithm:
 Input: An integer 'n' representing the number of terms to generate in the Fibonacci sequence.
 Output: A list containing the first 'n' Fibonacci numbers.
Steps:
1. Initialize variables:
 Set a to 0 (first Fibonacci number).
 Set b to 1 (second Fibonacci number).
 Create an empty list result to store the sequence.
2. Generate the sequence:
 Append a to the result list.
 Loop for n-1 iterations:
 Calculate the next Fibonacci number c by adding a and b (c = a + b).
 Append c to the result list.
 Update a to the value of b.
 Update b to the value of c.
3. Return the result:
 Return the result list containing the Fibonacci sequence.

3. Sorting in a Array
Problem Statement – How to arrange array in ascending order
Sorting is the process of arranging elements in a list or array in a specific order, typically in ascending or descending
order. Sorting is a fundamental problem in computer science and has many applications in areas such as searching,
data compression, and data analysis.
There are many sorting algorithms that can be used to sort an array. Some of the most popular algorithms include:
1. Bubble Sort: This is a simple sorting algorithm that repeatedly steps through the list, compares adjacent
elements, and swaps them if they are in the wrong order.
2. Selection Sort: This algorithm sorts an array by repeatedly finding the minimum element from the
unsorted part of the array and putting it at the beginning.
3. Insertion Sort: This algorithm builds the final sorted array one item at a time, by inserting each item in the
correct position in the array.
4. Merge Sort: This algorithm divides the array into two halves, sorts each half separately, and then merges
the two halves together to form a sorted array.
5. Quick Sort: This algorithm picks an element as a pivot and partitions the array around the pivot, such that
elements smaller than the pivot are on one side and elements larger than the pivot are on the other side. It
then recursively sorts the two sub-arrays.

Algorithm :
 Take the size of the array from the user.
 Declare an array of given input size.
 Take the input of all elements of the array.
 Now run a for loop from 0 to size-1.
 And for every element check it from all the next elements to it. If the element is greater than swap that
number.
 In this way the array will get sorted in ascending order.

Searching an array
What is an Array?
A data structure called an array holds a fixed-length series of identical-type items. It is frequently used to store and
manipulate data collections because indexing enables efficient access.
Ex: intnumbers[] = {10, 20, 30, 40, 50};
Searching an Element in an Array
A typical operation in computer programming is looking for a particular element in an array. The efficiency of your
code may be greatly improved by using efficient searching algorithms whether you are searching for the existence of
a certain value locating the index of an element, or verifying if an element exists. The many methods for searching
for elements in an array using the C programming language will be discussed in this article.
There are mainly two ways to Search an Element in an Array:
1. Linear Search
A straightforward search strategy used to locate a given element in an array or list is called linear search, sometimes
referred to as sequential search. It operates by comparing each array member to the target value to find
a match or traverse the full array iteratively.
The fundamental steps in linear search are as follows:
1. Start with the array's topmost elements.
2. The target value should be compared to the current element.
3. The search is successful if the current element matches the requested value, and then the algorithm can
return the element's index or any other desired output.
4. Go to the following element in the array if the current element does not match the desired value.
5. Until a match is made or the end of the array is reached, repeat steps 2-4.

Algorithm to check anagram


Read two strings
Convert two strings to lowercase
Sort the characters in a string
Check the length of a strings
Compare two strings
If the condition is true then anagram string
Otherwise not an anagram string
 Stop
Example Walkthrough
Input:
 str1 = "listen"
 str2 = "silent"
1. Check lengths:
o length("listen")=6
o length("silent")=6 → Proceed.
2. Sort characters:
o Sort("listen") → "eilnst"
o Sort("silent") → "eilnst"
3. Compare sorted strings:
o "eilnst" = "eilnst" → Return True.
Output:
 True (The strings are anagrams).

You might also like