SlideShare ist ein Scribd-Unternehmen logo
1 von 4
Downloaden Sie, um offline zu lesen
Problem 1: Let C[1 .. 2n] denote the array that contains the result of merging A and B. In the
array C, the elements of A must appear in the same order as in A; likewise, the elements of B must
also appear in the same order as in B.
Each way of merging A and B can be described by choosing n positions in C, filling the chosen
positions using the elements A[1] through A[n] in that order from left to right, and filling the
remaining n positions with the elements B[1] through B[n] in that order from left to right. Thus,
the number of possible ways of merging A and B is equal to the number of ways of choosing n
positions from a set of 2n positions. In other words, the number of possible ways to merge A and
B is 2n
n .
Part (b): Consider a decision tree representing a comparison-based algorithm that merges two
sorted arrays of size n. From the result of Part (a), the number of leaves in the decision tree must
be 2n
n . Thus, the height of the tree, that is the number of comparisons in the worst-case, must be
at least log2
2n
n . To show that this quantity is at least 2n − o(n), we use Stirling’s approximation:
Fact 3: For any positive integer k,
√
2πk (k/e)k ≤ k! ≤
√
2πk (k/e)k+1.
Note that 2n
n = (2n)!/(n! n!). Using Fact 3, (2n)!/(n! n!) ≥ 22ne2/(
√
π n2.5). Therefore,
log2 [(2n)!/(n! n!)] ≥ 2n − (2.5 log2 n + log2
√
π − log2 e2
).
Since log2 n = o(n), the right hand side of Inequality (1) is 2n − o(n). This completes the proof.
Problem 2:
Idea: We first use A to find the lower median x of S. If i specifies the position of the lower median
of S, then the ith smallest is indeed x. Otherwise, we use the Partition function on S to get
low and high sides of S. We recurse on the appropriate side of S, depending upon the relationship
between i and the position of the median of S. Since we use the median element as the pivot in
Partition, larger of the two sides of S has at most ⌈n/2⌉ elements. This leads to the running of
O(n).
Algorithm: Since the algorithm is recursive, we use the parameters a and b to specify a subarray
of S. The parameter r indicates that we want to find the rth smallest value in S[a .. b]. (The
initial call to the algorithm is Order-Statistic (S, 1, n, i).)
ALGORITHMS AND DATA STRUCTURES
Our online Tutors are available 24*7 to provide Help with Algorithms And Data structures
Homework/Assignment or a long term Graduate/Undergraduate Algorithms And Data structures Project.
Our Tutors being experienced and proficient in Algorithms And Data structures ensure to provide high
quality Algorithms And Data structures Homework Help. Upload your Algorithms And Data structures
Assignment at ‘Submit Your Assignment’ button or email it to info@assignmentpedia.com. You can use
our ‘Live Chat’ option to schedule an Online Tutoring session with our Algorithms And Data structures
Tutors.
Order-Statistic (S, a, b, r) /* Returns the rth smallest in S[a .. b]. */
1. Let p = b-a+1 and let m = floor((p+1)/2). Use the given
algorithm A to find the lower median x of S[a .. b].
(Thus, A returns the mth smallest value in S[a .. b].)
2. if (r = m) then return x
else
2.1. Use Partition on S[a .. b] using x as the pivot.
Let q be the index returned by Partition.
2.2 if (r < m) then /* Find the rth smallest on the low side of S. */
Order-Statistic (S, a, q-1, r)
else /* Find the (r-m)th smallest on the high side of S. */
Order-Statistic (S, q+1, b, r-m)
Correctness: Suppose |S| = n and we are looking for the ith smallest value. Algorithm A returns
the lower median x, that is, the mth smallest value, where m = ⌊(n + 1)/2⌋. The correctness of the
algorithm is a consequence of the following observations:
(a) If i = m, then the required value is indeed x.
(b) If i < m, then the ith smallest remains the the ith smallest on the low side of the partition.
(c) If i > m, then the ith smallest is the (i − m)th smallest on the high side of the partition.
Running time analysis: Let T(n) be the running time of the algorithm on an array of size n.
The call to Algorithm A uses O(n) time. The call to Partition also runs in O(n) time. Since the
median value is used as the pivot, the subsequent recursive call is on a subarray of size at most
⌈n/2⌉. The recursion ends when the subarray size is 1. Therefore, the recurrence for T(n) is:
T(n) ≤ T(⌈n/2⌉) + c n
for n ≥ 2 and T(1) = c1, for some constants c and c1.
We can solve the above recurrence using the Master Theorem. Comparing with the Master
Theorem template, we note that a = 1, b = 2 and f(n) = c n. Thus, logb a = 0 and nlogb a = n0 = 1.
Thus, f(n) = Ω(nlogb a+ǫ), with ǫ = 1. Thus, if the regularity condition holds, then Part 3 of Master
Theorem can be applied.
The regularity condition holds since af(n/b) = cn/2 = (1/2)f(n). Therefore, by Part 3 of
Master Theorem, T(n) = Θ(f(n)) = Θ(n). Thus, the running time is indeed linear in n.
Problem 3: For k = 1, the value x1 is stored with probability 1. Thus, the result holds for k = 1
trivially. So, we assume that k ≥ 2.
Consider any xi, where 1 ≤ i ≤ k. At the end of the kth step, the value stored will be xi if and
only if both of the following conditions hold:
1. At Step i, the value xi replaced the stored value.
2. At each of the subsequent steps i + 1, i + 2, . . ., k, the stored value was not replaced.
Letting E1 and E2 denote the two events above, we see that the required probability is given by
Pr{E1} × Pr{E2}.
Note that Pr{E1} = 1/i since in Step i, the value xi replaces the stored value with probability
1/i. For each subsequent step j (i + 1 ≤ j ≤ k), the probability that xi is not replaced is given by
1 − (1/j) = (j − 1)/j. Therefore,
Pr{E2} =
k
j=i+1
(j − 1)/j
=
i
i + 1
×
i + 1
i + 2
× · · · ×
k − 1
k
=
i
k
Therefore, the required probability = Pr{E1} × Pr{E2} = 1/k.
Problem 4:
Notation: For each node vi, pi denotes the profit obtained by placing a restaurant at vi. We use
d(vi, vj) to denote the distance between nodes vi and vj. Recall that every pair of restaurants must
be separated by a distance of at least k.
Main idea: For each node vi, an optimal solution may or may not place a restaurant at that
node. The dynamic programming algorithm keeps track of both of these possibilities.
For each node vi, we compute and store two values, denoted by A[i] and B[i], 1 ≤ i ≤ n.
Here, A[i] (B[i]) represents the maximum profit for the subproblem v1, . . . , vi when a restaurant
is placed (not placed) at vi. After computing all the n entries of A and B, the solution to the
problem is given by max{A[n], B[n]}. We now discuss how the values in arrays A and B can be
computed.
By definition, A[1] = p1 and B[1] = 0. Now, suppose we have calculated values A[1 .. i] and
B[1 .. i] for some i ≥ 1. We can compute A[i + 1] and B[i + 1] as follows.
(a) B[i + 1] = max{A[i], B[i]}. (Reason: If a restaurant is not placed at vi+1, then the best
profit for the subproblem v1 . . . vi+1 is the same as that for the subproblem v1 . . . vi .)
(b) Try to find the first node vj to the left of vi+1 (i.e., largest j ∈ [1 .. i]) such that d(vj, vi+1) ≥ k.
If such a node vj is found, then A[i + 1] = pi+1 + max{A[j], B[j]}; otherwise, A[i + 1] = pi+1.
(Reason: If a restaurant is placed at vi+1, then restaurants can’t be placed at any of the
nodes that are at a distance less than k from vi+1.)
Pseudocode for the algorithm:
1. A[1] = p1 and B[1] = 0.
2. for i = 1 to n − 1 do
(a) B[i + 1] = max{A[i], B[i]}.
(b) Try to find the first node vj to the left of vi+1 such that d(vj, vi+1) ≥ k. If such a node
vj is found, then A[i + 1] = pi+1 + max{A[j], B[j]}; otherwise, A[i + 1] = pi+1.
3. Output max{A[n], B[n]} as the solution.
Running time analysis: Steps 1 and 3 take O(1) time. So, the dominant part of the running
time is due to Step 2.
To implement Step 2 efficiently, we first compute the distance D[i] of each node vi from v1,
1 ≤ i ≤ n. (In other words, D[i] = d(v1, vi), 1 ≤ i ≤ n.) To do this, note that D[1] = 0 and
D[i] = D[i − 1] + d(vi−1, vi), 2 ≤ i ≤ n. Since the d(vi, vi−1) values are given, we can compute all
the n entries of D in O(n) time. Once we have all the entries of D, notice that for any vi and vj,
where j < i, the value d(vj, vi) = D[i] − D[j] can be computed in O(1) time.
For each iteration of the loop in Step 2, Step 2(a) runs in O(1) time. In Step 2(b), using the
array D, we can determine whether node vj exists, and if so, find the node in O(n) time. (This
uses a simple backward scan from vi+1.) After this, it takes only O(1) time to compute the value
of A[i + 1]. Thus, Step 2(b) runs in O(n) time. Since the for loop runs n − 1 times, the time for
Step 2 and hence the running time of the algorithm is O(n2).
visit us at www.assignmentpedia.com or email us at info@assignmentpedia.com or call us at +1 520 8371215

Weitere ähnliche Inhalte

Mehr von Assignmentpedia

Radar application project help
Radar application project helpRadar application project help
Radar application project help
Assignmentpedia
 
Parallel computing homework help
Parallel computing homework helpParallel computing homework help
Parallel computing homework help
Assignmentpedia
 
Network costing analysis
Network costing analysisNetwork costing analysis
Network costing analysis
Assignmentpedia
 
Matlab simulation project
Matlab simulation projectMatlab simulation project
Matlab simulation project
Assignmentpedia
 
Matlab programming project
Matlab programming projectMatlab programming project
Matlab programming project
Assignmentpedia
 
Image processing project using matlab
Image processing project using matlabImage processing project using matlab
Image processing project using matlab
Assignmentpedia
 
Help with root locus homework1
Help with root locus homework1Help with root locus homework1
Help with root locus homework1
Assignmentpedia
 
Theory of computation homework help
Theory of computation homework helpTheory of computation homework help
Theory of computation homework help
Assignmentpedia
 
Help With Digital Communication Project
Help With  Digital Communication ProjectHelp With  Digital Communication Project
Help With Digital Communication Project
Assignmentpedia
 
Filter Implementation And Evaluation Project
Filter Implementation And Evaluation ProjectFilter Implementation And Evaluation Project
Filter Implementation And Evaluation Project
Assignmentpedia
 
Doppler Processing Project
Doppler Processing ProjectDoppler Processing Project
Doppler Processing Project
Assignmentpedia
 
Digital Signal Processing
Digital Signal ProcessingDigital Signal Processing
Digital Signal Processing
Assignmentpedia
 

Mehr von Assignmentpedia (20)

Radar application project help
Radar application project helpRadar application project help
Radar application project help
 
Parallel computing homework help
Parallel computing homework helpParallel computing homework help
Parallel computing homework help
 
Network costing analysis
Network costing analysisNetwork costing analysis
Network costing analysis
 
Matlab simulation project
Matlab simulation projectMatlab simulation project
Matlab simulation project
 
Matlab programming project
Matlab programming projectMatlab programming project
Matlab programming project
 
Links design
Links designLinks design
Links design
 
Image processing project using matlab
Image processing project using matlabImage processing project using matlab
Image processing project using matlab
 
Help with root locus homework1
Help with root locus homework1Help with root locus homework1
Help with root locus homework1
 
Transmitter subsystem
Transmitter subsystemTransmitter subsystem
Transmitter subsystem
 
Computer Networks Homework Help
Computer Networks Homework HelpComputer Networks Homework Help
Computer Networks Homework Help
 
Theory of computation homework help
Theory of computation homework helpTheory of computation homework help
Theory of computation homework help
 
Econometrics Homework Help
Econometrics Homework HelpEconometrics Homework Help
Econometrics Homework Help
 
Video Codec
Video CodecVideo Codec
Video Codec
 
Radar Spectral Analysis
Radar Spectral AnalysisRadar Spectral Analysis
Radar Spectral Analysis
 
Pi Controller
Pi ControllerPi Controller
Pi Controller
 
Help With Digital Communication Project
Help With  Digital Communication ProjectHelp With  Digital Communication Project
Help With Digital Communication Project
 
Fpga Design Project
Fpga Design ProjectFpga Design Project
Fpga Design Project
 
Filter Implementation And Evaluation Project
Filter Implementation And Evaluation ProjectFilter Implementation And Evaluation Project
Filter Implementation And Evaluation Project
 
Doppler Processing Project
Doppler Processing ProjectDoppler Processing Project
Doppler Processing Project
 
Digital Signal Processing
Digital Signal ProcessingDigital Signal Processing
Digital Signal Processing
 

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Kürzlich hochgeladen (20)

"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 

Algorithms and data structures help

  • 1. Problem 1: Let C[1 .. 2n] denote the array that contains the result of merging A and B. In the array C, the elements of A must appear in the same order as in A; likewise, the elements of B must also appear in the same order as in B. Each way of merging A and B can be described by choosing n positions in C, filling the chosen positions using the elements A[1] through A[n] in that order from left to right, and filling the remaining n positions with the elements B[1] through B[n] in that order from left to right. Thus, the number of possible ways of merging A and B is equal to the number of ways of choosing n positions from a set of 2n positions. In other words, the number of possible ways to merge A and B is 2n n . Part (b): Consider a decision tree representing a comparison-based algorithm that merges two sorted arrays of size n. From the result of Part (a), the number of leaves in the decision tree must be 2n n . Thus, the height of the tree, that is the number of comparisons in the worst-case, must be at least log2 2n n . To show that this quantity is at least 2n − o(n), we use Stirling’s approximation: Fact 3: For any positive integer k, √ 2πk (k/e)k ≤ k! ≤ √ 2πk (k/e)k+1. Note that 2n n = (2n)!/(n! n!). Using Fact 3, (2n)!/(n! n!) ≥ 22ne2/( √ π n2.5). Therefore, log2 [(2n)!/(n! n!)] ≥ 2n − (2.5 log2 n + log2 √ π − log2 e2 ). Since log2 n = o(n), the right hand side of Inequality (1) is 2n − o(n). This completes the proof. Problem 2: Idea: We first use A to find the lower median x of S. If i specifies the position of the lower median of S, then the ith smallest is indeed x. Otherwise, we use the Partition function on S to get low and high sides of S. We recurse on the appropriate side of S, depending upon the relationship between i and the position of the median of S. Since we use the median element as the pivot in Partition, larger of the two sides of S has at most ⌈n/2⌉ elements. This leads to the running of O(n). Algorithm: Since the algorithm is recursive, we use the parameters a and b to specify a subarray of S. The parameter r indicates that we want to find the rth smallest value in S[a .. b]. (The initial call to the algorithm is Order-Statistic (S, 1, n, i).) ALGORITHMS AND DATA STRUCTURES Our online Tutors are available 24*7 to provide Help with Algorithms And Data structures Homework/Assignment or a long term Graduate/Undergraduate Algorithms And Data structures Project. Our Tutors being experienced and proficient in Algorithms And Data structures ensure to provide high quality Algorithms And Data structures Homework Help. Upload your Algorithms And Data structures Assignment at ‘Submit Your Assignment’ button or email it to info@assignmentpedia.com. You can use our ‘Live Chat’ option to schedule an Online Tutoring session with our Algorithms And Data structures Tutors.
  • 2. Order-Statistic (S, a, b, r) /* Returns the rth smallest in S[a .. b]. */ 1. Let p = b-a+1 and let m = floor((p+1)/2). Use the given algorithm A to find the lower median x of S[a .. b]. (Thus, A returns the mth smallest value in S[a .. b].) 2. if (r = m) then return x else 2.1. Use Partition on S[a .. b] using x as the pivot. Let q be the index returned by Partition. 2.2 if (r < m) then /* Find the rth smallest on the low side of S. */ Order-Statistic (S, a, q-1, r) else /* Find the (r-m)th smallest on the high side of S. */ Order-Statistic (S, q+1, b, r-m) Correctness: Suppose |S| = n and we are looking for the ith smallest value. Algorithm A returns the lower median x, that is, the mth smallest value, where m = ⌊(n + 1)/2⌋. The correctness of the algorithm is a consequence of the following observations: (a) If i = m, then the required value is indeed x. (b) If i < m, then the ith smallest remains the the ith smallest on the low side of the partition. (c) If i > m, then the ith smallest is the (i − m)th smallest on the high side of the partition. Running time analysis: Let T(n) be the running time of the algorithm on an array of size n. The call to Algorithm A uses O(n) time. The call to Partition also runs in O(n) time. Since the median value is used as the pivot, the subsequent recursive call is on a subarray of size at most ⌈n/2⌉. The recursion ends when the subarray size is 1. Therefore, the recurrence for T(n) is: T(n) ≤ T(⌈n/2⌉) + c n for n ≥ 2 and T(1) = c1, for some constants c and c1. We can solve the above recurrence using the Master Theorem. Comparing with the Master Theorem template, we note that a = 1, b = 2 and f(n) = c n. Thus, logb a = 0 and nlogb a = n0 = 1. Thus, f(n) = Ω(nlogb a+ǫ), with ǫ = 1. Thus, if the regularity condition holds, then Part 3 of Master Theorem can be applied. The regularity condition holds since af(n/b) = cn/2 = (1/2)f(n). Therefore, by Part 3 of Master Theorem, T(n) = Θ(f(n)) = Θ(n). Thus, the running time is indeed linear in n.
  • 3. Problem 3: For k = 1, the value x1 is stored with probability 1. Thus, the result holds for k = 1 trivially. So, we assume that k ≥ 2. Consider any xi, where 1 ≤ i ≤ k. At the end of the kth step, the value stored will be xi if and only if both of the following conditions hold: 1. At Step i, the value xi replaced the stored value. 2. At each of the subsequent steps i + 1, i + 2, . . ., k, the stored value was not replaced. Letting E1 and E2 denote the two events above, we see that the required probability is given by Pr{E1} × Pr{E2}. Note that Pr{E1} = 1/i since in Step i, the value xi replaces the stored value with probability 1/i. For each subsequent step j (i + 1 ≤ j ≤ k), the probability that xi is not replaced is given by 1 − (1/j) = (j − 1)/j. Therefore, Pr{E2} = k j=i+1 (j − 1)/j = i i + 1 × i + 1 i + 2 × · · · × k − 1 k = i k Therefore, the required probability = Pr{E1} × Pr{E2} = 1/k. Problem 4: Notation: For each node vi, pi denotes the profit obtained by placing a restaurant at vi. We use d(vi, vj) to denote the distance between nodes vi and vj. Recall that every pair of restaurants must be separated by a distance of at least k. Main idea: For each node vi, an optimal solution may or may not place a restaurant at that node. The dynamic programming algorithm keeps track of both of these possibilities. For each node vi, we compute and store two values, denoted by A[i] and B[i], 1 ≤ i ≤ n. Here, A[i] (B[i]) represents the maximum profit for the subproblem v1, . . . , vi when a restaurant is placed (not placed) at vi. After computing all the n entries of A and B, the solution to the problem is given by max{A[n], B[n]}. We now discuss how the values in arrays A and B can be computed. By definition, A[1] = p1 and B[1] = 0. Now, suppose we have calculated values A[1 .. i] and B[1 .. i] for some i ≥ 1. We can compute A[i + 1] and B[i + 1] as follows.
  • 4. (a) B[i + 1] = max{A[i], B[i]}. (Reason: If a restaurant is not placed at vi+1, then the best profit for the subproblem v1 . . . vi+1 is the same as that for the subproblem v1 . . . vi .) (b) Try to find the first node vj to the left of vi+1 (i.e., largest j ∈ [1 .. i]) such that d(vj, vi+1) ≥ k. If such a node vj is found, then A[i + 1] = pi+1 + max{A[j], B[j]}; otherwise, A[i + 1] = pi+1. (Reason: If a restaurant is placed at vi+1, then restaurants can’t be placed at any of the nodes that are at a distance less than k from vi+1.) Pseudocode for the algorithm: 1. A[1] = p1 and B[1] = 0. 2. for i = 1 to n − 1 do (a) B[i + 1] = max{A[i], B[i]}. (b) Try to find the first node vj to the left of vi+1 such that d(vj, vi+1) ≥ k. If such a node vj is found, then A[i + 1] = pi+1 + max{A[j], B[j]}; otherwise, A[i + 1] = pi+1. 3. Output max{A[n], B[n]} as the solution. Running time analysis: Steps 1 and 3 take O(1) time. So, the dominant part of the running time is due to Step 2. To implement Step 2 efficiently, we first compute the distance D[i] of each node vi from v1, 1 ≤ i ≤ n. (In other words, D[i] = d(v1, vi), 1 ≤ i ≤ n.) To do this, note that D[1] = 0 and D[i] = D[i − 1] + d(vi−1, vi), 2 ≤ i ≤ n. Since the d(vi, vi−1) values are given, we can compute all the n entries of D in O(n) time. Once we have all the entries of D, notice that for any vi and vj, where j < i, the value d(vj, vi) = D[i] − D[j] can be computed in O(1) time. For each iteration of the loop in Step 2, Step 2(a) runs in O(1) time. In Step 2(b), using the array D, we can determine whether node vj exists, and if so, find the node in O(n) time. (This uses a simple backward scan from vi+1.) After this, it takes only O(1) time to compute the value of A[i + 1]. Thus, Step 2(b) runs in O(n) time. Since the for loop runs n − 1 times, the time for Step 2 and hence the running time of the algorithm is O(n2). visit us at www.assignmentpedia.com or email us at info@assignmentpedia.com or call us at +1 520 8371215