1

I am still new to graphs and am building a weighted directed graph for a school project. I was able to complete all the functions except the one that find the minimum weighted path from a node to another. I am able to find a path, but don't know how I would store all the paths then get the minimum weighted one. My graph is represented by a vector .

I have created a helper function that gives me a path from the source to the destination and a function that finds the weight of the path. They both work but I can't get the one that find the path to find all the paths instead of the first one it finds.

int WeightedDigraph::Myhelp(int to) const{
    for (mytup::const_iterator k = adjList.begin(); k != adjList.end(); ++k) {
        if(get<1>(*k) == to){ //destination
                return get<0>(*k); //source
        }
    }
    return 10000;
}

list<int> WeightedDigraph::FindMinimumWeightedPath(int from, int to) const {
    list<int> minpath;
    minpath.push_front(to);
    int src = Myhelp(to);
    while(src != from){
        minpath.push_front(src);
            src = Myhelp(src);
    }
    return minpath;
}

This code returns a list for "a" path from "from" to "to". Instead, I want it to return the one with the least weight. I have to function getPathWeight(const list& path ) already setup to find the weight of each path and compare them but how can I get all paths in one place?

Update for minimum, working example with includes:

Main.cpp

    #include "WeightedDigraph.h"
#include <iostream>
#include <string>
#include <list>
using namespace std;

int main(int argc, char* argv[])
{

    if(argc != 4) {
        cerr << "Incorrect number of command line arguments." << endl;
        cerr << "Usage: " << argv[0] << " <filename> <start vertex> <dest vertex>" << endl;
        exit(EXIT_FAILURE);
    }

    WeightedDigraph graph(argv[1]);

    cout << "The graph has " << graph.GetOrder() << " vertices and " << graph.GetSize() << " arcs" << endl;

    int source = atoi(argv[2]);
    int dest = atoi(argv[3]);

    if (graph.DoesPathExist(source, dest)) {
        list<int> path = graph.FindMinimumWeightedPath(source, dest);
        //then the path will be used for other functions
    return 0;
}

WeightedDiagraph.cpp:

#include "WeightedDigraph.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <limits>
#include<list>

using namespace std;


void WeightedDigraph::InsertArc(int from, int to, double weight) {
    tuple<int, int, double> newtup (from, to, weight); 
    adjList.push_back(newtup);
}

double WeightedDigraph::GetPathWeight(const list<int> & path) const {
    //working
}

//other functions that are not needed for my question

WeightedDiagraph.h:

#ifndef WeightedDigraph_H
#define WeightedDigraph_H

#include<list>
#include<string>
#include<vector>
#include<tuple>

using namespace std;

typedef vector<tuple<int,int,double>> mytup;

class WeightedDigraph {
public:
    mytup adjList;
    //all methods

private:
    int numVertices;
    int numArcs;

    int from;
    int to;
    double weight;

}
user3812411
  • 342
  • 3
  • 17
  • 3
    It would be helpful if you could include a `main()` function and the `#include` statements, and then shrink your code to the smallest program possible that produces your problem. We call this a [mcve]. This allows members to copy, paste, compile, and then Help you. – Gardener Apr 17 '19 at 19:56
  • 2
    This is a good place to use the classic Dijkstra's algorithm (see related question https://stackoverflow.com/questions/3447566/dijkstras-algorithm-in-c ) – efofex Apr 17 '19 at 19:59
  • @Gardener is it better now? Thank you. – user3812411 Apr 17 '19 at 20:11
  • Would you just not sum up all the weights of all the paths and then pick the smallest one? – Sailanarmo Apr 17 '19 at 20:19
  • There might be enough to determine the problem but it's not a [mcve]. `InsertArc` and `GetPathWeight` are missing from the class definition. There is a `#endif` missing. It's incomplete. An MCVE would have everything in one file unless the problem is a linking issue and with all inputs hard-coded unless the problem is an input issue. Not minimal, and without the inputs it's not verifiable. That said, the real point of the MCVE is to eliminate the need to ask questions. It's hard to craft a MCVE without also discovering and fixing the bug. – user4581301 Apr 17 '19 at 20:41
  • @user4581301 would a link to the complete project online work? – user3812411 Apr 17 '19 at 23:39
  • @Sailanarmo this is exactly my problem. I don't know how to get all the paths in one point to then be able to check all the weights and compare them. – user3812411 Apr 17 '19 at 23:39
  • @user3812411 Much better. Optimal would be to make the files smaller and to try to put everything into one file. The longer it is and the more copies and pastes that are needed to get it to an IDE slows down the member until they decide to move on. See the explanation by user4581301 above. – Gardener Apr 17 '19 at 23:47
  • Missing GetOrder(), DoesPathExist(), and FindMinimumWeightedPath(). Sorry, I've got to head home now. Some people are smart enough to read the code and find bugs. I use the compiler and a debugger to do that. Hopefully one of the smarter members will have time to act like a compiler and read the code. – Gardener Apr 17 '19 at 23:56
  • Almost certainly a link to the code would not be minimal. Plus links rot. A question that depends on links will become useless once the links are gone. Avoid links to code except as supplemental information for more context. All I think you need here is to touch up the code you have in the question and make sure that it A) compiles and B demonstrates the bug when run. – user4581301 Apr 17 '19 at 23:56
  • A quick blurb on the MCVE: The goal of the MCVE is to isolate the bug in as little code as possible. This leaves the bug with no place to hide and picking it off at that point is usually easy. Demanding a MCVE forces the asker to apply a powerful debugging technique and, usually long before they get to the ultimate end, often find and fix the bug themselves. If they do this before asking the question, odds are really good they don't need to ask the question anymore. – user4581301 Apr 17 '19 at 23:58
  • @user4581301 the think is my problem is not a bug but a way of thinking. I just need an explanation on how this should be done. – user3812411 Apr 18 '19 at 01:16

0 Answers0