Question

Give a dynamic programming algorithm that runs within the time complexity. Also give the space complexity of the algorithm. PleaseGiven a directed graph with non-negative integer edge weights, a pair of vertices s and t, and integers K and W, describe a d

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

et total number of vertices be n and total number of edges be m.

Let dp[n][n][K] be the dynamic programming array where dp[i][j][p] stores the weight of the path between vertex i and vertex j using exactly p edges.

Initially, dp[i][j][p] = infinity for all i, j, p

for i in 1 to n:

for j in 1 to n:

for p in 1 to K:

if (exists path between i and j using p vertices):

dp[i][j][p] = path_weight(i, j)

if(i == s and j == t and dp[i][j][p] == W) return true;

return false

Time complexity == O((n+m)WK)

Space complexity = O(n^2. K)

Please give it a thumbs up if this helped you, also provide your valuable feedback.

Add a comment
Know the answer?
Add Answer to:
Give a dynamic programming algorithm that runs within the time complexity. Also give the space complexity of the algorit...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT