0% found this document useful (0 votes)
13 views12 pages

02 EKH Algorithm Flowchart Complexity Lecture

The document provides an overview of algorithms, their properties, types, and the importance of flowcharts in visualizing algorithmic logic. It also discusses pseudocode as a tool for planning algorithms and introduces algorithmic complexity, including time and space complexity with examples. Additionally, it highlights the trade-offs between time and space efficiency in algorithm selection.

Uploaded by

jahinalam45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views12 pages

02 EKH Algorithm Flowchart Complexity Lecture

The document provides an overview of algorithms, their properties, types, and the importance of flowcharts in visualizing algorithmic logic. It also discusses pseudocode as a tool for planning algorithms and introduces algorithmic complexity, including time and space complexity with examples. Additionally, it highlights the trade-offs between time and space efficiency in algorithm selection.

Uploaded by

jahinalam45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Algorithm

Lecture: Introduction to Algorithms

What is an Algorithm?

• Definition: An algorithm is a step-by-step set of instructions to solve a problem or perform


a task.
• Example: A recipe to bake a cake, or instructions to solve a math problem.

Key Properties of Algorithms

1. Input: Zero or more inputs are externally supplied.


2. Output: At least one output is produced.
3. Definiteness: Each step is precisely defined.
4. Finiteness: The algorithm must end after a finite number of steps.
5. Effectiveness: Each step must be basic enough to be carried out.

Simple Example

Problem: Add two numbers.


Algorithm:
1. Start
2. Read number A
3. Read number B
4. Add A and B, store in SUM
5. Print SUM
6. End

Types of Algorithms

1. Brute Force – Try all possible solutions


2. Greedy – Make the best choice at each step
3. Divide and Conquer – Divide the problem, solve parts, combine
4. Dynamic Programming – Store results of subproblems to avoid re-computation
5. Backtracking – Try partial solutions and backtrack if needed
6. Recursive Algorithms – Call the function within itself
Flowchart
The first design of flowchart goes back to 1945 which was designed by John Von Neumann.
Unlike an algorithm, Flowchart uses different symbols to design a solution to a problem. It is
another commonly used programming tool.
In general, a flowchart is a diagram that uses different symbols to visually present the flow of data.
By looking at a flow chart one can understand the operations and sequence of operations performed
in a system. This is why flowchart is often considered as a blueprint of a design used for solving a
specific problem.
A flowchart is defined as a symbolic or a graphical representation of an algorithm that uses
different standard symbols.
Flowchart Symbols:

Guidelines for drawing a flowchart.


1. The Title for every flowchart is compulsory.
2. There must be START and END point for every flowchart.
3. The symbols used in flowchart should have only one entry point on the top. The exit point
for symbols (except for decision/diamond symbol) is on the button.
4. There should be two exit points for decision symbol; exit points can be on the bottom and
one side or on the sides.
5. The flow of flowchart is generally from top to bottom. But in some cases, it can also flow
to upward direction
6. The direction of the flow of control should be indicated by arrowheads.
7. The operations for every step should be written inside the symbol.
8. The language used in flowchart should be simple so that it can be easily understood.
9. The flowlines that show the direction of flow of flowchart must not cross each other.
10. While connecting different pages of the same flowchart, Connectors must be used.

Some examples of algorithm and flowchart.


Example1: To calculate the area of a circle
Algorithm:
Step1: Start
Step2: Input radius of the circle say r
Step3: Use the formula πr2 and store result in a variable AREA
Step4: Print AREA
Step5: Stop

Flowchart:
Example 2: Design an algorithm and flowchart to input fifty numbers and calculate their
sum.
Algorithm:
Step1: Start

Step2: Initialize the count variable to zero

Step3: Initialize the sum variable to zero

Step4: Read a number say x

Step 5: Add 1 to the number in the count variable

Step6: Add the number x to the sum variable.

Step7: Is the count variable in the memory greater than 50?

If yes, display the sum: go to step 8.

If No, Repeat from step 4


Step8: Stop

Flowchart
Algorithm vs Flowchart

Feature Algorithm Flowchart

A step-by-step textual procedure to A graphical diagram representing


Definition
solve a problem the steps of an algorithm

Written in plain language or Drawn using standard symbols and


Format
pseudocode arrows

Purpose To describe the logic clearly To visualize the logic and flow

Ease of Easier for computers/programmers to Easier for humans to understand


Understanding interpret visually

For presenting logic, training, or


Use Case For planning code or logic
debugging

Flowchart software (e.g.,


Example Tools Paper, text editor, IDE
Lucidchart, draw.io, Visio)

Why Flowcharts Are Important

1. Simplifies Understanding

• Visual representation is easier to grasp than lines of code or text.


• Ideal for beginners learning logic or programming.

2. Improves Communication

• Helps teams, clients, or non-programmers understand system logic.


• Reduces misunderstandings by using standardized symbols.

3. Aids in Problem-Solving

• Helps break down complex problems into clear, manageable steps.


• Makes it easier to identify logical errors or inefficiencies.

4. Helps in Debugging

• You can trace the flow to find where a problem occurs.


• Easier to spot missing steps or incorrect logic.

5. Supports Planning Before Coding

• Used in the design phase of software development.


• Saves time and effort by planning the logic first.

6. Useful in Teaching and Learning

• Excellent tool for explaining:


o Conditional logic (if/else)
o Loops (while, for)
o Input/output flow
o Recursion
7. Documentation Tool

• Serves as a visual document of how a system or algorithm works.


• Useful for onboarding new team members or for future updates.

Real-World Examples

Area How Flowcharts Help

Software Development Visualize app logic before coding

Business Processes Map customer support or order workflows

Education Teach algorithmic thinking and programming

Engineering Design control systems, automation logic

Data Analysis Show ETL (Extract, Transform, Load) workflows

Summary: Benefits of Flowcharts

✔ Easy to understand
✔ Visualizes logic clearly
✔ Reduces errors
✔ Aids in debugging and documentation
✔ Improves collaboration and communication
Pseudocode: A Beginner’s Guide

What is Pseudocode?

Pseudocode is a plain-language, code-like description of an algorithm. It’s not written in any


specific programming language — it simply outlines what the program should do, step by step, in
a format that’s easy to read and translate into real code.
Think of it as the bridge between your logic and actual code.

Why Use Pseudocode?

• Helps plan your program before coding


• Focuses on logic, not syntax
• Makes it easier to communicate ideas
• Useful for beginners to understand algorithms
• Helps avoid bugs by thinking through steps first

Basic Rules of Pseudocode

There’s no strict standard, but here are common conventions:


• Use capital letters for keywords: IF, THEN, ELSE, WHILE, FOR, END
• Use indentation to show structure
• Use simple English to describe operations
• Don’t worry about exact syntax or data types

Common Pseudocode Keywords

Keyword Purpose

START / END Marks the beginning and end of the algorithm

INPUT Read data from the user

OUTPUT Display data to the user

IF, THEN, ELSE Conditional logic

FOR, WHILE Loops

SET, ← Assign value to a variable

CALL Run a subroutine or function


Examples of Pseudocode

1. Find the Sum of Two Numbers

START
INPUT A
INPUT B
SET SUM ← A + B
OUTPUT SUM
END

2. Check if a Number is Even or Odd

START
INPUT number
IF number % 2 = 0 THEN
OUTPUT "Even"
ELSE
OUTPUT "Odd"
END IF
END

3. Find the Largest of Three Numbers

START
INPUT A, B, C
IF A > B AND A > C THEN
OUTPUT A
ELSE IF B > C THEN
OUTPUT B
ELSE
OUTPUT C
END IF
END

4. Loop Example – Print Numbers 1 to 5

START
FOR i ← 1 TO 5 DO
OUTPUT i
END FOR
END

5. Factorial of a Number (Using Loop)

START
INPUT N
SET FACT ← 1
FOR i ← 1 TO N DO
SET FACT ← FACT * i
END FOR
OUTPUT FACT
END

Pseudocode vs Flowchart

Aspect Pseudocode Flowchart

Text or Visual Text Visual

Best For Logical design before coding Teaching, presentations

Complex Logic Easier to express in detail Harder to visualize complex logic

Looks Like Code? Yes, closer to programming No

Editable Quickly? Yes Not as easily

When to Use Which?

Situation Use Pseudocode Use Flowchart

Explaining to programmers

Teaching beginners

Planning code structure

Showing a quick overview of logic

Documenting system design


Lecture: Complexity of Algorithms

1. What is Algorithmic Complexity?

Algorithmic complexity refers to the resources an algorithm consumes, typically:


• Time: How long the algorithm takes to run
• Space: How much memory (RAM) it uses
This helps you:
• Compare different algorithms
• Choose the most efficient one for large inputs
• Understand how your code will scale

2. Time Complexity

What is Time Complexity?

It describes how the runtime of an algorithm increases with the size of the input, usually
represented by n.
We use Big O notation to describe the upper bound (worst-case) behavior.

Common Time Complexities

Complexity Name Example Algorithm Description

O(1) Constant Accessing array element Same time regardless of input size

O(log n) Logarithmic Binary Search Halves the problem at each step

O(n) Linear Linear Search Time grows directly with input size

O(n log n) Linearithmic Merge Sort, Quick Sort Efficient sorting

O(n²) Quadratic Bubble Sort Nested loops over the input

O(2ⁿ) Exponential Recursive Fibonacci Time doubles with each additional input

O(n!) Factorial Solving permutations Extremely slow growth

Examples

O(1) – Constant Time

def get_first_element(arr):
return arr[0]
→ Always one step, regardless of array size.
O(n) – Linear Time

def find_max(arr):
max_val = arr[0]
for val in arr:
if val > max_val:
max_val = val
return max_val
→ Must check each item once → grows with n.

O(n²) – Quadratic Time

def bubble_sort(arr):
for I in range(len(arr)):
for j in range(len(arr) – I – 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
→ Each item compared with each other → n × n.

How to Analyze Time Complexity

1. Count the number of basic operations (e.g., additions, comparisons).


2. Focus on the most dominant term (ignore constants and low-order terms).
o For 3n + 2, complexity is O(n)
o For 5n² + 4n + 7, complexity is O(n²)

3. Space Complexity

What is Space Complexity?

It measures how much additional memory an algorithm needs as the input size increases.

Example

def sum_array(arr):
total = 0
for num in arr:
total += num
return total
→ Uses constant space: one variable (total) → O(1)
When Space Matters

• Recursion: Each recursive call adds to the call stack


• Storing auxiliary structures: Hash tables, lists, matrices
• Dynamic Programming: Memoization uses extra space
Example – Fibonacci with memoization (O(n) space)
def fib(n):
memo = [0, 1]
for i in range(2, n+1):
memo.append(memo[i-1] + memo[i-2])
return memo[n]

4. Best, Worst, and Average Case

• Best Case: Minimum time (e.g., searching and finding on the first try)
• Worst Case: Maximum time (used for Big O)
• Average Case: Expected time for random inputs
Binary Search Example:

Case Time Complexity

Best O(1)

Worst O(log n)

Average O(log n)

5. Tradeoffs

Sometimes:
• Faster time needs more space (and vice versa)
• Choosing between algorithm efficiency and implementation simplicity

Algorithm Time Complexity Space Complexity

Merge Sort O(n log n) O(n)

Quick Sort O(n log n) O(log n)

Bubble Sort O(n²) O(1)

Dynamic Fib O(n) O(n)

Recursive Fib O(2ⁿ) O(n)

You might also like