I stuck in one challenging question, I read on my notes.
an Undirected, Weighted and Connected Graph G, (without negative weight and all weights are distinct) is given, we know in this graph the shortest path between any two vertexes is on Minimum Spanning Tree (MST). (for any pair of vertices and for any shortest path between them, it lies on MST). Which of The following Is True?
1) Graph G is a Tree.
2) weight of each {u,v} edge, at least is equal (same) to heaviest edge in shortest path from u to v.
3) shortest path between any two vertex u, v is unique.
4) suppose start from vertex s, Prime (for calculating MST) and Dijkstra (for calculating shortest path), process and add the
vertexes into their Trees, with the same order. (two algorithm works with same order in processing and adding node)
How can verify me these options? This is a challenging question.
No. For example: V = {1, 2, 3}, E = {(1, 2, 1), (2, 3, 2), (1, 3, 4)}(each edge is encoded as a tuple (one vertex, another vertex, weight)). It is not a tree, but all shortest path are on the minimum spanning tree.
Yes. If the weight of this edge is less than the weight of the heaviest edge which is in the shortest path, this edge is shorter than the shortest path(because there are no edges with negative weight). Thus, the shortest path is not the shortest. It is a contradiction.
No*. Let's assume that we have a graph with two vertices {1, 2} and one edge between them with zero weight. There are infinitely many shortest paths between the first and the second vertex([1, 2], [1, 2, 1, 2], ...)
*However, there is a unique simple shortest path between any two vertices because there is only one simple path between any two vertices in a tree, any path which does not fully lie in a minimum spanning tree is longer due to the problem statement and there is only one minimum spanning tree in a graph with distinct edges weights.
No. Consider this tree: V = {1, 2, 3, 4}, E = {(1, 2, 3), (2, 3, 2), (1, 4, 4)}. Let's assume that the start vertex is 1. Prim's algorithm will take the first vertex, than the second one, than the third one and only after this the fourth. But Dijkstra's algorithm will take the fourth vertex before the third one. It happens because the third vertex is located closer to the tree after processing the first two vertices, but it the total distance to it from the start node is larger.
I do not want to give the complete answer, but here is how you approach it:
Can you add edge[s] to the tree such that it is not a tree anymore and still the tree contains all the shortest paths?
What happens if there is an edge that is shorter than the heaviest edge?
it is confusing because the problem says "the shortest path between any two vertexes is on MST", but doesn't address the fact that there might be multiple shortest paths. So you can probably assume that "at least one shortest path lies on the tree". In which case just connect two vertices with an edge whose weight equals to cost through the MST, and you get the answer.
Again, you should start with what happens if the vertices are not added in the same order
Related
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
Suppose that we are given a directed graph H = (V, E). For each edge e, the weight of the edge, w(e) is either 2, 3 or 5. Modify the BFS so that it will compute the length of the shortest path from a single source vertex s. Explain why your algorithm is correct and determine its worst-case running time (You may assume that H is represented via an adjacency list).
How would you go about this? What makes the specific weight edges different from just any?
You can consider imaginary nodes between the edges. So if between 2 nodes there is an edge of length 2. You make an intermediary node and add edges of length 1 between them. Then use the normal breadth first search. (You also need to do this for nodes of length 3 and 5, adding 2 and 4 nodes). Since you only add a O(E) nodes it's the same complexity.
I am reading the application of BFS algorithm. One of the application which i read is to check weather a give graph is bipartite graph or not.Now I want to know, is there any algorithm to convert a graph into bipartite set/graph.
For example we have give a set of edges as
E={ (4, 1),( 1 ,2), (2 ,3),( 7, 2),( 1 ,5),( 8 ,4), (5 ,8),( 8, 9)}
and set of vertices
V= { 1,2,3,4,5,6,7,8}
we have to create bipartite set/graph.
Expected output as :-
s1={1 ,4, 8, 1 ,7 ,3}
s2={2, 5, 6, 9}
The general BFS algorithm to check if a graph is bipartite is:
Start from a vertex v_0, and label it s1.
Mark all v_0's neighbours as s2, and queue them.
Mark all the neighbour's neigbours as s1, and queue them
If, at any point, you get to mark as s2 a vertex already marked as s1 or vice versa, the graph is not bipartite. If, instead, you end up with an empty queue, you are done, and you have your partition.
EDIT
If what you want to know, instead, is how you can build a bipartite graph from a general one, you should first add a metric to compare two different solutions : otherwise, removing all the edges and assigning randomly the vertices to two groups, will always generate a ( trivial ) bipartite graph from any given one.
The edge bipartization problem, that is, deleting the minimum number of edges to make a graph bipartite, is NP-hard. Here are slides of a talk about how to solve it as efficiently as possible.
What's minimum number of minimum spanning trees(MST) of complete graph with N vertex?
I believe that the answer is 1.
It is possible to construct a complete graph with n nodes that has exactly one MST. To do this, construct a graph with n nodes labeled 1, 2, 3, ..., n. Then, add an edge of cost 0 from 1 to 2, from 2 to 3, from 3 to 4, ..., from n - 1 to n, and add edges connecting every other pair of nodes that has cost 1. Clearly, picking all the zero-cost edges gives one possible spanning tree of this graph, and it's the minimum spanning tree because if any other choice of edges were picked, the cost would be at least 1. Moreover, this is the only MST in the graph that has cost 0, since if another set of edges were picked, that set would have to include at least one edge of cost at least 1, so the total MST would have cost at least 1.
Hope this helps!
Here is an exercise:
Either prove the following or give a counterexample:
(a) Is the path between a pair of vertices in a minimum spanning tree
of an undirected graph necessarily the shortest (minimum weight) path?
(b) Suppose that the minimum spanning tree of the graph is unique. Is
the path between a pair of vertices in a minimum spanning tree of an
undirected graph necessarily the shortest (minimum weight) path?
My Answer is
(a)
No, for example, for graph 0, 1, 2, 0-1 is 4, 1-2 is 2, 2-0 is 5, then 0-2’s true shortest path is 5, but the mst is 0-1-2, in mst, 0-2 is 6
(b)
My problem comes into this (b).
I don't understand how whether the MST is unique can affect the shortest path.
First, my understanding is that when the weights of edges are not distinct, multiple MST may exist at the same time, right?
Second, even if MST is unique, the answer of (a) above still applies for (b), right?
So lets take a look at a very simple graph:
(A)---2----(B)----2---(C)
\ /
---------3----------
The minimum spanning tree for this graph consists of the two edges A-B and B-C. No other set of edges form a minimum spanning tree.
But of course, the shortest path from A to C is A-C, which does not exist in the MST.
EDIT
So to answer part (b) the answer is no, because there is a shorter path that exists that is not in the MST.
Regarding (a), I agree.
Regarding (b), for some graphs, there may be more minimal spanning trees with the same weight. Consider a circle C3 with vertices a,b,c; weights a->b=1, b->c=2, a->c=2. This graph has two MSTs, {a-b-c} and {c-a-b}.
Nevertheless, your counterexample still holds, because the MST is unique there.
AD a)
a very simple visualization would be:
MSP in a graph:
Shortest Path between A and C:
AD b)
I guess that the uniqueness of MSP means that there is only 1 MSP MSP in a graph. So:
First) Yes, if the weights of edges are not distinct, multiple MST may exist at the same time. In case of our graph, another possible MSP would include arc D-C instead of A-B (as an example).
Second) The uniqueness of MST does not influence the answer for a). As an example:
Isn't the MST related to the start node?!
Then he is trying to get the shortest path from the MST start node. Therefore, yes, the shortest path is given by the MST starting from A!