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 8676021
In Process

The Archive Base Latest Questions

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

I am aware of the Dijkstra’s shortest path algorithm. However, if I were to

  • 0

I am aware of the Dijkstra’s shortest path algorithm. However, if I were to modify it so that instead of finding the shortest path it would find the longest path using a greedy algorithm. What would I have to do to the code below:

Here is what Im using:

as a compare function to select the correct node in the shortest path version:

 if (Cost(potential_node) > Cost(current_node) + cost(source , current_node)) then
 cost (potential_node) = cost(current_node) + cost (source, current_node)

However, to get to the flip side this is not working:

 if (Cost(potential_node) < Cost(current_node) + cost(source , current_node)) then
 cost (potential_node) = cost(current_node) + cost (source, current_node)

A bit confused, would really appreciate some feedback

  • 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:05:14+00:00Added an answer on June 12, 2026 at 8:05 pm

    The longest path problem is NP-Hard, and thus there is no known polynomial solution.

    The suggested modification does not work, because when dijkstra’s algorithm marks a node as “closed” it means – there will never be a shorter path to it. The claim is not true when trying to do it for longest path, if you closed a node – it doesn’t mean there is no longer path to it.

    Recall that this is exactly what we prove on Dijkstra’s algorithm at each step (more “relaxations” will not find any shorter path), but if you find a current longest path to a vertex using the midification – it doesn’t mean it is indeed the longest one – there might be one longest path not yet explored.


    Edit – counter example when it doesn’t work (forgive me I am a terrible ascii artist)

            A
          /   \
         /     \
        1       2  
       /         \
      B-----5---> C
    V = {A,B,C} ;  E = { (A,B,1), (A,C,2), (B,C,5) }
    

    Now, starting from A, this approach will first find C as the “longest path” and close it. From now on – there is no changes to C according to Dijkstra’s algorithm, so you will conclude that the longest path from A to C is of length 2, while the path A->B->C is longer.

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

Sidebar

Related Questions

I am aware that Dijkstra's algorithm can find the minimum distance between two nodes
im aware that j2se 6 has introduced the JavaCompiler API; however, it's cumbersome to
I aware that Java object constructors implicitly initialize their instance's non-static fields. However, I'm
I am aware that XDebug's delta should not be considered accurate. However, when tracing
I'm aware that sleep should be accessed in a static context. However I need
So I am aware that JavaFx's method of updating the GUI while using a
Are aware of any solution that let me deactivate my facebook account through an
I aware that this will be a less programming question, but still... How can
I'm aware of using custom configuration values for my custom services, but what if
Context I am aware of http://closure-library.googlecode.com/svn/trunk/closure/goog/demos/editor/equationeditor.html# However, when I try it, it does not

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.