DSA - Introduction To Algorithms
DSA - Introduction To Algorithms
DSA - Introduction To Algorithms
Algorithms
Lecture 2:
Introduction to Algorithms
1
Data Structures and Algorithms Fall 2017
Algorithms
2
Data Structures and Algorithms Fall 2017
Algorithms
1. Boil water
2. Add tea in boiled water
3. Add milk
4. Add sugar
5. Stir
3
Data Structures and Algorithms Fall 2017
Algorithms
4
Data Structures and Algorithms Fall 2017
• An algorithm is a
– well ordered collection of
– clear and simple instructions of
– definite and effectively computable operations that when executed
– produces a result and
– stops executing at some point in a finite amount of time rather than just
going on and on infinitely.
• Finite: an algorithm is finite with respect to set of instructions, use of
resources, time of computation.
• Unambiguous: the operations described are understood without further
simplification.
• Effectively computable: Instructions are precise and computable.
• Order: Instructions have a specified logical order.
• Solution: produces a result.
• It Must be Correct.
• It must be composed of series of concrete steps.
• There can be no ambiguity as to which steps will be
performed next.
• It must be composed of finite number of steps.
• It must terminate.
• It takes zero or more inputs
• It should be efficient and flexible
• It should use less memory space as much as possible
• It results in one or more outputs
6
Data Structures and Algorithms Fall 2017
Various steps in developing Algorithms
7
Data Structures and Algorithms Fall 2017
Algorithms
8
Data Structures and Algorithms Fall 2017
Algorithms
Algorithm:
1. Add ages of students to find the total age
2. Divide the total age by the number of students
9
Data Structures and Algorithms Fall 2017
Fundamental Questions about
Algorithms
Given an algorithm, we are led to ask:
• Correctness:
– Does it terminate?
– Is it correct producing the desired result?
– We require algorithms that are correct for all possible inputs.
• Determined:
– Is the result of the algorithm determined?
– We require determined algorithms that produce same output for all possible inputs.
– Ignore randomized algorithms at the moment.
• Running time:
– How long an algorithm will take to produce the desired result?
– Ideally, we want the fastest possible algorithm for our problem.
• Computational resources:
– How much memory will it use?
• Flow Chart
• Pseudocode
Set K=LB
K<=
UB No
Yes
Show LA[K]
K=K+1
Exit
14
Engr. Shamila Nasreen, Data Structures & Algorithms
Levels of Flowcharts
17
Data Structures and Algorithms Fall 2017
Efficiency of an algorithm
18
Data Structures and Algorithms Fall 2017
Time Complexity of an Algorithm
19
Data Structures and Algorithms Fall 2017
Time Complexity of an Algorithm
20
Data Structures and Algorithms Fall 2017
Time Complexity of an Algorithm
21
Data Structures and Algorithms Fall 2017
Time Complexity of an Algorithm
• Worst Case: It is the longest time that an algorithm will use over all
instances of size n for a given problem to produce a desired result.
• Best Case: It is the shortest time ( or least space ) that the algorithm
will use over all instances of size n for a given problem to produce a
desired result.
22
Data Structures and Algorithms Fall 2017
Space Complexity
23
Data Structures and Algorithms Fall 2017
Space Complexity
24
Data Structures and Algorithms Fall 2017
Space Complexity
25
Data Structures and Algorithms Fall 2017
Why algorithm analysis?
As computers get faster and problem sizes get bigger, analysis will
become more important.
Why? The difference between good and bad algorithms will get bigger.
26
Data Structures and Algorithms Fall 2017
Why Study Algorithms and Data
Structures?
27
Data Structures and Algorithms Fall 2017
Why Study Algorithms and Data
Structures?
To become better computer scientist
28
Data Structures and Algorithms Fall 2017
Algorithms are Everywhere
• Search Engines
• GPS navigation
• Self-Driving Cars
• E-commerce
• Medical diagnosis
• Robotics
• Smart Homes
Set K=LB
K<=
UB No
Yes
Show LA[K]
K=K+1
Exit
31
Engr. Shamila Nasreen, Data Structures & Algorithms
Algorithm for inserting an element into an array
33
Engr. Shamila Nasreen, Data Structures & Algorithms
Algorithm for deleting an element from an array