0

I asked a question about the copy-strategy for Dijkstra and I'm trying to code a program similar to this answer wherein this situation, a directed, weighted graph G = (V, E) contains weights of either 0 or 1. Using the copy strategy I need to use Dijkstra's algorithm to find a path with at least 1 edge with weight 0 and minimum number of edges with weight 1.

My issue is that I am doing this with adjacency lists, where adj[u][v] contains the weight of the edge U->V. I know I need to have 2 copies of the graph so I can connect the edges with weight 0 from G to G', and I made the copy of the entire graph using the vector copy constructor, but I have gotten to a point where I do not know how to connect the lists to show that an edge connects a vertex from one graph to a vertex from the other so that the Dijkstra algorithm can move from one to the other when checking edges.

jjj
  • 575
  • 1
  • 3
  • 16
rp03
  • 3
  • 2
  • You made a mistake when you used the vector copy constructor. If you have two adjacency lists then you cannot easily have links between the adjacency lists. Copy the graph but within the adjacency list, not to a separate adjacency list. – john Jul 30 '20 at 06:13
  • What do you mean make a copy of the graph within the adjacency list? – rp03 Jul 30 '20 at 06:40
  • Suppose you have N vertices, then you need to add N more vertices for the copy. So you double the size of your adjacency list. Then for every edge from `i` to `j` in the original graph you add a new edge from `i+N` to `j+N`. That way you end up with two copies of your graph but both are in the same adjacency list. It's a simple operation, 3 or 4 lines of code. Then you need to add the zero weight links but that's easy because for each vertex `i` it's mirror is either vertex `i+N` or `i-N` depending on whether `i=N`. – john Jul 30 '20 at 06:43
  • @rp03 he needs the copies, because ha has a colored graph and the result needs to have a sub-path, that goes from one specific color to another. The answer to his previous post suggests to create a new graph out of 3 copies and link them in a specific way – jjj Jul 30 '20 at 13:13

1 Answers1

0

You should build a new graph, that is constructed the the way trincot described in his answer to you previous post and work only on this single graph. This makes it easier to implement dijkstra, because you have to handle the "go to other graph" case.

So you basically copy the graph 3 times, but each time, you rename the vertices slightly different. This way you can easily add the links in this new graph.

for(auto vertex : graph){
    // init newgraph[vertex]
    for(auto edge : vertex){
        // edge is just the other vertex of this edge
        newgraph[vertex].push_back(edge); //just copy
        newcosts[vertex][edge] = costs[vertex][edge]; // just copy
    }
    if(colorof(vertex) == "red")
        newgraph[vertex].push_back(vertex + "_");
        newcosts[vertex][edge + "_"] = 0;
}
for(auto vertex : graph){
    // init newgraph[vertex+"_"]
    for(auto edge : vertex){
        newgraph[vertex + "_" ].push_back(edge + "_" ); //just copy
        newcosts[vertex + "_" ][edge + "_" ] = costs[vertex][edge]; // just copy
    }
    if(colorof(vertex) == "blue")
        newgraph[vertex + "_" ].push_back(vertex + "__");
        newcosts[vertex + "__"][edge] = 0;
}
for(auto vertex : graph){
    // init newgraph[vertex+"_"]
    for(auto edge : vertex){
        newgraph[vertex + "__" ].push_back(edge + "__" ); //just copy
        newcosts[vertex + "__" ][edge + "__" ] = costs[vertex][edge]; // just copy
    }
}

Where graph contains you graph, costs[a][b] contains the costs from vertex a to b. I used strings as names for vertices to make naming easier, you can store them in a map if you like or you use integers and vectors for higher speed.

For dijkstra you have to store the length in the "middle" graph in addition to the normal path length for every vertex. You do this the same way, but you only increase the length, when both vertices are in the "middle" graph. and you only update the vertex, when you condition for the middle graph is true.

jjj
  • 575
  • 1
  • 3
  • 16