Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

EXPERIMENT 1.3

Student Name: Manohar Chaudhary UID: 21BCS4422


Branch: CSE Section/Group: FL-603-B
Semester: 6th Date of Performance: 30/01/2024
Subject Name: AP Lab-2 Subject Code:21CSP-351

1. Aim: To demonstrate the concept of Heap model.

I. Problem statement - You are given an array of integers stones where stones[i]
is the weight of the ith stone.
We are playing a game with the stones. On each turn, we choose the heaviest
two stones and smash them together. Suppose the heaviest two stones have
weights x and y with x <= y. The result of this smash is:
If x == y, both stones are destroyed, and
If x != y, the stone of weight x is destroyed, and the stone of weight y has new
weight y - x.
At the end of the game, there is at most one stone left.

II. Problem Statement- There are n cities connected by some number of flights.
You are given an array flights where flights[i] = [fromi, toi, pricei] indicates
that there is a flight from city fromi to city toi with cost pricei.
You are also given three integers src, dst, and k, return the cheapest price from
src to dst with at most k stops. If there is no such route, return -1.

2. Objective:
✓ Highlight the importance of concept of Heap model.
✓ Define the Heap model and its significance in computer science.

3. Code
1.
class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:

while len(stones) > 1:


stones.sort()
m1=max(stones)
stones.remove(m1)
m2=max(stones)
stones.remove(m2)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if m1 < m2:
m2 = m2 - m1
stones.append(m2)

elif m2 < m1:


m1 = m1 - m2
stones.append(m1)

if len(stones) == 0:
return 0
return stones[0]

2.
from collections import defaultdict

class Solution:
def findCheapestPrice(self, n, flights, src, dst, k):
graph = defaultdict(list)

# Build the graph


for u, v, price in flights:
graph[u].append((v, price))

# Initialize dp array
dp = [[-1] * (k + 2) for _ in range(n)]

# Define DFS function


def dfs(src, dst, k):
if k < 0:
return float('inf')
if src == dst:
return 0
if dp[src][k] != -1:
return dp[src][k]

min_price = float('inf')
for neighbor, price in graph[src]:
cheap = dfs(neighbor, dst, k - 1)
if cheap != float('inf'):
min_price = min(min_price, cheap + price)

dp[src][k] = min_price
return min_price

# Call the DFS function


ans = dfs(src, dst, k + 1)
return -1 if ans == float('inf') else ans
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

# Example usage:
n = 3
flights = [[0,1,100],[1,2,100],[0,2,500]]
src = 0
dst = 2
k = 1
solution = Solution()
print(solution.findCheapestPrice(n, flights, src, dst, k))

4. OUTPUT
1.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
2.

5. Learning Outcomes :
✓ Understood what a Heap is and its representation in computer science.

✓ Implemented Heap-based solutions for finding kth largest/smallest elements and shortest paths in
graphs.

✓ Understood the trade-offs between using Heaps and alternative approaches in algorithm design.

✓ Developed and optimized Heap-based algorithms.


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

You might also like