Вы находитесь на странице: 1из 6

# Path Finder (100 Marks)

Problem Statement:

In a busy city, like Mumbai, a smart navigation system is required, which will take a user profile
as its input, instead of just the source and destination. This profile includes the following
parameters:

Source, destination, type of route (FASTEST / SHORTEST), start time and target time
(time is specified in minutes, as an integer number starting from 0 and not timestamp)

Note:
a) SHORTEST means path with minimum numbers of nodes between source and
destination.
b) FASTEST means fastest path between source and destination.

## User Profile Example: (A,D,FASTEST,1,6)

The user will travel, only if, the user is able to reach the destination, on or before the
specified target time.

Also, because of the heavy traffic, a rule is passed in the city, stating, only 1 vehicle can
travel at a time on a given link. This is why, while calculating the travel time for a link, our
smart navigation system should consider the time taken by the previous users, who have

We need to build this system, which will take the user profile(s) as input and prints
the decisions of the user(s). ('Y' if the user will travel, else 'N')

## Rules of the System:

1. Shortest/fastest path needs to be determined using the ideal travel time of the links at the
beginning.

2. Actual travel time of the above path needs to be calculated, considering the output or all
previously processed profiles. If any previous user's profile is chosen and if that user's
route is in conflict with current user's path links, current user need to resolve the conflicts
by waiting until a proper time window is found to travel on that link.

3. Check the actual time, calculated above, against target time, mentioned in the user profile,
and take decision to travel or not.

4. Routes (in point 1) should be selected by considering only the ideal travel time and not the
actual travel time calculated in point 2.

5. Current user cannot make previous users, whose decision was 'Y' to wait in any case.

6. If user profile is incorrect, then the decision for that user would be 'N'.

## Input to the system (all without spaces):

1. Nodes (Comma separated)

2. Links - Are unidirectional and equal length, connecting the nodes and has ideal travel time
(Comma separated)

## 1. Comma separated decisions corresponding to each profile.

-----------------------------------------------------------------------

## Sample Test Case 1:

Input:

A,B,C,D
(A,B,8),(B,C,1),(C,D,1),(D,A,1),(B,A,2),(A,D,2),(D,C,2),(C,B,2)
(B,A,FASTEST,1,4),(B,A,FASTEST,1,4),(B,A,SHORTEST,1,5),(B,A,FASTEST,1,6),(D,C,FASTE
ST,4,7),(A,B,FASTEST,1,10)

Output:

Y,N,N,Y,Y,Y

Explanation:
Based on the Nodes and Links, we can prepare the below graph:

## For 1st User:

Source and destination are node B and node A respectively, with 'FASTEST' route. Also, it says
that user will leave from node A on 1st min and would like to reach node F on or before 4th min.

Step 1: From the above graph, the possible routes from B to A are B-A and B-C-D-A.
Based on ideal time of the links, route B-A will take 2 mins, while route B-C-D-A will take 3
mins. Thus, route B-A is the fastest and hence, it is selected.

Step 2: For 1st user, the actual travel time would be same as the ideal time as no other
users have already decided to travel. Thus actual travel time, of route B-A is 2 mins.
Step 3: Since the user will start travelling on 1st min and actual travel time is 2 mins, the
user will reach point A on 3rd min, which is before the target 4th min. This is why the result
for this user is 'Y'.

## For 2nd User:

Step 1: The possible routes from B to A are B-A and B-C-D-A. Based on ideal time of the
links, considering no other user is travelling, route B-A will take 2 mins, while route B-C-D-
A will take 3 mins. Thus, route B-A is the fastest and hence, it is selected.

Step 2: Since the 1st user will already be travelling on B-A from 1st to 3rd min, this user
cannot start on 1st min and will have to wait for 1st user to complete its travel. So, this user
will have to wait till 3rd min i.e. 2 mins from start time mentioned in user's profile. The
actual time of this user's travel will hence become waiting time + route ideal time i.e. 2 + 2
= 4 mins. Please note that after this calculation, it may seem that B-C-D-A would be the
fastest route, but the selected route cannot be changed after step 1.

Step 3: Since the user will start travelling on 1st min and actual time is 4 mins, the user will
reach point A on 5th min, which is more than the target time of this user's profile 4th min.
This is why the result for this user is 'N'.

## For 3rd User:

Step 1: The possible routes from A to B are A-B and A-D-C-B. Since the shortest route is
required, the ideal time of the links will not be counted, but the number of nodes in the
route are to be counted. Thus route A-B would be selected as the SHORTEST route.

Step 2: Since no previous users are travelling on link A-B, the actual time would be the
ideal time of the link A-B i.e. 8 mins.

Step 3: This user will start on 1st min and reach on 9th min (adding 8 mins of travel time to
start time), which is greater than the target time in this user's profile i.e. 5th min. This is
why the result for this user is 'N'.

## For 4th User:

Step 1: The possible routes from B to A are B-A and B-C-D-A. Based on ideal time of the
links, considering no other user is travelling, route B-A will take 2 mins, while route B-C-D-
A will take 3 mins. Thus, route B-A is the fastest and hence, it is selected.

Step 2: Since the 1st user will already be travelling on B-A from 1st to 3rd min, this user
cannot start on 1st min and will have to wait for 1st user to complete its travel. So, this user
will have to wait till 3rd min i.e. 2 mins from start time mentioned in user's profile. The
actual time of this user's travel will hence become waiting time + route ideal time i.e. 2 + 2
= 4 mins. Please note that after this calculation, it may seem that B-C-D-A would be the
fastest route, but the selected route cannot be changed after step 1.
Step 3: Since the user will start travelling on 1st min and actual time is 4 mins, the user will
reach point A on 5th min, which is less than the target time of this user's profile. This is why
the result for this user is 'Y'.

## For 5th User:

Step 1: From the above graph, the possible routes from D to C are D-C and D-A-B-C.
Based on ideal time of the links, route D-C will take 2 mins, while route D-A-B-C will take
10 mins. Thus, route B-A is the fastest and hence, it is selected.

Step 2: As no other previous users are travelling on D-C, the actual travel time would be
same as the ideal time. Thus actual travel time, of route D-C is 2 mins.

Step 3: Since the user will start travelling on 4th min and actual travel time is 2 mins, the
user will reach point A on 6th min, which is before the target 7th min. This is why the result
for this user is 'Y'.

## For 6th User:

Step 1: The possible routes from A to B are A-B and A-D-C-B. Based on ideal time of the
links, considering no other user is travelling, route A-B will take 8 mins, while route A-D-C-
B will take 6 mins. Thus, route A-D-C-B is the fastest and hence, it is selected.

Step 2: As no previous users are travelling on link A-D, there is no waiting time for this link
and user can travel on this link from 1st min and reach point D at 3rd min. Since 5th user
will already be travelling on link D-C from 4th min to 6th min, this user will not be able to
start on 3rd min from point D and complete its travel on link D-C before 4th min. This is
why this user will have to wait until 5th user completes its travel on link D-C i.e. until 6th
min. So, this user will start from point D on 6th min and reach point C on 8th min. Again,
there are no users travelling on link C-B, so this user can start from point C at 8th min and
need not wait. Thus for this user, the total waiting time is (from 3rd min to 6th min) 3 mins,
making actual travel time = 3 (waiting time) + 6 (ideal time) = 9 mins

Step 3: This user will start on 1st min and actual travel time is 9 mins, the user will reach at
point B on 10th min, which is equal to the target time in the user's profile. This is why the
result for this user is 'Y'.

## Sample Test Case 2:

Input:
A,B,C,D,E,F
(A,B,1),(A,C,2),(B,E,1),(E,A,2),(E,D,1),(D,F,1),(D,C,3),(F,E,1),(F,D,1),(C,A,2),(C,D,3)
(A,F,FASTEST,1,6),(A,E,FASTEST,1,3),(A,D,SHORTEST,2,9),(A,E,FASTEST,3,5)

Output:
Y,N,Y,Y
Explanation: Based on the Nodes and Links, we can prepare the below graph:

## For 1st User :

Source and destination are node A and node F respectively, with 'FASTEST' route. Also, it says
that user will leave from node A on 1st min and would like to reach node F on or before 6th min.

Step 1: From the above graph, based on the ideal travel time, the fastest route is A-B-E-D-
F.

## Step 2: Actual travel time: 1+1+1+1 = 4

Step 3: User would reach node F on 5th min (1+4), which is less than 6. Hence result for
this user is 'Y'

## Step 1: Fastest route between A and E: A-B-E

Step 2: Because result of 1st user was 'Y', he will travel on A-B-E-D-F starting on 1st min.
Since, only 1 user can travel at a time on any given link, this user would not be able to start
on 1st min, and will have to wait until user 1 completely travels link A-B. Thus, this user will
start on 2nd min and would reach node E on 4th min (No need to wait on B, since this user

Step 3: User would reach on 4th min, which is more than 3. Hence result for this profile is
'N'.

## Step 1: Shortest route between A and D is A-C-D.

Step 2: Since no other previous accepted profile's path conflicts with this path, actual travel
time is same as ideal travel time, which is 5 minutes.

Step 3: User would reach on 7th min, which is less than 9. Hence result for this profile is
'Y'.

## Step 1: Fastest route between A and E is A-B-E.

Step 2: Total travel time: 2 (No need to wait as by 3rd min, User 1 would have traveled link
A-B, making it free for other users)

Step 3: User would reach on 5th min, which is equal to 5. Hence result for this profile is 'Y'.