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

  • SEARCH
  • Home
  • 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 995007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:39:18+00:00 2026-05-16T06:39:18+00:00

for the past four days I am trying to understand the dijkstra’s algorithm. But

  • 0

for the past four days I am trying to understand the dijkstra’s algorithm. But I can’t. I have a vector of points. From that I created a cost matrix. But I don’t know how to make the dijkstra’s algorithm. Sources are available in net, but I am not from Computer science background, So I can’t understand them. I am trying to make a function like this

vector<int> dijkstra(costMatrix[][])
{
  ....
  ....
  return vector<int>pathPointindex
}

main()
{
    vector<Point> availablePoints;
    costMatrix[][]=createCostMatrix();
    vector<int> indexes=dijikstra(costMatrix)
    for(int i=0;i<indexes.size();i++)
       cout << "path points are " << availablePoints[indexes[i]] << endl;
}

If anybody, can you please post the code. I am not lazy. But my project already crossed the deadline one day ago. Now I lost my hope to understand the logic. Now Just I want the function. “A man in need is the angel indeed” .

EDIT: Special thanks to “Loki astari” for his excellent answer

  • 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-16T06:39:19+00:00Added an answer on May 16, 2026 at 6:39 am

    I advise you to look at TopCoder tutorial that have very pragmatic apporach.
    You will need to find out how STL priority queue works and make sure you don’t have any negative edge weights in your graph.

    Decent full implementation can be found here. You will have to add Path vector to it and implement RecoverPath method in order to get nodes on path from source to sink. To use this solution you will also need to convert your adjacency matrix to adjacency list in following way:

    for (int i=0;i<nNodes;i++)
        for (int j=0;j<nNodes;j++){
            if (costMatrix[i][j] != NO_EDGE_VALUE){
                G[i].pb(make_pair(j,costMatrix[i],[j]));
            }
        }
    

    EDIT: If your graph is dense I would advise you to use Ford Bellman algorithm is much simpler and shouldn’t be much slower.

    EDIT2: To calculate path you should add in the header

    int P[MAX]; /*array with links to parents*/
    for(i=0; i<=nodes; i++) P[i] = -1; /*magic unset value*/
    
    // dijkstra
    while(!Q.empty()) {
        ....
        if(!F[v] && D[u]+w < D[v]) {
            D[v] = D[u] + w;
            /*By setting P[v] value we will remember what is the 
              previous node on path from source */
            P[v] = u; // <-- added line
            Q.push(pii(v, D[v]));
        }
        ...
    }
    

    Then you have to add RecoverPath method (it works only when path exists)

    vector<int> RecoverPath(int src, int dest){
        vector<int> path;
        int v = dest;
        while (v != src) {
            path.push_back(v);
            v = P[v];
        }
        path.push_back(src);
        std::reverse(path.begin(),path.end());
        return path;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have four flags Current = 0x1 Past = 0x2 Future = 0x4 All
In past I use dynamic sql and datatable to get data from database. Such
Over the past few years I've changed from having a long flowing page of
For the past few weeks, I've been trying to learn about just how email
In the past, some of my projects have required me to create a movie
In the past I've worked with a number of programmers who have worked exclusively
In the past I've used profman2 to create MAPI profiles for servers that need
In the past week or so, I've noticed that Visual Studio 2010 is not
I've spent the past few days at the school of hard knocks learning how
For the past couple of hours I've been trying to format a MySQL timestamp

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.