Search code examples
c++graphpathdijkstra

How to use always the same type of edge in a directed weighted graph?


I'm doing a project for college and we are simulating a public transport (Only bus) system using directed weighted graphs in c++. Each node represents a stop and each edge represents the line of the bus and the line is a parameter of the edge besides the weight. So between stops we can have multiple buses doing that bit of the trip. I'm using the Dijkstra algorithm to find the shortest path in distance but I would like to add another option which is to choose the path with the least line changes (i.e. even if the path is longer, choose the one that uses always the same bus).

I've thought about implementing it with binary weight but I'm not sure if it would work. Any help is appreciated!


Solution

  • For each node where there are more than one line, split the node into several connected by edges with weight the cost of changing lines. The run Dijkstra.