Professional Documents
Culture Documents
Slides in PDF
Slides in PDF
Algorithms Masterclass
Learning Data Structures and Algorithms
Step 5: Trim door casing Step 4: Determine the layout Step 3: Prepare sub ooring
fl
fl
What is an Algorithm?
Algorithms in our daily lives
Step 3: Go to office
What is an Algorithm?
Algorithms in our daily lives
Step 1 : Go to Starbucks
Input Data
Calculation
Compression algorithms
What is an Algorithm?
Sample Algorithms that are used by BIG Companies
How to find the shortest path on the map?
Graph algorithms
What is an Algorithm?
Sample Algorithms that are used by BIG Companies
How to arrange solar panels on the International Space Station?
-Correctness
-Efficiency
Why are Data Structures and Algorithms important?
⚠
Why are Data Structures and Algorithms important?
Why are Data Structures and Algorithms important?
Input data
Output
Da
ta
Stru
ctur
e sa
nd
Alg
ori
thm
Processing s
Why are Data Structures and Algorithms important?
Algorithms
COMPUTER SCIENCE
ALGORITHMS
Algorithms
Why are Data Structures and Algorithms important?
Why are Data Structures and Algorithms important?
Data Structure
Data
Algorithms
COMPUTER SCIENCE
ALGORITHMS
Algorithms Algorithm
Why are Data Structures and Algorithms in INTERVIEWS?
Integer
Float
Linear Non Linear
Character Tree
Boolean
Array Linked List
Stack
Queue
Types of Data Structure
Types of Algorithms
Simple recursive algorithms
Algorithm Sum(A, n)
if n=1
return A[0]
s = Sum(A, n-1) /* recurse on all but last */
s = s + A[n-1] /* add last element */
return s
Types of Algorithms
- Combine the solutions to the subproblems into a solution to the original problem
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 optimum solution
Randomized algorithms
- Use a random number at least once during the computation to make a decision
Recursion
Recursion
Objectives:
- What is Recursion?
- Why do we need Recursion?
- The Logic behind the Recursion
- Recursive vs Iterative Solutions
- How to Write Recursion in 3 Steps?
- Find Fibonacci Series using Recursion
What is Recursion?
What is Recursion?
Recursion : a way of solving a problem by having a function calling itself
…
What is Recursion?
Recursion : a way of solving a problem by having a function calling itself
4. It is used in many algorithms (divide and conquer, greedy and dynamic programming)
fi
recursiveMethod(4)
recursiveMethod(4) 4
recursiveMethod(3) 3
recursiveMethod(2) 2
STACK Memory
recursiveMethod(1) 1
recursiveMethod(0) n is less than 1
Time efficient? No Yes In case of recursion system needs more time for pop and push
elements to stack memory which makes recursion less time
efficient
Easy to code? Yes No We use recursion especially in the cases we know that a
problem can be divided into similar sub problems.
!
When to Use/Avoid Recursion?
When to Use/Avoid Recursion?
When to use it?
- When we can easily breakdown a problem into similar subproblem
- 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 15
!
- 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
(n-1)!
0! = 1
1! = 1
factorial(-1) ??
factorial(-2) ??
fl
Write Recursion in 3 Steps
public int factorial(int n) {
if (n<0) {
return -1;
}
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n-1);
}
factorial(4) = 24
factorial(4) 6
4 * factorial(3) 2
3 * factorial(2)
2 * factorial(1)
- 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 and 1
bonacci(-1) ?
bonacci(1.5) ??
fi
fi
?
fl
bonacci(4) =3 2 1
bonacci(3) + bonacci(2) 1 0
bonacci(1) + bonacci(0)
1 1
bonacci(2) + bonacci(1)
1 0
bonacci(1) + bonacci(0)
fi
fi
fi
fi
fi
fi
fi
fi
fi
Recursion Interview
Questions
Recursion Interview Questions
Recursion Interview Questions - 1
How to nd the sum of digits of a positive integer number using recursion ?
sumofDigits(-11) ??
sumofDigits(1.5) ??
fi
fl
Recursion Interview Questions - 2
How to calculate power of a number using recursion?
xn=x*x*x*..(n times)..*x
24=2*2*2*2 xn = x * xn-1
xa * xb=xa+b
x3 * x4=x3+4
power(2, 1.2) ??
power(2, -1) ??
fl
Recursion Interview Questions - 3
How to nd GCD ( Greatest Common Divisor) of two numbers using recursion?
GCD is the largest positive integer that divides the numbers without a remainder
b=0
Positive integers
fi
fl
Recursion Interview Questions - 4
How to convert a number from Decimal to Binary using recursion
13 to binary
10 1010
1101 f(n) = n mod 2 + 10 * f(n/2)
13/2
10/2 6
5 1
0
6/2
5/2 3
2 0
1 101 * 10 + 0 = 1010
3/2
2/2 1 1
0 10 * 10 + 1 = 101
1/2 0 1 1 * 10 + 0 = 10
fl
Big O
Big O
Objectives:
- What is Recursion?
- Why do we need Recursion?
- The Logic behind the Recursion
- Recursive vs Iterative Solutions
- How to Write Recursion in 3 Steps?
- Find Fibonacci Series using Recursion
What is Big O?
What is Big O?
Big O is the language and metric we use to describe the efficiency of algorithms.
🧑💻
Internet
Size : 1TB
Time Complexity : A way of showing how the runtime of a function increases as the size of input increases.
What is Big O?
Big O is the language and metric we use to describe the efficiency of algorithms.
Electronic - O(s)
Physical - O(1)
Time Complexity : A way of showing how the runtime of a function increases as the size of input increases.
What is Big O?
Types of Runtimes:
O(N), O(N2), O(2N)
w
h
Time complexity : O(wh)
What is Big O?
Big O Notations
Big O Notations
Big O Notations
- Best case
- Average case
- Worst case
Big O Notations
O(N2)
Quick sort algorithm O(N log N)
O(N)
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)
Runtime Complexities
Runtime Complexities
Complexity Name Sample
O(1) Constant Accessing a specific element in array
O(N) Linear Loop through array elements
O(LogN) Logarithmic Find an element in sorted array
O(N2) Quadratic Looking ar a every index in the array twice
O(2N) Exponential Double recursion in Fibonacci
Runtime Complexities
Complexity Name Sample
O(1) Constant Accessing a specific element in array
O(N) Linear Loop through array elements
O(LogN) Logarithmic Find an element in sorted array
O(N2) Quadratic Looking ar a every index in the array twice
O(2N) Exponential Double recursion in Fibonacci
Runtime Complexities
Complexity Name Sample
O(1) Constant Accessing a specific element in array
O(N) Linear Loop through array elements
O(LogN) Logarithmic Find an element in sorted array
O(N2) Quadratic Looking ar a every index in the array twice
O(2N) Exponential Double recursion in Fibonacci
Runtime Complexities
Complexity Name Sample
O(1) Constant Accessing a specific element in array
O(N) Linear Loop through array elements
O(LogN) Logarithmic Find an element in sorted array
O(N2) Quadratic Looking ar a every index in the array twice
O(2N) Exponential Double recursion in Fibonacci
Binary search
Runtime Complexities
Complexity Name Sample
O(1) Constant Accessing a specific element in array
O(N) Linear Loop through array elements
O(LogN) Logarithmic Find an element in sorted array
O(N2) Quadratic Looking ar a every index in the array twice
O(2N) Exponential Double recursion in Fibonacci
Runtime Complexities
Complexity Name Sample
O(1) Constant Accessing a specific element in array
O(N) Linear Loop through array elements
O(LogN) Logarithmic Find an element in sorted array
O(N2) Quadratic Looking ar a every index in the array twice
O(2N) Exponential Double recursion in Fibonacci
if (n==0 || n==1) {
return n;
}
return fibonacci(n-1) + fibonacci(n-2);
}
Runtime Complexities
Space Complexity
Space Complexity
O(n) O(n2)
Space Complexity
static int sum(int n)
if (n <= 0)
return 0
return n + sum(n-1)
}
1 sum(3)
2 → sum(2) Space complexity : O(n)
3 → sum(1)
4 → sum(0)
}
Space Complexity
static int pairSumSequence(int n)
var sum = 0
for (int i = 0; i <= n; i++)
sum = sum + pairSum(i, i+1)
return sum Space complexity : O(1)
static int pairSum(int a, int b)
return a + b
}
O(2N) O(N)
O(N+logN) O(N)
O(2*2N+1000N100) O(2N)
Drop Constants and Non Dominant Terms
- It is very possible that O(N) code is faster than O(1) code for specific inputs
- Different computers with different architectures have different constant factors.
- Different algorithms with the same basic idea and computational complexity might have
slightly different constants
Add vs Multiply
for (a=0; arrayA.length; a++) {
System.out.println(arrayA[a]);
} for (a=0; arrayA.length; a++) {
for (b=0; arrayB.length; b++) {
for (b=0; arrayB.length; b++) { System.out.println(arrayB[b] + arrayA[a]);
System.out.println(arrayB[b]); }
} }
- If your algorithm is in the form “do this, then when you are all done, do that” then you add the runtimes.
- If your algorithm is in the form “do this for each time you do that” then you multiply the runtimes.
Any assignment statements and if statements that are executed once regardless of the size of
Rule 1 O(1)
the problem
Rule 3 A nested loop of the same type takes quadratic time complexity O(n2)
sampleArray 5 4 10 … 8 11 68 87 …
}
biggestNumber = sampleArray[index];
O(1)
O(1)} } O(n)
O(1)
O(n)
}
System.out.println(biggestNumber); O(1)
}
Explanation:
A= 11 4 12 7 n=4
findMaxNumRec(A,1) A[0]=11
}
M(n)=O(1)+M(n-1)
M(n)=1+M(n-1)
M(1)=O(1) =1+(1+M((n-1)-1))
=2+M(n-2)
M(n-1)=O(1)+M((n-1)-1)
=2+1+M((n-2)-1)
M(n-2)=O(1)+M((n-2)-1) =3+M(n-3)
.
.
=a+M(n-a)
=n-1+M(n-(n-1))
=n-1+1
=n
Level
f(4) 0
f(3) f(3) 1
4 0 1 20
O(branchesdepth)
3 1 2 2 * previous level = 2 21
2 2 4 2 * previous level = 2 *21=22 22
1 3 8 2 * previous level = 2 *22=23 23
O(n)
Big O Interview
Questions
Interview Question 1
- Create a function which calculates the sum and product of elements of array
- Find the time complexity for created method.
Interview Question 2
- Create a function which prints to the console the pairs from given array.
- Find the time complexity for created method.
Interview Question 3
- What is the time complexity for this method?
void printUnorderedPairs(int[] array) {
for (int i=0; i<array.length; i++) {
for (int j=i+1; j<array.length; j++) {
System.out.println(array[i] + ", "+ array[j]);
}
}
}
34, 35
45
Interview Question 3
- What is the time complexity for this method?
void printUnorderedPairs(int[] array) {
for (int i=0; i<array.length; i++) {
for (int j=i+1; j<array.length; j++) {
System.out.println(array[i] + ", "+ array[j]);
}
}
}
}
.
1 1st 10
2nd 9
(n-1)+(n-2)+(n-3)+..+2+1 =5 10/2
.
n n/2
=1+2+…+(n-3)+(n-2)+(n-1) 1
=n(n-1)/2
=n2/2 - n/2 n*n/2 = n2/2 O(N2)
=n2
Interview Question 4
What is the runtime of the below code?
O(1)
}
}
}
a = arrayA.length;
Time Complexity : O(ab)
b = arrayB.length;
Interview Question 5
What is the runtime of the below code?
b = arrayB.length;
Time Complexity : O(ab)
a = arrayA.length;
Interview Question 6
- Create a method which takes an array as a parameter and reverses it.
- Find the runtime of the created method?
input output
1 2 3 4 5 5 4 3 2 1
Interview Question 7
Which of the following are equivalent to O(N)? Why?
1. O(N + P) , where P < N/2 O(N)
2. O(2N) O(N)
3. O(N+logN) O(N)
4. O(N + NlogN) O(NlogN)
5. O(N+M)
Interview Question 8
What is the runtime of the below code?
M(n)
}
static int factorial(int n) {
if (n < 0) {
return -1;
} else if (n == 0) { O(1)
return 1;
} else {
return n * factorial(n-1); M(n-1)
}
}
n! = 1*2*3*…*n
M(n)=1+M(n-1)
3! = 1*2*3=6
=1+(1+M((n-1)-1))
}
M(n)=O(1)+M(n-1)
=2+M(n-2)
M(1)=O(1) =2+1+M((n-2)-1)
=3+M(n-3)
M(n-1)=O(1)+M((n-1)-1) .
.
M(n-2)=O(1)+M((n-2)-1) =a+M(n-a) Time Complexity : O(N)
=n+M(n-n)
=n+1
=n
Interview Question 9
What is the runtime of the below code?
void allFib(int n) {
for (i=0; i<n; i++) {
fib(i);
}
}
static int fib(int n) {
if (n<=0) {
return 0; branchesdepth O(2N)
} else if (n==1) {
return 1;
} else {
return fib(n-1) + fib(n-2);
}
}
fib(1) 21 steps
fib(2) 22 steps
fib(3) 23 steps
Total work = 21+22+23+24+…+2n
fib(4) 24 steps Time Complexity : O(2N)
= 2n+1-2
…
fib(n) 2n steps
Interview Question 10
What is the runtime of the below code?
static int powersOf2(int n) {
}
if (n<1) {
return 0;
} else if (n==1) {
O(1)
System.out.println(1);
return 1;
} else {
var prev = powersOf2(n/2);
}
var curr = prev*2;
System.out.println(curr);
return curr; } O(1)
n=50
powersOf(50) return 32
Time Complexity : O(logN)
powersOf(25) return 16
powersOf(12) return 8
powersOf(6) return 4
powersOf(3) return 2
x2
powersOf(1) return 1
Arrays
Arrays
Arrays
- It is a box of macaroons.
- All macaroons in this box are next to each other
- Each macaroon can be identified uniquely based on their location
- The size of box cannot be changed
“a”
4 3 1 6 8 10
[0] [1] [2] [3] [4] [5]
4 3 1 6 8 10
[0] [1] [2] [3] [4] [5]
Types of Arrays
Types of Arrays
Arrays
Two dimensional
Three dimensional
Four dimensional
.
.
.
N dimensional
Types of Arrays
One dimensional array : an array with a bunch of values having been declared with a single index.
5 4 10 11 8 11 68 87 12
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Two dimensional array : an array with a bunch of values having been declared with double index.
a[0][0][1]
Types of Arrays
One dimensional array :
5 4 10 11 8 11 68 87 12
1 33 55 91 20 51 62 74 13
5 4 10 11 8 11 68 87 12
24 50 37 40 48 30 59 81 93
5 4 10 11 8 11 68 87 12
Memory
5 4 10 11 8 11 68 87 12
Arrays in Memory
Two Dimensional array
1 2 3
4 5 6
7 8 9
Memory
Row 1 Row 3
1 2 3 4 5 6 7 8 9
Row 2
Arrays in Memory
Three Dimensional array {{{ 0, 1, 2},
{ 3, 4, 5},
{ 6, 7, 8},
{ 9, 10, 11},
{ 12, 13, 14},
{ 15, 16, 17}}};
Memory
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Creating an Array
When we create an array , we:
- Declare - creates a reference to array
- Instantiation of an array - creates an array
- Initialization - assigns values to cells in array
Memory
arr
Creating an Array
When we create an array , we:
- Declare - creates a reference to array
- Instantiation of an array - creates an array
- Initialization - assigns values to cells in array
Memory
0 0 0 0 0
xx11
xx11
arr
Creating an Array
When we create an array , we:
- Declare - creates a reference to array
- Instantiation of an array - creates an array
- Initialization - assigns values to cells in array
Memory
10 10 10 10 10
xx11
xx11
arr
Creating an Array
When we create an array , we:
- Declare - creates a reference to array
- Instantiation of an array - creates an array
- Initialization - assigns values to cells in array
dataType[] arr
arr[0] = 1
arr[1] = 2
Insertion in Array
myArray[3] = “d”
myArray[5] = “f”
Insertion in Array
“f”
“a”
[0]
“b”
[1]
“c”
[2]
“d”
[3]
“e”
[4]
💥
“h”
[5]
FULL❗
Insertion in Array
Insertion , when an array is full.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
Array Traversal
myArray[0] = “a”
myArray[1] = “b”
myArray[2] = “c”
myArray[3] = “d”
myArray[4] = “e”
myArray[5] = “f”
Accessing Array Element
Memory
1 2 3 4 5
xx11
xx11
arr
Accessing Array Element
<arrayName>[index]
Accessing Array Element
For example:
myArray[0] = “a”
myArray[3] = “d”
Finding Array Element
myArray2 = 1 2 3 4 5 6 7 8 9
Finding Array Element
10 20 30
-2 31 40 -231 -231
Output
Average = 1.5
1 day(s) above average
a[0][4] a[2][5]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
[0] 1 33 55 91 20 51 62 74 13
[1] 5 4 10 11 8 11 68 87 12
[2] 24 50 37 40 48 30 59 81 93
Memory
10
0 20
0 30
0 40
0 50
0 60
0
xx11
xx11
arr
arrayName[2][5] = 30
arrayName[0][0] = 50
arrayName[0][0] = 60 Occupied
Access an element of Two Dimensional Array
a[i][j] —> i is row index and j is column index
a[2][5]
a[0][4]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
[0] 1 33 55 91 20 51 62 74 13
[1] 5 4 10 11 8 11 68 87 12
[2] 24 50 37 40 48 30 59 81 93
Traversing Two Dimensional Array
1 33 55 91
5 4 10 11
24 50 37 40
1 33 55 91 5 4 10 11 24 50 37 40
Searching Two Dimensional Array
Is this the element?
1 33 55 91
5 4 44 11
24 50 37 40
Searching for 44
[0] 1 33 55 91
[1] 5 -2431 10 11
[2] 24 50 37 -24031
arrayName[2][3] = Integer.MIN_VALUE
arrayName[1][1] = Integer.MIN_VALUE
Time and Space Complexity of 2D Array
- Random access
When to avoid
- Reserve memory
When to use/avoid Arrays
When to use
- To store multiple variables of same data type
- Random access
When to avoid
- Reserve memory
Array Project
Array Project
Find Number of Days Above Average Temperature
Output
Average = 1.5
1 day(s) above average
Array Interview
Questions
Missing Number
Find the missing number in an integer array of 1 to 100
1,2,3,4,5,6…50,51,52..100
1,2,3,4,5,6,7,8,9,10 = 55
7
1,2,3,4,5,6,8,9,10 = 48
1,2,3,4,5,6…n = n(n+1)/2
Pairs / Two Sum
Write a program to find all pairs of integers whose sum is equal to a given number.
20 10 30 50 40 60
maxProduct = 0
20 10 30 50 40 60
isUnique True
20 10 30 50 40 60
Permutation True
2 1 3 5 4 6
1 3 2 4 6 5
1 2 3
4 5 6 Rotate 90 degrees
7 8 9
Rotate Matrix
Given an image represented by an NxN matrix write a method to rotate the image by 90
degrees.
1 2 3 7 2 1 7 4 1
4 5 6 4 5 6 8 5 2
7 8 9 9 8 3 9 6 3
for i = 0 to n
temp = top[i]
top[i] = left[i]
left[i] = bottom[i]
bottom[i] = right[i]
right[i] = temp
Rotate Matrix
Given an image represented by an NxN matrix write a method to rotate the image by 90
degrees.
Linked List
www.appmillers.com
What is a Linked List?
Linked List is a form of a sequential collection and it does not have to be in order. A Linked list is made up of independent nodes
that may contain any type of data and each node has a reference to the next node in the link.
Cars (nodes)
Head
Tail
Links
www.appmillers.com
What is a Linked List?
Cars (nodes)
Head
Tail
Links
www.appmillers.com
Linked List vs Array
Linked List = Head 001 1 111 2 222 4 333 5 null
Tail 333
Array = 0 1 2 3 4 5
www.appmillers.com
Types of Linked List
- Singly Linked List
- Circular Singly Linked List
- Doubly Linked List
- Circular Doubly Linked List
Tail 333
Tail 333
www.appmillers.com
Head 001 Player1 111 Player2 222 Player3 333 Player4 001
Tail 333
www.appmillers.com
Types of Linked List
Doubly Linked List
Head 001 null 1 111 001 2 222 111 4 333 222 5 null
Tail 333
www.appmillers.com
Types of Linked List
Head 001 null S1 111 001 S2 222 111 S3 333 222 S4 null
Tail 333
www.appmillers.com
Types of Linked List
Head 001 333 1 111 001 2 222 111 4 333 222 5 001
Tail 333
www.appmillers.com
Types of Linked List
Cmd+shift+tab
Head 001 333 APP1 111 001 APP2 222 111 APP3 333 222 APP4 001
Tail 333
www.appmillers.com
Linked List in Memory
Arrays in memory : 0 1 2 3 4 5
y
or
em
M
0 1 2 3 4 5
www.appmillers.com
Linked List in Memory
Linked list:
2
y
or
em
M
4
Tail 5 1 Head
www.appmillers.com
Creation of Singly Linked List
1 null
111
Tail 111
Create a blank Node and assign a value to it
and reference to null.
www.appmillers.com
Creation of Singly Linked List
www.appmillers.com
Insertion to Linked List in Memory
1. At the beginning of the linked list.
2. After a node in the middle of linked list
3. At the end of the linked list.
www.appmillers.com
2 0
y
or
em
M
4
5 1 ❌ Head
www.appmillers.com
Insertion to Linked List in Memory
Insert a new node after a node
2 0
y
❌
or
em
Head
M
4
5 1
3
www.appmillers.com
Insertion to Linked List in Memory
Insert a new node at the end of linked list
3 2 0
y
or
em
Head
M
4
5 1
www.appmillers.com
Insertion Algorithm
No
Node value - create node
head=None?
Location - assign value
Yes
- head = node
- tail = node
Yes
- node.next = head
Location = rst
- head = node
No
No
- Find location (loop)
- current.next = node
- node.next =nextNode
www.appmillers.com
fi
Insertion Algorithm
No
Node value - create node
head=None?
Location - assign value
Yes
- head = node
- tail = node
Yes
- node.next = head
Location = rst
- head = node
No
No
- Find location (loop)
- current.next = node
- node.next =nextNode
AppMiller
www.appmillers.com
fi
Head 444
001 1 111 2 222 4 333 5 null Tail
- node.next = head
- head = node 6
444
AppMiller
www.appmillers.com
s
- node.next = null
-
-
last.next = node
tail = node
6 null
444
AppMiller
www.appmillers.com
s
- nd location (loop)
-
-
current.next = node
node.next =nextNode
6 333
444
AppMiller
www.appmillers.com
fi
s
No
Terminate
www.appmillers.com
No
Yes
Start Check Head? If nodeValue=currentNode
nodeValue
Yes
No
Terminate
www.appmillers.com
Singly Linked list Deletion
- Deleting the rst node
- Deleting any given node
- Deleting the last node
Head null
001 1 null
001
Tail null
001
AppMiller
www.appmillers.com
s
fi
Singly Linked list Deletion
- Deleting the rst node
- Deleting any given node
- Deleting the last node
Head 111
001 1 111 2 222 4 333 5 null
Tail 333
AppMiller
www.appmillers.com
s
fi
Singly Linked list Deletion
- Deleting the rst node
- Deleting any given node
- Deleting the last node
Tail 333
AppMiller
www.appmillers.com
s
fi
Singly Linked list Deletion
- Deleting the rst node
- Deleting any given node
- Deleting the last node
Head null
001 1 null
001
Tail null
001
AppMiller
www.appmillers.com
s
fi
Singly Linked list Deletion
- Deleting the rst node
- Deleting any given node
- Deleting the last node
Tail 333
222
AppMiller
www.appmillers.com
s
fi
Singly Linked list Deletion Algorithm
No
Location head=None?
Yes
- head = null
- tail = null
Yes
Location = rst
- head = rst.next
No
- head = null
- tail = null
Yes
Terminate - Find node before last node Location = last
(loop)
- beforeLast.next = null
- tail = beforeLast
No
- nd location (loop)
- current.next = nextNode.next
AppMiller
www.appmillers.com
fi
fi
fi
s
Head 001
null 1 111 2 222 4 333 5 null
Tail 333
null
www.appmillers.com
Time and Space Complexity of Singly Linked List
Tail 333
www.appmillers.com
Circular Singly
Linked List
www.appmillers.com
Circular Singly Linked List
Singly Linked List
Tail 333
Tail 333
www.appmillers.com
Create - Circular Singly Linked List
001
Tail 001
Start
nodeValue - Create Blank Node
- node.next=node
- node.value=nodeValue
Terminate
- head=node
- tail=node
www.appmillers.com
Tail 333
www.appmillers.com
newNode
6 001
444
Head 001
444 1 111 2 222 3 333 4 001
444
Tail 333
www.appmillers.com
Insertion - Circular Singly Linked List
- Insert at the specified location of linked list
newNode
6 222
444
Tail 333
www.appmillers.com
Insertion - Circular Singly Linked List
- Insert at the end of linked list
newNode
7 001
555
Tail 333
555
www.appmillers.com
Insertion Algorithm - Circular Singly Linked List
No
No
Terminate
- newNode.next = tail.next Yes
- tail.next = newNode location = last ?
- tail = newNode
No
- find location (loop)
- newNode.next = currentNode.next
- currentNode.next = newNode
www.appmillers.com
Tail 333
print(node1.value) 1
Yes
Start
print(node2.value) 2
Check Head? print(currentNode.value)
print(node3.value) 3
print(node4.value) 4 No
Terminate
www.appmillers.com
Searching - Circular Singly Linked List
node1 node2 node3 node4
Tail 333
No
Yes
nodeValue Check Head? If currentNode=node
No
Yes
Terminate
www.appmillers.com
Deletion - Circular Singly Linked List
- Deleting the first node
- Deleting any given node
- Deleting the last node
Tail 333
www.appmillers.com
Head null
001 1 null
001
001
Tail null
001
www.appmillers.com
Deletion - Circular Singly Linked List
Deleting the first node
Head 111
001 1 111 2 222 4 333 5 111
001
Tail 333
www.appmillers.com
Deletion - Circular Singly Linked List
Deleting any given node
Tail 333
www.appmillers.com
Deletion - Circular Singly Linked List
Deleting the last node
Head 001
null 1 null
001
001
Tail 001
null
www.appmillers.com
Deletion - Circular Singly Linked List
Deleting the last node
Tail 333
222
www.appmillers.com
Deletion Algorithm - Circular Singly Linked List
No
Location head=Null?
Yes
- head = Null
- tail = Null
- first.next = Null
Yes
Location = first
- head = first.next
- tail.next = head
No
- head = Null
- tail = Null
- first.next = Null
Yes
Terminate Location = last
- Find node before last node
(loop)
- Tail = curNode
- curNode.next = head
No
www.appmillers.com
Head 001
null 1 111 2 222 4 333 5 null
001
Tail 333
null
www.appmillers.com
Time and Space Complexity of Circular Singly Linked List
Tail 333
www.appmillers.com
Doubly Linked List
www.appmillers.com
Doubly Linked List
Singly Linked List
Tail 333
Head 001 null 1 111 001 2 222 111 3 333 222 4 null
Tail 333
www.appmillers.com
Create - Doubly Linked List
111
Tail 111
Start
nodeValue - Create Blank Node - node.next=Null
- node.value=nodeValue - node.prev=Null
Terminate
- head=node
- tail=node
www.appmillers.com
Head 001 null 1 111 001 2 222 111 3 333 222 4 null
Tail 333
www.appmillers.com
newNode
null 10 001
002
Head 001
002 002
null 1 111 001 2 222 111 4 333 222 5 null
Tail 333
www.appmillers.com
Insertion - Doubly Linked List
- Insert at the specified location of linked list
newNode
111 20 222
555
Tail 333
www.appmillers.com
Insertion - Doubly Linked List
- Insert at the end of linked list
newNode
333 20 null
444
Head 001 null 1 111 001 2 222 111 4 333 222 5 null
444
Tail 333
444
www.appmillers.com
Insertion Algorithm - Doubly Linked List
nodeValue
check Head?
Yes - create newNode
location - assign value
No
- newNode.prev = null
Yes
- newNode.next = head location =
- head.prev = newNode first ?
- head = newNode
No
- newNode.next = null
Terminate - newNode.prev = tail Yes location =
- tail.next = newNode last ?
- tail = newNode
Head 001 null 1 111 001 2 222 111 3 333 222 4 null
Tail 333
print(node1.value) 1
Yes
print(node2.value)
Start 2
Check Head?
Loop head to tail
print(currentNode.value)
print(node3.value) 3
print(node4.value) 4
No
Terminate
www.appmillers.com
Head 001 null 1 111 001 2 222 111 3 333 222 4 null
Tail 333
print(node4.value) 4
Yes
print(node3.value)
Start 3
Check Head?
Loop tail to head
print(currentNode.value)
print(node2.value) 2
print(node1.value) 1
No
Terminate
www.appmillers.com
Head 001 null 1 111 001 2 222 111 3 333 222 4 null
Tail 333
No
Yes If nodeValue =
Start Check Head?
currentNode
nodeValue
Yes
No
Terminate
www.appmillers.com
Deletion - Doubly Linked List
- Deleting the first node
- Deleting any given node
- Deleting the last node
Head 001 null 1 111 001 2 222 111 3 333 222 4 null
Tail 333
www.appmillers.com
node1
Head null
001 null 1 null
001
Tail null
001
www.appmillers.com
Deletion - Doubly Linked List
Deleting the first node
Head 111
001 null 1 111 null
001 2 222 111 4 333 222 5 null
Tail 333
www.appmillers.com
Deletion - Doubly Linked List
Deleting any given node
Tail 333
www.appmillers.com
Deletion - Doubly Linked List
Deleting the last node
node1
Head null
001 null 1 null
001
Tail null
001
www.appmillers.com
Deletion - Doubly Linked List
Deleting the last node
Tail 333
222
www.appmillers.com
Deletion Algorithm - Doubly Linked List
No
Location head=Null?
Yes
- head = null
- tail = null
Yes
Location = first
- head = head.next
- head.prev = null
No
- head = null
- tail = null
Yes
Terminate Location = last
- tail = tail.prev
- tail.next = null
No
- find location-1 (loop)
- curNode.next = curNode.next.next
- curNode.next.prev = curNode
www.appmillers.com
Head 001
null null 1 111 001
null 2 222 null
111 4 333 null
222 5 null
Tail 333
null
www.appmillers.com
Time and Space Complexity of Doubly Linked List
Head 001 null 1 111 001 2 222 111 4 333 222 5 null
Tail 333
www.appmillers.com
Circular Doubly
Linked List
www.appmillers.com
Circular Doubly Linked List
Doubly Linked List
Head 001 null 1 111 001 2 222 111 3 333 222 4 null
Tail 333
Head 001 333 1 111 001 2 222 111 4 333 222 5 001
Tail 333
www.appmillers.com
Create - Circular Doubly Linked List
001
Tail 001
Start
nodeValue - Create Blank Node - head=node
- node.value=nodeValue - tail=node
Terminate
- node.next=node
- node.prev=node
www.appmillers.com
Head 001 333 1 111 001 2 222 111 4 333 222 5 001
Tail 333
www.appmillers.com
newNode
333 6 001
444
Head 444
001 444
333 1 111 001 2 222 111 4 333 222 5 001
444
Tail 333
www.appmillers.com
Insertion - Circular Doubly Linked List
- Insert at the specified location of linked list
newNode
111 20 222
555
Tail 333
www.appmillers.com
Insertion - Circular Doubly Linked List
- Insert at the end of linked list
newNode
333 20 001
444
Tail 333
444
www.appmillers.com
Insertion Algorithm - Circular Doubly Linked List
nodeValue
check Head?
Yes - create newNode
location - assign value
No
- newNode.prev = tail
Yes
- newNode.next = head
location = first ?
- head.prev = newNode
- head = newNode
- tail.next = newNode
No
- newNode.next = head
- newNode.prev = tail
Yes
Terminate - head.prev = newNode location = last ?
- tail.next = newNode
- tail = newNode
Head 001 333 1 111 001 2 222 111 3 333 222 4 001
Tail 333
No
print(node1.value) 1
Yes Loop head to tail
Start
print(node2.value) Check
2 Head? print(currentNode.value)
If currentNode= tail
print(node3.value) 3
print(node4.value) 4 No
Yes
Terminate
www.appmillers.com
Head 001 333 1 111 001 2 222 111 3 333 222 4 001
Tail 333
No
print(node4.value) 4
Yes Loop tail to head
print(node3.value)
Start Check
3 Head? print(currentNode.value
If currentNode = head
print(node2.value) 2
print(node1.value) 1 No
Yes
Terminate
www.appmillers.com
Head 001 333 1 111 001 2 222 111 3 333 222 4 001
Tail 333
No
Yes If
Start Check Head? nodeValue=currentNode or if
nodeValue currentNode = tail
Yes
No
Terminate
www.appmillers.com
Deletion - Circular Doubly Linked List
- Deleting the first node
- Deleting any given node
- Deleting the last node
Head 001 333 1 111 001 2 222 111 3 333 222 4 001
Tail 333
www.appmillers.com
node1
Head null
001 null
001 1 null
001
001
Tail null
001
www.appmillers.com
Deletion - Circular Doubly Linked List
Deleting the first node
Head 111
001 333 1 111 333
001 2 222 111 4 333 222 5 001
111
Tail 333
www.appmillers.com
Deletion - Circular Doubly Linked List
Deleting any given node
Tail 333
www.appmillers.com
Deletion - Circular Doubly Linked List
Deleting the last node
node1
Head null
001 null
001 1 null
001
001
Tail null
001
www.appmillers.com
Deletion - Circular Doubly Linked List
Deleting the last node
Tail 333
222
www.appmillers.com
Deletion Algorithm - Circular Doubly Linked List
No
Location head=null?
Yes
- head.prev = null
- head.next = null
- head = null
- tail = null
Yes
- head = head.next Location = first
- head.prev = tail
- tail.next = head
- head.prev = null No
- head.next = null
- head = null
- tail = null Yes
Terminate Location = last
- tail = tail.prev
- tail.next = head
- head.prev = tail
No
- find location-1 (loop)
- curNode.next = curNode.next.next
- curNode.next.prev = curNode
www.appmillers.com
Head null
001 null
333 1 111 null
001 2 222 null
111 4 333 null
222 5 null
001
Tail 333
null
www.appmillers.com
Time and Space Complexity of Circular Doubly Linked List
Head 001 333 1 111 001 2 222 111 3 333 222 4 001
Tail 333
www.appmillers.com
Time Complexity of Array vs Linked List
Array Linked List
Creation O(1) O(1)
Insertion at first position O(1) O(1)
Insertion at last position O(1) O(1)
Insertion at nth position O(1) O(n)
Searching in Unsorted data O(n) O(n)
Searching in Sorted data O(logn) O(n)
Traversing O(n) O(n)
Deletion at first position O(1) O(1)
Deletion at last position O(1) O(n)/O(1)
Deletion at nth position O(1) O(n)
Deletion of array/linked list O(1) O(n)/O(1)
Access nth element O(1) O(n)
www.appmillers.com
Linked List Interview
Questions
Remove Duplicates
Write a method to remove duplicates from an unsorted linked list.
Tail 333
currentNode = node1
hashSet = {1} {1,2} {1,2,3}
while currentNode.next is not Null
If next node’s value is in hashSet
Delete next node
Otherwise add it to hashSet
www.appmillers.com
Return Nth to Last
Implement and algorithm to find the nth to last element of a singly linked list.
P1 P2
Tail 444
N=2 Node4
www.appmillers.com
Partition
Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater
than or equal to x.
Tail 444
x = 10
node2
currentNode = node1
Tail = node1 3 null
currentNode.next = null 111
node1
001
Tail 001
www.appmillers.com
Partition
Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater
than or equal to x.
Tail 444
node3
x = 10
9 null
currentNode = node1
222
Tail = node1
currentNode.next = null
node2 node1
111 001
Tail 001
www.appmillers.com
Partition
Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater
than or equal to x.
Tail 444
node4
x = 10
10 null
currentNode = node1
333
Tail = node1
currentNode.next = null
node2 node2 node1
Tail 001
www.appmillers.com
Partition
Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater
than or equal to x.
Tail 444
node5
x = 10
15 null
currentNode = node1
444
Tail = node1
currentNode.next = null
node2 node2 node1 node4
Tail 001
www.appmillers.com
Partition
Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater
than or equal to x.
Tail 444
x = 10
currentNode = node1
Tail = node1
currentNode.next = null
node2 node2 node1 node4 node5
Tail 001
www.appmillers.com
Sum Lists
You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored
in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and
returns the sum as a linked list.
617 7 + 5 = 12
+ 295 1+9+1 = 11
912 6+2+1 = 9
7 1 6
2 1 9
5 9 2
www.appmillers.com
Intersection
Given two (singly) linked lists, determine if the two lists intersect. Return the intersecting node. Note that the
intersection is defined based on reference, not value. That is, if the kth node of the first linked list is the exact
same node (by reference) as the jth node of the second linked list, then they are intersecting.
3 1 5 9
7 2 1
2 4 6
3 1 5 9 7 2 1
2 4 6 7 2 1
www.appmillers.com
Intersection
Given two (singly) linked lists, determine if the two lists intersect. Return the intersecting node. Note that the
intersection is defined based on reference, not value. That is, if the kth node of the first linked list is the exact
same node (by reference) as the jth node of the second linked list, then they are intersecting.
listA 3 1 5 9
7 2 1
listB 2 4 6
len(listA) = 7
7-6=1
len(listB) = 6
www.appmillers.com
Stack
What is a Stack?
Stack is a data structure that stores items in a Last-In/First-Out manner.
www.appmillers.com
What is a Stack?
Stack is a data structure that stores items in a Last-In/First-Out manner.
LIFO method
www.appmillers.com
What is a Stack?
Stack is a data structure that stores items in a Last-In/First-Out manner.
40
30
20
10
www.appmillers.com
Why do we need Stack?
appmillers.com
linkedin.com Stack
gmail.com
udemy.com
www.appmillers.com
Stack Operations
- Create Stack
- Push
- Pop
customStack()
- Peek
- isEmpty
- isFull
- deleteStack
www.appmillers.com
Push Method
customStack = [ ]
customStack.push(1)
www.appmillers.com
Push Method
customStack = [1]
customStack.push(1)
customStack.push(2)
www.appmillers.com
Push Method
customStack = [1,2]
customStack.push(3)
customStack.push(2)
2
1
www.appmillers.com
Push Method
customStack = [1,2,3]
customStack.push(4)
customStack.push(3)
3
2
1
www.appmillers.com
Push Method
customStack = [1,2,3,4]
customStack.push(4)
4
3
2
1
www.appmillers.com
Pop Method
customStack = [1,2,3]
[1,2,3,4]
customStack.pop() 4
4
3
2
1
www.appmillers.com
Pop Method
customStack = [1,2]
[1,2,3]
customStack.pop() 3
3
2
1
www.appmillers.com
Pop Method
customStack = [1]
[1,2]
customStack.pop() 2
2
1
www.appmillers.com
Pop Method
customStack = []
[1]
customStack.pop() 1
www.appmillers.com
Pop Method
customStack = []
customStack.pop()
The stack is Empty
www.appmillers.com
Peek Method
customStack = [1,2,3,4]
customStack.peek() 4
4
3
2
1
www.appmillers.com
isEmpty Method
customStack = [1,2,3,4]
customStack.isEmpty() False
4
3
2
1
www.appmillers.com
isFull Method
customStack = [1,2,3,4]
customStack.isFull() False
4
3
2
1
www.appmillers.com
Delete Method
customStack = [1,2,3,4]
customStack.delete()
4
3
2
1
www.appmillers.com
Stack Creation - Array vs Linked List
Stack using Array
- Easy to implement
- Fixed size
www.appmillers.com
Time and Space Complexity of Stack using Array
www.appmillers.com
Stack using Linked List
Create a Stack
Head null
www.appmillers.com
Stack using Linked List
Push() Method
2 null
111
222
Head null
111
222 1 null
111 2
1
www.appmillers.com
Stack using Linked List
Push() Method
3 null
222
333
Head 333
222 2 111 1 null 3
222 111 2
1
www.appmillers.com
Stack using Linked List
Pop() Method
Head 333
222 3 222 2 111 1 null 3
333 222 111 2
1
www.appmillers.com
Stack using Linked List
Pop() Method
Head 222
111 2 111 1 null
222 111 2
1
www.appmillers.com
Stack using Linked List
Peek() Method
www.appmillers.com
www.appmillers.com
www.appmillers.com
www.appmillers.com
When to Use/Avoid Stack
Use:
- LIFO functionality
- The chance of data corruption is minimum
Avoid: 4
- Random access is not possible 3
2
1
www.appmillers.com
Queue
What is a Queue?
Queue is a data structure that stores items in a First-In/First-Out manner.
www.appmillers.com
What is a Queue?
Queue is a data structure that stores items in a First-In/First-Out manner.
End Front
50 40 30 20 10
www.appmillers.com
Why do we need Queue?
-Utilize first coming data first , while others wait for their turn.
-FIFO method - First In First Out
www.appmillers.com
Why do we need Queue?
Point sale system of a restaurant
www.appmillers.com
Why do we need Queue?
Printer queue
www.appmillers.com
Why do we need Queue?
Call center phone systems
www.appmillers.com
Queue Operations
- Create Queue
- Enqueue
- Dequeue
- Peek
- isEmpty
- isFull
- deleteQueue
www.appmillers.com
Queue Operations
Implementation
1. Array
- Linear Queue
- Circular Queue
2. Linked List
1 2 3
Tail 333
www.appmillers.com
www.appmillers.com
Linear Queue using Array
enQueue Method
newQueue.enqueue(1)
beginnigOfQueue = 0
topOfQueue = 0
www.appmillers.com
Linear Queue using Array
enQueue Method
newQueue.enqueue(2)
beginnigOfQueue = 0
topOfQueue = 1
1 2
www.appmillers.com
Linear Queue using Array
enQueue Method
newQueue.enqueue(3)
beginnigOfQueue = 0
topOfQueue = 2
1 2 3
www.appmillers.com
Linear Queue using Array
deQueue Method
newQueue.dequeue() 1
0
beginnigOfQueue = 1
topOfQueue = 2
1 2 3
www.appmillers.com
Linear Queue using Array
deQueue Method
newQueue.dequeue() 2
1
beginnigOfQueue = 2
topOfQueue = 2
2 3
www.appmillers.com
Linear Queue using Array
peek Method
newQueue.peek() 1
beginnigOfQueue = 0
topOfQueue = 2
1 2 3
www.appmillers.com
Linear Queue using Array
isEmpty Method
newQueue.isEmpty() False
beginnigOfQueue = 0
topOfQueue = 2
1 2 3
www.appmillers.com
Linear Queue using Array
isFull Method
newQueue.isFull() False
beginnigOfQueue = 0
topOfQueue = 2
1 2 3
www.appmillers.com
Linear Queue using Array
delete Method
newQueue.delete()
1 2 3
www.appmillers.com
Time and Space Complexity of Linear Queue using Array
www.appmillers.com
Why do we need Circular Queue?
deQueue method causes blank cells.
10 20 30
10 20 30 40 50 60
topOfQueue = 2 +1 = 3 +1 = 4 +1 = 5
www.appmillers.com
Circular Queue using Array
Create a Queue
newQueue = Queue(6)
beginnigOfQueue = -1
topOfQueue = -1
www.appmillers.com
Circular Queue using Array
enQueue Method
newQueue.enQueue(5)
beginnigOfQueue = 0
topOfQueue = 0
B T
www.appmillers.com
Circular Queue using Array
enQueue Method
newQueue.enQueue(6)
beginnigOfQueue = 0
0
topOfQueue = 1
5 6
B T
www.appmillers.com
Circular Queue using Array
enQueue Method
newQueue.enQueue(7)
beginnigOfQueue = 0
1
topOfQueue = 2
5 6 7
B T
www.appmillers.com
Circular Queue using Array
deQueue Method
newQueue.deQueue() 5
0
beginnigOfQueue = 1
topOfQueue = 5
5 6 7 8 9 10
B T
www.appmillers.com
Circular Queue using Array
deQueue Method
newQueue.deQueue() 6
1
beginnigOfQueue = 2
topOfQueue = 5
5 6 7 8 9 10
B T
www.appmillers.com
Circular Queue using Array
deQueue Method
newQueue.enQueue(1)
beginnigOfQueue = 2
5
topOfQueue = 0
5
1 6 7 8 9 10
B T
www.appmillers.com
Circular Queue using Array
Peek Method
newQueue.peek() 6
beginnigOfQueue = 1
topOfQueue = 5
6 7 8 9 10
B T
www.appmillers.com
Circular Queue using Array
isFull Method
newQueue.isFull() False
beginnigOfQueue = 1
topOfQueue = 5
6 7 8 9 10
B T
www.appmillers.com
Circular Queue using Array
isFull Method
newQueue.isFull() True
beginnigOfQueue = 0
topOfQueue = 5
5 6 7 8 9 10
B T
www.appmillers.com
Circular Queue using Array
isFull Method
newQueue.isFull() True
beginnigOfQueue = 1
topOfQueue = 0
5 6 7 8 9 10
T B
www.appmillers.com
Circular Queue using Array
isEmpty Method
newQueue.isEmpty() False
beginnigOfQueue = 1
topOfQueue = 0
5 6 7 8 9 10
T B
www.appmillers.com
Circular Queue using Array
Delete Method
newQueue.delete()
beginnigOfQueue = 1
topOfQueue = 0
5 6 7 8 9 10
T B
www.appmillers.com
Time and Space Complexity of Circular Queue using Array
www.appmillers.com
Queue using Linked List
Create a Queue
Create an object of Linked List class
Head null
Tail null
www.appmillers.com
Queue using Linked List
enQueue() Method
Head null
111 1 null
222 2 null
111 222
2 1
Tail null
222
111
www.appmillers.com
Queue using Linked List
enQueue() Method
Head null
111 1 null
222 2 null
333 3 null
Tail null
222
333
111
www.appmillers.com
Queue using Linked List
deQueue() Method
Head 111
222 1 222 2 333 3 null
Tail 333
www.appmillers.com
Queue using Linked List
deQueue() Method
Head 222
333 2 333 3 null
222 333
3 2
Tail 333
www.appmillers.com
Queue using Linked List
peek() Method
Tail 333
peek()
return head.value
www.appmillers.com
Tail 333
isEmpty()
If head is Null:
True
www.appmillers.com
Head 111
null 1 222 2 333 3 null
Tail 333
null
delete()
head = Null
tail = Null
www.appmillers.com
www.appmillers.com
Queue - Array vs Linked List
www.appmillers.com
fi
When to Use/Avoid Queue?
Use:
- FIFO functionality
- The chance of data corruption is minimum
Avoid:
- Random access is not possible
3 2 1
www.appmillers.com
Stack and Queue
Interview Questions
Stack and Queue Interview Questions
www.appmillers.com
Three in One
Describe how you could use a single Array to implement three stacks.
www.appmillers.com
Stack Min
How would you design a stack which, in addition to push and pop, has a function min which returns the
minimum element? Push, pop and min should all operate in O(1).
push(5) min() 5
push(6) min() 5
push(3) min() 3
push(7) min() 3 7
pop() min() 3 3
pop() min() 5 6
5
www.appmillers.com
Stack of Plates
Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a
new stack when the previous stack exceeds some threshold. Implement a data structure SetOfStacks that mimics this.
SetOfStacks should be composed of several stacks and should create a new stack once the previous one exceeds capacity,
SetOfStacks.push() and SetOfStacks.pop() should behave identically to a single stack (that is, pop( ) should return the same
values as it would if there were just a single stack).
Follow Up:
Implement a function popAt (int index) which performs a pop operation on a specific sub - stack.
www.appmillers.com
Stack of Plates
Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a
new stack when the previous stack exceeds some threshold. Implement a data structure SetOfStacks that mimics this.
SetOfStacks should be composed of several stacks and should create a new stack once the previous one exceeds capacity,
SetOfStacks.push() and SetOfStacks.pop() should behave identically to a single stack (that is, pop( ) should return the same
values as it would if there were just a single stack).
Follow Up:
Implement a function popAt (int index) which performs a pop operation on a specific sub - stack.
pop( )
www.appmillers.com
Queue via Stacks
Implement Queue class which implements a queue using two stacks.
Dequeue()
Enqueue()
7
3
6
5
8
Stack1 Stack2
www.appmillers.com
Animal Shelter
An animal shelter, which holds only dogs and cats, operates on a strictly "first in, first out" basis. People must adopt either
the "oldest" (based on arrival time) of all animals at the shelter, or they can select whether they would prefer a dog or a cat
(and will receive the oldest animal of that type). They cannot select which specific animal they would like. Create the data
structures to maintain this system and implement operations such as enqueue, dequeueAny, dequeueDog, and dequeueCat.
Newest Oldest
🐈 🐕 🐈 🐈 🐈 🐕 🐈 🐕
Enqueue(Dog)
Enqueue(Cat)
www.appmillers.com
Animal Shelter
An animal shelter, which holds only dogs and cats, operates on a strictly "first in, first out" basis. People must adopt either
the "oldest" (based on arrival time) of all animals at the shelter, or they can select whether they would prefer a dog or a cat
(and will receive the oldest animal of that type). They cannot select which specific animal they would like. Create the data
structures to maintain this system and implement operations such as enqueue, dequeueAny, dequeueDog, and dequeueCat.
Newest Oldest
🐈 🐕 🐈 🐈 🐈 🐕 🐈 🐕
DequeueAny()
DequeueDog()
DequeueCat()
www.appmillers.com
Tree / Binary Tree
What is a Tree?
A tree is a nonlinear data structure with hierarchical relationships between its elements without having any
cycle, it is basically reversed from a real life tree.
Drinks
Hot Cold
Green Black Americano Latte Cappuccino Cola Fanta Soda Wine Beer
www.appmillers.com
ff
What is a Tree?
A tree is a nonlinear data structure with hierarchical relationships between its elements without having any
cycle, it is basically reversed from a real life tree.
Drinks
Hot Cold
Green Black Americano Latte Cappuccino Cola Fanta Soda Wine Beer
Properties:
- Represent hierarchical data
- Each node has two components : data and a link to its sub category
- Base category and sub categories under it
www.appmillers.com
ff
What is a Tree?
A tree is a nonlinear data structure with hierarchical relationships between its elements without having any
cycle, it is basically reversed from a real life tree.
N1
N2 N3
N4 N5 N6
N7 N8
Tree Properties:
- Represent hierarchical data
- Each node has two components : data and a link to its sub category
- Base category and sub categories under it
www.appmillers.com
Why Tree?
- Quicker and Easier access to the data
- Store hierarchical data, like folder structure, organization structure, XML/HTML data.
www.appmillers.com
fi
Why Tree?
- Quicker and Easier access to the data
- Store hierarchical data, like folder structure, organization structure, XML/HTML data.
- There are many different types of data structures which performs better in various situations
- Binary Search Tree, AVL, Red Black Tree, Trie
www.appmillers.com
Tree Terminology
root
N1
Root : top node without parent
Edge : a link between parent and child
Leaf : a node which does not have children
Sibling : children of same parent edge N2 N3
Ancestor : parent, grandparent, great grandparent of a node
N4 N5 N6
sibling
N7 N8
leaf
www.appmillers.com
Tree Terminology
N1
Root : top node without parent
Edge : a link between parent and child
Leaf : a node which does not have children
Sibling : children of same parent
N2 N3
Ancestor : parent, grandparent, great grandparent of a node
ancestors of N7 N4 N5 N6
N7 N8
www.appmillers.com
Tree Terminology
N1
Root : top node without parent
Edge : a link between parent and child
Leaf : a node which does not have children
Sibling : children of same parent
N2 N3
Ancestor : parent, grandparent, great grandparent of a node
Depth of node : a length of the path from root to node
Depth of N4 = 2 N4 N5 N6
N7 N8
www.appmillers.com
Tree Terminology
N1
Root : top node without parent
Edge : a link between parent and child
Leaf : a node which does not have children
Sibling : children of same parent
N2 N3
Ancestor : parent, grandparent, great grandparent of a node
Depth of node : a length of the path from root to node
Height of node : a length of the path from the node to the deepest node
Heigh of N3 =1 N4 N5 N6
N7 N8
www.appmillers.com
Tree Terminology
N1
Root : top node without parent
Edge : a link between parent and child
Leaf : a node which does not have children
Sibling : children of same parent
N2 N3
Ancestor : parent, grandparent, great grandparent of a node
Depth of node : a length of the path from root to node
Height of node : a length of the path from the node to the deepest node
Depth of tree : depth of root node
N4 N5 N6
Depth of tree = 0
N7 N8
www.appmillers.com
Tree Terminology
N1
Root : top node without parent
Edge : a link between parent and child
Leaf : a node which does not have children
Sibling : children of same parent
N2 N3
Ancestor : parent, grandparent, great grandparent of a node
Depth of node : a length of the path from root to node
Height of node : a length of the path from the node to the deepest node
Depth of tree : depth of root node
Height of tree : height of root node N4 N5 N6
Height of tree = 3
N7 N8
www.appmillers.com
Binary Tree
- Binary trees are the data structures in which each node has at most two children, often referred to as
the left and right children
- Binary tree is a family of data structure (BST, Heap tree, AVL, red black trees, Syntax tree)
N1
N2 N3
N4 N5 N6
N7 N8
www.appmillers.com
Why Binary Tree?
- Binary trees are a prerequisite for mode advanced trees like BST, AVL, Red Black Trees
- Huffman coding problem , heap priority problem and expression parsing problems can be solved
efficiently using binary trees,
N1
N2 N3
N4 N5 N6
N7 N8
www.appmillers.com
Types of Binary Tree
Full Binary Tree
N1
N2 N3
N4 N5
N6 N7
www.appmillers.com
Types of Binary Tree
Perfect Binary Tree
N1
N2 N3
N4 N5 N6 N7
www.appmillers.com
Types of Binary Tree
Complete Binary Tree
N1
N2 N3
N4 N5 N6 N7
N8 N9
www.appmillers.com
Types of Binary Tree
Balanced Binary Tree
N1
N2 N3
N4 N5 N7
www.appmillers.com
Binary Tree
- Linked List
- Array
Drinks
Hot Cold
www.appmillers.com
ff
Binary Tree
Linked List
222 Drinks 333
111
222 333
null Tea null null Coffee null null Cola null null Fanta null
www.appmillers.com
Binary Tree
Array Drinks
Hot Cold
0 1 2 3 4 5 6 7 8
www.appmillers.com
ff
Binary Tree
Array Drinks
Hot Cold
0 1 2 3 4 5 6 7 8
Drinks Hot Cold
www.appmillers.com
ff
Binary Tree
Array Drinks
Hot Cold
0 1 2 3 4 5 6 7 8
Drinks Hot Cold Tea Coffee
www.appmillers.com
ff
Binary Tree
Array Drinks
Hot Cold
0 1 2 3 4 5 6 7 8
Drinks Hot Cold Tea Coffee Non alcoholic Alcoholic
www.appmillers.com
ff
Binary Tree using Linked List
N1
- Creation of Tree
- Insertion of a node
- Deletion of a node N2 N3
newTree = Tree()
N8 N9
www.appmillers.com
N8 N9
www.appmillers.com
fi
fi
Root Node
N2 N3
Left Subtree
N4 N5 N6 N7
Right Subtree
N8 N9
N1 N2
www.appmillers.com
Binary Tree - PreOrder Traversal
N1
Root Node
N2 N3
Left Subtree
N4 N5 N6 N7
Right Subtree
N8 N9
N1 N2 N4 N8 N9
www.appmillers.com
Binary Tree - PreOrder Traversal
N1
Root Node
N2 N3
Left Subtree
N4 N5 N6 N7
Right Subtree
N8 N9
N1 N2 N4 N8 N9 N5 N3 N6 N7
www.appmillers.com
Binary Tree - PreOrder Traversal
N1
Root Node
N2 N3
Left Subtree
N4 N5 N6 N7
Right Subtree
N8 N9
N1 N2 N4 N8 N9 N5 N3 N6 N7
www.appmillers.com
Binary Tree - PreOrder Traversal
N1
N2 N3
N4 N5 N6 N7
N8 N9
N1 N2 N4 N8 N9 N5 N3 N6 N7
www.appmillers.com
Binary Tree - InOrder Traversal
N1
Left Subtree
N2 N3
Root Node
N4 N5 N6 N7
Right Subtree
N8 N9
N8 N4
www.appmillers.com
Binary Tree - InOrder Traversal
N1
Left Subtree
N2 N3
Root Node
N4 N5 N6 N7
Right Subtree
N8 N9
N8 N4 N9 N2 N5
www.appmillers.com
Binary Tree - InOrder Traversal
N1
Left Subtree
N2 N3
Root Node
N4 N5 N6 N7
Right Subtree
N8 N9
N9 N4 N9 N2 N5 N1 N6 N3 N7
www.appmillers.com
Binary Tree - InOrder Traversal
N1
Left Subtree
N2 N3
Root Node
N4 N5 N6 N7
Right Subtree
N8 N9
N9 N4 N9 N2 N5 N1 N6 N3 N7
www.appmillers.com
Binary Tree - InOrder Traversal
N1
N2 N3
N4 N5 N6 N7
N8 N9
N8 N4 N9 N2 N5 N1 N6 N3 N7
www.appmillers.com
Binary Tree - Post Traversal
N1
Left Subtree
N2 N3
Right Subtree
N4 N5 N6 N7
Root Node
N8 N9
N8 N9
www.appmillers.com
Binary Tree - Post Traversal
N1
Left Subtree
N2 N3
Right Subtree
N4 N5 N6 N7
Root Node
N8 N9
N8 N9 N4 N5 N2
www.appmillers.com
Binary Tree - Post Traversal
N1
Left Subtree
N2 N3
Right Subtree
N4 N5 N6 N7
Root Node
N8 N9
N8 N9 N4 N5 N2 N6 N7 N3 N1
www.appmillers.com
Binary Tree - Post Traversal
N1
Left Subtree
N2 N3
Right Subtree
N4 N5 N6 N7
Root Node
N8 N9
N8 N9 N4 N5 N2 N6 N7 N3 N1
www.appmillers.com
Binary Tree - Post Traversal
N1
N2 N3
N4 N5 N6 N7
N8 N9
N8 N9 N4 N5 N2 N6 N7 N3 N1
www.appmillers.com
Binary Tree - LevelOrder Traversal
Level 1 N1
Level 2 N2 N3
Level 3 N4 N5 N6 N7
Level 4 N8 N9
www.appmillers.com
Binary Tree - LevelOrder Traversal
Level 1 N1
Level 2 N2 N3
Level 3 N4 N5 N6 N7
Level 4 N8 N9
N1 N2 N3 N4 N5
www.appmillers.com
Binary Tree - LevelOrder Traversal
Level 1 N1
Level 2 N2 N3
Level 3 N4 N5 N6 N7
Level 4 N8 N9
N1 N2 N3 N4 N5 N6 N7 N8 N9
www.appmillers.com
Binary Tree - LevelOrder Traversal
N1
N2 N3
N4 N5 N6 N7
N8 N9
N1 N2 N3 N4 N5 N6 N7 N8 N9
www.appmillers.com
Binary Tree - Search
Level 1 N1
Level Order Traversal
N5 Success
Level 2 N2 N3
Level 3 N4 N5 N6 N7
Level 4 N8 N9
www.appmillers.com
Binary Tree - Insert a Node
- A root node is null N1
- The tree exists and we have to look for a rst vacant
place
N2 N3
N4 N5 N6 N7
newNode
N8 N9 N10
www.appmillers.com
fi
Binary Tree - Delete a Node
Level Order Traversal N1
N3
N2 N9
N3
Step 1 - Find the Node
Step 2 - Find Deepest Node
Step 3 - Set Deepest Node’s value to Current Node
Step 4 - Delete Deepest Node
N4 N5 N6 N7
N8 N9
www.appmillers.com
N2 N3
N4 N5 N6 N7
N8 N9
www.appmillers.com
Binary Tree using Array
N1
- Creation of Tree
- Insertion of a node
- Deletion of a node
N2 N3
- Search for a value
- Traverse all nodes
- Deletion of tree
N4 N5 N6 N7
www.appmillers.com
N2 N3
N4 N5 N6 N7
0 1 2 3 4 5 6 7 8
www.appmillers.com
Binary Tree using Array
N1
N2 N3
N4 N5 N6 N7
0 1 2 3 4 5 6 7 8
N1 N2 N3
www.appmillers.com
Binary Tree using Array
N1
N2 N3
N4 N5 N6 N7
0 1 2 3 4 5 6 7 8
N1 N2 N3 N4 N5
www.appmillers.com
Binary Tree using Array
newBT = BTclass()
N1
Fixed size Array
lastUsedIndex
N2 N3
N4 N5 N6 N7
0 1 2 3 4 5 6 7 8
N1 N2 N3 N4 N5 N6 N7
www.appmillers.com
Binary Tree (Array) - Insert a Node
N1
- The Binary Tree is full
- We have to look for a rst vacant place
lastUsedIndex = 9 N2 N3
newNode
Index = 10
indexOfParent = 10/2 = 5
N8 N9 New
0 1 2 3 4 5 6 7 8 9 10 11
N1 N2 N3 N4 N5 N6 N7 N8 N9 New
www.appmillers.com
fi
Binary Tree (Array) - Traversal
N1
N8 N9
www.appmillers.com
fi
fi
Root Node
N2 N3
Left Subtree
N4 N5 N6 N7
Right Subtree
N8 N9
www.appmillers.com
Binary Tree (Array) - PreOrder Traversal
N1
Root Node
N2 N3
Left Subtree
N4 N5 N6 N7
Right Subtree
N8 N9
www.appmillers.com
Binary Tree (Array) - InOrder Traversal
N1
Left Subtree
N2 N3
Root Node
N4 N5 N6 N7
Right Subtree
N8 N9
www.appmillers.com
Binary Tree (Array) - InOrder Traversal
N1
Left Subtree
N2 N3
Root Node
N4 N5 N6 N7
Right Subtree
N8 N9
www.appmillers.com
Binary Tree (Array) - Post Traversal
N1
Left Subtree
N2 N3
Right Subtree
N4 N5 N6 N7
Root Node
N8 N9
www.appmillers.com
Binary Tree (Array) - Post Traversal
N1
Left Subtree
N2 N3
Right Subtree
N4 N5 N6 N7
Root Node
N8 N9
www.appmillers.com
Binary Tree (Array) - LevelOrder Traversal
Level 1 N1
Level 2 N2 N3
Level 3 N4 N5 N6 N7
Level 4 N8 N9
www.appmillers.com
Binary Tree - LevelOrder Traversal
N1
N2 N3
N4 N5 N6 N7
N8 N9
www.appmillers.com
Binary Tree (Array) - Search
Level 1 N1
Level Order Traversal
N5 5
Level 2 N2 N3
Level 3 N4 N5 N6 N7
0 1 2 3 4 5 6 7 8
N1 N2 N3 N4 N5 N6 N7
www.appmillers.com
Binary Tree (Array) - Delete a Node
Level Order Traversal N1
N3
N2 N9
N3
deepestNode = lastUsedIndex
N4 N5 N6 N7
N8 N9
N1 N2 N3
N9 N4 N5 N6 N7 N8 N9
0 1 2 3 4 5 6 7 8 9 10 11
www.appmillers.com
Binary Tree (Array) - Delete Binary Tree
arr = Null
N1
N2 N3
N4 N5 N6 N7
N8 N9
www.appmillers.com
Binary Tree (Array vs Linked List)
Array Linked List
www.appmillers.com
20
5 15
What is a Binary Search Tree?
- In the left subtree the value of a node is less than or equal to its parent node’s value.
- In the right subtree the value of a node is greater than its parent node’s value
70
50 90
30 60 80 100
20 40
www.appmillers.com
What is a Binary Search Tree?
- In the left subtree the value of a node is less than or equal to its parent node’s value.
- In the right subtree the value of a node is greater than its parent node’s value
70
50 90
30 60 80 100
20 40
www.appmillers.com
Why Binary Search Tree?
- It performs faster than Binary Tree when inserting and deleting nodes
70
50 90
30 60 80 100
20 40
www.appmillers.com
Common Operations on Binary Search Tree
- Creation of BST
- Insertion of a node 70
- Deletion of a node
- Search for a value
- Traverse all nodes 50 90
- Deletion of BST
30 60 80 100
20 40
www.appmillers.com
70
newBST = BST() 50 90
30 60 80 100
20 40
www.appmillers.com
Binary Search Tree - Insert a Node
10 70
Smaller Bigger
50 90
30 60 80 100
20 40
www.appmillers.com
Binary Search Tree - Insert a Node
10 70
Smaller Bigger
50 90
30 60 80 100
20 40
www.appmillers.com
Binary Search Tree - Insert a Node
95 70
Smaller Bigger
50 90
30 60 80 100
20 40
www.appmillers.com
Binary Search Tree - Traversal
70
Depth rst search
- Preorder traversal
- Inorder traversal 50 90
- Post order traversal
Breadth rst search
- Level order traversal
30 60 80 100
20 40
www.appmillers.com
fi
fi
50 90
Left Subtree
30 60 80 100
Right Subtree
20 40
www.appmillers.com
Binary Search Tree - PreOrder Traversal
70
Root Node
50 90
Left Subtree
30 60 80 100
Right Subtree
20 40
www.appmillers.com
Binary Search Tree - InOrder Traversal
70
Left Subtree
50 90
Root Node
30 60 80 100
Right Subtree
20 40
www.appmillers.com
Binary Search Tree- InOrder Traversal
70
Left Subtree
50 90
Root Node
30 60 80 100
Right Subtree
20 40
www.appmillers.com
Binary Search Tree- Post Traversal
70
Left Subtree
50 90
Right Subtree
30 60 80 100
Root Node
20 40
www.appmillers.com
Binary Search Tree - Post Traversal
70
Left Subtree
50 90
Right Subtree
30 60 80 100
Root Node
20 40
www.appmillers.com
Binary Search Tree - LevelOrder Traversal
Level 1 70
Level 2 50 90
Level 3 30 60 80 100
Level 4 20 40
www.appmillers.com
Binary Search Tree - LevelOrder Traversal
70
50 90
30 60 80 100
20 40
www.appmillers.com
Binary Search Tree - Search
40 70
50 90
30 60 80 100
20 40 Success
www.appmillers.com
Binary Search Tree - Delete a Node
Case 1: The node to be deleted is a leaf node 70
30 60 80 100
20 40
www.appmillers.com
50 90
30 60 80 100
20 40
www.appmillers.com
Binary Search Tree - Delete a Node
70
50 90
30 60 80 100
20
www.appmillers.com
Binary Search Tree - Delete a Node
70
80
50 90
30 60 80 100
20 40 85
www.appmillers.com
Binary Search Tree - Delete
70
root = null
50 90
30 60 80 100
20 40 85
www.appmillers.com
Binary Search Tree - Time and Space Complexity
www.appmillers.com
20
AVL Tree 10 30
5 15
What is an AVL Tree?
An AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right
subtrees cannot be more than one for all nodes.
70
50 90
30 60 80 100
20 40
www.appmillers.com
What is an AVL Tree?
An AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right
subtrees cannot be more than one for all nodes.
70
Height of leftSubtree = 3
difference = 1
Height of rightSubtree = 2 50 90
Height of leftSubtree = 1
difference = 0
Height of rightSubtree = 1
30 80 100
Height of leftSubtree = 2
difference = 2
Height of rightSubtree = 0
20 40
www.appmillers.com
What is an AVL Tree?
An AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right
subtrees cannot be more than one for all nodes.
If at any time heights of left and right subtrees differ by more than one, then rebalancing is done to restore
AVL property, this process is called rotation
70
50 90
30 80 100
20 40
www.appmillers.com
What is an AVL Tree?
Examples
Height of leftSubtree = 2 70
difference = 0
Height of rightSubtree = 2
Height of leftSubtree = 1 50 90
difference = 0
Height of rightSubtree = 1
Height of leftSubtree = 1
difference = 0
Height of rightSubtree = 1 30 60 80 100
www.appmillers.com
What is an AVL Tree?
Examples
Height of leftSubtree = 3 70
difference = 1
Height of rightSubtree = 2
Height of leftSubtree = 2 50 90
difference = 1
Height of rightSubtree = 1
Height of leftSubtree = 1
difference = 0
Height of rightSubtree = 1 30 60 80 100
Height of leftSubtree = 1
difference = 1
Height of rightSubtree = 0
20
www.appmillers.com
What is an AVL Tree?
Examples
70
Height of leftSubtree = 4 50 90
difference = 2
Height of rightSubtree = 2
30 60 80 100
20
10
www.appmillers.com
What do we need AVL Tree?
10
10, 20, 30, 40, 50, 60, 70
20
Search for 60
40
50
60
70
www.appmillers.com
What do we need AVL Tree?
10
10, 20, 30, 40, 50, 60, 70
20
Search for 60
40
40
50
20 60 60
70
10 30 50 70
www.appmillers.com
Common Operations on AVL Tree
- Creation of AVL trees, 40
- Search for a node in AVL trees
- Traverse all nodes in AVL trees
- Insert a node in AVL trees 20 60
- Delete a node from AVL trees
- Delete the entire AVL trees
10 30 50 70
www.appmillers.com
rooNode = Null 20 60
10 30 50 70
www.appmillers.com
50 90
Left Subtree
30 60 80 100
Right Subtree
20 40
Time complexity : O(N)
Space complexity : O(N)
www.appmillers.com
50 90
Root Node
30 60 80 100
Right Subtree
20 40
Time complexity : O(N)
Space complexity : O(N)
www.appmillers.com
50 90
Right Subtree
30 60 80 100
Root Node
20 40
Time complexity : O(N)
Space complexity : O(N)
www.appmillers.com
50 90
30 60 80 100
20 40
50 90
30 60 80 100
Success
20 40
www.appmillers.com
50 90
30 60 80 100
Success
20 40
www.appmillers.com
50 90
30 60 80 100
20 40
www.appmillers.com
50 90
30 60 80 100
20 40
www.appmillers.com
AVL Tree - Insert a Node
70
Case 2 : Rotation is required
30 60 80 100
20
www.appmillers.com
30 60 80 100
20
www.appmillers.com
AVL Tree - Insert a Node
70
Case 2 : Rotation is required
30 60 80 100
Left
20
Left
10
www.appmillers.com
AVL Tree - Insert a Node
70
Case 2 : Rotation is required
Right rotation
30 60 80 100
20
10
www.appmillers.com
AVL Tree - Insert a Node
20 70
Case 2 : Rotation is required
30 60
www.appmillers.com
AVL Tree - Insert a Node
70
Case 2 : Rotation is required Left
30 60
20
www.appmillers.com
AVL Tree - Insert a Node
70
Case 2 : Rotation is required
30 60
20
www.appmillers.com
AVL Tree - Insert a Node
Right Rotation - example 2
50
70
30 70
50 90
20 60 90
30 60
20
www.appmillers.com
AVL Tree - Insert a Node
Algorithm of Left Left (LL) Condition
20 20
30 newRoot =
newRoot =
10 10 30
20
disbalancedNode = 30
10
www.appmillers.com
30 60 80 100
20
www.appmillers.com
AVL Tree - Insert a Node
70
Case 2 : Rotation is required
30 60 80 100
Left
20
Right
25
www.appmillers.com
AVL Tree - Insert a Node
70
Case 2 : Rotation is required
1. Left rotation
2. Right rotation
30 60 80 100
20
25
www.appmillers.com
1. Left rotation
2. Right rotation
30 60 80 100
25
20
www.appmillers.com
1. Left Rotation
2. Right Rotation
70 70 70
50 90 50 90 50 90
20 25 20 30
25 20
www.appmillers.com
rotateLeft(disbalancedNode)
newRoot = disbalancedNode.rightChild
disbalancedNode.rightChild = disbalancedNode.rightChild.leftChild
newRoot.leftChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot
rotateRight(disbalancedNode)
newRoot = disbalancedNode.leftChild
disbalancedNode.leftChild = disbalancedNode.leftChild.rightChild
newRoot.rightChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot
www.appmillers.com
rotateLeft(disbalancedNode)
newRoot = disbalancedNode.rightChild
disbalancedNode.rightChild = disbalancedNode.rightChild.leftChild
newRoot.leftChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot
30
newRoot = 20
30
disbalancedNode =
20
disbalancedNode = 10
10
20
10
newRoot =
20
10
www.appmillers.com
rotateRight(disbalancedNode)
newRoot = disbalancedNode.leftChild
disbalancedNode.leftChild = disbalancedNode.leftChild.rightChild
newRoot.rightChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot
30 20
newRoot = 20
10 newRoot =
20
10 30
disbalancedNode = 30
10
www.appmillers.com
rotateLeft(disbalancedNode)
newRoot = disbalancedNode.rightChild
disbalancedNode.rightChild = disbalancedNode.rightChild.leftChild
newRoot.leftChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot
Time complexity : O(1)
Space complexity : O(1)
rotateRight(disbalancedNode)
newRoot = disbalancedNode.leftChild
disbalancedNode.leftChild = disbalancedNode.leftChild.rightChild
newRoot.rightChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot
www.appmillers.com
70
www.appmillers.com
AVL Tree - Insert a Node
50
Case 2 : Rotation is required
70
Right
75
www.appmillers.com
AVL Tree - Insert a Node
50
Case 2 : Rotation is required
Left Rotation
70
75
www.appmillers.com
AVL Tree - Insert a Node
75 50 Right
Case 2 : Rotation is required
60 70
www.appmillers.com
AVL Tree - Insert a Node
50
Case 2 : Rotation is required
60 70
75
www.appmillers.com
AVL Tree - Insert a Node
Case 2 : Rotation is required
50 65
40 65 50 70
40 60 75
60 70
75
www.appmillers.com
AVL Tree - Insert a Node
RR - right right condition
40 40
30
newRoot = disbalancedNode =
50
30 50
40
disbalancedNode = 30
50
www.appmillers.com
70
www.appmillers.com
AVL Tree - Insert a Node
50
Case 2 : Rotation is required
70
Left
65
www.appmillers.com
AVL Tree - Insert a Node
50
Case 2 : Rotation is required
1. Right rotation
2. Left rotation
70
65
www.appmillers.com
1. Right rotation
2. Left rotation
65
70
www.appmillers.com
rotateRight(disbalancedNode)
newRoot = disbalancedNode.leftChild
disbalancedNode.leftChild = disbalancedNode.leftChild.rightChild
newRoot.rightChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot
rotateLeft(disbalancedNode)
newRoot = disbalancedNode.rightChild
disbalancedNode.rightChild = disbalancedNode.rightChild.leftChild
newRoot.leftChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot
www.appmillers.com
rotateRight(disbalancedNode)
newRoot = disbalancedNode.leftChild
disbalancedNode.leftChild = disbalancedNode.leftChild.rightChild
newRoot.rightChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot 30
30 newRoot = 35
disbalancedNode = 35
disbalancedNode = 40
40
40
35
newRoot =
35
40
www.appmillers.com
rotateLeft(disbalancedNode)
newRoot = disbalancedNode.rightChild
disbalancedNode.rightChild = disbalancedNode.rightChild.leftChild
newRoot.leftChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot
35
35
30 newRoot =
newRoot =
30 40
40
35
disbalancedNode = 30
40
www.appmillers.com
rotateRight(disbalancedNode)
newRoot = disbalancedNode.leftChild
disbalancedNode.leftChild = disbalancedNode.leftChild.rightChild
newRoot.rightChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot
Time complexity : O(1)
Space complexity : O(1)
rotateLeft(disbalancedNode)
newRoot = disbalancedNode.rightChild
disbalancedNode.rightChild = disbalancedNode.rightChild.leftChild
newRoot.leftChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot
www.appmillers.com
www.appmillers.com
Left 25 35
Left 20
15
www.appmillers.com
Left 20 35
15 25
www.appmillers.com
15 30
Left
Left rotation
5 25 35
Right
10
www.appmillers.com
10 30
Left rotation
5 25 35 Right
15
50 Right
60
www.appmillers.com
10 30 Right
5 25 50 Right
15
35 60
70
www.appmillers.com
10 50
Left rotation
5 30 60 Right
15
Right rotation
25 35 70
Left
65
www.appmillers.com
50 90
30 60 80 100
20 40
www.appmillers.com
50 90
30 60 80 100
20 40
www.appmillers.com
AVL Tree - Delete a Node
Case 1 - Rotation is not required
50 90
30 60 80 100
20
www.appmillers.com
AVL Tree - Delete a Node
Case 1 - Rotation is not required
50 90
30 60 80 100
20 40 85
www.appmillers.com
AVL Tree - Delete a Node
30 60 80 100
20 40 85
www.appmillers.com
50 90
Left
30 60 80 100
Left
20 85
www.appmillers.com
AVL Tree - Delete a Node
Case 2 - Rotation is required
70
70
Left Right Condition (LR)
50 90
Left
Left rotation
30 60 80 100
Right
20 85
www.appmillers.com
AVL Tree - Delete a Node
Case 2 - Rotation is required
70
70
Left Right Condition (LR)
Right rotation
50 90
30 60 85
20 80
www.appmillers.com
AVL Tree - Delete a Node
Case 2 - Rotation is required
70
70
Right Right Condition (RR)
Left rotation
50 90
Right
30 60 80 100
Right
70 85
www.appmillers.com
AVL Tree - Delete a Node
Case 2 - Rotation is required
70
70
Right Left Condition (RL)
50 90
Right
Right rotation
30 60 80 100
Left
55 85
www.appmillers.com
AVL Tree - Delete a Node
Case 2 - Rotation is required
70
70
Right Left Condition (RL)
Left rotation
50 90
55 80 100
60 85
www.appmillers.com
AVL Tree - Delete a Node (all together)
- Left Left Condition (LL)
Delete 40
- Left Right Condition (LR)
70
70
- Right Right Condition (RR)
- Right Left Condition (RL)
50 90
Right rotation
30 60 80 100
20 40 65 110
15
www.appmillers.com
20 60 80 100
15 30 65 110
www.appmillers.com
20 60 80 100
30 65 110
www.appmillers.com
Left rotation
20 60 80 100
30 110
www.appmillers.com
20 50 80 100
110
www.appmillers.com
20 50 90 110
105
www.appmillers.com
Right rotation
20 50 90 110
105
www.appmillers.com
10
- Right Right Condition (RR)
- Right Left Condition (RL)
15
20
10
5 15
20
www.appmillers.com
50 90
30 60 80 100
20 40
www.appmillers.com
Time and Space Complexity of AVL
www.appmillers.com
Binary Search Tree vs AVL Tree
BST AVL
www.appmillers.com
5
Binary Heap 10 30
15 20
What is a Binary Heap?
A Binary Heap is a Binary Tree with following properties.
- A Binary Heap is either Min Heap or Max Heap. In a Min Binary Heap, the key at root must be minimum among all keys present in
Binary Heap. The same property must be recursively true for all nodes in Binary Tree.
- It’s a complete tree (All levels are completely filled except possibly the last level and the last level has all keys as left as possible). This
property of Binary Heap makes them suitable to be stored in an array.
10 20
30 40 50 60
70 80
www.appmillers.com
Possible Solutions
5 10 20 30 40 50
4
- Store the numbers in Linked List in sorted manner
Tail 333
null
www.appmillers.com
Why we need a Binary Heap?
Find the minimum or maximum number among a set of numbers in logN time. And also we want to
make sure that inserting additional numbers does not take more than O(logN) time
Practical Use
- Prim’s Algorithm
- Heap Sort
- Priority Queue
www.appmillers.com
5 80
10 20 70 60
30 40 50 60 50 40 30 20
70 80 5 10
www.appmillers.com
30 40 50 60
Implementation Options
- Array Implementation
- Reference /pointer Implementation 70 80
www.appmillers.com
10 20
30 40 50 60
0 1 2 3 4 5 6 7 8
5 10 20 30 40 50 60
www.appmillers.com
Common Operations on Binary Heap
- Creation of Binary Heap
Initialize Array
set size of Binary Heap to 0
0 1 2 3 4 5 6 7 8
www.appmillers.com
10 20
30 40 50 60
0 1 2 3 4 5 6 7 8
5 10 20 30 40 50 60
www.appmillers.com
Common Operations on Binary Heap
- Size Binary Heap
5
Return number of filled cells
10 20
30 40 50 60
0 1 2 3 4 5 6 7 8
5 10 20 30 40 50 60
www.appmillers.com
Common Operations on Binary Heap
- Level Order Traversal
10 20
30 40 50 60
0 1 2 3 4 5 6 7 8
5 10 20 30 40 50 60
www.appmillers.com
Binary Heap - Insert a Node
5
1
10 20
30 40 50 60
0 1 2 3 4 5 6 7 8
5 10 20 30 40 50 60
www.appmillers.com
Binary Heap - Insert a Node
5
1
10 20
30 40 50 60
0 1 2 3 4 5 6 7 8
5 10 20 1
30 40 50 60 30
www.appmillers.com
Binary Heap - Insert a Node
5
10 20
1 40 50 60
30
0 1 2 3 4 5 6 7 8
5 5
10 20 10
1
30 40 50 60 30
www.appmillers.com
Binary Heap - Extract a Node
5
10 20
30 40 50 60
80
0 1 2 3 4 5 6 7 8
5 10 20 30 40 50 60 80
www.appmillers.com
Binary Heap - Extract a Node
80
10 20
30 40 50 60
0 1 2 3 4 5 6 7 8
80 10 20 30 40 50 60
www.appmillers.com
Binary Heap - Extract a Node
10
80 20
30 40 50 60
0 1 2 3 4 5 6 7 8
10 80 20 30 40 50 60
www.appmillers.com
Binary Heap - Delete
array = Null
10
80 20
30 40 50 60
0 1 2 3 4 5 6 7 8
10 80 20 30 40 50 60
www.appmillers.com
Time and Space Complexity of Binary Heap
www.appmillers.com
R
Trie E S
M L
What is a Trie?
A Trie is a tree-based data structure that organizes information in a hierarchy.
Properties: AB
- It is typically used to store or search strings in a space and time efficient way.
- Any node in trie can store non repetitive multiple characters
- Every node stores link of the next character of the string
- Every node keeps track of ‘end of string’ I AIM
www.appmillers.com
What is a Trie?
A Trie is a tree-based data structure that organizes information in a hierarchy.
AIR
A
www.appmillers.com
What is a Trie?
A Trie is a tree-based data structure that organizes information in a hierarchy.
AIT
A
R
RT
www.appmillers.com
What is a Trie?
A Trie is a tree-based data structure that organizes information in a hierarchy.
BAR A
AB
I A
RT
R R
www.appmillers.com
What is a Trie?
A Trie is a tree-based data structure that organizes information in a hierarchy.
BIL AB
A
I A
AI
RT
R R L
www.appmillers.com
What is a Trie?
A Trie is a tree-based data structure that organizes information in a hierarchy.
BM
AB
A
I AI
AIM
A
RT
R R L
www.appmillers.com
Whe we need Trie?
To solve many standard problems in efficient way
- Spelling checker
AB
A
- Auto completion
I AIM
AI
A
RT
R R L
www.appmillers.com
- Spelling checker
AB
A
- Auto completion
Map I AIM
AI
A
Characters Link to Trie Node
RT
R R L
End of String
www.appmillers.com
- Insertion in Trie
- Search for a String in trie
- Deletion from Trie I AIM
AI
A
RT
R R L
www.appmillers.com
Map
End of String
www.appmillers.com
Insert a String in a Trie
Case 1 : A Trie is Blank
APP
Map
Map
End of String = N
Characters Link to Trie Node
Map
End of String = N
Characters Link to Trie Node
Map
End of String = N
Characters Link to Trie Node
End of String = Y
www.appmillers.com
Insert a String in a Trie
Case 2: New string’s prefix is common to another strings prefix
API
Map
Map
End of String = N
Characters Link to Trie Node
Map
End of String = N
Characters Link to Trie Node
Map
End of String = N
Characters Link to Trie Node
Map
End of String = Y
www.appmillers.com
Insert a String in a Trie
Case 3: New string’s prefix is already present as complete string
APIS
Map
Map
End of String = N
Characters Link to Trie Node
Map
End of String = N
Characters Link to Trie Node
Map
End of String = N
Characters Link to Trie Node
Map
S
Characters Link to Trie Node
End of String = Y
Map
End of String = N
Characters Link to Trie Node
Map
End of String = N
Characters Link to Trie Node
Map
End of String = N
Characters Link to Trie Node
Map
S
Characters Link to Trie Node
End of String = Y
BCD
B A
Return : The string does not exist in Trie
www.appmillers.com
Search for a String in a Trie
Case 2: String exists in Trie
API
A
Return : TRUE
www.appmillers.com
Search for a String in a Trie
Case 3: String is a pre x of another string, but it does not exist in a Trie
AP
A
Return : FALSE
www.appmillers.com
fi
Delete a String from Trie
Case 1: Some other prefix of string is same as the one that we want to delete. (API, APPLE)
I P
www.appmillers.com
Delete a String from Trie
Case 2: The string is a prefix of another string. (API, APIS)
I P
S L
www.appmillers.com
Delete a String from Trie
Case 3: Other string is a prefix of this string. (APIS, AP)
Dictionary
P End of String = N
Dictionary
I
Dictionary
End of String = N
Characters Link to Trie Node
I
S
End of String = Y
Dictionary Dictionary
www.appmillers.com
Delete a String from Trie
Case 4: Not any node depends on this String (K)
A
AK Dictionary
K
P
End of String = N
Dictionary
I P
Dictionary Dictionary
End of String = N
www.appmillers.com
Practical use of Trie
- Auto completion
- Spelling checker
www.appmillers.com
Hashing
What is Hashing?
Hashing is a method of sorting and indexing data. The idea behind hashing is to allow large amounts of data to
be indexed using keys commonly created by formulas
Magic function
Apple 18
Application 20
Appmillers 22
0 1 .. 18 19 20 21 22 23
www.appmillers.com
Why Hashing?
It is time efficient in case of SEARCH Operation
Array O(logN)
Tree O(logN)
www.appmillers.com
Hashing Terminology
Hash function : It is a function that can be used to map of arbitrary size to data of fixed size.
Key : Input data by a user
Hash value : A value that is returned by Hash Function
Hash Table : It is a data structure which implements an associative array abstract data type, a structure that can
map keys to values
Collision : A collision occurs when two different keys to a hash function produce the same output.
Hash Function
Apple 18
Application
20 Hash Table
Appmillers
22
0 1 .. 18 19 20 21 22 23
www.appmillers.com
Hashing Terminology
Hash function : It is a function that can be used to map of arbitrary size to data of fixed size.
Key : Input data by a user
Hash value : A value that is returned by Hash Function
Hash Table : It is a data structure which implements an associative array abstract data type, a structure that can
map keys to values
Collision : A collision occurs when two different keys to a hash function produce the same output.
Hash function
ABCD 20
ABCDEF 20
ABCDEF
Collision
0 1 ..
..
18 19 💥
20
ABCD
21 22 23
www.appmillers.com
Hash Functions
Mod function
mod(400, 24) 16
mod(700, 24) 4
0 1 .. 4 5 .. 16 .. 23
.. 700 .. 400 ..
www.appmillers.com
Hash Functions
ASCII function
public int modASCII(String word, int cellNumber) {
int total = 0;
for (int i=0; i<word.length(); i++) {
total += word.charAt(i);
System.out.println(total);
}
return total % cellNumber;
}
modASCII("ABC", 24) 6
A 65 65+66+67 = 198 24
192 8
B 66
6
C 67
0 1 . 6 7 .. 16 .. 23
.. ABC .. ..
www.appmillers.com
Hash Functions
Properties of good Hash function
- It distributes hash values uniformly across hash tables
Hash function
ABCD 20
ABCDEF 20
ABCDEF
Collision
0 1 ..
..
18 19 💥
20
ABCD
21 22 23
www.appmillers.com
Hash Functions
Properties of good Hash function
- It distributes hash values uniformly across hash tables
- It has to use all the input data
ABCD
ABCDEF
Hash function
ABC 18
Collision
0 1 ..
.. ABCD
💥
18 19 20 21 22 23
www.appmillers.com
Collision Resolution Techniques
💥
0
Hash function
Collision
1
2 ABCD EFGH
ABCD 2 3
4
EFGH 2
5
IJKLM
6
10
11
12
13
14
15
www.appmillers.com
Collision Resolution Techniques
Resolution Techniques
Linear Probing
Quadratic Probing
Double Hashing
www.appmillers.com
Collision Resolution Techniques
Direct Chaining : Implements the buckets as linked list. Colliding elements are stored in this lists
0
Hash function
1
5
IJKLM 2
6
Miller 7
7 444 Miller Null
8 444
10
11
12
13
14
15
www.appmillers.com
Collision Resolution Techniques
Open Addressing: Colliding elements are stored in other vacant buckets. During storage and lookup these are found
through so called probing.
Linear probing : It places new key into closest following empty cell
0
2 ABCD
Hash function 3 EFGH
4 KLM
5
ABCD 2
6
EFGH 2 7
IJKLM 2 8
10
11
12
13
14
15
www.appmillers.com
IJ
Collision Resolution Techniques
Open Addressing: Colliding elements are stored in other vacant buckets. During storage and lookup these are found
through so called probing.
Quadratic probing : Adding arbitrary quadratic polynomial to the index until an empty cell is found
0
IJKLM 2 8
10
11
12
13
14
15
www.appmillers.com
IJ
Collision Resolution Techniques
Open Addressing: Colliding elements are stored in other vacant buckets. During storage and lookup these are found
through so called probing.
Double Hashing : Interval between probes is computed by another hash function
0
2 ABCD
Hash function 3
4
2 + 4= 6
5
ABCD 2
6 EFGH
2 + 4= 6
EFGH 2 7
2 + (2*4) = 8
IJKLM 2 8 KLM
9
10
Hash Function 2 11
12
13
EFGH 4 14
IJKLM 4 15
www.appmillers.com
IJ
Hash Table is Full
Direct Chaining
This situation will never arise.
Hash function
333
RSTU 1 3 444
IJKLM Null
444
www.appmillers.com
Hash Table is Full
Open addressing
Create 2X size of current Hash Table and recall hashing for current keys
Hash function
EFGH 1 1 EFGH
1 EFGH
IJKLM 3 2 ABCD
2 ABCD
NOPQ 0
3 IJKLM
3 IJKLM
RSTU 1
4 RSTU
www.appmillers.com
Pros and Cons of Collision Resolution Techniques
Direct chaining
- Hash table never gets full
- Huge Linked List causes performance leaks (Time complexity for search operation becomes O(n).)
Open addressing
- Easy Implementation
- When Hash Table is full, creation of new Hash table affects performance (Time complexity for search
operation becomes O(n).)
www.appmillers.com
Pros and Cons of Collision Resolution Techniques
Hash function
NOPQ 0 4 RSTU
RSTU 1
www.appmillers.com
Personal Computer
Practical Use of Hashing
Login : elshad@google.com
Password: 123456
Google Servers
Hash value: *&71283*a12
www.appmillers.com
fi
Practical Use of Hashing
www.appmillers.com
fi
Practical Use of Hashing
File system : File path is mapped to physical location on disk
Path: /Documents/Files/hashing.txt
1 /Documents/
Files/hashing.txt Physical location: sector 4
www.appmillers.com
Pros and Cons of Hashing
x When Hash function is not good enough Insertion/Deletion/Search operations take O(n) time
www.appmillers.com
Sorting Algorithms 1 2 3 4 5
What is Sorting?
By definition sorting refers to arranging data in a particular format : either ascending or descending.
www.appmillers.com
What is Sorting?
By definition sorting refers to arranging data in a particular format : either ascending or descending.
www.appmillers.com
What is Sorting?
Practical Use of Sorting
Microsoft Excel : Built in functionality to sort data
Online Stores: Online shopping websites generally have option sor sorting by price, review,
ratings..
www.appmillers.com
Types of Sorting
Sorting
www.appmillers.com
Types of Sorting
Space used
In place sorting : Sorting algorithms which does not require any extra space for sorting
Example : Bubble Sort
70 10 80 30 20 40 60 50 90
10 20 30 40 50 60 70 80 90
Out place sorting : Sorting algorithms which requires an extra space for sorting
Example : Merge Sort
70 10 80 30 20 40 60 50 90
10 20 30 40 50 60 70 80 90
www.appmillers.com
Types of Sorting
Stability
Stable sorting : If a sorting algorithm after sorting the contents does not change the sequence of
similar content in which they appear, then this sorting is called stable sorting.
70 10 80 40 20 40 60 50 90
10 20 40 40 50 60 70 80 90
UnStable sorting : If a sorting algorithm after sorting the content changes the sequence of similar
content in which they appear, then it is called unstable sort.
Example : Quick Sort
70 10 80 40 20 40 60 50 90
10 20 40 40 50 60 70 80 90
www.appmillers.com
Types of Sorting
Stability
UnStable sorting example
Unsorted data Sorted by name Sorted by age (stable) Sorted by age (unstable)
Name Age Name Age Name Age Name Age
Renad 7 Nick 6 Nick 6 Nick 6
Nick 6 Parker 7 Richard 6 Richard 6
Richard
Parker
6
7
Renad
Richard
7
6 { Parker
Renad
7
7
Renad
Parker
7
7
So a 7 So a 7 So a 7 So a 7
www.appmillers.com
fi
fi
fi
fi
Sorting Terminology
Increasing Order
- Example : 1, 3, 5, 7, 9 ,11
Decreasing Order
- Example : 11, 9, 7, 5, 3, 1
- If successive element is less than or equal to its previous element in the sequence.
- Example : 11, 9, 7, 5, 5, 3, 1
- If successive element is greater than or equal to its previous element in the sequence
- Example : 1, 3, 5, 7, 7, 9, 11
www.appmillers.com
Sorting Algorithms
Bubble sort
Selection sort
Insertion sort
Bucket sort
Merge sort
Quick sort
Heap sort
- Stability
- Space efficient
- Time efficient
www.appmillers.com
Bubble Sort
- Bubble sort is also referred as Sinking sort
- We repeatedly compare each pair of adjacent items and swap them if they are in the wrong order
5 9 3 1 2 8 4 7 6
www.appmillers.com
Bubble Sort
- Bubble sort is also referred as Sinking sort
- We repeatedly compare each pair of adjacent items and swap them if they are in the wrong order
5 3 1 2 8 4 7 6 9
www.appmillers.com
Bubble Sort
- Bubble sort is also referred as Sinking sort
- We repeatedly compare each pair of adjacent items and swap them if they are in the wrong order
3 1 2 5 4 7 6 8 9
www.appmillers.com
Bubble Sort
- Bubble sort is also referred as Sinking sort
- We repeatedly compare each pair of adjacent items and swap them if they are in the wrong order
1 2 3 4 5 6 7 8 9
www.appmillers.com
Bubble Sort
- Bubble sort is also referred as Sinking sort
- We repeatedly compare each pair of adjacent items and swap them if they are in the wrong order
1 2 3 4 5 6 7 8 9
www.appmillers.com
Bubble Sort
- Bubble sort is also referred as Sinking sort
- We repeatedly compare each pair of adjacent items and swap them if they are in the wrong order
1 2 3 4 5 6 7 8 9
www.appmillers.com
Bubble Sort
- Bubble sort is also referred as Sinking sort
- We repeatedly compare each pair of adjacent items and swap them if they are in the wrong order
1 2 3 4 5 6 7 8 9
www.appmillers.com
Bubble Sort
- Bubble sort is also referred as Sinking sort
- We repeatedly compare each pair of adjacent items and swap them if they are in the wrong order
1 2 3 4 5 6 7 8 9
www.appmillers.com
Bubble Sort
- Bubble sort is also referred as Sinking sort
- We repeatedly compare each pair of adjacent items and swap them if they are in the wrong order
1 2 3 4 5 6 7 8 9
www.appmillers.com
Bubble Sort
When to use Bubble Sort?
www.appmillers.com
Selection Sort
- In case of selection sort we repeatedly find the minimum element and move it to the sorted part of array
to make unsorted part sorted.
5
1 7 4 3 8 6 1 9 2
min
www.appmillers.com
Selection Sort
- In case of selection sort we repeatedly find the minimum element and move it to the sorted part of array
to make unsorted part sorted.
1 7
2 4 3 8 6 5 9 2
min
www.appmillers.com
Selection Sort
- In case of selection sort we repeatedly find the minimum element and move it to the sorted part of array
to make unsorted part sorted.
1 2 4
3 3 8 6 5 9 7
min
www.appmillers.com
Selection Sort
- In case of selection sort we repeatedly find the minimum element and move it to the sorted part of array
to make unsorted part sorted.
1 2 3 4 8 6 5 9 7
min
www.appmillers.com
Selection Sort
- In case of selection sort we repeatedly find the minimum element and move it to the sorted part of array
to make unsorted part sorted.
1 2 3 4 8
5 6 5 9 7
min
www.appmillers.com
Selection Sort
- In case of selection sort we repeatedly find the minimum element and move it to the sorted part of array
to make unsorted part sorted.
1 2 3 4 5 6 8 9 7
min
www.appmillers.com
Selection Sort
- In case of selection sort we repeatedly find the minimum element and move it to the sorted part of array
to make unsorted part sorted.
1 2 3 4 5 6 7
8 9 7
min
www.appmillers.com
Selection Sort
- In case of selection sort we repeatedly find the minimum element and move it to the sorted part of array
to make unsorted part sorted.
1 2 3 4 5 6 7 9
8 8
min
www.appmillers.com
Selection Sort
- In case of selection sort we repeatedly find the minimum element and move it to the sorted part of array
to make unsorted part sorted.
1 2 3 4 5 6 7 8 9
min
www.appmillers.com
Selection Sort
- In case of selection sort we repeatedly find the minimum element and move it to the sorted part of array
to make unsorted part sorted.
1 2 3 4 5 6 7 8 9
www.appmillers.com
Selection Sort
When to use Selection Sort?
www.appmillers.com
Insertion Sort
- Divide the given array into two part
- Take first element from unsorted array and find its correct position in sorted array
- Repeat until unsorted array is empty
5
3
2 4
3 4 7 2 8 6 9 1
www.appmillers.com
Insertion Sort
- Divide the given array into two part
- Take first element from unsorted array and find its correct position in sorted array
- Repeat until unsorted array is empty
2 3 4 5 7
6 8 6 9 1
www.appmillers.com
Insertion Sort
- Divide the given array into two part
- Take first element from unsorted array and find its correct position in sorted array
- Repeat until unsorted array is empty
2
1 3 4 5 6 7 8 9 1
www.appmillers.com
Insertion Sort
When to use Insertion Sort?
- When we have continuous inflow of numbers and we want to keep them sorted
www.appmillers.com
Bucket Sort
- Create buckets and distribute elements of array into buckets
- Sort buckets individually
- Merge buckets after sorting
5 3 4 7 2 8 6 9 1
www.appmillers.com
Bucket Sort
- Create buckets and distribute elements of array into buckets
- Sort buckets individually
- Merge buckets after sorting
3 4 7 2 8 6 9 1
5
Bucket 1 Bucket 2 Bucket 3
www.appmillers.com
Bucket Sort
- Create buckets and distribute elements of array into buckets
- Sort buckets individually
- Merge buckets after sorting
7 2 8 6 9 1
3 5 4
Bucket 1 Bucket 2 Bucket 3
www.appmillers.com
Bucket Sort
- Create buckets and distribute elements of array into buckets
- Sort buckets individually
- Merge buckets after sorting
8 6 9 1
3 2 5 4 7
Bucket 1 Bucket 2 Bucket 3
www.appmillers.com
Bucket Sort
- Create buckets and distribute elements of array into buckets
- Sort buckets individually
- Merge buckets after sorting
9 1
3 2 5 4 6 7 8
Bucket 1 Bucket 2 Bucket 3
www.appmillers.com
Bucket Sort
- Create buckets and distribute elements of array into buckets
- Sort buckets individually
- Merge buckets after sorting
3 2 1 5 4 6 7 8 9
Bucket 1 Bucket 2 Bucket 3
www.appmillers.com
Bucket Sort
- Create buckets and distribute elements of array into buckets
- Sort buckets individually
- Merge buckets after sorting
1 2 3 4 5 6 7 8 9
Bucket 1 Bucket 2 Bucket 3
www.appmillers.com
Bucket Sort
When to use Bucket Sort?
www.appmillers.com
Merge Sort
- Merge sort is a divide and conquer algorithm
- Divide the input array in two halves and we keep halving recursively until they become too
small that cannot be broken further
- Merge halves by sorting them
www.appmillers.com
Merge Sort
6 4 3 7 5 1 2
www.appmillers.com
Merge Sort
6 4 3 7 5 1 2
www.appmillers.com
Merge Sort
When to use Merge Sort?
www.appmillers.com
Quick Sort
- Quick sort is a divide and conquer algorithm
- Find pivot number and make sure smaller numbers located at the left of pivot and bigger numbers
are located at the right of the pivot.
- Unlike merge sort extra space is not required
70 10 80 30 90 40 60 20 50 10 20 30 40 50 80 60 70 90
10 30 40 20 50 80 60 70 90 10 20 30 40 50 60 70 80 90
10 20 40 30 50 80 60 70 90 10 20 30 40 50 60 70 80 90
10 20 30 40 50 80 60 70 90 10 20 30 40 50 60 70 80 90
www.appmillers.com
Quick Sort
- Quick sort is a divide and conquer algorithm
- Find pivot number and make sure smaller numbers located at the left of pivot and bigger
numbers are located at the right of the pivot.
- Unlike merge sort extra space is not required
3 5 8 1 2 9 4 7 6
L L R R P
www.appmillers.com
Quick Sort
- Quick sort is a divide and conquer algorithm
- Find pivot number and make sure smaller numbers located at the left of pivot and bigger
numbers are located at the right of the pivot.
- Unlike merge sort extra space is not required
3 5 4 1 2 9 8 7 6
L L LR R P
www.appmillers.com
Quick Sort
- Quick sort is a divide and conquer algorithm
- Find pivot number and make sure smaller numbers located at the left of pivot and bigger
numbers are located at the right of the pivot.
- Unlike merge sort extra space is not required
3 5 4 1 2 6 8 7 9
L L R R P
www.appmillers.com
Quick Sort
- Quick sort is a divide and conquer algorithm
- Find pivot number and make sure smaller numbers located at the left of pivot and bigger
numbers are located at the right of the pivot.
- Unlike merge sort extra space is not required
1 2 4 3 5 6 8 7 9
L R L PP
www.appmillers.com
Quick Sort
- Quick sort is a divide and conquer algorithm
- Find pivot number and make sure smaller numbers located at the left of pivot and bigger
numbers are located at the right of the pivot.
- Unlike merge sort extra space is not required
1 2 4
3 4
3 5 6 8 7 9
L R P L LRR LPP
www.appmillers.com
Quick Sort
- Quick sort is a divide and conquer algorithm
- Find pivot number and make sure smaller numbers located at the left of pivot and bigger
numbers are located at the right of the pivot.
- Unlike merge sort extra space is not required
1 2 3 4 5 6 8
7 8
7 9
L R P
www.appmillers.com
Quick Sort
When to use Quick Sort?
www.appmillers.com
Heap Sort
- Step 1 : Insert data to Binary Heap Tree
- Step 2 : Extract data from Binary Heap
- It is best suited with array, it does not work with Linked List
- The value of any given node must be less or equal of its children (min heap)
- The value of any given node must be greater or equal of its children (max heap)
10 20
30 40 50 60
70 80
www.appmillers.com
Heap Sort
- Step 1 : Insert data to Binary Heap Tree
- Step 2 : Extract data from Binary Heap
15 10 40 20 50 10 30 45 5
15
10 40
20 50 10 30
www.appmillers.com
Heap Sort
- Step 1 : Insert data to Binary Heap Tree
- Step 2 : Extract data from Binary Heap
15 10 40 20 50 10 30 45 5
10
15 10
20 50 40 30
45 5
www.appmillers.com
Heap Sort
- Step 1 : Insert data to Binary Heap Tree
- Step 2 : Extract data from Binary Heap
5 10
10 10
15 50 40 30
45 20
www.appmillers.com
Heap Sort
- Step 1 : Insert data to Binary Heap Tree
- Step 2 : Extract data from Binary Heap
5 10 10 15 20 30
10
15 30
20 50 40 45
www.appmillers.com
Heap Sort
- Step 1 : Insert data to Binary Heap Tree
- Step 2 : Extract data from Binary Heap
5 10 10 15 20 30 40 45 50
40
45 50
www.appmillers.com
Sorting Algorithms
Name Time Complexity Space Complexity Stable
www.appmillers.com
Linear Search in Java
www.appmillers.com
Linear Search Pseudocode
- Create function with two parameters which are an array and a value
- Loop through the array and check if the current array element is equal to the value
- If it is return the index at which the element is found
- If the value is never found return -1
www.appmillers.com
www.appmillers.com
Binary Search Pseudocode
- Create function with two parameters which are a sorted array and a value
- Create two pointers : a left pointer at the start of the array and a right pointer at the
end of the array.
- Based on left and right pointers calculate middle pointer
- While middle is not equal to the value and start<=end loop:
- if the middle is greater than the value move the right pointer down
- if the middle is less than the value move the left pointer up
- If the value is never found return -1
www.appmillers.com
Graph
What you will learn
What is a graph? Why do we need it?
Graph Terminologies
Topological Sorting
All pairs shortest path (BFS, Dijkstra, Bellman Ford and Floyd Warshall algorithms)
www.appmillers.com
What is a Graph?
Graph consists of a finite set of Vertices(or nodes) and a set of Edges which connect a pair of nodes.
Node
A D
Edge
B C
www.appmillers.com
Why Graph?
www.appmillers.com
Graph Terminology
- Vertices (vertex) : Vertices are the nodes of the graph
- Edge : The edge is the line that connects pairs of vertices
V1 V3
V5
V2 V4
www.appmillers.com
Graph Terminology
- Vertices : Vertices are the nodes of the graph
- Edge : The edge is the line that connects pairs of vertices
- Unweighted graph : A graph which does not have a weight associated with any edge
- Weighted graph : A graph which has a weight associated with any edge
- Undirected graph : In case the edges of the graph do not have a direction associated with them
- Directed graph : If the edges in a graph have a direction associated with them
V1 V3
8 12 V5
V2 V4
www.appmillers.com
Graph Terminology
- Vertices : Vertices are the nodes of the graph
- Edge : The edge is the line that connects pairs of vertices
- Unweighted graph : A graph which does not have a weight associated with any edge
- Weighted graph : A graph which has a weight associated with any edge
- Undirected graph : In case the edges of the graph do not have a direction associated with them
- Directed graph : If the edges in a graph have a direction associated with them
- Cyclic graph : A graph which has at least one loop
V1 V3
V5
V2 V4
www.appmillers.com
Graph Terminology
- Vertices : Vertices are the nodes of the graph
- Edge : The edge is the line that connects pairs of vertices
- Unweighted graph : A graph which does not have a weight associated with any edge
- Weighted graph : A graph which has a weight associated with any edge
- Undirected graph : In case the edges of the graph do not have a direction associated with them
- Directed graph : If the edges in a graph have a direction associated with them
- Cyclic graph : A graph which has at least one loop
- Acyclic graph : A graph with no loop
V1 V3
V5
V2
www.appmillers.com
Graph Terminology
- Vertices : Vertices are the nodes of the graph
- Edge : The edge is the line that connects pairs of vertices
- Unweighted graph : A graph which does not have a weight associated with any edge
- Weighted graph : A graph which has a weight associated with any edge
- Undirected graph : In case the edges of the graph do not have a direction associated with them
- Directed graph : If the edges in a graph have a direction associated with them
- Cyclic graph : A graph which has at least one loop
- Acyclic graph : A graph with no loop
- Tree: It is a special case of directed acyclic graphs
V1
V2 V3
V5
www.appmillers.com
Graph Types
Graph
Directed Undirected
www.appmillers.com
Graph Types
1. Unweighted - undirected
V1 V3
V5
V2 V4
www.appmillers.com
Graph Types
1. Unweighted - undirected
2. Unweighted - directed
V1 V3
V5
V2 V4
www.appmillers.com
Graph Types
1. Unweighted - undirected
2. Unweighted - directed
3. Positive - weighted - undirected
3
V1 V3
2
5
4 V5
3
V2 V4
3
www.appmillers.com
Graph Types
1. Unweighted - undirected
2. Unweighted - directed
3. Positive - weighted - undirected
4. Positive - weighted - directed
3
V1 V3
2
5
4 V5
3
V2 V4
3
www.appmillers.com
Graph Types
1. Unweighted - undirected
2. Unweighted - directed
3. Positive - weighted - undirected
4. Positive - weighted - directed
5. Negative - weighted - undirected
-3
V1 V3
2
-5
4 V5
3
V2 V4
3
www.appmillers.com
Graph Types
1. Unweighted - undirected
2. Unweighted - directed
3. Positive - weighted - undirected
4. Positive - weighted - directed
5. Negative - weighted - undirected
2
-5
4 V5
3
V2 V4
3
www.appmillers.com
Graph Representation
Adjacency Matrix : an adjacency matrix is a square matrix or you can say it is a 2D array. And the elements of the
matrix indicate whether pairs of vertices are adjacent or not in the graph
A B C D E
A B A 0 1
0 1
0 1
0 0
B 1
0 0 0 0 1
0
E
C 1
0 0 0 1
0 0
C D D 1
0 0 1
0 0 1
0
0 1
0 0 1
0 0
E
www.appmillers.com
Graph Representation
Adjacency List : an adjacency list is a collection of unordered list used to represent a graph. Each list describes the
set of neighbors of a vertex in the graph.
A B C D
A B
B A E
E C A D
D A C E
C D E B D
www.appmillers.com
Graph Representation
If a graph is complete or almost complete we should use Adjacency Matrix
If the number of edges are few then we should use Adjacency List
A B C D E
A B C D
A 0 1
0 1
0 1
0 0
B A E
B 1
0 0 0 0 1
0
C A D
C 1
0 0 0 1
0 0
D A C E
D 1
0 0 1
0 0 1
0
E B D
E 0 1
0 0 1
0 0
A B
C D
www.appmillers.com
Graph Traversal
Graph Traversal
It is a process of visiting all vertices in a given Graph
A B
- Breadth First Search
- Depth First Search
C D G
E F
www.appmillers.com
Level 1 A B
Level 2 C D G
Level 3 E F
www.appmillers.com
Breadth First Search Algorithm
BFS
A B
A B C D G E F
E F
Queue A B C D G E F
www.appmillers.com
}
enqueue any starting vertex O(1)
while queue is not empty
}
O(V)
p = dequeue() O(1) O(E)
if p is unvisited O(1)
O(Adj)
mark it visited O(1)
enqueue unvisited adjacent vertices of p O(Adj)
www.appmillers.com
A B
C D G
E F
www.appmillers.com
Depth First Search Algorithm
Stack
DFS
G A B
push any starting vertex
while stack is not empty
p = pop() B
if p is unvisited
mark it visited
Push all adjacent D
unvisited vertices of p C D G
G
F
A C E F D B G
E
E F
A
www.appmillers.com
}
push any starting vertex O(1)
while stack is not empty
}
O(V)
p = pop() O(1) O(E)
if p is unvisited O(1)
O(Adj)
mark it visited O(1)
push unvisited adjacent vertices of p O(Adj)
www.appmillers.com
BFS vs DFS
Level 1 A B A B
G G
Level 2 C D C D
Level 3 E F E F
BFS DFS
www.appmillers.com
BFS vs DFS
BFS DFS
How does it work internally? It goes in breath rst It goes in depth rst
If we know that the target is close to the If we already know that the target vertex is
When to use? staring point buried very deep
www.appmillers.com
fi
fi
Topological Sort
Topological Sort
Topological Sort: Sorts given actions in such a way that if there is a dependency of one action on another, then the
dependent action always comes later than its parent action.
Buy
breakfast
Exercise fruits
Prepare
Bath Breakfast
Wash
Breakfast dishes
Work
Topological Sort Algorithm
Stack
D C D
ABCDEHFG
A
BACDEHFG
C E
BDACEFGH
E
H F G
}
A B
topologicalSort(G)
for all nodes
O(V)
if vertex not visited
topologicalVisit(node)
C D
topologicalVisit(node)
for each nodes
if not visited
topologicalVisit(neighbor)
} O(E)
H
E
F G
www.appmillers.com
30
Berlin
20
Berlin
10
10
www.appmillers.com
A
Level 1 A B
B C
Level 2 C D G
Level 3 E F
www.appmillers.com
A B C D G E F C D
E F
Queue A B C D G E F
www.appmillers.com
www.appmillers.com
Unweighted - undirected OK
Unweighted - directed OK
www.appmillers.com
Why BFS not work with weighted Graph?
A
10 50
A B C
A
30 5 9 30
B 6 B
D E F G
7 20
H I
www.appmillers.com
Why does DFS not work SSSPP?
DFS has the tendency to go “as far as possible” from source, hence it can never find “Shortest Path”
B C
D E F
www.appmillers.com
1
Dijkstra’s Algorithm 3
1
4
Dijkstra’s Algorithm for SSSPP
min min
B∞
Bv2
Bv 3 E∞
Ev5
Ev
2 1 4 9
min
A
Av0 6 D∞
Dv3
Dv G∞
Gv14
Gv
5 7
C∞
Cv5
Cv 8 F13
∞
Fv13
Fv
min min
www.appmillers.com
Dijkstra’s Algorithm for SSSPP
B 3 E
2 1 4 9
A 6 D G
5 7
C 8 F
www.appmillers.com
Dijkstra’s Algorithm with Negative Cycle
A path is called a negative cycle if:
There is a cycle (a cycle is a path of edges or vertices wherein a vertex is reachable from itself)
Total weight of cycle is a negative number
3
A B
4
-6
6 1
E
1 2
C D
1 + 3 + (-6) = -2
www.appmillers.com
Dijkstra’s Algorithm with Negative Cycle
We cannot never find a negative cycle in a graph
Path from A to B = 6 + 1 = -5
= -5+3+(-6)+1 = -7
3 = -7+3+(-6)+1 = -9
A B
4 = -9+3+(-6)+1 = -11
-6
6 1
E
1 2
C D
www.appmillers.com
1
4
Bellman Ford Algorithm
Single Source Shortest Path Algorithm
Unweighted - undirected OK OK OK
Unweighted - directed OK OK OK
Negative Cycle X X OK
www.appmillers.com
Bellman Ford Algorithm
Bellman Ford algorithm is used to find single source shortest path problem. If there is a negative cycle it catches it
and report its existence.
3
A B
4
6
6 1
E
1 2
C D
www.appmillers.com
Bellman Ford Algorithm
∞ ∞
A 3
B
C->D 1 B ∞
D->C 2 C ∞
D->B 1
D ∞
E->B 4
E->D 2 E 0
www.appmillers.com
www.appmillers.com
www.appmillers.com
www.appmillers.com
E->B 4 D ∞ 2 E 2 E 2 E 2 E
E->D 2 E 0 0 - 0 - 0 - 0 -
www.appmillers.com
www.appmillers.com
C->D 1 B ∞
D->C 2 C ∞
D->B 1
D ∞
E->B 4
E->D 2 E 0
www.appmillers.com
www.appmillers.com
www.appmillers.com
E->B 4 D ∞ 2 E 2 E 7+(-6)=1 A
E->D 2 E 0 0 - 0 - 0
www.appmillers.com
www.appmillers.com
www.appmillers.com
www.appmillers.com
∞
76 ∞
43
A 3
B
10 20 30 40
A B C D E
4
-6 0
6 1
E
60
∞ 50
∞ 30
∞ 0
1 2 10 20 30
C D A B C D
∞ 2 ∞
A->
B->
C->D
www.appmillers.com
C
Unweighted - undirected OK OK OK
Unweighted - directed OK OK OK
Negative Cycle X X OK
www.appmillers.com
BFS vs Dijkstra vs Bellman Ford
BFS Dijkstra Bellman Ford
Time complexity O(V2) O(V2) O(VE)
Space complexity O(E) O(V) O(V)
Implementation Easy Moderate Moderate
Limitation Not work for weighted graph Not work for negative cycle N/A
OK OK OK
Unweighted graph Use this as time complexity is good and Not use as implementation not Not use as time complexity is
easy to implement easy bad
X OK OK
Weighted graph Use as time complexity is better Not use as time complexity is
Not supported
than Bellman bad
X X OK
Negative Cycle Use this as others not
Not supported Not supported
support
www.appmillers.com
A single source problem is about finding a path between a given vertex (called source) to all other
vertices in a graph such that the total distance between them (source and destination) is minimum.
5
5
35
London Barcelona 10
London
Barcelona
10
30
20 Berlin
Berlin
10
10
www.appmillers.com
The problem:
- Five offices in different cities.
- Travel costs between these cities are known.
- Find the cheapest way to reach each office from every
other of ce
Paris
10 Rome
5
35
London
Barcelona 10
30
20
Berlin
10
www.appmillers.com
fi
5 5 5
35
London Barcelona 10 London Barcelona 10 London Barcelona 10
20 30 20
Berlin Berlin Berlin
10 10
5 5 5
35 35 35
London Barcelona 10 London Barcelona 10 London Barcelona 10
20 20 20
Berlin Berlin Berlin
www.appmillers.com
Dry Run for All Pair Shortest Path Problem
6
∞ ∞
3
4
A 3 Source Vertex E Path
B
4 A E->D->B->A
6 0
6 1 B E->D->B
E
1 2 C E->D->C
C D
∞
2 2 ∞
2 D E->D
E -
Dijkstra
www.appmillers.com
Dry Run for All Pair Shortest Path Problem
4
∞ ∞
1
A 3 Source Vertex E Path Source Vertex D Path
B
4 A E->D->B->A A D->B->A
6 ∞
6 1 B E->D->B B D->B
E
1 2 C E->D->C C D->C
C D
∞
2 2 0 D E->D D -
E - E N/A
Dijkstra
www.appmillers.com
Dry Run for All Pair Shortest Path Problem
4 A E->D->B->A A D->B->A
6
6 1 B E->D->B B D->B
E
1 2 C E->D->C C D->C
C D
2 D E->D D -
E - E N/A
Dijkstra , BFS and Bellmand Ford
A C->D->B->A A B->A A -
B C->D->B B - B A->D->B
C - C B->A->C C A->D->C
www.appmillers.com
1
4
Floyd Warshall Algorithm
A
8
B Given Iteration 1
A B C D via A A B C D
4
1 A 0 8 ∞ 1 A 0 8 ∞ 1
1
2 B ∞ 0 1 ∞ B ∞ 0 1 ∞
C 4 ∞ 0 ∞ C 4 4+8=12 0 4+1=5
D C
9 D ∞ 2 9 0 D ∞ 2 9 0
C -> B
If D[C][B] > D[C][A] + D[A][B]:
D[C][B] = D[C][A] + D[A][B]
∞ > 4 + 8 = 12
C -> D
If D[C][D] > D[C][A] + D[A][D]:
D[C][D] = D[C][A] + D[A][D]
∞ > 4 + 1 = 5
www.appmillers.com
∞ > 8 + 1 = 9
D -> C
If D[D][C] > D[D][B] + D[B][C]:
D[D][C] = D[D][B] + D[B][C]
9 > 2 + 1 = 3
www.appmillers.com
C 4 12 0 5 C 4 12 0 5
2+1+4
D ∞ 2 2+1=3 0 D =7
2 3 0
www.appmillers.com
www.appmillers.com
A -8
B -8+1+4=-3
4
1 1
2
D C
9
- To go through cycle we need to go via negative cycle participating vertex at least twice
www.appmillers.com
Which algorithm to use for APSP?
Unweighted - undirected OK OK OK OK
Unweighted - directed OK OK OK OK
Negative Cycle X X OK X
www.appmillers.com
Which algorithm to use for APSP?
X OK OK OK
Weighted graph Not use as time Can be preferred as
Not supported Can be used complexity is bad
implementation easy
X X OK X
Negative Cycle Use this as others not
Not supported Not supported No supported
support
www.appmillers.com
Minimum Spanning Tree
A Minimum Spanning Tree (MST) is a subset of the edges of connected, weighted and undirected graph which :
5 5
10 10
20 15 15
35
25
10 10
AppMiller
www.appmillers.com
e
fi
s
ff
s
10
35 20 15
25
10
5 5
10 10
15 20 15
10
AppMiller
www.appmillers.com
s
Disjoint Set
It is a data structure that keeps track of set of elements which are partitioned into a number of
disjoint and non overlapping sets and each sets have representative which helps in identifying
that sets.
- Make Se
- Unio
- Find Set
AppMiller
www.appmillers.com
n
Disjoint Set
makeSet(N) : used to create initial set
union(x,y): merge two given sets
ndSet(x): returns the set name in which this element is there
A B C D E
union(A,B)
ndSet(B)
AB C D E
union(A, E)
ABE C D ndSet(E)
AppMiller
www.appmillers.com
fi
fi
fi
s
Kruskal’s Algorithm 2
4
Kruskal’s Algorithm
It is a greedy algorithm
It finds a minimum spanning tree for weighted undirected graphs in two steps
- Add increasing cost edges at each step
- Avoid any cycle at each step
A 5 A 5
B B
15 15
10
E 13 8 E 8
20
C D C D
6 6
www.appmillers.com
Cost = 0 +5 +6 +8 +15 = 34
A 5
B
15
E 8
C D
6
A
AB
ABCD
ABCDE B C
CD D E
www.appmillers.com
Kruskal(G)
for each vertex
O(v)
makeSet(v)
sort each edge in non decreasing order by weight O(eloge)
}
for each edge (u, v) O(e)
if findSet(u) ≠ findSet(v) O(1)
union(u, v) O(ev)
O(v)
cost = cost + edge(u,v) O(1)
www.appmillers.com
Prim’s Algorithm
It is a greedy algorithm
It finds a minimum spanning tree for weighted undirected graphs in following ways
1. Take any vertex as a source set its weight to 0 and all other vertices’ weight to infinity
2. For every adjacent vertices if the current weight is more than current edge then we set it to current edge
3. Then we mark current vertex as visited
4. Repeat these steps for all vertices in increasing order of weight
10 5
∞
10 5
∞
5 B D
B D
10 0
0
30
15 A
A 8
20
6 C E
C E
6 8
∞
15
20
6 ∞
8
www.appmillers.com
Kruskal vs Prim
Kruskal
- Concentrates on Edges
- Finalize edge in each iteration
5
5 5 5 A B
A
5 A B A B
A B
15
B
15
E 8
E 13 10 8
8
6 6
20 C D 6 C D
6 C D
C D
Prim’s
- Concentrates on Vertices
- Finalize Vertex in each iteration
A
5 A
5 A
5 A
5 A
5
B B B B B
15 15 15 15 15
E 13 10 E 13 10 E 13 10 E 13 10 E 13 10
8 8 8 8 8
20 6 20 6 20 6 20 6 20 6
C D C D C D C D C D
www.appmillers.com
Kruskal vs Prim
Kruskal
- Concentrates on Edges
- Finalize edge in each iteration
5
5 5 5 A B
A
5 A B A B
A B
15
B
15
E 8
E 13 10 8
8
6 6
20 C D 6 C D
6 C D
C D
Prim’s
- Concentrates on Vertices
- Finalize Vertex in each iteration
A
5 A
5 A
5 A
5 A
5
B B B B B
15 15 15 15 15
E 13 10 E 13 10 E 13 10 E 13 10 E 13 10
8 8 8 8 8
20 6 20 6 20 6 20 6 20 6
C D C D C D C D C D
www.appmillers.com
Kruskal vs Prim
Kruskal Applications
- Landing cables
- TV Network
- Tour Operations
- LAN Networks
- A network of pipes for drinking water or natural gas.
- An electric grid
- Single-link Cluster
Prim’s Applications
- Network for roads and Rail tracks connecting all the cities.
- Irrigation channels and placing microwave towers
- Designing a fiber-optic grid or ICs.
- Traveling Salesman Problem.
- Cluster analysis.
- Pathfinding algorithms used in AI(Artificial Intelligence).
www.appmillers.com
Route Between Nodes
Problem Statement:
Given a directed graph and two nodes (S and E), design an algorithm to find out whether there is a route from S to E.
E
A —> E False
F A A —> J True
A —> I True
I C D B
H G J
www.appmillers.com
Route Between Nodes
Problem Statement:
Given a directed graph and two nodes (S and E), design an algorithm to find out whether there is a route from S to E.
F A Pseudocode
- Create function with two parameters start and end nodes
- Create queue and enqueue start node to it
- Find all the neighbors of the just enqueued node and enqueue
I C D B them into the queue
- Repeat this process until the end of elements in graph
- If during the above process at some point in time we encounter
the destination node, we return True.
- Mark visited nodes as visited
H G J
www.appmillers.com
Minimal Tree
Problem Statement:
Given a sorted (increasing order) array with unique integer elements, write an algorithm to create a binary search
tree with minimal height.
Root Node
1 2 3 4 5 6 7 8 9
3 8
2 4 7 9
1 6
www.appmillers.com
List of Depths
Problem Statement:
Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i e , if
you have a tree with depth D, you’ll have D linked lists)
Level 1 1 1 None
001
001 111
Level 3 4 5 6 7
www.appmillers.com
List of Depths
Problem Statement:
Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each
depth (i e , if you have a tree with depth D, you’ll have D linked lists)
custDict
Pre Order Traversal Keys Values
1
1 3 001
2 111 3
2 3 2 001 111
www.appmillers.com
Check Balanced
Problem Statement:
Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined
to be a tree such that the heights of the two subtrees of any node never differ by more than one.
N1
N2 N3
N4 N5 N6
www.appmillers.com
Check Balanced
Problem Statement:
Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined
to be a tree such that the heights of the two subtrees of any node never differ by more than one.
N1
N2
N4 N5
www.appmillers.com
Check Balanced
Problem Statement:
Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined
to be a tree such that the heights of the two subtrees of any node never differ by more than one.
N4 N5 N6
www.appmillers.com
Validate BST
Problem Statement:
- The left subtree of a node contains only nodes with keys less than the node’s key
- The right subtree of a node contains only nodes with keys greater than the node’s key
- These conditions are applicable for both left and right subtrees
2 4
1 4 1 3
True
2 5
False
www.appmillers.com
Validate BST
Problem Statement:
Min Max
-∞ <= 3 <= ∞
www.appmillers.com
Validate BST
Problem Statement:
Min Max
-∞ <= 3 <= ∞
www.appmillers.com
Successor
Problem Statement:
Write an algorithm to find the next node (i.e in-order successor) of given node in a binary search tree. You may assume
that each node has a link to its parent.
4 In-order successor of 1 is 2
In-order successor of 3 is 4
8 In-order successor of 2 is 3
2
1 3 5 9
www.appmillers.com
Successor
Problem Statement:
Write an algorithm to find the next node (i.e in-order successor) of given node in a binary search tree. You may assume
that each node has a link to its parent.
- If right subtree of node is not None, then successor lies in right subtree.
- If right subtree of node is None, then successor is one of the ancestors.
2 8
1 3 5 9
www.appmillers.com
Build Order
Problem Statement:
You are given a list of projects and a list of dependencies (which is a list of pairs of projects, where the second project is
dependent on the first project). All of a project's dependencies must be built before the project is. Find a build order that will
allow the projects to be built. If there is no valid build order, return an error.
Input:
Projects : a, b, c, d, e, f
Dependencies: (a, d), (f, b), (b, d), (f, a), (d, c)
Output:
e, f, a, b, d, c
a b c
d e f
www.appmillers.com
Build Order
Problem Statement:
You are given a list of projects and a list of dependencies (which is a list of pairs of projects, where the second project is
dependent on the first project). All of a project's dependencies must be built before the project is. Find a build order that will
allow the projects to be built. If there is no valid build order, return an error.
www.appmillers.com
First Common Ancestor
Problem Statement:
Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional
nodes in a data structure. NOTE: This is not necessarily a binary search tree.
55
22 99
35 88 90 95
33
54
www.appmillers.com
First Common Ancestor
Problem Statement:
Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional
nodes in a data structure. NOTE: This is not necessarily a binary search tree.
55
findNodeinTree()
findNodeinTree(88, left-44) True
findNodeinTree(33, left-44) True
44 77
findNodeinTree(88, left-22) True
findNodeinTree(33, left-22) False
35 88 90 95
33
54
www.appmillers.com
First Common Ancestor
Problem Statement:
Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional
nodes in a data structure. NOTE: This is not necessarily a binary search tree.
55
44 77
22 99
35 88 90 95
33
54
www.appmillers.com
Greedy Algorithms
What is Greedy Algorithm?
- It is an algorithmic paradigm that builds the solution piece by piece
- In each step it chooses the piece that offers most obvious and immediate benefit
- It fits perfectly for those solutions in which local optimal solutions lead to global solution
Global solution
Local solutions
www.appmillers.com
What is Greedy Algorithm?
- Insertion Sort
- Selection Sort
- Topological Sort
- Prim’s Algorithm
- Kruskal Algorithm
www.appmillers.com
Greedy Algorithms
- Insertion Sort
- Selection Sort
- Topological Sort
- Prim’s Algorithm
- Kruskal Algorithm
www.appmillers.com
Greedy Algorithms
Insertion Sort
5 3 4 7 2 8 6 9 1
2 3 4 5 7 8 6 9 1
www.appmillers.com
Greedy Algorithms
Selection Sort
5 7 4 3 8 6 1 9 2
1 2 3 4 8 6 5 9 7
min
www.appmillers.com
Greedy Algorithms
Topological Sort
Stack
A
ACEH E
C
H
www.appmillers.com
Greedy Algorithms
Prims Algorithm
It is a greedy algorith
It nds a minimum spanning tree for weighted undirected graphs in following ways
1. Take any vertex as a source set its weight to 0 and all other vertices’ weight to in nity
2. For every adjacent vertices if the current weight is more than current edge then we set it
to current edge
3. Then we mark current vertex as visite
4. Repeat these steps for all vertices in increasing order of weight
10 5
∞
10 5
∞
5 B D
B D
10 0
0
30
15 A
A 8
20
6 C E
C E
6 8
∞
20
15
6 ∞
8
www.appmillers.com
fi
fi
Greedy Algorithms
Kruskal’s Algorithm
It is a greedy algorith
It nds a minimum spanning tree for weighted undirected graphs in two ways
- Add increasing cost edges at each ste
- Avoid any cycle at each step
A 5 A 5
B B
15 15
13 10
E 8 E 8
20
6 6
C D C D
www.appmillers.com
fi
m
Activity A1 A2 A3 A4 A5 A6
Start 0 3 1 5 5 8 2 Activities
Finish 6 4 2 8 7 9
Activity A3 A2 A1 A5 A4 A6
Start 1 3 0 5 5 8 4 Activities
Finish 2 4 6 7 8 9
www.appmillers.com
Activity selection problem
www.appmillers.com
You are given coins of di erent denominations and total amount of money. Find the minimum
number of coins that you need yo make up the given amount.
In nite supply of denominations : {1,2,5,10,20,50,100,1000}
Example 1
Total amount : 70
Answer: 2 —> 50 + 20 = 70
Example 2
www.appmillers.com
fi
ff
Coin Change Problem
You are given coins of di erent denominations and total amount of money. Find the minimum
number of coins that you need yo make up the given amount.
www.appmillers.com
fi
ff
Coin Change Problem
Find the biggest coin that is less than given total number
Add coin to the result and subtract coin from total number
If V is equal to zero:
Then print result
else:
Repeat Step 2 and 3
www.appmillers.com
10 kg, Value : 20
20 kg, Value : 10
30 kg
10 kg, Value : 30
www.appmillers.com
Fractional Knapsack Problem
Given a set of items, each with a weight and a value, determine the number of each item to include
in a collection so that the total weight is less than or equal to a given limit and the total value is as
large as possible.
100 / 20 = 5 120 / 30 = 4 60 / 10 = 6
50 kg
www.appmillers.com
Fractional Knapsack Problem
Given a set of items, each with a weight and a value, determine the number of each item to include
in a collection so that the total weight is less than or equal to a given limit and the total value is as
large as possible.
60 / 10 = 6 100 / 20 = 5 120 / 30 = 4
50 kg
www.appmillers.com
Fractional Knapsack Problem
Take items with the highest ratio sequentially until weight allows
www.appmillers.com
What is Divide and Conquer Algorithm?
Divide and conquer is an algorithm design paradigm which works by recursively breaking down a problem
into subproblems of similar type, until these become simple enough to be solved directly. The solutions to the
subproblems are then combined to give a solution to the original problem.
Website
Func 1 Func 1
Func 2 Func 2
Func 3 Func 3
Func 4 Func 4
www.appmillers.com
Dynamic Programming
Dynamic Programming - Number Factor
Problem Statement:
Given N, find the number of ways to express N as a sum of 1, 3 and 4.
Example 1
- N=4
- Number of ways = 4
- Explanation : There are 4 ways we can express N. {4},{1,3},{3,1},{1,1,1,1}
Example 2
- N=5
- Number of ways = 6
- Explanation : There are 6 ways we can express N. {4,1},{1,4},{1,3,1},{3,1,1},{1,1,3},{1,1,1,1,1}
www.appmillers.com
NumberFactor(N)
If N in (0,1,2) return 1
If N = 3 return 2
Else
return NumberFactor(N-1) + NumberFactor(N-3) + NumberFactor(N-4)
www.appmillers.com
NumberFactor(8)
www.appmillers.com
NumberFactor(8)
www.appmillers.com
NumberFactor(N):
If N in (0,1,2) return 1
If N = 3 return 2
Else
rec1 = NumberFactor(N-1)
rec2 = NumberFactor(N-3)
rec3 = NumberFactor(N-4)
www.appmillers.com
NumberFactor(N, dp):
If N in (0,1,2) return 1
If N = 3 return 2
Else
rec1 = NumberFactor(N-1)
rec2 = NumberFactor(N-3)
rec3 = NumberFactor(N-4)
www.appmillers.com
NumberFactor(N, dp):
If N in (0,1,2) return 1
If N = 3 return 2
Elif N in dp return dp[N]
Else
rec1 = NumberFactor(N-1)
rec2 = NumberFactor(N-3)
rec3 = NumberFactor(N-4)
www.appmillers.com
NumberFactor(N, dp):
If N in (0,1,2) return 1
If N = 3 return 2
Elif N in dp return dp[N]
Else
rec1 = NumberFactor(N-1)
rec2 = NumberFactor(N-3)
rec3 = NumberFactor(N-4)
dp[N] = rec1 + rec2 + rec3
www.appmillers.com
NumberFactor(N, dp):
If N in (0,1,2) return 1
If N = 3 return 2
Elif N in dp return dp[N]
Else
rec1 = NumberFactor(N-1)
rec2 = NumberFactor(N-3)
rec3 = NumberFactor(N-4)
dp[N] = rec1 + rec2 + rec3
return dp[N]
www.appmillers.com
If N in (0,1,2) return 1
If N = 3 return 2
Elif N in dp return dp[N] Step 2
Else
rec1 = NumberFactor(N-1)
rec2 = NumberFactor(N-3)
rec3 = NumberFactor(N-4)
dp[N] = rec1 + rec2 + rec3 Step 3
return dp[N] Step 4
www.appmillers.com
NF(0) NF(1) NF(2) NF(3) NF(4) NF(5) NF(6) NF(7) NF(0) NF(1) NF(2) NF(3) NF(4) NF(5) NF(6) NF(7)
1 1 1 2 NF6+NF4+NF3
1 1 1 2 2+1+1=4 NF4+NF2+NF1 NF5+NF3+NF2 NF6+NF4+NF3
NF(0) NF(1) NF(2) NF(3) NF(4) NF(5) NF(6) NF(7) NF(0) NF(1) NF(2) NF(3) NF(4) NF(5) NF(6) NF(7)
NF(0) NF(1) NF(2) NF(3) NF(4) NF(5) NF(6) NF(7) NF(0) NF(1) NF(2) NF(3) NF(4) NF(5) NF(6) NF(7)
NF(0) NF(1) NF(2) NF(3) NF(4) NF(5) NF(6) NF(7) NF(0) NF(1) NF(2) NF(3) NF(4) NF(5) NF(6) NF(7)
www.appmillers.com
Bottom Up Approach
NF(0) NF(1) NF(2) NF(3) NF(4) NF(5) NF(6) NF(7) NF(0) NF(1) NF(2) NF(3) NF(4) NF(5) NF(6) NF(7)
1 1 1 2 NF3+NF1+NF0
1 1 1 2 2+1+1=4 NF4+NF2+NF1 NF6+NF3+NF2 NF6+NF4+NF3
NF(0) NF(1) NF(2) NF(3) NF(4) NF(5) NF(6) NF(7) NF(0) NF(1) NF(2) NF(3) NF(4) NF(5) NF(6) NF(7)
NF(0) NF(1) NF(2) NF(3) NF(4) NF(5) NF(6) NF(7) NF(0) NF(1) NF(2) NF(3) NF(4) NF(5) NF(6) NF(7)
NF(0) NF(1) NF(2) NF(3) NF(4) NF(5) NF(6) NF(7) NF(0) NF(1) NF(2) NF(3) NF(4) NF(5) NF(6) NF(7)
www.appmillers.com
Bottom Up Approach
numberFactor(n)
tb = {1,1,1,2}
for i in range(4, n+1):
tb.append(tb[i-1]+tb[i-3]+tb[i-4])
return tb[n]
www.appmillers.com
Example 1
6 7 1 30 8 2 4
Answer
- Maximum amount = 4
- Houses that are stolen : 7, 30, 4
Option1 = 6 + f(5)
Max(Option1, Option2)
Option2 = 0 + f(6)
www.appmillers.com
1
maxValueHouse(houses, currentHouse)
If currentHouse > length of houses
return 0
Else
stealFirstHouse = currentHouse + maxValueHouse(houses, currentHouse+2)
skipFirstHouse = maxValueHouse(houses, currentHouse+1)
return max(stealFirstHouse, skipFirstHouse)
www.appmillers.com
maxValueHouse(0)
maxValueHouse(2) maxValueHouse(1)
www.appmillers.com
maxValueHouse(0)
maxValueHouse(2) maxValueHouse(1)
www.appmillers.com
maxValueHouse(houses, currentHouse)
If currentHouse > length of houses
return 0
Else
stealFirstHouse = currentHouse + maxValueHouse(houses, currentHouse+2)
skipFirstHouse = maxValueHouse(houses, currentHouse+1)
return max(stealFirstHouse, skipFirstHouse)
www.appmillers.com
www.appmillers.com
www.appmillers.com
www.appmillers.com
6 7 1 30 8 2 4
max(H0+HR2, HR1)
max(H0+HR2, HR1) max(H1+HR3, HR2) max(H2+HR4, HR3) max(H3+HR5, HR4) max(H4+HR6, HR5)
max(H0+HR2, HR1) max(H1+HR3, HR2) max(H2+HR4, HR3) max(H3+HR5, HR4) max(H4+HR6, HR5) max(H5+HR7, HR6)
max(H0+HR2, HR1) max(H1+HR3, HR2) max(H2+HR4, HR3) max(H3+HR5, HR4) max(H4+HR6, HR5) max(H5+HR7, HR6) max(H6+HR8, HR7)
www.appmillers.com
6 7 1 30 8 2 4
max(H0+HR2, HR1) max(H1+HR3, HR2) max(H2+HR4, HR3) max(H3+HR5, HR4) max(H4+HR6, HR5) max(H5+HR7, HR6) max(H6+HR8, HR7)
max(H0+HR2, HR1) max(H1+HR3, HR2) max(H2+HR4, HR3) max(H3+HR5, HR4) max(H4+HR6, HR5) max(H5+HR7, HR6) 4
max(H0+HR2, HR1) max(H1+HR3, HR2) max(H2+HR4, HR3) max(H3+HR5, HR4) max(H4+HR6, HR5) max(2+0, 4)=4 4
41 41 34 34 12 4 4
www.appmillers.com
6 7 1 30 8 2 4
www.appmillers.com
Bottom Up Approach
houseRobberBU(houses, currentIndex)
tempAr = [0]*(length(houses)+2)
for i in length(houses)-1 until -1
tempAr[i] = max(houses[i]+tempAr[i+2], tempAr[i+1])
return tempAr[0]
www.appmillers.com
Example 1
- S1 = “catch
- S2 = “carch
- Output =
- Explanation : Replace “r” with “t”
Example 2
- S1 = “table
- S2 = “tbres
- Output =
- Explanation : Insert “a” to second position, replace “r” with “l” and delete “s”
www.appmillers.com
1
Else
deleteOp = 1 + findMinOperation(s1, s2, index1, index2+1)
insertOp = 1 + findMinOperation(s1, s2, index1+1, index2)
replaceOp = 1 + findMinOperation(s1, s2, index1+1, index2+1)
return min(deleteOp, insertOp, replaceOp)
www.appmillers.com
www.appmillers.com
www.appmillers.com
www.appmillers.com
www.appmillers.com
A Recipe for Problem Solving
www.appmillers.com
A Recipe for Problem Solving
💣
5 STEPS FOR PROBLEM SOLVING
www.appmillers.com
A Recipe for Problem Solving
EXPLORE EXAMPLES
BREAK IT DOWN
SOLVE / SIMPLIFY
www.appmillers.com
Step 1 - UNDERSTAND THE PROBLEM
www.appmillers.com
Step 1 - UNDERSTAND THE PROBLEM
www.appmillers.com
Step 1 - UNDERSTAND THE PROBLEM
5. What should I label the important piece of data that are the
part of a problem?
www.appmillers.com
Step 1 - UNDERSTAND THE PROBLEM
Write a function that takes two numbers and returns their sum
Implement addition
Yes
5. What should I label the important piece of data that are the part of a
problem?
Add, Sum
www.appmillers.com
A Recipe for Problem Solving
UNDERSTAND THE PROBLEM
EXPLORE EXAMPLES
www.appmillers.com
Step 2 - EXPLORE EXAMPLES
www.appmillers.com
Step 2 - EXPLORE EXAMPLES
www.appmillers.com
Step 2 - EXPLORE EXAMPLES
www.appmillers.com
Step 3 - BREAK IT DOWN
www.appmillers.com
SOLVE / SIMPLIFY
www.appmillers.com
SOLVE / SIMPLIFY
www.appmillers.com
fi
fi
fi
fi
LOOK BACK REFACTOR
www.appmillers.com
Summarize
UNDERSTAND THE PROBLEM
EXPLORE EXAMPLES
BREAK IT DOWN
SOLVE / SIMPLIFY
www.appmillers.com