Home Week-6 Week-8

 

PDSA - Week 7

Balanced search tree (AVL Tree)

Binary search tree

AVL Tree

Example for creation of AVL Tree

 

Implementation

Sample Input

Output

 

 

Greedy Algorithm

 

Interval scheduling

Scenario example

▪ IIT Madras has a special video classroom for delivering online lectures

▪ Different teachers want to book the classroom

▪ Slots may overlap, so not all bookings can be honored

▪ Choose a subset of bookings to maximize the number of teachers who get to use the room

 

Algorithm

  1. Sort all jobs which based on end time in increasing order.
  2. Take the interval which has earliest finish time.
  3. Repeat next two steps till you process all jobs.
  4. Eliminate all intervals which have start time less than selected interval’s end time.
  5. If interval has start time greater than current interval’s end time, at it to set. Set current interval to new interval.

 

Implementation

Output

Analysis

 

Example

In the table below, we have 8 activities with the corresponding start and finish times, It might not be possible to complete all the activities since their time frame can conflict. For example, if any activity starts at time 0 and finishes at time 4, then other activities can not start before 4. It can be started at 4 or afterwards.

What is the maximum number of activities which can be performed without conflict?

ActivityStart timeFinish time
A12
B34
C06
D14
E45
F59
G911
H810

Answer

5

 

 

Minimize lateness

Scenario example

▪ IIT Madras has a single 3D printer

▪ A number of users need to use this printer

▪ Each user will get access to the printer, but may not finish before deadline

▪ Goal is to minimize the lateness

Algorithm

  1. Sort all job in ascending order of deadlines

  2. Start with time t = 0

  3. For each job in the list

    1. Schedule the job at time t
    2. Finish time = t + processing time of job
    3. t = finish time
  4. Return (start time, finish time) for each job

 

Implementation

Output

Analysis

 

 

Huffman Algorithm

Algorithm

  1. Calculate the frequency of each character in the string.
  2. Sort the characters in increasing order of the frequency.
  3. Make each unique character as a leaf node.
  4. Create an empty node z. Assign the minimum frequency to the left child of z and assign the second minimum frequency to the right child of z. Set the value of the z as the sum of the above two minimum frequencies.
  5. Remove these two minimum frequencies from Q and add the sum into the list of frequencies.
  6. Insert node z into the tree.
  7. Repeat steps 3 to 5 for all the characters.
  8. For each non-leaf node, assign 0 to the left edge and 1 to the right edge.

 

Example

Implementation

Output

 

Huffman Implementation using Min Heap

Contribute by:- Jivitesh Sabharwal

Output

 

Analysis