D1-Intro To Algorithm DataStructures
D1-Intro To Algorithm DataStructures
D1-Intro To Algorithm DataStructures
Algorithms
Unit 3: Introduction To Data Structures and
Algorithms
1
What is a Data Structure?
● Data Structures are different ways of organizing data on your computer, that can be
used effectively.
2
What is a Data Structure?
● Data Structures are different ways of organizing data on your computer, that can be
used effectively.
3
What is a Data Structure?
● Data Structures are different ways of organizing data on your computer, that can be
used effectively.
4
What is an Algorithm?
Set of instructions to perform a Task
Step 5: Trim door casing Step 4: Determine the layout Step 3: Prepare sub flooring
5
What is an Algorithm?
● Algorithm in Computer Science: Set of rules for a computer program to
accomplish a Task. It takes a set of input(s) and produces the desired output.
For example:
Input Data
● An algorithm to add two numbers:
1. Take two number inputs
2. Add numbers using the + operator
Calculation
3. Display the result
6
What is an Algorithm?
● Sample Algorithms that are used by BIG Companies.
● How do Google and Facebook transmit live video across the internet?
Compression algorithms
7
What is an Algorithm?
● Sample Algorithms that are used by BIG Companies.
● How to find the shortest path on the map?
Graph algorithms
8
What is an Algorithm?
What makes a good algorithm?
● Correctness
● Efficiency
9
What is an Algorithm?
Characteristics
10
Algorithm
Example
● Problem: Design an algorithm to add two numbers and display the result.
● Example
○ Step 1 - START
○ Step 2 - declare three integers a, b & c
○ Step 3 - define values of a & b
○ Step 4 - add values of a & b
○ Step 5 - store output of step 4 to c
○ Step 6 - print c
○ Step 7 - STOP
11
Why are Data Structures and Algorithms important?
12
Why are Data Structures and Algorithms important?
Input data
Output
Processing
13
Why are Data Structures and Algorithms important?
COMPUTER SCIENCE
ALGORITHMS
14
Why are Data Structures and Algorithms important?
15
Types of Data Structure
Data Structure can be categorized into: Linear Data Structures and Non-Linear Data Structures
16
Types of Data Structure
Linear Data Structures
● In linear data structures, the elements are arranged in sequence one after the
other. Since elements are arranged in particular order, they are easy to
implement
● Generally there are four ways of representing linear data structure:
○ Arrays
○ Linked Lists
○ Stack
○ Queue
17
Data Structures
Non-Linear Data Structures
● Unlike linear data structures, elements in non-linear data structures are not in
any sequence. Instead they are arranged in a hierarchical manner where one
element will be connected to one or more elements.
● Type of non-linear data structures are:
○ Trees
○ Graphs
18
Linear Vs. Non-linear Data Structures
Linear Data Structures Non-Linear Data Structures
The data items are arranged in sequential order, one The data items are arranged in non-sequential order
after the other (hierarchical manner)
All the items are present on the single layer The data items are present at different layers
It can be traversed on a single run. That is, if we start It requires multiple runs. That is, if we start from the
from the first element, we can traverse all the elements first element it might not be possible to traverse all the
sequentially in a single pass. elements in a single pass.
The memory utilization is not efficient. Different structures utilize memory in different efficient
ways depending on the need.
The time complexity increase with the data size. Time complexity remains the same.
19
Types of Algorithms
20
Types of Algorithms
21
Types of Algorithms
● Greedy algorithms
○ We take the best we can without worrying about future consequences
○ We hope that by choosing a local optimum solution at each step, we will end up at a global opt
● Randomized algorithms
○ Use a random number at least once during the computation to make a decision
22
Recursion
23
What is Recursion?
● Recursion : a way of solving a problem by having a function calling itself
24
What is Recursion?
● Recursion : a way of solving a problem by having a function calling itself
25
What we need Recursion?
● Recursive thinking is really important in programming and it helps you break down big
problems into smaller ones and easier to use
● when to choose recursion?
○ If you can divide the problem into similar sub problems
■ Design an algorithm to compute nth…
■ Write code to list the n…
■ Implement a method to compute all.
■ Practice
● The prominent usage of recursion in data structures like trees and graphs.
● It is used in many algorithms (divide and conquer, greedy and dynamic programming)
26
The Logic Behind Recursion
1. A method calls itself
2. Exit from infinite loop
27
The Logic Behind Recursion
STACK Memory
28
The Logic Behind Recursion
recursiveMethod(4) 4
recursiveMethod(3) 3
STACK Memory
recursiveMethod(2) 2
recursiveMethod(1) 1
29
Recursive vs Iterative Solutions
Recursive Iterative
30
When to Use/Avoid Recursion?
● When to use it?
○ When we can easily breakdown a problem into similar subproblems
○ When we are fine with extra overhead (both time and space) that comes with it
○ When we need a quick working solution instead of efficient one
○ When traverse a tree
○ When we use memoization in recursion
● When to avoid it?
○ If time and space complexity matters for us.
○ Recursion uses more memory. If we use embedded memory. For example an application
that takes more memory in the phone is not efficient
○ Recursion can be slow
31
Write Recursion in 3 Steps
● Factorial
○ It is the product of all positive integers less than or equal to n.
○ Denoted by n! (Christian Kramp in 1808).
○ Only positive numbers.
○ 0!=1
● Example 1
○ 4! = 4*3*2*1=24
● Example 2
○ 10! = 10*9*8*7*6*5*4*3*2*1=36,28,800
○ n! = n*(n-1)*(n-2)*…*2*1
32
Write Recursion in 3 Steps
● Step 1 : Recursive case - the flow
n! = n * (n-1) * ( n-2) * … * 2 * 1 n! = n * (n-1)!
(n-1)!
33
Write Recursion in 3 Steps
factorial(4) = 24
6
factorial(4)
4 * factorial(3) 2
3 * factorial(2) 1
2 * factorial(1)
34
Write Recursion in 3 Steps
● Fibonacci numbers
○ Fibonacci sequence is a sequence of numbers in which each number is the sum of the two
preceding ones and the sequence starts from 0 and 1
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89…
35
Write Fibonacci Recursion in 3 Steps
1 fibonacci(1) + fibonacci(0)
1
fibonacci(2) + fibonacci(1)
1 0
fibonacci(1) + fibonacci(0)
36
Questions
1. Find the sum of digits of a positive integer number using recursion.
2. How to calculate power of a number using recursion?
3. How to find GCD ( Greatest Common Divisor) of two numbers using recursion?
4. How to convert a number from Decimal to Binary using recursion.
37
Analysis of Algorithms
38
Introduction to Analysis of Algorithms
● To classify data structures and algorithms as “good,” we must have precise ways of analyzing
them.
● The primary analysis tool involves characterizing the running times (Time Complexity) of
algorithms, with space usage (Space Complexity) also being of interest.
● Running time is a measure of “goodness,” since time is a precious resource—computer
solutions should run as fast as possible.
● In general, the running time of an algorithm or data structure operation increases with the
input size, although it may also vary for different inputs of the same size. Also, the running
time is affected by the hardware environment (e.g., the processor, clock rate, memory, disk)
and software environment (e.g., the operating system, programming language) in which the
algorithm is implemented and executed
39
Introduction to Analysis of Algorithms
● One way to study the efficiency of an algorithm is to implement it and
experiment by running the program on various test inputs while recording the
time spent during each execution.
● A simple mechanism for collecting such running times in Java is based on use
of the currentTimeMillis method of the System class.
● However, the measured times reported by method currentTimeMillis will vary
greatly from machine to machine, and may likely vary from trial to trial, even
on the same machine.
40
Introduction to Analysis of Algorithms
● To evaluate the efficiency of any two algorithms in a way that is independent of
the hardware and software environment.
○ Is performed by studying a high-level description of the algorithm (either
in the form of an actual code fragment, or language-independent
pseudocode).
■ Counting Primitive Operations: Instead of trying to determine the specific
execution time of each primitive operation, we will simply count how many
primitive operations are executed, and use this number t as a measure of the
running time of the algorithm.
41
Introduction to Analysis of Algorithms
● Counting Primitive Operations
42
Big O Notations
To capture the order of growth of an algorithm’s running time, we will associate, with each algorithm, a
function f (n) that characterizes the number of primitive operations that are performed as a function of
the input size n. Alternatively, we can say “ f (n) is order of g(n)” for the big-Oh notation.
● Big O is the language and metric we use to describe the efficiency of algorithms.
● The running time required by the given algorithm
falls under three types:
○ Best case - minimum time required in executing the
program
○ Average case - average time required in executing
the program
○ Worst case - maximum time required in executing
the program
43
Big O Notations
● Big O : It is a complexity that is going to be less or equal to the worst case.
● Big - Ω (Big-Omega) : It is a complexity that is going to be at least more than the
best case.
● Big Theta (Big - Θ) : It is a complexity that is within bounds of the worst and the
best cases.
n
5 4 10 … 8 11 68 87 12 … 90 13 77 55 9
Big O - O(N)
Big Ω - Ω(1)
Big Θ - Θ(n/2)
44
Runtime Complexities
45
Runtime Complexities
46
Runtime Complexities
47
Runtime Complexities
48
Runtime Complexities
49
Runtime Complexities
50
Space Complexity
51
Space Complexity
static int sum(int n) {
static int pairSumSequence(int n){
if (n <= 0) {
var sum = 0;
return 0; for (int i = 0; i <= n; i++){
} sum = sum + pairSum(i,i+1);
return n + sum(n-1); }
} return sum;
}
static int pairSum(int a, int b){
Space complexity : ? return a + b;
}
Space complexity :?
52
Drop Constants and Non Dominant Terms
Drop Constant
O(2N) O(N)
O(N+logN) O(N)
O(2*2N+1000N100) O(2N)
53
How to measure the codes using Big O?
sampleArray
Time complexity :
O(1) + O(n) + O(1) = O(n)
54
Questions
1. Create a function which calculates the sum and product of elements of array
-Find the time complexity for created method.
55
Questions
3. What is the runtime of the below code?
56