0% found this document useful (0 votes)
5 views

Algo Assignment 3

Uploaded by

abdulnaveed2004
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Algo Assignment 3

Uploaded by

abdulnaveed2004
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Question # 01:

a)
value weight ratio
60 10 6
100 20 5
120 30 4
i) Item with highest ratio -> Item 1 (Total Capacity remaining = 40)
ii) Item with next highest ratio -> Item 2 (Capacity remaining = 20)
iii) Take as much as possible from Item 3 -> 20/30, value=120(20/30) = 80
Total = 60+80+100=240

b)
value weight ratio
100 10 10
50 5 10
500 50 10
Take highest ratio -> Item 1, Item 2 (capacity = 45)
Take remaining Item 3 -> 45/50*500 = 450
Total = 100+50+450=600
Optimal Solution -> Take all of Item 3 and then Item 1.
Total = 500+100 = 600

c)
Algorithm FractionalKnapsack(p, w, W, n):
items = [(p[i]/w[i], p[i], w[i]) for i = 1 to n]
sort(items in descending order by value per weight)
total_value = 0
for each (value_per_weight, price, weight) in items:
if W == 0: break
take = min(weight, W)
total_value += take * value_per_weight
W -= take
return total_value
Time Complexity: O(nlogn), O(nlogn) or sorting and O(n) for iterating.
Question # 02

a)
This strategy does not guarantee an optimal solution because by picking activities with the least finish
time first, you will risk excluding earlier activities that could allow for more total activities
If A1(1,2), A2(3,4), A3(0,6) are ordered by descending order of finish time A3, A2, A1.
If A3 is picked no other activity will be chosen
Optimal Solution: Pick A1 and A2 for 2 activities, this shows the strategy fails to maximize the number of
activities.

b)
This strategy also fails to guarantee optimal solutions because by picking the activities with the latest start
time first you may exclude earlier activities that would allow for more non-overlapping choices.

Question # 03
Input:
activities = [(start, finish)]
Steps:
Sort activities by start time. If two activities have the same start time, sort by finish time.
Initialize a min-heap to store the finish times of lecture halls.
Initialize:
hall-assignment = [] (to store hall numbers assigned to activities)
hall-count = 0 (to count the number of lecture halls used)
For each activity in activities:
If the heap is not empty and the earliest finish time ≤ activity start:
Pop the earliest finish time from the heap.
Else:
Increment hall-count (new hall needed).
Assign the activity to the hall (add hall count to hall-assignment).
Push activity.finish into the heap.
Return hall-assignment.
Time Complexity: O(nlgn)

Question # 04
Inputs:

● graph: Adjacency list (j,a[i,j])(j, a[i, j])(j,a[i,j]), where a[i,j]a[i, j]a[i,j] is the airfare from country
iii to jjj.
● n: Total number of countries.
● k: Destination country.
current_country = 1 (start at country 1)
total_cost = 0
while current_country ≠ k:
Check if no outgoing flights from current_country:
Return "unreachable."
Find the flight (nextcountry, airfare)(next_country, airfare)(nextcountry, airfare) with the
minimum airfare
Add airfare to total_cost.
Set current_country = next_country.
Return total_cost

Average case: O(d), where d is the number of outgoing flights.


Worst case: O(m), where m is the total number of flights.
For n−1 steps, overall complexity: O(n⋅d)

Question # 05
Minimum Coins (V)
V = value in cents
denominations = [25, 10, 5, 1] (descending order)
result = []
for c in denominations:
count = V // c
V = V - (count * c)
Append count coins of c to result
if V = = 0:
break
Gives locally optimal choice

Question # 06
DFS(G)
G = directed graph
Initialize all vertices, u in G
color[u] = "white"
parent[u] = NULL
time = 0
For each vertex u in G:
if color[u] = = "white":
DFS-Visit(u)
DFS_visit(u)

Color[u] = "GRAY"
Discovery_time[u] = time++
For each vertex v in Adj[u]:
If color[v] == "white":
parent[v] = u
Print "Edge (u,v): Tree Edge"
DFS_visit(v)
Else if color[v] == "GRAY":
Print "Edge (u,v): back edge"
Else if color[v] == "Black" and discovery_time[u] < discovery_time[v]:
Print "Edge (u,v): Forward Edge"
Else:
Print "Edge (u,v): cross edge"
Color[u] = "Black"
finish_time[u] = time++
Modified DFS:

DFS_visit(u)

1) Color[u] = "GRAY"
Discovery_time[u] = time++
2) For each vertex v in Adj[u]:
If color[v] == "white":
parent[v] = u
Print "Edge (u,v): tree edge"
DFS_visit(v)
Else if color[v] == "GRAY":
Print "Edge (u,v): back edge"
Else if color[v] = = "Black" and discovery_time[u] < discovery_time[v]:
Print "Edge (u,v): Forward Edge"
Else:
Print "Edge (u,v): cross edge"
3) Color[u] = "Black"
finish_time[u] = time++

b)
DFS_visit(u)
i) Color[u] = "GRAY"
Discovery_time[u] = time++
ii) For each vertex v in Adj[u]:
If color[v] = = "white":
parent[v] = u
Print "Edge (u,v): tree edge"
DFS_visit(v)
Else if colour[v] = = “Grey” and v != parent[u]
Print “Edge(u,v):Back Edge”
iii) colour[u] = “Black”
finish_time[u] = time++

Question # 07

G: Directed Graph
Initialize all vertices u in G:
color[u] = "white"
discovery_time[u] = NIL
finish_time[u] = NIL
time = 0
For each vertex u in G:
if color[u] == "white":
if DFS_Visit(u):
return True

return False

DFS_Visit(u):
color[u] = "Gray"
discovery_time[u] = time++
For each vertex v in Adj[u]:
if color[v] == "white":
parent[v] = u
if DFS_Visit(v):
return True
else if color[v] == "Gray" and v != parent[u]:
return True
color[u] = "Black"
finish_time[u] = time++
return False

Question # 08
i) Indegree:
m, n, s, p, w -> 0 degree
q, r, t, x, y, z -> 0 degree
ii) Push all vertices with indegree = 0 (m, n, s, p, w) in queue
iii) while the queue is not empty:
a) Extract a vertex with indegree = 0 from the queue
b) Add u to the topological order list
c) Decrement the indegree of all adjacent vertices of u
d) If the indegree of any adjacent vertex becomes 0, push it into the queue
iv) Repeat step 3 until queue is empty
The topological order of the vertices will be:
m -> n -> s -> p -> w -> q -> r -> t -> x -> y -> z

Question # 09
countSimplePaths(G, a, b)

Initialize:
indegree = {}
for each vertex v in G.V:
indegree[v] = 0
for each edge (u, v) in G.E:
indegree[v] += 1
queue = []
for each vertex v in G.V:
if indegree[v] = = 0:
queue.enqueue(v)
counter = 0

while queue is not empty:


u = queue.dequeue()
if u = = b:
counter += 1
for each edge (u, v) in G.E:
indegree[v] = 1
for each vertex v in G.V:
if indegree[v] = = 0:
queue.enqueue(v)
Return counter

Time complexity: O(|V| + |E|)

You might also like