Week 7N
Week 7N
Week 7 Revision
Balanced search tree (AVL Tree)
Greedy Algorithm
Interval scheduling
Minimize lateness
Huffman Algorithm
AVL Tree
Implementation
1 class AVLTree:
2 # Constructor:
3 def __init__(self,initval=None):
4 self.value = initval
5 if self.value:
6 self.left = AVLTree()
7 self.right = AVLTree()
8 self.height = 1
9 else:
10 self.left = None
11 self.right = None
12 self.height = 0
13 return
14
15 def isempty(self):
16 return (self.value == None)
17
18 def isleaf(self):
19 return (self.value != None and self.left.isempty() and
self.right.isempty())
20
21 def leftrotate(self):
22 v = self.value
23 vr = self.right.value
24 tl = self.left
25 trl = self.right.left
26 trr = self.right.right
27 newleft = AVLTree(v)
28 newleft.left = tl
29 newleft.right = trl
30 self.value = vr
31 self.right = trr
32 self.left = newleft
33 return
34
35 def rightrotate(self):
36 v = self.value
37 vl = self.left.value
38 tll = self.left.left
39 tlr = self.left.right
40 tr = self.right
41 newright = AVLTree(v)
42 newright.left = tlr
43 newright.right = tr
44 self.right = newright
45 self.value = vl
46 self.left = tll
47 return
48
49
50 def insert(self,v):
51 if self.isempty():
52 self.value = v
53 self.left = AVLTree()
54 self.right = AVLTree()
55 self.height = 1
56 return
57 if self.value == v:
58 return
59 if v < self.value:
60 self.left.insert(v)
61 self.rebalance()
62 self.height = 1 + max(self.left.height, self.right.height)
63 if v > self.value:
64 self.right.insert(v)
65 self.rebalance()
66 self.height = 1 + max(self.left.height, self.right.height)
67
68 def rebalance(self):
69 if self.left == None:
70 hl = 0
71 else:
72 hl = self.left.height
73 if self.right == None:
74 hr = 0
75 else:
76 hr = self.right.height
77 if hl - hr > 1:
78 if self.left.left.height > self.left.right.height:
79 self.rightrotate()
80 if self.left.left.height < self.left.right.height:
81 self.left.leftrotate()
82 self.rightrotate()
83 self.updateheight()
84 if hl - hr < -1:
85 if self.right.left.height < self.right.right.height:
86 self.leftrotate()
87 if self.right.left.height > self.left.right.height:
88 self.right.rightrotate()
89 self.leftrotate()
90 self.updateheight()
91
92 def updateheight(self):
93 if self.isempty():
94 return
95 else:
96 self.left.updateheight()
97 self.right.updateheight()
98 self.height = 1 + max(self.left.height, self.right.height)
99
100 def inorder(self):
101 if self.isempty():
102 return([])
103 else:
104 return(self.left.inorder()+ [self.value]+ self.right.inorder())
105 def preorder(self):
106 if self.isempty():
107 return([])
108 else:
109 return([self.value] + self.left.preorder()+
self.right.preorder())
110 def postorder(self):
111 if self.isempty():
112 return([])
113 else:
114 return(self.left.postorder()+ self.right.postorder() +
[self.value])
115
116 A = AVLTree()
117 nodes = eval(input())
118 for i in nodes:
119 A.insert(i)
120
121 print(A.inorder())
122 print(A.preorder())
123 print(A.postorder())
Sample Input
Output
At each stage, make the next choice based on some local criterion
Never go back and revise an earlier decision
Example :
Dijkstra's
Prim's
Kruskal's
Interval scheduling
Minimize lateness
Huffman coding
Interval scheduling
▪ IIT Madras has a special video classroom for delivering online lectures
▪ Choose a subset of bookings to maximize the number of teachers who get to use the room
Algorithm
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? [NAT]
Activity Start time Finish time
A 1 3
B 3 4
C 0 7
D 1 2
E 5 6
F 5 9
G 10 11
H 7 8
Answer
Example
A popular meeting hall in a city receives many overlapping applications to hold meetings. The
manager wishes to satisfy as many customers as possible. Each application is a tuple (id,
start_day, end_day) where id , start_day and end_day are the unique id assigned to the
application, starting day of the meeting and ending day of meeting ends inclusive respectively.
Write a function no_overlap(L) to return the list of customer ids whose applications are
accepted that ensures optimal scheduling. Let L be a list tuples with (id, start_day,
end_day) .
Sample Input
1 L = [
2 (0, 1, 2),
3 (1, 1, 3),
4 (2, 1, 5),
5 (3, 3, 4),
6 (4, 4, 5),
7 (5, 5, 8),
8 (6, 7, 9),
9 (7, 10, 13),
10 (8, 11, 12)
11 ]
Sample output
1 [0, 3, 6, 8]
Solution
Analysis
Minimize lateness
▪ IIT Madras has a single 3D printer
▪ Each user will get access to the printer, but may not finish before deadline
Algorithm
Example
Analysis
Huffman Algorithm
Algorithm
Example
We received a message 10010000010001110111100011010 encoded using Huffman coding of
a,b,c,d and e generated by the given Huffman tree. Which of the following is the correct
decoded message for the given encoded message? [MCQ]
(a) cbaababdebadb
(b) cbaabcbdebadc
(c) cbaababdecadc
(d) cbaabcbedcadc
Answer
(c)
Implementation
1
2 class Node:
3 def __init__(self,frequency,symbol = None,left = None,right = None):
4 self.frequency = frequency
5 self.symbol = symbol
6 self.left = left
7 self.right = right
8
9 # Solution
10
11 def Huffman(s):
12 huffcode = {}
13 char = list(s)
14 freqlist = []
15 unique_char = set(char)
16 for c in unique_char:
17 freqlist.append((char.count(c),c))
18 nodes = []
19 for nd in sorted(freqlist):
20 nodes.append((nd,Node(nd[0],nd[1])))
21 while len(nodes) > 1:
22 nodes.sort()
23 L = nodes[0][1]
24 R = nodes[1][1]
25 newnode = Node(L.frequency + R.frequency, L.symbol + R.symbol,L,R)
26 nodes.pop(0)
27 nodes.pop(0)
28 nodes.append(((L.frequency + R.frequency, L.symbol +
R.symbol),newnode))
29
30 for ch in unique_char:
31 temp = newnode
32 code = ''
33 while ch != temp.symbol:
34 if ch in temp.left.symbol:
35 code += '0'
36 temp = temp.left
37 else:
38 code += '1'
39 temp = temp.right
40 huffcode[ch] = code
41 return huffcode
42
43
44
45 s = input()
46 res = Huffman(s)
47 for char in sorted(res):
48 print(char,res[char])
Analysis
At each recursive step, extract letters with minimum frequency and replace by composite
letter with combined frequency
Store frequencies in an array
Linear scan to find minimum values
|𝐴| = 𝑘, number of recursive calls is k – 1
Complexity is
Instead, maintain frequencies in an heap
Extracting two minimum frequency letters and adding back compound letter are both 𝑂(log
𝑘)
Complexity drops to 𝑂(k log 𝑘)