-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreedy algorithms.py
More file actions
69 lines (62 loc) · 1.79 KB
/
Copy pathgreedy algorithms.py
File metadata and controls
69 lines (62 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#Lesson no : 41
# Greedy Algorithms
# Question no 1 : Activiy selection problem(statement in notes)
"""def maxactivitis(activities):
activities.sort(key=lambda x:x[2])
i=0
firstactivity=activities[0][0]
print(firstactivity)
for j in range(len(activities)):
if activities[j][1]>activities[i][2]:
print(activities[j][0])
i=j
activities=[ ['a1',0,6],
['a2',3,4],
['a3',1,2],
['a4',5,8],
['a5',5,7],
['a6',8,9]
]
maxactivitis(activities)"""
# Question no 2 : coin change problem(statement in notes)
"""def coinchange(totalamont,coinslist):
coinslist.sort()
coinslist.reverse()
index=0
while True:
if coinslist[index]<=totalamont:
print(coinslist[index])
totalamont-=coinslist[index]
else:
index+=1
if totalamont==0:
break
coins=[1,2,5,20,50,100]
coinchange(201,coins)"""
# Question no 3 :fractional knaosack problem(statement in notes)
"""class items:
def __init__(self,weight,value):
self.weight=weight
self.value=value
self.ratio=value/weight
def knapsack(itemslist,weightcapacity):
itemslist.sort(key=lambda x:x.ratio,reverse=True)
totalvalue=0
usedweight=0
for i in itemslist:
if usedweight+i.weight<=weightcapacity:
usedweight += i.weight
totalvalue+=i.value
else:
unusedweight=weightcapacity-usedweight
value=i.ratio*unusedweight
totalvalue+=value
usedweight+=unusedweight
if weightcapacity==0:
break
print('max value obtained is '+ str(totalvalue))
item1=items(20,100)
item2=items(30,120)
item3=items(10,60)
items=[item1,item2,item3]
knapsack(items,50)"""