Minimum Refueling Stops: Problem 8

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

Problem 8

Minimum
Refueling
Stops
Group IX
Muhammad Zain
Ali Haider
Muhammad Askari
1 Problem

2 Design Approach

Table of 3 Algorithm

contents 4 Correctness

5 Time Complexity

6 Implementation
1
Problem
Introduction & Explanation
Problem Introduction

A group of friends who are going on a vacation


to a beach by a car. One of them is suffering
from a severe fever and needs to be taken to
hospital in nearest town immediately.
Problem Explanation
 The hospital is located in town at coordinate 0, and the
car is currently at a distance of D units from the town.

 There are N petrol stations along the road between the car's
current location and the town where the friends can stop to
acquire additional petrol.

 The car consumes one unit of petrol for every unit of


distance traveled. The initial amount of petrol in the car's
tank denoted by P.

 The goal is to determine the minimum number of stops the


friends need to make for petrol in order to reach the town, or if
the friends can’t reach the town at all.
2
Design
Approach
Problem Solving & Reasoning
Design Approach
To solve this problem, a greedy approach can be adopted.
The key idea is to always stop at the petrol station that allows
the car to reach the farthest distance possible. By doing so, the
friends can minimize the number of stops for petrol. This
approach works because if a station is skipped in favor of a
later station, it would mean that the car would run out of
petrol before reaching the next station.
Reasoning

A greedy approach will always choose the petrol


station that allows the car to reach the farthest distance
possible. This strategy ensures that the car will make
the minimum number of stops for petrol.
Drawbacks of Greedy Approach

The Greedy algorithm doesn’t always produce the


optimal solution. i.e., the best choice at each step of
the problem is choosen without reconsidering the
previous steps once choosen.
3
Algorithm
Explanation & Design of Strategy
Algorithm

Here is an algorithmic strategy to solve the problem:

1. Initialize the current petrol amount (P_curr) to the initial


petrol amount (P) and the number of stops (stops) to 0.

2. Initialize the current distance from the town (curr_distance)


to the car's distance (D).
Algorithm
3. While curr_distance > 0:

a. If P_curr is greater than or equal to curr_distance, it means


the car can reach the town without any additional stops.
Return the number of stops made so far.

b. Otherwise, find the petrol station (station) within the


remaining distance (curr_distance) that provides the
maximum petrol amount.

c. If no petrol station is found, it means the car cannot reach the


town with the given amount of petrol. Return -1 to indicate an
impossible scenario.

d. Update P_curr by adding the petrol amount obtained from the


station.
Algorithm
e. Increment the number of stops.

f. Update curr_distance by subtracting the distance from


the current position to the selected petrol station.

4. If the loop terminates without returning in step 3, return -1


to indicate an impossible scenario (e.g., insufficient petrol
or unreachable town).
4
Correctness
Proof of Correctness
Proof of Correctness

To prove the correctness of the provided algorithm, we need


to show two properties:

1. The algorithm always terminates and returns the correct


minimum number of stops required.

2. The algorithm's solution is optimal, meaning it finds the


minimum number of stops necessary to reach the town
5
Time
Complexity
Analysis of Algorithm
Time Complexity Analysis
 The loop in the MinimizePetrolStops function iterates until curr_distance
becomes zero, or until it returns in case of an impossible scenario. The
maximum number of iterations for the loop is D (distance from the town),
as the loop decrements curr_distance by the distance to each selected
petrol station. Therefore, the time complexity of the loop is O(D).

 Within the loop, there is a nested loop that iterates over the petrol stations
to find the one with the maximum petrol amount. The maximum number of
iterations for this nested loop is N (number of petrol stations), as it checks
each station to find the maximum petrol amount. Therefore, the time
complexity of the nested loop is O(N).

 Considering that the nested loop is inside the main loop, the overall time
complexity of the code is the product of the time complexities of the two
loops: O(D * N)
6
Implementation
Code in C#
PetrolStopsMinimization Class
using System;
namespace DAAppt
{
class PetrolStopsMinimization
{
public static int MinimizePetrolStops(int D, int P, int N, int[] petrolStations)
{
int P_curr = P; // Current petrol amount
int stops = 0;
int curr_distance = D; //from town
while (curr_distance > 0)
{

if (P_curr >= curr_distance)


return stops; //car can reach the town without any additional stops
else
{
Console.WriteLine("\nbefore refueling");
Console.WriteLine("Current petrol: " + P_curr);
Console.WriteLine("Current distance: " + curr_distance);
int maxPetrol = 0;
int maxPetrolStationIndex = -1;
PetrolStopsMinimization Class

// Find the petrol station within the remaining distance that provides the maximum petrol
amount
for (int i = 0; i < N; i++)
{
int distanceToStation = curr_distance - petrolStations[i];

if (distanceToStation >= 0 && petrolStations[i] > maxPetrol)


{
maxPetrol = petrolStations[i];

maxPetrolStationIndex = i;
petrolStations[i] -= maxPetrol;

}
}
PetrolStopsMinimization Class

Console.WriteLine("\nRefuel {0} units from Petrol Station {1} ", maxPetrol,


maxPetrolStationIndex);

if (maxPetrolStationIndex == -1)
return -1;

P_curr += maxPetrol;
curr_distance -= petrolStations[maxPetrolStationIndex];
stops++;
}
Console.WriteLine("\nafter refueling");
Console.WriteLine("Current petrol: " + P_curr);
Console.WriteLine("Current distance: " + curr_distance);
}
return -1;
}
}
}
Main Class
using System;
namespace DAAppt
{
class Program
{
static void Main(string[] args)
{
int D = 100; // Distance from the town
int P = 30; //initial petrol amount
int N = 5; //no of petrol stations
int[] petrolStations = { 30, 20, 40, 10, 50}; // Petrol amounts at each station

int minStops = PetrolStopsMinimization.MinimizePetrolStops(D, P, N,


petrolStations);
Console.WriteLine("Minimum stops required: " + minStops);
}
}
}
Output
That’s All
Folks!

You might also like