Let G(V,E) be a directed connected graph with no negative cycles in it. All the edges have non-negative weight, except ONE edge. Find a simple shortest path from s,t in V.
My idea -
Do a BFS on the graph, find the edge with the negative weight.
Add this negative weight to all the edges, so we eliminate the negative weight.
Do Dijkstra algorithm.
My idea doesn't work.
Can you please help me find out why?
Thank you.
The reason that your approach doesn't work is that it unfairly penalizes paths with more edges.
Imagine two paths from a source node to a destination, one with more edges, but lower weight, and another with a fewer edges with higher weight. Let's assume that the weight added to each edge is 3.
Original paths:
S -> 1 -> 1 -> 1 -> 1 -> 1 -> T wt = 5
S -> 4 -> 3 -> T wt = 7
Paths after adding weight:
S -> 4 -> 4 -> 4 -> 4 -> 4 -> T wt = 20
S -> 7 -> 6 -> T wt = 13
As you can see, the second path is now incorrectly identified as the shorter one.
The problem with your approach is that it may create more negative edges if the one negative edge has a large negative value.
You can look into Bellman-Ford shortest path algorithm to solve this problem:
1) The first step is to initialize distances from source to all vertices as infinite and distance to source itself as 0. Create an array dist[] of size |V| with all values as infinite except dist[src] where src is source vertex.
2) This step calculates shortest distances. Do following |V|-1 times where |V| is the number of vertices in given graph.
…..a) Do following for each edge u-v
………………If dist[v] > dist[u] + weight of edge uv, then update dist[v]
………………….dist[v] = dist[u] + weight of edge uv
3) This step reports if there is a negative weight cycle in graph. Do following for each edge u-v
……If dist[v] > dist[u] + weight of edge uv, then “Graph contains negative weight cycle”
The idea of step 3 is, step 2 guarantees shortest distances if graph doesn’t contain negative weight cycle. If we iterate through all edges one more time and get a shorter path for any vertex, then there is a negative weight cycle
Related
Suppose that the we have a bidirectional graph with V amount of vertices and E amount of edges, where all of the edges' weight are 1. Now, every vertices bar the source has its own level and suppose that we want to reach level L. Suppose that we start from level 1. When our level is i, we can only pass through the vertices that are either level 0 or level i. After passing a vertex that has level i, we level up by 1.
Now, given a starting point and a target level, how do we compute the minimum steps (costs) needed to reach the target level?
I believe that this problem can be solved with modified BFS algorithm. As of now, I can determine whether such path(s) exist(s) or not but I haven't been successful on computing the costs.
Any help and suggestions would be greatly appreciated.
For example, let's have a graph with V = 5, E = 4, L = 3, and starting point at vertex 0.
The following lines are the level of each vertex:
0 NO-LEVEL
1 0
2 0
3 2
4 1
and these following E lines represent the edges:
0 1
1 2
2 3
0 4
With this input, the output should be: 5 (0->4->0->1->2->3).
However, my code's output is 3 (0->1->2->3).
I am still confused on how to include the 0->4->0 process when counting the distance.
I'd suggest making L copies of every vertex. Vertex (v,i) means vertex v "at" level i. Then set the edge costs equal to infinity if we cannot go from (v,i) to (w,j). For example, if vertex v has level 3 and vertex w has level 2, then the cost from (v,3) to (w,2) is infinity.
Then solve a standard shortest path problem on the resulting network.
Show that if the edge set of a graph G(V,E) with n nodes
can be partitioned into 2 trees,
then there is at least one vertex of degree less than 4 in G.
...................................................................................
I have tried to prove this problem with the help of the method of contradiction.
Assume that all vertices of the graph G has degree >= 4.
Assume the graph G is partitioned into two trees T1 and T2.
With the help of the above assumptions the only observation I could make is that for every vertex v in G
degree of v must be greater than or equal to 2 in either T1 or T2.
I don't know how proceed with this. Please help.
If my approach for solving this problem is wrong then please provide a different solution.
You started with a good approach. Lets assume all vertices in G has degree of 4 (or above) and sssume the graph G is partitioned into two trees T1 and T2.
We know that number of edge in tree is n-1 (when n is number of vertices). Therefor in each of T1 and T2 we have n-1 edges (consider n to be |V|) -> combine we have 2n-2 edges in G -> |E| = 2n-2
From the other hand we know that each v in G -> d(v) > 4 . And we know that sum of degree in graph equal to 2|E|. therefor, 2*|E| >= 4*n (I took the minimum degree for each vertex and each edge contribute 2 to the sum of the degree). So we got |E| >= 2*n.
Contradiction -> There is have to be one vertex with degree less then 4
I am aware that the problem has been discussed here more than once. However, I need to find the maximum number K of vertex-disjoint paths in a directed graph with a running time of |V| x |E|.
I know the algorithm of transforming each vertex into v_in, v_out and adding an edge with capacity 1 from v_in to v_out and for each pair of vertices (u,v) add an edge with capacity 1 from u_out to v_in and then compute the max flow in this network. However, after my calculations this algorithm takes O(E) preprocessing + O(VE^2) or O(V^2E) for max flow. Am I doing something wrong?
Use James B Orlin's + KRT (King, Rao, Tarjan)'s algorithm instead of Ford Fulkerson to calculate flow and it'll be O(V*E). (See https://en.wikipedia.org/wiki/Maximum_flow_problem)
Okay, I know that a directed acyclic graph (DAG) has E=V-1 edges. E = number of edges. V = number of vertices.
So the question is, "In a directed graph G, the number of edges is always less than the number of vertices." True or false?
Thanks for the help.
Assume N vertices/nodes, and let's explore building up a DAG with maximum edges. Consider any given node, say N1. The maximum # of nodes it can point to, or edges, at this early stage is N-1. Let's choose a second node N2: it can point to all nodes except itself and N1 - that's N-2 additional edges. Continue for remaining nodes, each can point to one less edge than the node before. The last node can point to 0 other nodes.
Sum of all edges: (N-1) + (N-2) + .. + 1 + 0 == (N-1)(N)/2
Given an undirected graph in which each node has a Cartesian coordinate in space that has the general shape of a tree, is there an algorithm to convert the graph into a tree, and find the appropriate root node?
Note that our definition of a "tree" requires that branches do not diverge from parent nodes at acute angles.
See the example graphs below. How do we find the red node?
here is a suggestion on how to solve your problem.
prerequisites
notation:
g graph, g.v graph vertices
v,w,z: individual vertices
e: individual edge
n: number of vertices
any combination of an undirected tree g and a given node g.v uniquely determines a directed tree with root g.v (provable by induction)
idea
complement the edges of g by orientations in the directed tree implied by g and the yet-to-be-found root node by local computations at the nodes of g.
these orientations will represent child-parent-relationsships between nodes (v -> w: v child, w parent).
the completely marked tree will contain a sole node with outdegree 0, which is the desired root node. you might end up with 0 or more than one root node.
algorithm
assumes standard representation of the graph/tree structure (eg adjacency list)
all vertices in g.v are marked initially as not visited, not finished.
visit all vertices in arbitrary sequence. skip nodes marked as 'finished'.
let v be the currently visited vertex.
2.1 sweep through all edges linking v clockwise starting with a randomly chosen e_0 in the order of the edges' angle with e_0.
2.2. orient adjacent edges e_1=(v,w_1), e_2(v,w_2), that enclose an acute angle.
adjacent: wrt being ordered according to the angle they enclose with e_0.
[ note: the existence of such a pair is not guaranteed, see 2nd comment and last remark. if no angle is acute, proceed at 2. with next node. ]
2.2.1 the orientations of edges e_1, e_2 are known:
w_1 -> v -> w_2: impossible, as a grandparent-child-segment would enclose an acute angle
w_1 <- v <- w_2: impossible, same reason
w_1 <- v -> w_2: impossible, there are no nodes with outdegree >1 in a tree
w_1 -> v <- w_2:
only possible pair of orientations. e_1, e_2 might have been oriented before. if the previous orientation violates the current assignment, the problem instance has no solution.
2.2.2 this assignment implies a tree structure on the subgraphs induced by all vertices reachable from w_1 (w_2) on a path not comprising e_1 (e_2`). mark all vertices in both induced subtrees as finished
[ note: the subtree structure might violate the angle constraints. in this case the problem has no solution. ]
2.3 mark v visited. after completing steps 2.2 at vertex v, check the number nc of edges connecting that have not yet been assigned an orientation.
nc = 0: this is the root you've been searching for - but you must check whether the solution is compatible with your constraints.
nc = 1: let this edge be (v,z).
the orientation of this edge is v->z as you are in a tree. mark v as finished.
2.3.1 check z whether it is marked finished.
if it is not, check the number nc2 of unoriented edges connecting z.
nc2 = 1: repeat step 2.3 by taking z for v.
if you have not yet found a root node, your problem instance is ambiguous:
orient the remaining unoriented edges at will.
remarks
termination:
each node is visited at max 4 times:
once per step 2
at max twice per step 2.2.2
at max once per step 2.3
correctness:
all edges enclosing an acute angle are oriented per step 2.2.1
complexity (time):
visiting every node: O(n);
the clockwise sweep through all edges connecting a given vertex requires these edges to be sorted.
thus you need O( sum_i=1..m ( k_i * lg k_i ) ) at m <= n vertices under the constraint sum_i=1..m k_i = n.
in total this requires O ( n * lg n), as sum_i=1..m ( k_i * lg k_i ) <= n * lg n given sum_i=1..m k_i = n for any m <= n (provable by applying lagrange optimization).
[ note: if your trees have a degree bounded by a constant, you theoretically sort in constant time at each node affected; grand total in this case: O(n) ]
subtree marking:
each node in the graph is visited at max 2 times by this procedure if implemented as a dfs. thus a grand total of O(n) for the invocation of this subroutine.
in total: O(n * lg n)
complexity (space):
O(n) for sorting (with vertex-degree not constant-bound).
problem is probably ill-defined:
multiple solutions: e.g. steiner tree
no solution: e.g. graph shaped like a double-tipped arrow (<->)
A simple solution would be to define a 2d rectangle around the red node or the center of your node and compute each node with a moore curve. A moore curve is a space-filling curve, more over a special version of a hilbert curve where the start and end vertex is the same and the coordinate is in the middle of the 2d rectangle. In generell your problem looks like a discrete addressing space problem.