Algorithms
● An algorithm is a well-defined, step-by-step procedure or a set of rules designed to solve a
specific problem or perform a computation.
● Key characteristics of an algorithm:
- Finiteness: It must terminate after a finite number of steps.
- Definiteness: Each step must be precisely and unambiguously defined.
- Input: It must have zero or more well-defined inputs.
- Output: It must produce one or more well-defined outputs.
- Effectiveness: Each step must be sufficiently basic to be executable, at least in principle, by a person using pencil
and paper in a finite amount of time.This points to the idea that each instruction within an algorithm must be
unambiguous and achievable. There should be no "magic" steps or vague instructions.
- 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.
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.
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?
1. The problem that is to be solved by this algorithm i.e. clear problem definition.
Eg - Problem to add 3 numbers
2. The constraints (Rules) of the problem must be considered while solving the problem.
Eg - All the inputs should be numbers, no other characters are allowed
3. The input to be taken to solve the problem.
Eg - Those three numbers which need to be added together
4. The output is to be expected when the problem is solved.
Eg - Single output that is the sum of three numbers
Let’s Make Sum Chicken Curry
ALGORITHM Chicken Curry
PROBLEM - To cook a tasty chicken curry
CONSTRAINTS - Proper amount of salt, water and spices
INPUTS - Chicken, Onions, Tomatoes, Ginger-Garlic Paste, Oil/Ghee, Spices, Salt, Water, Fresh Coriander
OUTPUT - Cooked Chicken Curry
START/BEGIN
Step 1 : Prepare the ingredients
Step 2 : Heat the pan
Step 3 : Pour Oil and heat it
Step 4 : Add each ingredient to the pan
Step 5 : Stir the contents inside the pan for 10 or 15 minutes
Step 6 : Add water, close the lid
Step 7 : For each 15 Minutes open lid and check the chicken
If time == 30 Minutes
Turn of the stove and serve the chicken
If cooked
Turn of the stove and server the chicken
Else
Continue cooking
End For
END
PROBLEM - To add three numbers
CONSTRAINTS - Only numbers are allowed
INPUTS - Three numbers
OUTPUT - Sum of three numbers
START
Step 1 : Declare 3 integer variables num1, num2, and num3.
Step 2 : Take the three numbers, to be added, as inputs in variables num1, num2, and num3 respectively.
Step 3 : Declare an integer variable sum to store the resultant sum of the 3 numbers.
Step 4 : Add the 3 numbers and store the result in the variable sum.
Step : Print the value of the variable sum
END
PROBLEM - Find the largest number among three numbers
CONSTRAINTS - Only numbers are allowed
INPUTS - Three numbers
OUTPUT - Largest among three numbers
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a > b
If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
Algorithm as a technology
● They are the backbone of many technologies, from everyday applications like
navigation apps to complex systems like artificial intelligence and data science
● Efficiency - Algorithms determine how efficiently a computer performs a task,
impacting speed and resource usage
● Problem Solving - They provide a systematic way to solve complex problems in
various fields
● AI and Machine Learning - Algorithms are the foundation of AI and machine
learning, enabling systems to learn, adapt, and make decisions
● Everyday Applications - Algorithms are used in navigation apps, search engines,
social media feeds, and countless other applications
● Navigation Apps - Algorithms calculate the fastest routes, taking into account traffic
conditions.
● Search Engines - Algorithms determine the relevance of web pages for search queries.
● Social Media - Algorithms curate personalized feeds based on user preferences and
interactions.
Algorithm Analysis
● Time Complexity - Measures the amount of time an algorithm takes to run as a function
of the input size.
● Space Complexity - Measures the amount of memory space an algorithm requires to
operate, again as a function of input size
● Asymptotic Analysis - Focuses on the algorithm's behavior as the input size approaches
infinity. It helps compare algorithms with different time and space complexities and
identify their best and worst-case scenarios.
● Empirical Analysis - Involves implementing the algorithm and measuring its performance
on different inputs and hardware. This provides practical insights into how the algorithm
performs in real-world scenarios
● Analysis can be done on the worst-case, average-case, or best-case scenarios, providing a
more comprehensive understanding of the algorithm's behavior.
Best Case Scenario
● Definition - This scenario describes the most optimal situation where an algorithm
performs with the minimum number of operations or the fastest time to complete,
given favorable or optimal inputs
Worst Case Scenario
● Definition - This scenario represents the least favorable conditions, where the
algorithm takes the maximum amount of time or operations to complete due to
pessimal inputs or special circumstances
Average Case Scenario
● Definition - This scenario estimates the typical performance of an algorithm when
subjected to a variety of inputs, considering the distribution of these inputs. It
offers a more realistic representation of how an algorithm will behave under
normal conditions
Key Steps in Algorithm Design
1. Problem Definition - Clearly define what problem needs to be solved.
2. Input and Output Identification - Determine the data the algorithm will take as
input and the desired output.
3. Algorithm Design Techniques - Select appropriate techniques like divide and
conquer, greedy algorithms, or dynamic programming.
4. Algorithm Implementation - Translate the chosen algorithm into a specific
programming language.
5. Testing and Optimization - Thoroughly test the algorithm for correctness and
efficiency, and optimize it for performance
Flow charts
● 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
● Flowcharts have their use cases in various fields such as software development, business process
modeling, and engineering.
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, which in turn helps
other people to understand them easily.
● Flowcharts provide a universal visual language that can be understood by anyone across different
teams and helps reduce miscommunications.
● They are an optimal solution for documenting standard operating procedures, workflows, or business
processes. This 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.
Types of Flowcharts
There are many types of flowcharts, each is designed to represent different kinds of processes and
information. Some common types of flowcharts are:
● Process Flowchart - It represents the sequence of steps in a process. They are frequently used in
business process modeling, manufacturing, and project management
● Swimlane Flowchart - It organizes the process into different lanes, each representing a different
person or department and is used for illustrating how different teams or departments collaborate
within a process
● Workflow Diagram - It represents how tasks, documents, or information move through a system and
is commonly used in office processes or software development
● Data Flow Diagram (DFD) - It focuses on detailing the inputs, processes, and outputs. Used in
system design and analysis to model the flow of data within a system
● Decision Flowchart - It focuses on mapping out decision points within a process and the possible
outcomes of each decision. It is used in decision-making scenarios
Symbols used in Flowchart Designs
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
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 and it will connect flowchart in the
same page.
6. Off-Page Connector/Reference
Off-Page Connector is represented by a pentagon and it will connect flowchart within two or more pages.
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.
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