How do I find all possible shortest paths from a source vertex to more than one destination, such that the edge-weights in the shortest paths tree are in decreasing order of weights?
Let us assume here a strict decreasing of the weights among a path.
This implies that when considering a given path, we have to memorize/consider not only the corresponding total distance, but also the value of the last weight.
Therefore, at a given node, we have to memorize different possible paths, i.e. different pairs (total distance, last weight), and not only one. This does not not mean that all combinations must be memorized. For example, if we have two paths (13,2) and (15,2), only (13,2) is kept. In the same spirit, if we get (10,3) and (10,2), only (10,3) must be kept.
This is illustrated in figure hereafter, hopefully self explaining. I assume directed edges here.
3 4 2
A(3,3) -->C (6,2), (11,5)---->D (11,4), (13,3) ------>F (13,2)
| | | NB: (15,2) skipped
|3 |5 |3
| | 4 |
S ------->B (6,6)------------>E (10, 4)
6
In practice, existing short path finding algorithms should be adapted for that. My understanding is that for example the Floyd–Warshall algorithm
can be adapted for that purpose.
Related
I have a simple graph like this:
(v1)---(v2)---(v3)---(v10)
| | |
(v4)---(v5)---(v6)
| | |
(v7)---(v8)---(v9)
I convert the game board into grids and each grid is a vertex and each vertex has a weight.
Now I need an algorithm to find most weighted way between one vertex which can be one of vertices from (v1) to (v9) and another vertex is (v10).
Example for vertices weight: Consider that each vertex weight is 1, so, If my path be (v1)->(v2)->(v3)->(v10), my path weight will be 4.
I already used BFS algorithm to find all possible paths, then calculate weights of proper paths (those paths which end with (v10)) between all paths. But BFS can't find all possible paths for me (maybe BF. However, I can use a trick for that, but I wonder if there is an algorithm for making my job easy and more efficient :).
I am trying to solve the problem 24-1 of introduction to algorithms, It refers to the Yen's optimization of Bellman-Ford algirithm, I find the introduction in wiki,improvement:
Yen's second improvement first assigns some arbitrary linear order on all vertices and then partitions the set of all edges into two subsets. The first subset, Ef, contains all edges (vi, vj) such that i < j; the second, Eb, contains edges (vi, vj) such that i > j. Each vertex is visited in the order v1, v2, ..., v|V|, relaxing each outgoing edge from that vertex in Ef. Each vertex is then visited in the order v|V|, v|V|−1, ..., v1, relaxing each outgoing edge from that vertex in Eb. Each iteration of the main loop of the algorithm, after the first one, adds at least two edges to the set of edges whose relaxed distances match the correct shortest path distances: one from Ef and one from Eb. This modification reduces the worst-case number of iterations of the main loop of the algorithm from |V| − 1 to |V|/2.
Unfortunately, I can't prove that how the method that can make at least two edges relax distances match the correct shortest path distances: one from Ef and one of Eb.
Ef and Eb are acyclic with topological sort.
Let's prove the Upper bound of main loop is |V|/2
For all vertices v, there are two options for shortest distance from source s. One is d[s,v] = INFINITE, another is d[s,v] = FINITE. Thus, we need to consider the case d[s,v] is finite, which means there must exist some shortest path from s to v.
Let suppose p = (v0,v1,v2,....,vk-1,vk) is that path, where vo = s and vk = v. Let us consider how many times there is a change in direction in p, that is, a situation in which (vi-1,vi) belongs to Ef and (vi,vi+1) belongs to Eb. Based on the proof of Lemma 24.2, p has at most |V|-1 edges. Therefore there are at most |V|-2 changes in direction. Because Ef and Eb are topological sort, any portion of the path where there is no change in direction can be computed with the correct d values in the first or second half of a single pass once the node that begins the no-change-in-direction sequence has the correct d value. Each change in directions requires a half pass in the new direction of the path.
|V-1| | first edge direction | passes
----- | -------------------- | ------
even | forward | (|V|-1)/2
even | backward | (|V|-1)/2 +1
odd | forward | |V|/2
odd | backward | |V|/2
So this modification reduces the worst-case number of iterations of
the main loop of the algorithm from |V| − 1 to |V|/2.
In my implementation I need to find 4 non intersecting spanning trees in a 2D torus. Assuming all the links are bidirectional. and Bidirectional links are not intersecting.
Ex: my 2D torus is of 3 * 3
| | |
--0-1-2--
| | |
--3-4-5--
| | |
--6-7-8--
| | |
So, each link here is actually representing a bidirectional link for example there are two edges between 0 and 3. one from 0 towards 3 and one from 3 towards 0.
In output I should get 4 non intersecting spanning trees.
Although I have thought of some algorithm but it fails somehow.
ALgorithm:
I have the torus represented in the form of adjacency matrix. And a link between 0 and 3 are represented by 1 in adjacency matrix at position AM[0][3] and AM[3][0]
Node consist of (node_number, parent,weight)
Intially I maintain a priorList in which all nodes are set to their (node_number, -1, INT_MAX) but root of spanning tree which is set to (node_number, -1, 0)
Extract node from priorList with minimum weight, such that its parent does not have already a child. (In first iteration it would always be root). This I do by checking in the set of already extracted nodes in MST.
If found then update all its neighbours other than the nodes already extracted from priorList.
here by updating I mean update their parents, if already discovered.
Now add the extracted node in 1st step into the MST.
Go to step 1 until priorList is not empty.
It will give 1 MST but will get stuck while getting the 2nd MST from the same Adjacency matrix which was left as output while calculation of 1st MST(By which I mean if I had already used some edges in the 1st MST then I would have removed that edges from the Matrix).
Remember all 4 spanning trees should start from the same given root, and try if I can somehow use variant of Prim's algorithm(http://en.wikipedia.org/wiki/Prim%27s_algorithm).
I don't know any other algorithm as of now, but lets see if someone can help.
I'm trying to solve this problem using an algorithm for at least a week but to no avail. I have a set of roads (1,2,3,...) and for each road there can be at least 1 vehicle that can pass (A,B,C,...). My problem is how to get the least number of rides for one to take to reach one road to another. For example:
road | vehicle(s)
1 | A,B
2 | A,B,C
3 | C,D
4 | C,D
5 | D,E
The output should be:
from 1, ride A or B to 2
from 2, ride C to 4
from 4, ride D to 5
The policy here is to exhaust one ride as much as possible, but the output should have the least number of ride changes.
Example 2:
road | vehicle(s)
1 | A,B
2 | A,B,C
3 | C,D
4 | C,D
5 | A,E
6 | E,F
7 | F
8 | F,H
9 | F,G
10 | F,G
11 | F
12 | F,G
13 | G,H
14 | H
Output:
from 1, ride A to 5
from 5, ride E to 6
from 6, ride F to 8
from 8, ride H to 14
Please help me to write the algorithm for the problem. I need to solve this for my project. Thanks!
PS: If you need other examples or clarification, just tell me in the comments below.
One way to think about this problem is to model it as a graph search problem. For each of the different vehicles v, construct a graph Gv whose nodes are all of the roads in the network with arcs between the nodes that are connected by vehicle v. For example, in your first example, the graph GA would have five nodes, of which there is only a connection from node 1 to node 2, while the graph GC would have five nodes with edges from 2 to 3, 3 to 4, and 4 to 2.
Now, consider the final graph G constructed as follows. G is formed by taking all of the graphs Gv for all of the vehicles v (meaning that there are multiple copies of each node in the original graph), then adding edges between corresponding nodes in the different graphs if there are multiple different vehicles that all reach a road. For example, in the graph G for the original problem, you would have G contain subgraphs GA and GB, ..., GE. Since road 1 is serviced by both vehicles A and B, you would add an edge between the nodes for road 1 in the graphs GA and GB, and since road 2 is serviced by vehicles A, B, and C, there would be edges connecting all of the road 2 nodes for subgraphs GA, GB, and GC.
You are interested in minimizing the number of transfers you need to make, and so we'd like to set up some costs in this graph that measure that. In particular, we say that any edge contained completely in one of the subgraphs Gv has cost zero, since once you're on a vehicle of a given type you don't need to make any changes to move around the roads the vehicle services. However, we assign a cost of one to each of the edges linking the subgraphs for different vehicles, since following this sort of edge would mean that you're changing which vehicle you're using. If you now trace out any path in this graph, the total cost of that path is the number of vehicle changes you make.
To finalize the construction, we need to have some way of finding a cost from the starting road to the destination. To do this, we'll add two new nodes s and t to the graph representing the start and end of the journey. Node s will have cost-zero directed edges to the first road of each of the subgraphs, since when you start off the journey you can pick any of the vehicles as your first step. Similarly, every subgraph's node for the final road will have a zero-cost edge to t, since once you arrive at the destination road it doesn't matter what vehicle you're in.
In this setup, any path from s to t has a cost equal to the total number of transfers you need to make. Finding the shortest path in this graph from s to t therefore gives you both the route to take and the cost of that path. You can solve this using Dijkstra's algorithm, for example.
The runtime of this algorithm isn't stellar, but it's still polynomial time. Suppose that there are n roads and m different vehicles. The graph we create will have m different subgraphs in it, each of which is for one vehicle, and in the worst case these graphs have O(n2) edges in them. If we then link together all the corresponding nodes of these subgraphs where you can change lines, then in the worst case there will be O((mn)2) such edges, since there are m graphs of n nodes that all might be connected to one another. We thus have a graph with O(mn) nodes and O((mn)2) edges, so we can run Dijkstra's algorithm in O((mn)2 + mn log mn) time.
The main idea that I think is important to take away from this problem is that you can often solve problems about finding paths in graphs subject to certain constraints by creating a new graph formed out of multiple copies of the old graph and then coming up with a good edge-cost function for that graph.
Hope this helps!
I want to design a algorithm. In a directed graph G=(V,E), and each arc has a numerical weight. The algorithm needs to return a set S of arcs of maximum weight so that no two arcs in A have the same tail. Assume that it has at least 7 nodes and 10 arcs. Could anyone give some hints about this algorithm?
You say that your arcs are not allowed to have the same tail. So I would divide the set of arcs into several "bins" which are determined by the tail of the arc. I.e. you take each arc, look at its tail, and put it into the corresponding bin.
Consider the graph consistign of the following arcs:
(1->2)
(1->3)
(2->1)
(2->4)
(3->2)
Then, we have something as follows:
bin 1 | 2 | 3 | 4
arc 2 3 | 1 4 | 2 | (empty)
weight .. .. | .. .. | .. |
It is now clear, that we can take at most one arc from each bin. In order to maximize the sum, we can pick always the one with the largest weight in each bin.
Edit: Note that your algorithm does not need to do this whole bin-thing. It can just walk across all arcs and update the solution dynamically.