0

I am currently working on Dijkstra's Shortest Path problem. I have nothing specific in this project, algorithm is standard (implement with set of pairs) except I need to print the types of edges from one vertex to another.

Imagine I have 4 vertices and 5 edges. There exist a pair of vertices p(v1, v2) such that there are 2 or more edges connecting v1 and v2. For instance, we want to find distance from London to Paris. We know that we both can drive by car (one type of edge) or we can buy a plane ticket (another type of edge). What I want to do is to print the type of edge.

Example: I have two ways to reach Paris from London: London -> Calais -> Paris, min time 5 hours, by car; London -> Paris, min time 1 hour, by plane.

I know exactly, how to print min time or min distance, how to print the path, etc. But, how can I print the type of edge (type of transportation) such as 'by plane' or 'by car'? Here is what I tried:

struct neighbor {

int target_vertex;
double weight;
int type;
// for type: 0 - car
// 1 - bus
// 2 - plane

};

But still, I could not figure out, how would I store those edge 'types' while computing shortest path.

Code here: https://gist.github.com/anonymous/5943c448e47ebf0d3964baa53361459d

oneturkmen
  • 1,288
  • 1
  • 11
  • 27

2 Answers2

0

You already have this information, it is stored in the prev_type[x] array. This array contains the type of transport you used to reach final node t. It works in combination with the array prev[] which remembers the parent nodes, or the node node from which you reached the current one. So you start from your t (final node) and call prev[t], to get its parent, and than prev_type[t] will contain the type of transport used to reach t. Continue this way going back until you reach your s (start) node.

NiVeR
  • 9,644
  • 4
  • 30
  • 35
  • This is the problem, that it contains the types that do not exist. For instance, from one to another city the only way is by plane (shortest). But it says -> "bus bus plane" – oneturkmen Dec 01 '16 at 08:50
0

Solved the problem, predefining all the scenarious possible, from one city to another (basically - all combinations of the cities).

 if (choice == 1) {
    switch (from) {
        case 0: {
            if (to == 1) std::cout << " by foot ";
            if (to == 2) std::cout << " by foot -> by bus ";
            if (to == 3) std::cout  << " by air ";
            break;
        }
        case 1: {
            if (to == 0) std::cout << " by foot ";
            if (to == 2) std::cout << " by bus ";
            if (to == 3) std::cout << " by bus -> by car ";
            break;
        }
        case 2: {
            if (to == 0) std::cout << " by bus -> by foot ";
            if (to == 1) std::cout << " by bus ";
            if (to == 3) std::cout << " by car ";
            break;
        }
        case 3: {
            if (to == 1) std::cout << " by car -> by bus ";
            if (to == 2) std::cout << " by car ";
            if (to == 0) std::cout << " by air ";
        }
    }

from = the starting city.

to = the destination we are heading to.

I am sure the solution is not the best one, but for this specific case with small number of nodes and edges it is applicable.

oneturkmen
  • 1,288
  • 1
  • 11
  • 27