SlideShare a Scribd company logo
Fundamentals of Algorithms
Dr. AMIT KUMAR @ JUET
Prerequisite
 Introduction to programming in ‘C’ and data
structure (with sufficient familiarity in the
usage of array, pointers, recursion, searching,
sorting, tree etc…. )
Dr. AMIT KUMAR @ JUET
Purpose of this subject
A rigorous introduction to the design and analysis
of algorithms
 Not a lab or programming course
 Not a math course, either
Dr. AMIT KUMAR @ JUET
Evaluation scheme: (Fundamental of Algorithms Theory)
 Test – I 15
 Test – II 25
 Test- III 35
 Assignments 5
 Quiz 5
 Tutorial/Problem Solving 10
 Attendance 05
Total 100
Dr. AMIT KUMAR @ JUET
Course Content
 Course Content:
 Analysis of algorithm: Asymptotic Notation, Sorting and merging
Algorithm
 Tree and related data Structure: Heap, Priority Queues, B-Tree, AVL,
Splay Tree, Red-Black Tree, Threaded Tree
 Files: Classification, Record Organization, Retrieval System, External
Sorting
 Set, Dictionary: Design, Analysis, integration and applications
 Fundamental techniques: Divide and Conquer method, Dynamic
Programming, Introduction to Greedy Method
 Hashing: technique, collision resolution and analysis
 Text Processing: String operation, pattern matching algorithm, tries,
text compression, text similarity testing.
Dr. AMIT KUMAR @ JUET
BOOKS and References:
 Textbook: Introduction to Algorithms, Cormen,
Leiserson, Rivest, Stein
 Aho, Hopcraft, Ullman: Data Structure and
Algorithms
 Kruse, Tonso, Leung: Data Structure and
program Design in C
 Sahni: Data structure and algorithm and
application in C++
 Weiss: Data Structure and Algorithm analysis in
C/C++
Dr. AMIT KUMAR @ JUET
Design and Analysis of Algorithms
• Analysis: predict the cost of an algorithm in terms of resources
and performance
• Design: design algorithms which minimize the cost
Dr. AMIT KUMAR @ JUET
Dr. AMIT KUMAR @ JUET
Algorithms
An algorithm is a finite sequence of step by step,
discrete, unambiguous instructions for solving a
particular problem
 Has input data, and is expected to produce output data
 Each instruction can be carried out in a finite amount of
time in a deterministic way
Dr. AMIT KUMAR @ JUET
Algorithms
 In simple terms, an algorithm is a series of instructions
to solve a problem (complete a task).
 Problems can be in any form
 Business
 Get a part from Vancouver to Ottawa by morning
 Allocate manpower to maximize profit
 Life
 I am hungry. How do I order pizza?
 Explain how to tie shoelaces to a five year old child
Dr. AMIT KUMAR @ JUET
Algorithms (Criteria)
1. Input: Zero or more quantities are externally
supplied.
2. Output: At least one quantity is produced.
3. Definiteness: Each instruction is clear and
unambiguous.
4. Finiteness: If we trace out the instructions of an
algorithm, then for all cases, the algorithm
terminates after a finite number of steps.
5. Effectiveness: Every instruction must be very basic
so that it can be carried out, in principle, by a
person using only pencil and paper. It is not enough
that each operation be definite as in criteria 3, it
also must be feasible.
Dr. AMIT KUMAR @ JUET
Algorithms
 We deal with data processing problems
 Translated to programs that can be run on a computer
 Since we can only input, store, process & output data
on a computer, the instructions in our algorithms will
be limited to these functions.
Dr. AMIT KUMAR @ JUET
Algorithm Description
 Understand the problem before solving it
 Identify & name each Input/Givens
 Identify & name each Output/Results
 Assign a name to our algorithm (Name)
 Combine the previous 3 pieces of information into a
formal statement (Definition)
 Results := Name (Givens)
Dr. AMIT KUMAR @ JUET
Method
 Once we have prepared the Algorithm Description, we
need to solve the problem
 We develop a series of instructions (limited to those
described previously) that, when executed, will
compute the desired Results from the Givens
(Method)
Dr. AMIT KUMAR @ JUET
Writing of Algorithm
Algorithm is basically a sequence of instruction written
in simple English type (pseudo) language. The
algorithms is basically divided into two parts.
 Algorithm heading: It consist of name of algorithm,
problem description, input and output.
 Algorithm body: It consists of logical body of the
algorithm.
Dr. AMIT KUMAR @ JUET
Assignment command
Syntax
X = 5Y + 16
On the left side of =, we put the name of a variable and on the right side
we put a value or an expression.
Each variable refers to a unique location in computer memory that contains
a value.
Interpretation
An assignement is executed in two steps :
1-evaluation of the expression found on the right side.
2-setting the returned value in the memory cell corresponding to variable.
Example
Let SideSize=15
Let Area=SideSizeSideSize
Dr. AMIT KUMAR @ JUET
Assignment in computer science and
equality in mathematics
a) The following instructions are the same in mathematics.
A=B B=A
not in computer science.
Let A=B is different from Let B=A
b) In mathematics we work with relations.
A relation B=A+1 means that it is true all the time
In computer science, we work with assignments. We can have:
A=5
B=A+1
A=2
The relation B=A+1 is true only after the second instruction
and before the third one.
Dr. AMIT KUMAR @ JUET
c) The instruction A=A+3 is false in mathematics.
In computer science Let A=A+3 means: the new value of A is
equal to the old one plus three.
d) The instruction A+5=3 is allowed in mathematics (it is an
equation).
Let A+5=3 has no meaning in computer science (the left side
must be a variable).
Assignment in computer science and
equality in mathematics
Dr. AMIT KUMAR @ JUET
Input command
Syntax
Get variable
The variable must be from Givens
Interpretation
Here the user must give a value. The given value is
assigned to the variable.
Example
Get Size_Side
Dr. AMIT KUMAR @ JUET
Output command
Syntax
Give variable
The variable must be from Results
Interpretation
The value of the variable is displayed.
Example
Give Area
Dr. AMIT KUMAR @ JUET
Algorithm 1.1
 Write an algorithm to find the sum of three given
numbers
 NAME: SUM3
 GIVENS: N1, N2, N3
 RESULTS: Total
 DEFINITION: Total := SUM3(N1, N2, N3)
 -------------------------
 METHOD:
Get N1
Get N2
Get N3
Let Total = N1 + N2 + N3
Give Total
Dr. AMIT KUMAR @ JUET
Algorithm 1.2
 Write an algorithm to find the result of a division
operation for the given two numbers X and Y
 NAME: Division
 GIVENS: X, Y
 RESULTS: Quotient
 DEFINITION: Quotient := Division(X, Y)
 -------------------------
 METHOD:
Get X
Get Y
Let Quotient = X/Y
Give Quotient
Dr. AMIT KUMAR @ JUET
Algorithm 1.3
 Write an algorithm to find the sum and product of the
two given numbers
 NAME: SumTimes
 GIVENS:Num1, Num2
 RESULTS: Total, Product
 DEFINITION: Total & Product := SumTimes(Num1, Num2)
 -------------------------
 METHOD:
Get Num1
Get Num2
Let Total = Num1 + Num2
Let Product = Num1 * Num2
Give Total
Give Product
Dr. AMIT KUMAR @ JUET
Algorithm 1.4
 Find the sum and average of three given numbers
 NAME:AVG3
 GIVENS:Num1, Num2, Num3
 RESULTS:Sum , Average
 DEFINITION:Sum & Average := AVG3(Num1, Num2, Num3)
 -------------------------
 METHOD:
Get Num1
Get Num2
Get Num3
Let Sum = Num1 + Num2 + Num3
Let Average = Sum /3
Give Sum
Give Average
Dr. AMIT KUMAR @ JUET
Variables
 Observe that we have used names for the data
items in our Givens and Results
 Num1, Num2, Num3, Sum, Average in Algorithm 1.4
 Each name refers to a unique location in computer
memory (one or more adjacent bytes) that contains
a value
 Since that value can change as the instructions in
our algorithm are executed, we call each data item a
variable
Dr. AMIT KUMAR @ JUET
Variables
 In our algorithm, when we use a variable name, we are
referring to the value stored in memory for that data
item
 Later in this lecture we will learn more about how to
define variables
Dr. AMIT KUMAR @ JUET
Intermediates
 Occasionally, in an algorithm, we need to have a
variable (in addition to those representing Givens or
Results) to store a value temporarily
 These are intermediate variables and we identify them
in the Algorithm Description as Intermediates
Dr. AMIT KUMAR @ JUET
Algorithm 1.5
 Given 3 assignment marks (out of 50, 20, 70), find the
average (calculated as a mark out of 100)
 General Concept
 How does one figure out the percentage of several
marks?
 Add them all up
 Divide by the maximum possible mark (50+20+70)
 Multiply by 100
Dr. AMIT KUMAR @ JUET
Algorithm 1.5
 Given 3 assignment marks (out of 50, 20, 70), find the average,
calculated as a mark out of 100
 NAME: CalcMark
 GIVENS: A1, A2, A3
 RESULTS: Mark
 INTERMEDIATES: Total, MaxMark (Constant)
 DEFINITION: Mark := CalcMark(A1, A2, A3)
 -------------------------
 METHOD:
Set MaxMark = 140 (Constant)
Get A1
Get A2
Get A3
Let Total = A1 + A2 + A3
Let Mark = Total/MaxMark * 100
Give Mark
Dr. AMIT KUMAR @ JUET
Algorithm 1.6
 Given a two digit number, find the
sum of its digits
 General Concept
 How can we break apart a number?
 41 = 4 Tens and 1 Ones
 so for the number 41, we want 4 + 1 = 5
 Use integer division
 DIV returns the integer part of a
division
 MOD returns the remainder of a
division
41  10 = 4
41 MOD 10 = 1
Dr. AMIT KUMAR @ JUET
Algorithm 1.6
 Given a two digit number, find the sum of its digits
 NAME: SumDig
 GIVENS: N
 RESULTS: Sum
 INTERMEDIATES: Tens, Ones
 DEFINITION: Sum := SumDig(N)
 -------------------------
 METHOD:
Get N
Let Tens = N div10
Let Ones = N mod 10
Let Sum = Tens + Ones
Give Sum
Dr. AMIT KUMAR @ JUET
Algorithm 1.7
 Write an algorithm which swaps the values of two
numbers
 Example 1
 Two car family. The wrong car is at the end of the driveway
 Move first car out on to the street
 Move second car out on to the street
 Move first car back in
 Move second car back in
 Example 2
 You are looking after a 3 year old. He wants milk and juice. You put the
milk in the blue glass and the juice in the red glass. The child is screaming
that you did it wrong.
 Get a third glass (white)
 Pour the milk from the blue glass to the white glass
 Pour the juice from the red glass to the blue glass
 Pour the milk from the white glass to the red glass
Dr. AMIT KUMAR @ JUET
Algorithm 1.7
 Write an algorithm which swaps the values of two
numbers
 NAME: Swap
 GIVENS: X, Y
 RESULTS: X, Y
 INTERMEDIATES: Temp
 DEFINITION: Swap (X, Y)
 -------------------------
 METHOD:
Get X
Get Y
Let Temp = X
Let X = Y
Let Y = Temp
Give X
Give Y
Dr. AMIT KUMAR @ JUET
Algorithm 1.8
 Write an algorithm which adds the given two numbers
(X and Y) and returns the sum in the given variable X
 NAME: AddXY
 GIVENS:X, Y
 RESULTS: X
 INTERMEDIATES: None
 DEFINITION: AddXY (X, Y)
 -------------------------
 METHOD:
Get X
Get Y
Let X = X+ Y
Give X
Dr. AMIT KUMAR @ JUET
Tracing an Algorithm
 The purpose of tracing an algorithm is to ensure
that it works
 This is a paper test. As we will see later, it should
be completed before writing the computer code
 Tracing involves
 Executing the sequence of instructions with a sample
set of values
 Computing the value of each variable after each
instruction is executed
 Checking for the correct result
Dr. AMIT KUMAR @ JUET
Tracing an Algorithm
 Step 1 - Number every instruction in the algorithm
 Step 2 – Make a table
 The first column of the table indicates which instruction
has been executed
 Subsequent columns list all the variables of the
algorithm (Givens, Results, Intermediates)
Dr. AMIT KUMAR @ JUET
Tracing an Algorithm
 Step 3 – Complete the table
 Each column of the table represents a variable
 Start at the first line of your algorithm. Identify what
will happen to each variable as that instruction is
executed
 Change any values which the instruction changes and leave all
other columns blank
Dr. AMIT KUMAR @ JUET
Trace 1.1
 Trace Algorithm 1.4 using the numbers 24, 31, and
35
METHOD:
(1) Get Num1
(2) Get Num2
(3) Get Num3
(4) Let Sum = Num1 + Num2 + Num3
(5) Let Average = Sum /3
(6) Give Sum
(7) Give Average
Line Num1 Num2 Num3 Sum Avg
1 24
2 31
3 35
4 90
5 30
6 output 90
7 output 30
Dr. AMIT KUMAR @ JUET
Trace 1.2
 Trace Algorithm 1.5 with the numbers 40, 18, 26
METHOD:
(1) Set MaxMark =140
(2) Get A1
(3) Get A2
(4) Get A3
(5) Let Total = A1 + A2 + A3
(6) Let Mark = Total/MaxMark * 100
(7) Give Mark
Ln A1 A2 A3 MM Ttl Mark
1 140
2 40
3 18
4 26
5 84
6 60
7 output 60
NB THE ANSWER IS NOT 69 (40/50 + 18/20 + 26/70)/3
Dr. AMIT KUMAR @ JUET
Trace 1.3
 Trace Algorithm 1.7 when X and Y have the values 25
and 88, respectively
METHOD:
(1) Get X
(2) Get Y
(3) Let Temp = X
(4) Let X = Y
(5) Let Y = Temp
(6) Give X
(7) Give Y
LN X Y Temp
1 25
2 88
3 25
4 88
5 25
6 output 88
7 output 25
Dr. AMIT KUMAR @ JUET
Flow Charts
 Is a pictorial representation of Algorithms
 Can be created in MS Visio, MS word, Paint etc.
 Begin and End with an Oval
 Get/Give use a parallelogram
 Lets use a rectangle
 Flow is shown with arrows
Dr. AMIT KUMAR @ JUET
Algorithm 1.1
NAME: SUM3
GIVENS: N1, N2, N3
RESULTS: Total
DEFINITION:
Total := SUM3(N1, N2, N3)
Start
SUM3
Get N1
Get N2
Get N3
Let Total = N1 + N2 + N3
Give Total
Finish
SUM3
Dr. AMIT KUMAR @ JUET
Algorithm 1.2
NAME: Division
GIVENS: X, Y
RESULTS: Quotient
DEFINITION:
Quotient := Division(X, Y)
Start
DIVISION
Get X
Get Y
Let Quotient = X/Y
Give
Quotient
Finish
DIVISIONDr. AMIT KUMAR @ JUET
Algorithm 1.3
NAME: SumTimes
GIVENS:Num1, Num2
RESULTS: Total, Product
DEFINITION:
Total & Product :=
SumTimes(Num1, Num2)
Start
SUMTIMES
Get Num1
Get Num2
Let Total = Num1 + Num2
Let Product = Num1 * Num2
Give Total
Give Product
Finish
SUMTIMES
Dr. AMIT KUMAR @ JUET
Algorithm 1.4
NAME:AVG3
GIVENS:Num1, Num2, Num3
RESULTS:Sum , Average
DEFINITION:
Sum & Average :=
AVG3(Num1, Num2, Num3)
Start
AVG3
Get Num1
Get Num2
Get Num3
Let Sum = Num1 + Num2 + Num3
Let Average = Sum/3
Give Suml
Give Average
Finish
AVG3
Dr. AMIT KUMAR @ JUET
Algorithm 1.5
NAME: CalcMark
GIVENS: A1, A2, A3
RESULTS: Mark
INTERMEDIATES:
Total,
MaxMark (Constant)
DEFINITION:
Mark := CalcMark(A1, A2, A3)
Start
CALCMARK
Get A1
Get A2
Get A3
Let Total = A1 + A2 + A3
Let Mark = Total/MaxMark
Give Mark
Finish
CALCMARK
Set MaxMark =140
Dr. AMIT KUMAR @ JUET
Algorithm 1.6
NAME: SumDig
GIVENS: N
RESULTS: Sum
INTERMEDIATES: Tens, Ones
DEFINITION:
Sum := SumDig(N)
Start
SUMDIG
Get N
Let Tens = N div 10
Let Ones = N mod 10
Let Sum = Tens + Ones
Give Sum
Finish
SUMDIG
Dr. AMIT KUMAR @ JUET
Algorithm 1.7
NAME: Swap
GIVENS: X, Y
Change: X, Y
RESULTS: None
INTERMEDIATES: Temp
DEFINITION: Swap (X, Y)
Start
SWAP
Get X
Get Y
Let Temp = X
Let X = Y
Let Y = Temp
Give X
Give Y
Finish
SWAP
Dr. AMIT KUMAR @ JUET
Algorithm 1.8
NAME: AddXY
GIVENS:X, Y
Change: X
RESULTS:None
INTERMEDIATES: None
DEFINITION: AddXY (X, Y)
Start
ADDXY
Get X
Get Y
Let X = X + Y
Give X
Finish
ADDXY
Dr. AMIT KUMAR @ JUET

More Related Content

What's hot (20)

PPTX
Problem solving agents
Megha Sharma
 
PDF
Bayesian networks
Massimiliano Patacchiola
 
PPTX
Tic tac toe simple ai game
Seevaratnam Kajandan
 
PPT
Heuristic Search Techniques Unit -II.ppt
karthikaparthasarath
 
PPTX
Inductive analytical approaches to learning
swapnac12
 
PPTX
Moore and mealy machine
Mian Munib
 
PPTX
Sum of subset problem.pptx
V.V.Vanniaperumal College for Women
 
PDF
Logistic regression in Machine Learning
Kuppusamy P
 
PPTX
15 puzzle problem using branch and bound
Abhishek Singh
 
PPTX
Forms of learning in ai
Robert Antony
 
PPTX
Recognition-of-tokens
Dattatray Gandhmal
 
PDF
Parse Tree
A. S. M. Shafi
 
PPTX
2. forward chaining and backward chaining
monircse2
 
PPT
Minimum spanning tree
Hinal Lunagariya
 
PDF
Algorithms Lecture 7: Graph Algorithms
Mohamed Loey
 
PDF
Classification Based Machine Learning Algorithms
Md. Main Uddin Rony
 
PPTX
Cursors, triggers, procedures
Vaibhav Kathuria
 
PPTX
Predicate logic
Harini Balamurugan
 
Problem solving agents
Megha Sharma
 
Bayesian networks
Massimiliano Patacchiola
 
Tic tac toe simple ai game
Seevaratnam Kajandan
 
Heuristic Search Techniques Unit -II.ppt
karthikaparthasarath
 
Inductive analytical approaches to learning
swapnac12
 
Moore and mealy machine
Mian Munib
 
Sum of subset problem.pptx
V.V.Vanniaperumal College for Women
 
Logistic regression in Machine Learning
Kuppusamy P
 
15 puzzle problem using branch and bound
Abhishek Singh
 
Forms of learning in ai
Robert Antony
 
Recognition-of-tokens
Dattatray Gandhmal
 
Parse Tree
A. S. M. Shafi
 
2. forward chaining and backward chaining
monircse2
 
Minimum spanning tree
Hinal Lunagariya
 
Algorithms Lecture 7: Graph Algorithms
Mohamed Loey
 
Classification Based Machine Learning Algorithms
Md. Main Uddin Rony
 
Cursors, triggers, procedures
Vaibhav Kathuria
 
Predicate logic
Harini Balamurugan
 

Similar to Fundamentals of algorithms (20)

PPT
Uta005 lecture3
vinay arora
 
PPTX
ADA_Module 1_MN.pptx- Analysis and design of Algorithms
madhu614742
 
PDF
Lecture 2 role of algorithms in computing
jayavignesh86
 
PPTX
ANALYSIS AND DESIGN OF ALGORITHMS -M1-PPT
AIET
 
PPTX
CS8451 DAA Unit-I.pptx
BolliniNivas
 
PPT
Stacks queues lists
Luis Goldster
 
PPT
Stack squeues lists
James Wong
 
PPT
Stacks queues lists
Young Alista
 
PPT
Stacksqueueslists
Fraboni Ec
 
PPT
Stacks queues lists
Tony Nguyen
 
PPT
Stacks queues lists
Harry Potter
 
PDF
Design and analysis of algorithms
Dr Geetha Mohan
 
PPTX
Chapter 1 - Algorithm Analysis & Design 2021
g46179042
 
PPTX
IntroductionToAlgo_v1_1709293290768 (2).pptx
prasanna220904
 
PPTX
Ch1Levitin.pptx una presentacion muy ilustrativa
alfonsomelendez5
 
PDF
Design and Analysis Algorithms.pdf
HarshNagda5
 
PPT
AA Lecture 01 of my lecture os ghhhggh.ppt
maryamzahra3366
 
PPTX
PPT 1.1 - Introduction to Algorithms.pptx
hitacharyasharan
 
PPTX
PPT 1.1 - Introd. to Algo..pptxSDFGHJKXCVBNMXVBN
AmritaYadav91
 
PDF
Daa chapter 1
B.Kirron Reddi
 
Uta005 lecture3
vinay arora
 
ADA_Module 1_MN.pptx- Analysis and design of Algorithms
madhu614742
 
Lecture 2 role of algorithms in computing
jayavignesh86
 
ANALYSIS AND DESIGN OF ALGORITHMS -M1-PPT
AIET
 
CS8451 DAA Unit-I.pptx
BolliniNivas
 
Stacks queues lists
Luis Goldster
 
Stack squeues lists
James Wong
 
Stacks queues lists
Young Alista
 
Stacksqueueslists
Fraboni Ec
 
Stacks queues lists
Tony Nguyen
 
Stacks queues lists
Harry Potter
 
Design and analysis of algorithms
Dr Geetha Mohan
 
Chapter 1 - Algorithm Analysis & Design 2021
g46179042
 
IntroductionToAlgo_v1_1709293290768 (2).pptx
prasanna220904
 
Ch1Levitin.pptx una presentacion muy ilustrativa
alfonsomelendez5
 
Design and Analysis Algorithms.pdf
HarshNagda5
 
AA Lecture 01 of my lecture os ghhhggh.ppt
maryamzahra3366
 
PPT 1.1 - Introduction to Algorithms.pptx
hitacharyasharan
 
PPT 1.1 - Introd. to Algo..pptxSDFGHJKXCVBNMXVBN
AmritaYadav91
 
Daa chapter 1
B.Kirron Reddi
 
Ad

More from Amit Kumar Rathi (20)

PDF
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Amit Kumar Rathi
 
PDF
Fundamentals of Genetic Algorithms (Soft Computing)
Amit Kumar Rathi
 
PDF
Fuzzy Systems by using fuzzy set (Soft Computing)
Amit Kumar Rathi
 
PDF
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Amit Kumar Rathi
 
PDF
Associative Memory using NN (Soft Computing)
Amit Kumar Rathi
 
PDF
Back Propagation Network (Soft Computing)
Amit Kumar Rathi
 
PDF
Fundamentals of Neural Network (Soft Computing)
Amit Kumar Rathi
 
PDF
Introduction to Soft Computing (intro to the building blocks of SC)
Amit Kumar Rathi
 
PDF
Topological sorting
Amit Kumar Rathi
 
PDF
String matching, naive,
Amit Kumar Rathi
 
PDF
Shortest path algorithms
Amit Kumar Rathi
 
PDF
Sccd and topological sorting
Amit Kumar Rathi
 
PDF
Red black trees
Amit Kumar Rathi
 
PDF
Recurrence and master theorem
Amit Kumar Rathi
 
PDF
Rabin karp string matcher
Amit Kumar Rathi
 
PDF
Minimum spanning tree
Amit Kumar Rathi
 
PDF
Merge sort analysis
Amit Kumar Rathi
 
PDF
Loop invarient
Amit Kumar Rathi
 
PDF
Linear sort
Amit Kumar Rathi
 
PDF
Heap and heapsort
Amit Kumar Rathi
 
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Amit Kumar Rathi
 
Fundamentals of Genetic Algorithms (Soft Computing)
Amit Kumar Rathi
 
Fuzzy Systems by using fuzzy set (Soft Computing)
Amit Kumar Rathi
 
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Amit Kumar Rathi
 
Associative Memory using NN (Soft Computing)
Amit Kumar Rathi
 
Back Propagation Network (Soft Computing)
Amit Kumar Rathi
 
Fundamentals of Neural Network (Soft Computing)
Amit Kumar Rathi
 
Introduction to Soft Computing (intro to the building blocks of SC)
Amit Kumar Rathi
 
Topological sorting
Amit Kumar Rathi
 
String matching, naive,
Amit Kumar Rathi
 
Shortest path algorithms
Amit Kumar Rathi
 
Sccd and topological sorting
Amit Kumar Rathi
 
Red black trees
Amit Kumar Rathi
 
Recurrence and master theorem
Amit Kumar Rathi
 
Rabin karp string matcher
Amit Kumar Rathi
 
Minimum spanning tree
Amit Kumar Rathi
 
Merge sort analysis
Amit Kumar Rathi
 
Loop invarient
Amit Kumar Rathi
 
Linear sort
Amit Kumar Rathi
 
Heap and heapsort
Amit Kumar Rathi
 
Ad

Recently uploaded (20)

PDF
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
 
PDF
June 2025 Top 10 Sites -Electrical and Electronics Engineering: An Internatio...
elelijjournal653
 
PPTX
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
PPTX
Precooling and Refrigerated storage.pptx
ThongamSunita
 
PPT
FINAL plumbing code for board exam passer
MattKristopherDiaz
 
PDF
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 
PDF
A Brief Introduction About Robert Paul Hardee
Robert Paul Hardee
 
PDF
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
PPTX
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
PDF
William Stallings - Foundations of Modern Networking_ SDN, NFV, QoE, IoT, and...
lavanya896395
 
PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
DOCX
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
PDF
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
PPSX
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
PDF
How to Buy Verified CashApp Accounts IN 2025
Buy Verified CashApp Accounts
 
PDF
LLC CM NCP1399 SIMPLIS MODEL MANUAL.PDF
ssuser1be9ce
 
PDF
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
PDF
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
PPTX
Alan Turing - life and importance for all of us now
Pedro Concejero
 
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
 
June 2025 Top 10 Sites -Electrical and Electronics Engineering: An Internatio...
elelijjournal653
 
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
Precooling and Refrigerated storage.pptx
ThongamSunita
 
FINAL plumbing code for board exam passer
MattKristopherDiaz
 
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 
A Brief Introduction About Robert Paul Hardee
Robert Paul Hardee
 
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
William Stallings - Foundations of Modern Networking_ SDN, NFV, QoE, IoT, and...
lavanya896395
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
How to Buy Verified CashApp Accounts IN 2025
Buy Verified CashApp Accounts
 
LLC CM NCP1399 SIMPLIS MODEL MANUAL.PDF
ssuser1be9ce
 
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
Alan Turing - life and importance for all of us now
Pedro Concejero
 

Fundamentals of algorithms

  • 1. Fundamentals of Algorithms Dr. AMIT KUMAR @ JUET
  • 2. Prerequisite  Introduction to programming in ‘C’ and data structure (with sufficient familiarity in the usage of array, pointers, recursion, searching, sorting, tree etc…. ) Dr. AMIT KUMAR @ JUET
  • 3. Purpose of this subject A rigorous introduction to the design and analysis of algorithms  Not a lab or programming course  Not a math course, either Dr. AMIT KUMAR @ JUET
  • 4. Evaluation scheme: (Fundamental of Algorithms Theory)  Test – I 15  Test – II 25  Test- III 35  Assignments 5  Quiz 5  Tutorial/Problem Solving 10  Attendance 05 Total 100 Dr. AMIT KUMAR @ JUET
  • 5. Course Content  Course Content:  Analysis of algorithm: Asymptotic Notation, Sorting and merging Algorithm  Tree and related data Structure: Heap, Priority Queues, B-Tree, AVL, Splay Tree, Red-Black Tree, Threaded Tree  Files: Classification, Record Organization, Retrieval System, External Sorting  Set, Dictionary: Design, Analysis, integration and applications  Fundamental techniques: Divide and Conquer method, Dynamic Programming, Introduction to Greedy Method  Hashing: technique, collision resolution and analysis  Text Processing: String operation, pattern matching algorithm, tries, text compression, text similarity testing. Dr. AMIT KUMAR @ JUET
  • 6. BOOKS and References:  Textbook: Introduction to Algorithms, Cormen, Leiserson, Rivest, Stein  Aho, Hopcraft, Ullman: Data Structure and Algorithms  Kruse, Tonso, Leung: Data Structure and program Design in C  Sahni: Data structure and algorithm and application in C++  Weiss: Data Structure and Algorithm analysis in C/C++ Dr. AMIT KUMAR @ JUET
  • 7. Design and Analysis of Algorithms • Analysis: predict the cost of an algorithm in terms of resources and performance • Design: design algorithms which minimize the cost Dr. AMIT KUMAR @ JUET
  • 8. Dr. AMIT KUMAR @ JUET
  • 9. Algorithms An algorithm is a finite sequence of step by step, discrete, unambiguous instructions for solving a particular problem  Has input data, and is expected to produce output data  Each instruction can be carried out in a finite amount of time in a deterministic way Dr. AMIT KUMAR @ JUET
  • 10. Algorithms  In simple terms, an algorithm is a series of instructions to solve a problem (complete a task).  Problems can be in any form  Business  Get a part from Vancouver to Ottawa by morning  Allocate manpower to maximize profit  Life  I am hungry. How do I order pizza?  Explain how to tie shoelaces to a five year old child Dr. AMIT KUMAR @ JUET
  • 11. Algorithms (Criteria) 1. Input: Zero or more quantities are externally supplied. 2. Output: At least one quantity is produced. 3. Definiteness: Each instruction is clear and unambiguous. 4. Finiteness: If we trace out the instructions of an algorithm, then for all cases, the algorithm terminates after a finite number of steps. 5. Effectiveness: Every instruction must be very basic so that it can be carried out, in principle, by a person using only pencil and paper. It is not enough that each operation be definite as in criteria 3, it also must be feasible. Dr. AMIT KUMAR @ JUET
  • 12. Algorithms  We deal with data processing problems  Translated to programs that can be run on a computer  Since we can only input, store, process & output data on a computer, the instructions in our algorithms will be limited to these functions. Dr. AMIT KUMAR @ JUET
  • 13. Algorithm Description  Understand the problem before solving it  Identify & name each Input/Givens  Identify & name each Output/Results  Assign a name to our algorithm (Name)  Combine the previous 3 pieces of information into a formal statement (Definition)  Results := Name (Givens) Dr. AMIT KUMAR @ JUET
  • 14. Method  Once we have prepared the Algorithm Description, we need to solve the problem  We develop a series of instructions (limited to those described previously) that, when executed, will compute the desired Results from the Givens (Method) Dr. AMIT KUMAR @ JUET
  • 15. Writing of Algorithm Algorithm is basically a sequence of instruction written in simple English type (pseudo) language. The algorithms is basically divided into two parts.  Algorithm heading: It consist of name of algorithm, problem description, input and output.  Algorithm body: It consists of logical body of the algorithm. Dr. AMIT KUMAR @ JUET
  • 16. Assignment command Syntax X = 5Y + 16 On the left side of =, we put the name of a variable and on the right side we put a value or an expression. Each variable refers to a unique location in computer memory that contains a value. Interpretation An assignement is executed in two steps : 1-evaluation of the expression found on the right side. 2-setting the returned value in the memory cell corresponding to variable. Example Let SideSize=15 Let Area=SideSizeSideSize Dr. AMIT KUMAR @ JUET
  • 17. Assignment in computer science and equality in mathematics a) The following instructions are the same in mathematics. A=B B=A not in computer science. Let A=B is different from Let B=A b) In mathematics we work with relations. A relation B=A+1 means that it is true all the time In computer science, we work with assignments. We can have: A=5 B=A+1 A=2 The relation B=A+1 is true only after the second instruction and before the third one. Dr. AMIT KUMAR @ JUET
  • 18. c) The instruction A=A+3 is false in mathematics. In computer science Let A=A+3 means: the new value of A is equal to the old one plus three. d) The instruction A+5=3 is allowed in mathematics (it is an equation). Let A+5=3 has no meaning in computer science (the left side must be a variable). Assignment in computer science and equality in mathematics Dr. AMIT KUMAR @ JUET
  • 19. Input command Syntax Get variable The variable must be from Givens Interpretation Here the user must give a value. The given value is assigned to the variable. Example Get Size_Side Dr. AMIT KUMAR @ JUET
  • 20. Output command Syntax Give variable The variable must be from Results Interpretation The value of the variable is displayed. Example Give Area Dr. AMIT KUMAR @ JUET
  • 21. Algorithm 1.1  Write an algorithm to find the sum of three given numbers  NAME: SUM3  GIVENS: N1, N2, N3  RESULTS: Total  DEFINITION: Total := SUM3(N1, N2, N3)  -------------------------  METHOD: Get N1 Get N2 Get N3 Let Total = N1 + N2 + N3 Give Total Dr. AMIT KUMAR @ JUET
  • 22. Algorithm 1.2  Write an algorithm to find the result of a division operation for the given two numbers X and Y  NAME: Division  GIVENS: X, Y  RESULTS: Quotient  DEFINITION: Quotient := Division(X, Y)  -------------------------  METHOD: Get X Get Y Let Quotient = X/Y Give Quotient Dr. AMIT KUMAR @ JUET
  • 23. Algorithm 1.3  Write an algorithm to find the sum and product of the two given numbers  NAME: SumTimes  GIVENS:Num1, Num2  RESULTS: Total, Product  DEFINITION: Total & Product := SumTimes(Num1, Num2)  -------------------------  METHOD: Get Num1 Get Num2 Let Total = Num1 + Num2 Let Product = Num1 * Num2 Give Total Give Product Dr. AMIT KUMAR @ JUET
  • 24. Algorithm 1.4  Find the sum and average of three given numbers  NAME:AVG3  GIVENS:Num1, Num2, Num3  RESULTS:Sum , Average  DEFINITION:Sum & Average := AVG3(Num1, Num2, Num3)  -------------------------  METHOD: Get Num1 Get Num2 Get Num3 Let Sum = Num1 + Num2 + Num3 Let Average = Sum /3 Give Sum Give Average Dr. AMIT KUMAR @ JUET
  • 25. Variables  Observe that we have used names for the data items in our Givens and Results  Num1, Num2, Num3, Sum, Average in Algorithm 1.4  Each name refers to a unique location in computer memory (one or more adjacent bytes) that contains a value  Since that value can change as the instructions in our algorithm are executed, we call each data item a variable Dr. AMIT KUMAR @ JUET
  • 26. Variables  In our algorithm, when we use a variable name, we are referring to the value stored in memory for that data item  Later in this lecture we will learn more about how to define variables Dr. AMIT KUMAR @ JUET
  • 27. Intermediates  Occasionally, in an algorithm, we need to have a variable (in addition to those representing Givens or Results) to store a value temporarily  These are intermediate variables and we identify them in the Algorithm Description as Intermediates Dr. AMIT KUMAR @ JUET
  • 28. Algorithm 1.5  Given 3 assignment marks (out of 50, 20, 70), find the average (calculated as a mark out of 100)  General Concept  How does one figure out the percentage of several marks?  Add them all up  Divide by the maximum possible mark (50+20+70)  Multiply by 100 Dr. AMIT KUMAR @ JUET
  • 29. Algorithm 1.5  Given 3 assignment marks (out of 50, 20, 70), find the average, calculated as a mark out of 100  NAME: CalcMark  GIVENS: A1, A2, A3  RESULTS: Mark  INTERMEDIATES: Total, MaxMark (Constant)  DEFINITION: Mark := CalcMark(A1, A2, A3)  -------------------------  METHOD: Set MaxMark = 140 (Constant) Get A1 Get A2 Get A3 Let Total = A1 + A2 + A3 Let Mark = Total/MaxMark * 100 Give Mark Dr. AMIT KUMAR @ JUET
  • 30. Algorithm 1.6  Given a two digit number, find the sum of its digits  General Concept  How can we break apart a number?  41 = 4 Tens and 1 Ones  so for the number 41, we want 4 + 1 = 5  Use integer division  DIV returns the integer part of a division  MOD returns the remainder of a division 41 10 = 4 41 MOD 10 = 1 Dr. AMIT KUMAR @ JUET
  • 31. Algorithm 1.6  Given a two digit number, find the sum of its digits  NAME: SumDig  GIVENS: N  RESULTS: Sum  INTERMEDIATES: Tens, Ones  DEFINITION: Sum := SumDig(N)  -------------------------  METHOD: Get N Let Tens = N div10 Let Ones = N mod 10 Let Sum = Tens + Ones Give Sum Dr. AMIT KUMAR @ JUET
  • 32. Algorithm 1.7  Write an algorithm which swaps the values of two numbers  Example 1  Two car family. The wrong car is at the end of the driveway  Move first car out on to the street  Move second car out on to the street  Move first car back in  Move second car back in  Example 2  You are looking after a 3 year old. He wants milk and juice. You put the milk in the blue glass and the juice in the red glass. The child is screaming that you did it wrong.  Get a third glass (white)  Pour the milk from the blue glass to the white glass  Pour the juice from the red glass to the blue glass  Pour the milk from the white glass to the red glass Dr. AMIT KUMAR @ JUET
  • 33. Algorithm 1.7  Write an algorithm which swaps the values of two numbers  NAME: Swap  GIVENS: X, Y  RESULTS: X, Y  INTERMEDIATES: Temp  DEFINITION: Swap (X, Y)  -------------------------  METHOD: Get X Get Y Let Temp = X Let X = Y Let Y = Temp Give X Give Y Dr. AMIT KUMAR @ JUET
  • 34. Algorithm 1.8  Write an algorithm which adds the given two numbers (X and Y) and returns the sum in the given variable X  NAME: AddXY  GIVENS:X, Y  RESULTS: X  INTERMEDIATES: None  DEFINITION: AddXY (X, Y)  -------------------------  METHOD: Get X Get Y Let X = X+ Y Give X Dr. AMIT KUMAR @ JUET
  • 35. Tracing an Algorithm  The purpose of tracing an algorithm is to ensure that it works  This is a paper test. As we will see later, it should be completed before writing the computer code  Tracing involves  Executing the sequence of instructions with a sample set of values  Computing the value of each variable after each instruction is executed  Checking for the correct result Dr. AMIT KUMAR @ JUET
  • 36. Tracing an Algorithm  Step 1 - Number every instruction in the algorithm  Step 2 – Make a table  The first column of the table indicates which instruction has been executed  Subsequent columns list all the variables of the algorithm (Givens, Results, Intermediates) Dr. AMIT KUMAR @ JUET
  • 37. Tracing an Algorithm  Step 3 – Complete the table  Each column of the table represents a variable  Start at the first line of your algorithm. Identify what will happen to each variable as that instruction is executed  Change any values which the instruction changes and leave all other columns blank Dr. AMIT KUMAR @ JUET
  • 38. Trace 1.1  Trace Algorithm 1.4 using the numbers 24, 31, and 35 METHOD: (1) Get Num1 (2) Get Num2 (3) Get Num3 (4) Let Sum = Num1 + Num2 + Num3 (5) Let Average = Sum /3 (6) Give Sum (7) Give Average Line Num1 Num2 Num3 Sum Avg 1 24 2 31 3 35 4 90 5 30 6 output 90 7 output 30 Dr. AMIT KUMAR @ JUET
  • 39. Trace 1.2  Trace Algorithm 1.5 with the numbers 40, 18, 26 METHOD: (1) Set MaxMark =140 (2) Get A1 (3) Get A2 (4) Get A3 (5) Let Total = A1 + A2 + A3 (6) Let Mark = Total/MaxMark * 100 (7) Give Mark Ln A1 A2 A3 MM Ttl Mark 1 140 2 40 3 18 4 26 5 84 6 60 7 output 60 NB THE ANSWER IS NOT 69 (40/50 + 18/20 + 26/70)/3 Dr. AMIT KUMAR @ JUET
  • 40. Trace 1.3  Trace Algorithm 1.7 when X and Y have the values 25 and 88, respectively METHOD: (1) Get X (2) Get Y (3) Let Temp = X (4) Let X = Y (5) Let Y = Temp (6) Give X (7) Give Y LN X Y Temp 1 25 2 88 3 25 4 88 5 25 6 output 88 7 output 25 Dr. AMIT KUMAR @ JUET
  • 41. Flow Charts  Is a pictorial representation of Algorithms  Can be created in MS Visio, MS word, Paint etc.  Begin and End with an Oval  Get/Give use a parallelogram  Lets use a rectangle  Flow is shown with arrows Dr. AMIT KUMAR @ JUET
  • 42. Algorithm 1.1 NAME: SUM3 GIVENS: N1, N2, N3 RESULTS: Total DEFINITION: Total := SUM3(N1, N2, N3) Start SUM3 Get N1 Get N2 Get N3 Let Total = N1 + N2 + N3 Give Total Finish SUM3 Dr. AMIT KUMAR @ JUET
  • 43. Algorithm 1.2 NAME: Division GIVENS: X, Y RESULTS: Quotient DEFINITION: Quotient := Division(X, Y) Start DIVISION Get X Get Y Let Quotient = X/Y Give Quotient Finish DIVISIONDr. AMIT KUMAR @ JUET
  • 44. Algorithm 1.3 NAME: SumTimes GIVENS:Num1, Num2 RESULTS: Total, Product DEFINITION: Total & Product := SumTimes(Num1, Num2) Start SUMTIMES Get Num1 Get Num2 Let Total = Num1 + Num2 Let Product = Num1 * Num2 Give Total Give Product Finish SUMTIMES Dr. AMIT KUMAR @ JUET
  • 45. Algorithm 1.4 NAME:AVG3 GIVENS:Num1, Num2, Num3 RESULTS:Sum , Average DEFINITION: Sum & Average := AVG3(Num1, Num2, Num3) Start AVG3 Get Num1 Get Num2 Get Num3 Let Sum = Num1 + Num2 + Num3 Let Average = Sum/3 Give Suml Give Average Finish AVG3 Dr. AMIT KUMAR @ JUET
  • 46. Algorithm 1.5 NAME: CalcMark GIVENS: A1, A2, A3 RESULTS: Mark INTERMEDIATES: Total, MaxMark (Constant) DEFINITION: Mark := CalcMark(A1, A2, A3) Start CALCMARK Get A1 Get A2 Get A3 Let Total = A1 + A2 + A3 Let Mark = Total/MaxMark Give Mark Finish CALCMARK Set MaxMark =140 Dr. AMIT KUMAR @ JUET
  • 47. Algorithm 1.6 NAME: SumDig GIVENS: N RESULTS: Sum INTERMEDIATES: Tens, Ones DEFINITION: Sum := SumDig(N) Start SUMDIG Get N Let Tens = N div 10 Let Ones = N mod 10 Let Sum = Tens + Ones Give Sum Finish SUMDIG Dr. AMIT KUMAR @ JUET
  • 48. Algorithm 1.7 NAME: Swap GIVENS: X, Y Change: X, Y RESULTS: None INTERMEDIATES: Temp DEFINITION: Swap (X, Y) Start SWAP Get X Get Y Let Temp = X Let X = Y Let Y = Temp Give X Give Y Finish SWAP Dr. AMIT KUMAR @ JUET
  • 49. Algorithm 1.8 NAME: AddXY GIVENS:X, Y Change: X RESULTS:None INTERMEDIATES: None DEFINITION: AddXY (X, Y) Start ADDXY Get X Get Y Let X = X + Y Give X Finish ADDXY Dr. AMIT KUMAR @ JUET