4. Suppose you will travel on a railway and there are n ticket offices to buy a ticket along the railway. You will change trains during your journey. At the beginning, you are given for each 1 ≤ i < j ≤ n and pi,j is the price of a ticket for a train that goes from ticket office i to ticket office j. These prices are arbitrary. For instance, it is possible to see a price combination as p1,3 = 15 and p1,5 = 10. You start at ticket office 1 and must finish at the ticket office n (by using the trains).
(a) Suggest an efficient dynamic programming algorithm that minimizes the money you spent in your journey.
(b) What is the complexity of your algorithm?
(a) Suggest an efficient dynamic programming algorithm that minimizes the money you spent in your journey.
Explanation of programming approach
Here the dynamic programming is represented in more generalized way.
Here the starting ticket office number is customized. So user can start from any ticket office and it will show from source ticket office to all possible ticket offices’ minimum fares and the intermediate journey ticket office route.
As the source ticket office number is user defined, so if user take console input 1, it means journey starts from ticket office 1. It will give minimum fare for all destination ticket offices. Here all the destination ticket office are nth ticket office for a individual source to destination ticket office with the intermediate ticket office.
/* program to calculate minimum ticket fare from source ticket office to destination ticket office*/
#include<stdio.h>
#include<conio.h>
#define infy_value 9999999
void tickrt_price(int ticket_price[100][100],int n,int
start_station);
int main()
{
int ticket_price[100][100],i,j,n,st;
printf("How many stations are there:");
scanf("%d",&n);
printf("\nEnter the stations to stations ticket
fare:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if (i==j){
printf("\nStation: %d
to Station: %d (Rs.) ", i,j);
ticket_price[i][j]=0;
printf("%d",ticket_price[i][j]);
}
else{
printf("\nStation: %d to Station:
%d (Rs.) ", i,j);
scanf("%d",&ticket_price[i][j]);
}
}
}
printf("\n Station to station ticket fare details is
(Rs./-):\n\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t
",ticket_price[i][j]);
}
printf("\n");
}
printf("\nEnter the journey starting stastion
number:-> ");
scanf("%d",&st);
tickrt_price(ticket_price,n,st);
return 0;
}
void tickrt_price(int ticket_price[100][100],int n,int
start_station)
{
int ticket_fare[100][100],min_fare[100],pd[100];
int
travel_stn[100],c,min_fare_value,next_station,i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(ticket_price[i][j]==0)
ticket_fare[i][j]=infy_value;
else
ticket_fare[i][j]=ticket_price[i][j];
}
}
for(i=1;i<=n;i++)
{
min_fare[i]=ticket_fare[start_station][i];
pd[i]=start_station;
travel_stn[i]=0;
}
min_fare[start_station]=0;
travel_stn[start_station]=1;
c=1;
while(c<=n-1)
{
min_fare_value=infy_value;
for(i=1;i<=n;i++)
if(min_fare[i]<min_fare_value && !travel_stn[i])
{
min_fare_value=min_fare[i];
next_station=i;
}
travel_stn[next_station]=1;
for(i=1;i<=n;i++)
if(!travel_stn[i])
if(min_fare_value+ticket_fare[next_station][i]<min_fare[i])
{
min_fare[i]=min_fare_value+ticket_fare[next_station][i];
pd[i]=next_station;
}
c++;
}
for(i=1;i<=n;i++)
{
if(i!=start_station)
{
printf("\n");
printf("\nMinimum Train Fare from Station: %d to Station: %d =
Rs.%d /-",start_station,i,min_fare[i]);
printf("\nJourney path of station : Stn:%d",i);
j=i;
do
{
j=pd[j];
printf(" <===stn:%d ",j);
}while(j!=start_station);
}
}
}
/* minimize the money in journey function part */
void tickrt_price(int ticket_price[100][100],int n,int
start_station)
{
int ticket_fare[100][100],min_fare[100],pd[100];
int
travel_stn[100],c,min_fare_value,next_station,i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(ticket_price[i][j]==0)
ticket_fare[i][j]=infy_value;
else
ticket_fare[i][j]=ticket_price[i][j];
}
}
for(i=1;i<=n;i++)
{
min_fare[i]=ticket_fare[start_station][i];
pd[i]=start_station;
travel_stn[i]=0;
}
min_fare[start_station]=0;
travel_stn[start_station]=1;
c=1;
while(c<=n-1)
{
min_fare_value=infy_value;
for(i=1;i<=n;i++)
if(min_fare[i]<min_fare_value && !travel_stn[i])
{
min_fare_value=min_fare[i];
next_station=i;
}
travel_stn[next_station]=1;
for(i=1;i<=n;i++)
if(!travel_stn[i])
if(min_fare_value+ticket_fare[next_station][i]<min_fare[i])
{
min_fare[i]=min_fare_value+ticket_fare[next_station][i];
pd[i]=next_station;
}
c++;
}
for(i=1;i<=n;i++)
{
if(i!=start_station)
{
printf("\n");
printf("\nMinimum Train Fare from Station: %d to Station: %d =
Rs.%d /-",start_station,i,min_fare[i]);
printf("\nJourney path of station : Stn:%d",i);
j=i;
do
{
j=pd[j];
printf(" <===stn:%d ",j);
}while(j!=start_station);
}
}
}
output ( As the output screen is large so the overall output is divided in two parts)


(b) What is the complexity of your algorithm?
Here the algorithm is designed with a adjacency matrix representation where the ticket office to ticket office fares are expressed through matrix form with n x n matrix.
Now to generate fare from source ticket office to destination ticket office every pair of ticket offices are compared with other pair of ticket office. Here we implement nested loop structure where each loop has n iterations (Where total number of ticket offices are n).So total time complexity will be in n2 form and it can be represented as O(n2).
4. Suppose you will travel on a railway and there are n ticket offices to buy...
You are canoeing down a river and there are n renting posts along the way. Before starting your journey, you are given, for each 1 lessthanorequalto i lessthanorequalto j lessthanorequalto n, the fee f(i, j) for renting a canoe from post i to post j. These fees are arbitrary. For example it is possible that f(1, 3)= 10 and f(1, 4) = 5. You begin at trading post 1 and must end at trading post n (using rented canoes). Your...
There are n trading posts numbered 1 to n as you travel
downstream. At any trading post i you can rent a canoe to be
returned at any of the downstream trading posts j>i. You are
given a cost array R(i,j) giving the cost of these rentals for all
1≤i<j≤n. We can assume that R(i,i)=0, and that you can't go
upriver (so perhaps R(i,j)= ∞ if i>j). For example, one cost
array with n=4 might be the following.
The problem...
Suppose you have an array S indexed from 1 to n which contains n numbers, not in any particular order, and you wish to count how many times a given number x occurs in S. Consider the recursive algorithm below for this which finds the number of occurrences of x in the index range i...j in S. Of course, solving the problem would involve an initial call to the algorithm for the range 1.n: int CountOccur (int i,j) { int...
Suppose n activities apply for using a common resource. Activity ai (1 ≤ i ≤ n) has a starting time S[i] and a finish time F[i] such that 0 < S[i] < F[i]. Two activities ai and aj (1 ≤ i, j ≤ n) are compatible if intervals [S[i], F[i]) and [S[j], F[j]) do not overlap. We assume the activities have been sorted such that S [1] ≤ S [2] ≤ …≤ S[n]. (a) Design an O(n2) dynamic programming algorithm...
(5 +2 points) You are given an array A[1..n] of positive numbers where Ai] is the stock price on day i. You are allowed to buy the stock once and sell it at some point later. For each day you own the stock you pay S1 fee. Design a divide-and-conquer algorithm that will return a pair (i,j) such that buying the stock on day i and selling it on day j will maximize your gain The complexity of the algorithm...
There are n trading posts along a river. At any of the posts you can rent a canoe to be returned at any other post downstream. (It is next to impossible to paddle against the current.) For each possible departure point i and each possible arrival point j the cost of a rental from i to j is known. However, it can happen that the cost of renting from i to j is higher than the total cost of a...
Suppose you are given an array of n integers ai, az, ..., an. You need to find out the length of its longest sub-array (not necessarily contiguous) such that all elements in the sub-array are sorted in non-decreasing order. For example, the length of longest sub-array for (5, 10, 9, 13, 12, 25, 19, 30) is 5 and one such sub-array is (5, 10, 13, 19, 303. Note you may have multiple solutions for this case. Use dynamic programming to...
Suppose you are given an array of n integers a1, a2, ..., an. You need to find out the length of its longest sub-array (not necessarily contiguous) such that all elements in the sub-array are sorted in non-decreasing order. For example, the length of longest sub-array for {5, 10, 9, 13, 12, 25, 19, 70} is 6 and one such sub-array is {5, 10, 13, 19, 70}. Note you may have multiple solutions for this case. Use dynamic programming to...
(a) Your company participates in a competition and the fastest algorithm wins. You know of two different algorithms that can solve the problem in the competition. • Algorithm I solves problems by dividing them into five subproblems of half the size, recursively solving each subproblem, and then combining the solutions in linear time. • Algorithm 2 solves problems of size n by dividing them into 16 subproblems of size n/4, recursively solving each subproblem, and then combining the solutions in...
2. (25) [Rising trend] Textbook Exercise 17 in Chapter 6. The problem to solve is, in other words, to select from a sequence of n numbers a subset, including the first number, such that the selected numbers make a longest monotonously increasing sequence. In the exercise b, (i) write an optimal substructure (with an explanation),ii) write the iterative dynamic programming algorithm (pseudocode), and (iii) show the running-time analysis. Hints: the algorithm in the exercise a is a greedy algorithm; the...