I'm trying to understand the main concepts of graph theory and the algorithms within it. Most algorithms seem to contain a "Relaxation Condition" I'm unsure about what this is.
Could some one explain it to me please.
An example of this is dijkstras algorithm, here is the pseudo-code.
1 function Dijkstra(Graph, source):
2 for each vertex v in Graph: // Initializations
3 dist[v] := infinity // Unknown distance function from source to v
4 previous[v] := undefined // Previous node in optimal path from source
5 dist[source] := 0 // Distance from source to source
6 Q := the set of all nodes in Graph
// All nodes in the graph are unoptimized - thus are in Q
7 while Q is not empty: // The main loop
8 u := vertex in Q with smallest dist[]
9 if dist[u] = infinity:
10 break // all remaining vertices are inaccessible from source
11 remove u from Q
12 for each neighbor v of u: // where v has not yet been removed from Q.
13 alt := dist[u] + dist_between(u, v)
14 if alt < dist[v]: // Relax (u,v,a)
15 dist[v] := alt
16 previous[v] := u
17 return dist[]
Thanks
Relaxation step:
You have two nodes, u and v
For every node, you have a tentative distance from the source node (for all nodes except for the source, it starts at positive infinity and it only decreases up to reaching its minimum).
The relaxation step basically is asking this:
I already know that I can reach v with some path of distance dist[v]. Could I improve on this by going to v through u instead? (where the distance of the latter would be dist[u] + weight(u, v))
Graphically:
s ~~~~~~~> v
\ ^
\ |
\~~~~~> u
You know some path s~>v which has distance dist[v], and you know some path s~>u which has distance dist[u]. If dist[u] + weight(u, v) < dist[v], then the path s~>u->v is shorter than s~>v, so you'd better use that one!
(I write a~>b to mean a path of any length from a to b, while a->b I mean a direct single edge from a to b).
You may also want to check this lecture: http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-046JFall-2005/VideoLectures/detail/embed17.htm
One of the meanings of the english word "relaxation" is decreasing something. Because at lines 14,15 and 16 you are essentially checking if you can decrease(optimize) the currently computed distance, I guess that's why it is called "relaxation condition".
One way to remember is:
. relax the muscle to reduce tension or stress on it
So we say:
. relax outgoing edges of closest vertex in Dijkstra's algorithm
. relax edges repeatedly in Bellman-Ford algorithm
Both implying to try to reduce distance from start node to reach vertex which is on the other end of edge
Related
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)
The graph is positive weighted and might be acyclic or not.
input file consists of
vertex number, edge number, begining vertex, ending vertex
edge1(from, to, weight)
edge2(from, to, weight)
and so on.
the length of the path will be infinite if there is cycle in the graph and will be 0 if there is no way
the way I do is that I remove the same edges with less lengths and use bellman ford or dijkstra's algorithm in adjecent list or matrix and both work fine.
however, program should find the path at most 2 seconds and some input files contain 10000 vertices and 100000 edges
what should I do?
The time limit is 2 sec, it means the program should have near about ~10^6 iterations. See the limits of V = 10000 and e = 100000. This means an algorithm of O(V) or O(E) or O(V + E) or even O(E + VlogV)will easily compute your requirement well in given time.
Note E + Vlogv ~ (100000 + ~132877) which is less than 10^6
// This 10^6 limit is quit good for processors with 10^9 Hz frequency. So, even if your algo has 1000 instructions for each iteration, you will be in safe zone.
So, here is the proposed algorithm:
You will build the graph in O(E). Use Adjacency list data structure to represent the graph.
-> While building this data structure, store indegree of each vertex and also store count of vertices.
countV := V;
foreach edge_i
inDegree[to] := inDegree[to] + 1;
Check if there is cycle in the graph in O(V + E).
if no vertex with indegree = 0 and countV = 0
graph has no cycle
else if no vertex with indegree = 0 and countV != 0
graph has cycle
else select any vertex having 0 indegree
countV := countV - 1;
decrease all its directed neighbours' inDegree by 1.
So if you get a cycle, your answer is directly infinite.
Make a BFS or DFS to get whether the ending vertex is reachable from beginning vertex or not. This can be done in O(V + E) or even O(E). Let us take O(V + E). If not reacable your answer is directly 0.
Now, apply dijkstra but in relax condition, just check the opposite. i.e in the pseudocode given here, instead of doing
if alt < dist[v]:
dist[v] := alt;
do
if alt > dist[v]:
dist[v] := alt;
This can be done in O(E + VlogV). Hence overall complexity of the solution will be O(E + VlogV) which is well in constraints.
I made a slight modification to Bellman-Ford so that it only does "useful" relaxes. That is, relaxations that meant d(v) was updated.
define Relax(u, v):
if d(v) > d(u) + w(u,v) //w(u,v) = weight of edge u->v
d(v) = d(u) + w(u,v)
INIT // our usual initialization.
Queue Q
Q ← s // Q holds vertices whose d(v) values have been updated recently.
While (Q not empty)
{
u ← Frontof(Q);
for each neighbor v of u
{
Relax(u, v)
if d(v) was updated by Relax and v not in the Q //Here's where we're a bit smarter
ADD v to End of Q. //since we add to Q if
//the relaxation changed d(v)
}
}
Now, if all shortest paths have at most k arcs. Then the worst-case runtime is O(V*k) since we only go through k arcs in this smart version. This is a bit faster than the original O(V*E) since |k| < |E|
Can anyone please tell me of a type of graph for which this improved version is no better than the original Bellman-Ford algorithm? That is, for which the best-case performance is O(V*E)
Consider the graph where all edges have negative weight. In this graph, vertex u will be added to Q multiple times if it has multiple incomming edges.
The statement |k| < |E| is incorect: if there is a negative loop in graph, then k is infinite
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.
A friend presented me with a conjecture that seems to be true but neither of us can come up with a proof. Here's the problem:
Given a connected, bipartite graph with disjoint non-empty vertex sets U and V, such that |U|<|V|, all vertices are in either U or V, and there are no edges connecting two vertices within the same set, then there exists at least one edge which connects vertices a∈U and b∈V such that degree(a)>degree(b)
It's trivial to prove that there is at least one vertex in U with degree higher than one in V, but to prove that a pair exists with an edge connecting them is stumping us.
For any edge e=(a,b) with a∈U and b∈V, let w(e)=1/deg(b)-1/deg(a). For any vertex x, the sum of 1/deg(x) over all edges incident with x equals 1, because there are deg(x) such edges. Hence, the sum of w(e) over all edges e equals |V|-|U|. Since |V|-|U|>0, w(e)>0 for som edge e=(a,b), which means that deg(a)>deg(b).
Prove it by contradiction, i.e. suppose that deg(a) ≤ deg(b) ∀(a,b)∈E, where E is the edgeset of the graph (with the convention that the first element is in U and the second in V).
For F⊆E, designate by V(F) the subset of V which is reachable through edgeset F, that is:
V(F) = { b | (a,b)∈F }
Now build an edgeset F as follows:
F = empty set
For a ∈ U:
add any edge (a,b)∈E to F
Keep adding arbitrary edges (a,b)∈E to F until |V(F)| = |U|
The set V(F) obtained is connected to all nodes in U, hence by our assumption we must have
∑a∈U deg(a) ≤ ∑b∈V(F) deg(b)
However, since |U|=|V(F)| and |U|<|V| we know that there must be at least one "unreached" node v∈V\V(F), and since the graph is connected, deg(v)>0, so we obtain
∑a∈U deg(a) < ∑b∈V deg(b)
which is impossible; this should be an equality for a bipartite graph.