Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8679547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:59:54+00:00 2026-06-12T20:59:54+00:00

What is the time-complexity of the the algorithm (in pseudo-code) below? I have had

  • 0

What is the time-complexity of the the algorithm (in pseudo-code) below? I have had a hard time analyzing it. It is an implementation of the problem specified at another thread: Algorithm to find max cost path of length N in matrix, from [0,0] to last row. But because I’m new here I don’t have points enough to comment in the thread (I think)?. This is my first post, so excuse me if i did something wrong.

The algorithm uses memoization with the cache[][][][]. During the initialization cache[][][][] is filled with -1.

The method is called with maxPath(0,0,m,DOWN). Where m is the amount of steps to take, n<=m<=n^2 and the directions are defined as DOWN=0, LEFT=1, RIGHT=2.

function maxPath(i, j, fuel, direction)
    if i = n OR j = n OR j < 0 OR fuel = 0 then
         return 0;
    end if
    if cache[i][j][f-1][d] != -1 then
         return cache[i][j][f - 1][d];
    end if
    ret := 0
    acc+ = matrix[i][j]
    ret:=max(ret,maxPath(i+1, j, fuel-1, DOWN))
    if f > n - i then . If there is enough steps to move horizontally
        if d = RIGHT or d = DOWN then
            ret:=max(ret,maxPath(i, j+1, fuel-1, RIGHT))
        end if
        if d = LEFT or d = DOWN then
            ret:=max(ret,maxPath(i, j-1, fuel-1, LEFT))
        end if
    end if
    return cache[i,j,fuel-1,direction] = ret + matrix[i][j]
end function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:59:55+00:00Added an answer on June 12, 2026 at 8:59 pm

    Okey I have solved it and was thinking that I should share my results.

    If we let G=(V,E) be a graph where V={maxPath(i, j, p, d) : for all possible i, j, p and d } and E= {uv : u = f(i, j, p, d) and v = f(i’, j’, p’, d’) }. We have that G is a graph of states which is clearly DAG (directed acyclic graph). Then, we can compute maxPath with memoization.

    We have

        n possible values for i
        n possible values for j
        m possible values for fuel (which in worst case is n^2)
        3 possible values for direction
    

    Then we have n * n * m * 3 differents functions of maxPath to be called which is n^4*3 in worst case. So, we have O(n^4) different functions. Each call of function runs in O(1) time. Because the graph has a suboptimal structure, each subproblem is problem of the original one. So with memoization we dont have to compute the same ‘function’ two times, thus we get O(n^4).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simplex algorithm is said to have exponential worst case time complexity. Yet it is
Can an algorithm having a time complexity of O( n ) have a space
Could you please help me find out the time complexity of the Fleury' algorithm
Is it possible to solve a problem of O(n!) complexity within a reasonable time
I understand how to find the time complexity of an algorithm when I've been
What is the time complexity of the following implemented algorithm? I should notice that
How can I calculate the time complexity of a recursive algorithm? int pow1(int x,int
Assuming some algorithm has a polynomial time complexity T(n) , is it possible for
Is it possible for an algorithm/program to have the same worst-case and best-case time?
EDIT: What time complexity has algorithm implemented in this assembly ? .file a.c .section

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.