Finding the shortest distance between two nodes given multiple graphs - algorithm

Assume that we have a set of nodes and multiple graphs with different edges. I need to find the shortest path between two nodes. as an example given, there are three graphs as graph 01, graph 01 & graph 03 as shown in the figure. I need to find the shortest path between node 1 & node 7.
since there is no path in one graph, I have used multiple graphs. therefore the result should be like shown below.
though the below-shown path is used less number of edges compared above graph, since conversions between graphs are higher, the above path should be considered as the shortest path.
here, the most important term for a path to be shortest is the number of conversions from graph to graph.
how can I solve this problem?

The diagrams may be a bit misleading in this case. If the measure of distance for the purpose of "shortest path" is how many conversions between graphs happen on the route, then for each individual graph, we have edges of weight 0 between any pair of connected nodes (nodes that are reachable from each other within the same graph).
We then have edges of weight 1 between pairs of shared nodes between graphs.

Create a new graph in which:
A node is defined as (graphID, u) which represents a node u that is only reachable by using as last edge ones that belongs to the graph 'graphID'.
An edge between the nodes (graphID1, u1) and (graphID2, u2) has weight zero if 'graphID1' is equal to 'graphID2', and zero otherwise.
Finally, run a BFS 0/1 over your new graph considering multisources: (0, source), (1, source) and (2, source) and to get the answer you have to consider these possible targets in the new graph: (0, target), (1, target) and (2, target).
The complexity is O(V + E).
Check my code for further details: ideone.com/iDlm38

Related

Path with the minimum number of alterations in graph with colored edgest

I have a directed graph with colored edges (red & blue) that may contain cycles. The question is to write an algorithm given two vertices (s,t) that finds the path with the minimal number of color changes between s and t (if such path exists).
I have found a solution using a variation of Dijkstra (I created a new graph where each vertex correspond to an edge of the previous graph, and contains the color of the edge. For example: if (1,2) is an edge in the old graph, then (1/2) is a vertex in the new one. I connected "adjacent edges" vertices, and edges in the new graph that change color got a weight of 1, where same color transition is 0).
I am looking for a solution in linear time (of V and E). The above one uses VxE edges in the new graph.
Is there such solution to find the minimal path?
First phase: Reduction to the shortest path problem.
For every node i we create two nodes i_red and i_blue.
For every blue edge i->j we create two edges i_red->j_blue with weight 1 and i_blue->j_blue with weight 0.
We handle red edges in similar fashion.
We also need a start node which is connected with start_red and start_blue with connection weight of 0.
Similar to the target node, which is connected with target_red and target_blue with weight 0-connections.
Now, search for the shortest path from newly created start node to the newly created target node. There are twice as many nodes and twice as many edges as in the original graph, so the reduction is linear.
After you reduced the problem to the shortest path search, you could do the following:
Step: use only edges with weight 0, treat the graph as undirected one and with help of bfs you can find all components in this 0-edge-graph in linear time.
Step: run bfs on the graph where the components from the prior step are glued together as super-nodes, so all edges have weight 1 and bfs will find the shortest path.
Obviously all three parts of this algorithm (bfs in 0-edge-graph, glueing the components to super-nodes and the bfs in the resulting graph) run in linear time.

How to find shortest path in a directed graph that has edge weights either 0 or 1 in linear time?

I am looking for a way to augment the BFS method used to find the single source shortest paths in an unweighted directed graph and solve the above problem in O(N+M) time.
where N is the number of vertices, M is the number of edges
I have thought the following:
Contract the vertices of the graph that have an edge weight 0 between them. But this would definitely be wrong as then I would be changing the graph's properties and adding new edges to vertices that originally had none.
Changing the edge weights to 1 and 2. And then creating dummy vertices in the paths that are of length 2 to convert those edges to edges of weight 1. But this would give the wrong answer.
In more generality, how can I find single source shortest paths in a directed graph when the edge weights are between 0 and MAX in linear time. (MAX is the maximum edge weight)
You can use bfs with some modifications: maintain a deque instead of a queue and add a vertex to the front of the deque if 0 edge is used and to the back of the deque otherwise.(I mean 0-1 case now)
I think you can work this out with vertex contraction. Just a hint here:
First form connected components of vertices that are connected by 0-weight edges and select one representative member in every component. This will give you a contracted graph.
Then solve the unweighted problem.
The true path will be formed of "inter-edges" (weight 1) joining the representative members, and "intra-edges", joining vertices within the component, from the incoming inter-edge to the outgoing inter-edge. In other words, you need to be able to find the path from any representative to any other representative.

Traveling Saleperson Variation, Plotting a travel itinery

Im looking for the name of a problem and an algortihm for its solution.
I have a graph of connected nodes (A..Z) where every node is connected to every other node. I would like to plot the shortest path through these nodes that visits a given subset of nodes (A,D,K,W). The path may include nodes not in the subset ie A->C->W->D->K is acceptable. The cost of traveling between nodes is non negative, but not necessarily linear. Thus a path segment from A->B->C may be 'shorter' than A->C
I assume it is a variation of Traveling salesperson.
I don't know if there is a special name for this problem, but it is easily reduced to the original traveling salesman problem for the selected nodes.
Let the set of all nodes be V and the selected ones W. I would start by collapsing the nodes not in the W to get a multigraph (like a graph, but can have multiple edges between the same pair of nodes). Each edge here may be a simple one or a sequence of edges and nodes from V\W. To reduce it to a regular graph, we have only to pick the shortest of the edges available for each pair of nodes, since any other would clearly not be a part of the answer. Now we have to solve the resulting traveling salesman problem for the reduced graph and then reconstruct the corresponding path in the original graph -- we have written down the actual path in the original graph each edge in the reduced graph corresponds to.

Graph edge traversal algorithm, some edges required, some optional

I have an undirected graph with all vertices of even degree. In this graph there is a set of edges that must be covered exactly once, and a set of edges that should not be covered at all unless absolutely necessary. I need to find a set of one or more paths through the graph such that all of the required edges are covered exactly once, and the number of undesired edges traversed is minimized. No required edge can be traversed by more than one path, but the undesired edges can be traversed by any number of paths. It is not quite a Eulerian path because there are optional edges.
Each individual path's length is limited by a maximum number of required edges it can cover, although a path can cover any number of undesired edges.
The starting points and ending points need not be the same, but there is a set of possible starting points.
Some of the undesired edges are coincident with the required edges -- that is, a pair of vertices might have both a required edge and an undesired edge between them (although there will never be more than one edge of each type between a given pair of vertices).
What's a good algorithm to start with? Fundamentally, I am looking for an algorithm that can find a path that traverses required edges exactly once and avoids undesired edges when possible (but can traverse them more than once if necessary). I can build on that to do the rest.
You are looking for a subgraph within the original graph, such that it contains all the required edges, with a minimum of undesired edges such that it contains an Eulerian path.
It is known that a graph contains an Eulerian path if and only if it is CONNECTED and has all vertices ( except 2 ) of EVEN DEGREE. This can be ensured by doing the following :
Start by directly including all the desired edges in the final subgraph. You now have a graph with one or more connected components.
CASE 1 :
If the number of components in this subgraph is one, (i.e, all the edges are connected to each other ), we only have to ensure that all vertices ( except 2 ) have even degree. Since your original graph does have all vertices of even degree, it is possible to do this, but we also want to take care that the number of edges we add to achieve this is minimum ( since only undesired edges are left to add ).
One good heuristic way to do this, start from each of the odd degree vertices, and do a BFS ( breadth first search ) till you reach another vertex with odd degree. Do this for all the odd degree vertices, choose the 2 vertices which take the maximum undesired path between them to achieve this, and delete this path. Now the graph will have an Eulerian Path
( One thing to note is that such a pairing of odd vertices is always possible, since no graph can have an odd number of vertices of odd degree )
CASE 2 :
The subgraph consisting of only desired edges has more than one component. To ensure connectivity, do the following - Take the component with the minimum number of vertices. Do a BFS from each of the vertices, and choose the minimum length path that connects this component to ANY OTHER component.
Repeat this procedure till all components merge to form a single component. Now for even-ness follow the procedure outlined before for CASE 1.
I think this can be reduced into the Travelling Salesman problem. Create a transformed graph where the nodes represent compulsory edges and the edge weights represent the number of optional edges that must be traversed to get from one compulsory edge to the other.
Now the problem is to find the shortest path in this transformed graph which goes through all the nodes (aka compulsory edges). This is the TSP, which is NP-hard.
There will be some complications because the paths you can take after a compulsory edge depend on the direction in which you took it. You could solve this by turning each compulsory edge into two nodes, one for each direction. The TSP would then have to visit exactly one node from every pair.
e.g.
A===C
| /
| / (edges A<->B and B<->C are compulsory, A<=>C is optional)
|/
B
Transformed graph:
Nodes = { AB, BA, BC, CB }
Edges = { AB -> BC (cost 0), BA -> CB (cost 1), CB -> BA (cost 0), BC -> AB (cost 1) }
This is NP-hard: an instance of the Hamiltonian path problem can be transformed to an instance of your problem. Therefore, if you know how to solve your problem, you know how to solve the Hamiltonian path problem.
To transform, take a directed graph and double each vertex, say, into a red and a blue vertex. For each former vertex, make all inbound edges go to the red vertex, and all outbound edges go out of the blue vertex. Make a single new edge from the red to the blue vertex. Obviously if you can solve the Hamiltonian cycle problem for this graph, you can do so for the original graph.
Now label all new edges compulsory, all the old edges as optional. Mark a single red vertex as a possible entry point. If there's a solution to your problem, then it consists of a single path, which a Hamiltonian path for the original graph.

Single shortest path of an acyclic undirected disconnected graph

Is there a graph algorithm that given a start(v) and an end(u) will find a shortest path through the given set of edges, but if u is a disconnected vertex, it will also determine the shortest path to add missing edges until u is no longer disconnected?
I have a pixel matrix where lines are made of 255's(black) and 0's(white). lines(255) can have breaks or spurs and I must get rid of both. I could have a pixel matrix forest with say 7 or so trees of black pixels. I need to find the true end points of each tree, find the single shortest path of each tree, then union all skew trees together to form 1 single line(ie, a single shortest path from the furthest 2 end points in the original matrix). all edge weights could be considered 1.
Thanks
How about running Dijkstra's algorithm and if disconnected, connect v and u? What's your criteria for "best place to add a missing edge?" Do edges have weights (like distance)?
Edit:
For one idea of "the best place," you could try the path that has minimal sum of shortest paths between all connected pairs. Floyd–Warshall algorithm can be used to find shortest paths between all pairs. So, run Floyd-Warshall for each node in v's tree and u.
Your problem isn't well defined for disconnected graphs. I can always add and edge between v and u.
If you meant that given an acyclic undirected disconnected graph, actually known as a forest, and given a subset of edges as a subgraph, can you find the shortest path between vertices, than this problem is trivial since if there is a path in the full graph, there is one path only.
If this is a general graph G, and you are talking about a forest subgraph G', than we need more info. Is this weighted? Is it only positive weights? If it is unweighted, do some variant of Dijksta. Define the diameter of a tree to be the length of the longest path between two leaves (this is well defined in a tree, since ther is only one such path). Let S be the sum of the diameters of all the trees in G', then set the weight all edges out of G' to 2S, then Dijkstra's algorithm will automatically prefer to step through G', stepping outside G' only when there is no choice, since walking through G' would always be cheaper.
Here you have following scenarios:
case:1. (all edges>0)
Negate all edges
find the longest path in graph using this:
https://www.geeksforgeeks.org/longest-path-undirected-tree/
negate the final result
case2: edges might be or might not be negative
Read Floyd–Warshall algorithm for this

Resources