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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:25:23+00:00 2026-05-20T18:25:23+00:00

I have been fighting all day in understanding Dijkstra’s algorithm and implementing with no

  • 0

I have been fighting all day in understanding Dijkstra’s algorithm and implementing with no significant results. I have a matrix of cities and their distances. What I want to do is to given an origin point and a destination point, to find the shortest path between the cities.

Example:

     __0__ __1__ __2__ 
 0  |  0  | 34  |  0  |
    |-----|-----|-----|
 1  | 34  |  0  | 23  |
    |-----|-----|-----|
 2  |  0  | 23  |  0  |
     ----- ----- -----

I started wondering if there is an other way to solve this. What if I apply Prim’s algorithm from the origin’s point and then I loop through the whole tree created until I find the destination point?

  • 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-05-20T18:25:23+00:00Added an answer on May 20, 2026 at 6:25 pm

    You could apply Prim’s algorithm and then walk the resulting tree, but you answer may be wrong. Assume that you have a graph where each edge has the same weight. Prim’s algorithm simply chooses a minimal weight edge in the set of edges that could be added to the tree. It is possible that you will not choose an edge that will lead to a shortest path between two nodes.
    Assume:

         __0__ __1__ __2__ 
     0  |  0  |  1  |  1  |
        |-----|-----|-----|
     1  |  1  |  0  |  1  |
        |-----|-----|-----|
     2  |  1  |  1  |  0  |
         ----- ----- -----
    

    Starting from node 0 you could, via Prim’s, choose the edges 0-1 and 0-2 to make your tree. Alternately, you could pick edges 0-1 and 1-2 to make your tree. Under the first edge set, you could find the minimum length path from 0 to 2, but under the second edge set you would not find the minimal path. Since you can’t a-priori determine which edges get added in the Prim algorithm, you can’t use it to find a shortest path.

    You could consider the Bellman-Ford algorithm, but unless you’re dealing with negative edge weights I find Dijkstra’s algorithm preferable.

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

Sidebar

Related Questions

I've been fighting this all day. Inside my styles.xml file I have color information
I've been fighting with this problem all day and am just about at my
I've been fighting against IE8's compatibility mode all day and I'm about to chuck
Sorry if this is obvious or covered elsewhere, but i've been googling all day
I have been fighting this problem with the help of a RegEx cheat sheet,
I have been fighting this issue for some time so today I started a
Good evening, I am tired and have been fighting with this for hours. I
I have been fighting this issue for days now and about to beat my
Been fighting with Mercurial's .hgignore for a while under Windows. I have a folder
Have been searching all over the internet but struggling to find my answer to

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.