Finding corresponding graph from given shortest paths - algorithm

Given n by n points and the distance d between these points, I need to find the corresponding undirected weighted graph that would result in these distances. I tried using Prim's algorithm to find the MST, however this set is of size n-1 and does not include the n needed edges. E.g. given n by n distances
0 3 5
3 0 4
5 4 0
I need to find the corresponding edges:
1 - 2 = 3
1 - 3 = 5
2 - 3 = 4
Which results in the graph:
3
1 --------- 2
\ /
\5 /4
\ /
\ /
3
However Prim's would return only the first 2 edges since a MST doesn't contain any cycles.

One graph that would result in these distances is the graph that has an edge from every node to every other node and the length of that edge is the distance according to the matrix. (I'm not sure what you mean by unweighted directed because the example you give appears to be undirected and I'm not sure what the difference is between weights and lengths here).
Another option would be to consider the distances in increasing order, as you have done with Prim's algorithm, and, as well as checking to see if the edge is required to connect its two ends, check to see if the minimum distance between those ends in the graph reconstructed so far is the same as the distance in the matrix. If it is not, add the edge even if the ends are connected in the graph so far.

Related

Find Strongly Connected Graph such that the difference between the maximum and minimum edges is minimum [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Given a Directed Weighted Graph which is Strongly Connected. I need to find a strongly connected sub-graph out of this graph such that the difference between the maximum and minimum weight edges is minimum.
To be more clearly, I need to get rid of edges in such a way that after removing them, the graph will still be strongly connected and the difference between the maximum and minimum weight edges is minimum.
Here is an example:
The first line is the number of N nodes and M edges of this graph. The next M lines represent the edges of this graph.
3 6
1 2 8
2 3 32
3 1 16
1 3 81
3 2 243
2 1 27
The chosen N-nodes sub-graph would contain the edges:
1 2 8
2 3 32
3 1 16
The difference between the maximum and minimum weight edges is: 32 - 8 = 24.
Which is minimum among all choices.
I'm looking for an optimal solution. There are at most 3000 nodes and 5000 edges.
Given an algorithm that tests whether a given digraph G = (V, E) is strongly connected in O(f(|V|, |E|)) time, this problem can be solved in time O(|E|*f(|V|, |E|)) -- and better than that if testing for strong connectivity can be done more quickly after adding or removing a single edge to an already-tested digraph.
Sort edges in increasing order by weight, and number them in this order. Initially, add the first (lowest-weight) edge to the set E' of selected edges; for as long E' is not strongly connected, add the next edge to it. If this loop does not terminate then G is not strongly connected. Otherwise, when it stops, after adding, say, edge j, we have found a minimum-difference solution given that we include edge 1. Record this (1, j) solution as the incumbent.
Now remove edge 1 from E', so that edge 2 is the lowest-weight edge remaining in E'. Leave all other already-decided edges in place, and begin adding the next-lowest-weight edges again, starting at edge j+1, until an SCG forms. This can be repeated to compute the minimum-difference solution given that the lowest-weight edge included is edge i, for each i <= |V|. Keep the best overall.
Note that when solving for the starting point i+1, it isn't necessary to get rid of the edges decided for the previous starting point i: If edges i, i+1, ..., j-1 do not form an SCG, then edges i+1, i+2, ..., j-1 do not form an SCG either. Exploiting this means the overall outer loop runs just |E| times, instead of O(|E|^2) times.

Computing level of nodes in a graph

I want to compute the level of each node in a directed graph. I'm currently applying a depth-first search algorithm on vertices that have no incoming edges. Considering the graph below, for instance:
The expected result is:
Vertex | Level
1 | 0
2 | 1
3 | 2
4 | 1
5 | 3
6 | 4
In this particular case, if we start by applying DFS on 4, then all results for vertices 4, 3, 5 and 6 are going to be wrong, since 1 has level 0. I've tried to always consider the greatest result for each one of the nodes, so in this case the results for 3, 5, and 6 are replaced when applying DFS on 1. It works, but I can't find a way to correctly compute the level of vertex 4.
I'm working only with directed acyclic graphs.
I'm not including any code here because it is a pretty straightforward DFS implementation and I'm not struggling implementation-wise.
Any hint would be much appreciated.
You can compute the levels starting from each vertex without having an incoming edge. Then you can store the maximum value for each vertex until the end. For eg :- Vertex 3 will have values 1 and 2 when traversed from starting points vertex 1 and vertex 4 respectively. At last, you can update the vertices not having the incoming edge(number on child -1). If there's a situation where there multiple children of such a vertex, then you might want to select the child with maximum number on it for replacement and then run the algorithm from that vertex again to see if changes the numbers assigned to any of the other children.

Minimum Hop Count in Directed Graph based on Conditional Statement

A directed graph G is given with Vertices V and Edges E, representing train stations and unidirectional train routes respectively.
Trains of different train numbers move in between pairs of Vertices in a single direction.
Vertices of G are connected with one another through trains with allotted train numbers.
A hop is defined when a passenger needs to shift trains while moving through the graph. The passenger needs to shift trains only if the train-number changes.
Given two Vertices V1 and V2, how would one go about calculating the minimum number of hops needed to reach V2 starting from V1?
In the above example, the minimum number of hops between Vertices 0 and 3 is 1.
There are two paths from 0 to 3, these are
0 -> 1 -> 2 -> 7-> 3
Hop Count 4
Hop Count is 4 as the passenger has to shift from Train A to B then C and B again.
and
0 -> 5 -> 6 -> 8 -> 7 -> 3
Hop Count 1
Hop Count is 1 as the passenger needs only one train route, B to get from Vertices 0 to 3
Thus the minimum hop count is 1.
Input Examples
Input Graph Creation
Input To be solved
Output Example
Output - Solved with Hop Counts
0 in the Hop Count column implies that the destination can't be reached
Assuming number of different trainIDs is relatively small (like 4 in your example), then I suggest using layered graph approach.
Let number of vertices be N, number of edges M, and number of different trainIDs K.
Let's divide our graph to K graphs. (graphA, graphB, ...)
graphA contains only edges labeled with A, and so on.
Weight of each edge in each of the graphs is 0.
Now create edges between these graphs.
Edge between graphs is representing a 'hop'
grapha[i] connects to graphB[i], graphC[i], ...
Each of these edges has weight 1.
Now for every graph run Dijkstra's shortest path algorithm from V1 in that graph, and read results from V2 in all graphs, take minimal value.
This way minimum of results for running dijkstra's for every graph will be your minimum number of hops.
Memory complexity is O(K*(N+M))
And time complexity is O(K*(((2^K)*N+M)*log(KV)))
(2^K)*N comes from fact that for every 1<=i<=N, vertices graphA[i],graphB[i],... must be connected to each other, and this gives 2^K connections for every i, and (2^K)*N connections in total.
For cases where K is relatively small, like 4 in your example, but N and M are quite big, this algorithm works like a charm. It isn't suitable for situation where K is big though.
I'm not sure if that's clear. Tell me if you need more detailed explanation.
EDIT:
Hope this makes my algorithm more clear.
Black edges have weight 0, and red edges have weight 1.
By using layered graph approach, we translated our special graph into plain weighted graph, so we can just run Dijkstra's algorithm on it.
Sorry for ugly image.
EDIT:
Since max K = 10, we would like to remove 2^K from our time complexity. I believe this can be done by making edges that represent possible hops virtual, instead of physically storing them on adjacency list.

Give an efficient greedy algorithm that finds an optimal vertex cover for a tree in linear time

I'm trying to work on this problem...
Below mentioned is one algorithm..i figured out..
Input a graph
select a vertex with highest degree of matching with all the other nodes.
Remove the edges that are incident on this node.
Add the selected vertex and its edge to a set X.
Return X
Where X returns the minimum set of vertices that are required for a vertex cover.Is this way correct...?
Thanks
To select a vertex with highest degree can't guarantee to give the best solution. For example,
you have a tree with 7 vertices, edges are listed as follows:
1 2 // (1,2) is connected.
1 3
1 4
2 5
3 6
4 7
The minimum vertex cover is {2,3,4}, however, based on you greedy approach, you will choose {1} first, then you will choose at least 3 vertices to covered the left 3 edges.
Indeed, there is a greedy algorithm to solve the vertex cover problem for a tree, that is you find a leaf at each step (since the input is a tree, you can always find such leaf unless there is no edge left), then select the neighbor of the leaf to the vertex cover set X. Return X as the minimum vertex cover when the graph is empty. The complexity is O(E) when E = V-1 so that we can say it is a linear solution.

DFS and BFS Outputs?

So I'm confused with the outputs of both of the BFS and DFS algorithms.
As much as I understood BFS takes as input a graph suppose G and a vertex suppose x.
Output : returns a graph, which in for every vertex in G, the new graph
has the shortest way from vertex x to any other vertex in the graph.
is that right? if not, what is?
and how about DFS ? DFS's input is only a graph, does it mean DFS doesn't care where you start from? and what's DFS's Output?
Thanks
I'm not completely sure what it is that you wanted, but I'll give it a shot.
Let's say we have the following graph:
X - 1 - 2 - 3
| \
1 1
| \
2 2
| \
3 3
In this graph, X marks the node where we will start traversing from, and a number denotes a value that a particular node holds. This time X has 3 immediate neighboring nodes, that all hold a value 1.
For the sake of the example, let us assume that any node cannot be traversed twice. Let us also assume that the program always prints the value of the node it's standing on.
Without really getting in depth with the way BFS and DFS work (at all), the output would be like this:
BFS: X 1 1 1 2 2 2 3 3 3
DFS: X 1 2 3 1 2 3 1 2 3
Hope this answers your question.
DFS is a Graph traversal technique which takes the graph and a starting vertex(random) as input and gives a sequence of vertices as output.
The sequence contains those vertices which are reachable from the starting vertex.
i.e we are finding whether any vertex is reachable from any other vertex in a graph or not?

Resources