Modification for Dijkstra Algorithm [closed] - algorithm

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to know, when the start vertex s is given, the shortest path is computed only if a vertex is no more than three edges away from the start vertex.
I thought of doing this by counting the number of parents, and if number_of_parents<=3 then it is a valid path.
Please can someone clarify this for me using the algorithm?
Below is the standard Dijkstra Algorithm.
Dijkstra(G,W,s)
Initialize_Single_Source(G,s)
S= {}
Q = V[G]
while Q != {} do
u = extract_min(Q)
S = S U {u}
for each vertex v element of Adj[u] do
relax(u,v,w)

Instead of a vertex we use array L to determine levels, i.e., how far a node is from the source
Dijkstra(G,W, s)
Initialize_Single_Source(G,s)
S= {}
Q = V[G]
L = {an array to determine levels of each node,
initially all nodes have level "infinite" except for s who has level 0}
while Q != {} do
u = extract_min(Q)
u_level = L[u]
if(u_level > 3){
ignore u and don't add it to S;
continue;
}
S = S U {u}
for each vertex v element of Adj[u] do
relax(u,v,w)
in relax function you put L[v] = min(L[v], u_level + 1);

Related

Find the most reliable path - Dijkstra’s algorithm [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
We are given a directed graph G = (V, E) on which each edge (u, v) ∈ E has an associated
value r(u, v), which is a real number in the range 0 ≤ r(u, v) ≤ 1 that represents the reliability of a communication channel from vertex u to vertex v. We interpret r(u, v) as the
probability that the channel from u to will not fail, and we assume that these probabilities
are independent. Give an efficient algorithm to find the most reliable path between two given
vertices.
a
/ \
b<--c a directed to c; c directed to b; b directed to a
Let's say this is graph G = (V, E); vertex a is the root, one of the edge is a to c. a = u & c = v so edge is (u, v). I want to use Dijkstra’s algorithm to solve this, but not sure how.
a
\
b<--c path c: a -> c & b: a -> c -> b
Can someone explain the most reliable path in the simplest way possible?
This question is from Introduction to Algorithms, 3rd edition, chp 24.3
Thanks in advance!
We interpret r(u, v) as the probability that the channel from u to
will not fail, and we assume that these probabilities are independent.
From this you can deduce that the probability that a given path will not fail is equal to the product of the r(u,v) of all edges (u,v) that make up the path.
You want to maximize that product.
This is exactly like the shortest path problem, for which you surely know an algorithm, except instead of minimizing a sum, you are trying to maximize a product.
There is a cool tool to go from products to sums: it's the logarithm. Logarithm is an increasing function, thus maximizing a product is the same as maximizing the logarithm of that product. But logarithm has the additional cool property that the logarithm of a product is equal to the sum of the logarithms:
log(a * b * c * d * ...) = log(a) + log(b) + log(c) + log(d) + ...
Thus maximizing the product of the reliabilities r(u,v) is the same as maximizing the sum of the log-reliabilities log(r(u,v)).
Since the reliabilities are probabilities of the edges, they are values between 0 (excluded) and 1 (included). You can exclude 0 because if an edge had a reliability of 0, you can remove that edge from the graph. Since 0 < r(u,v) <= 1, it follows that log(r(u,v)) is negative or 0.
So you are trying to maximize a sum of negative values. This is exactly the same as minimizing the sum of the opposite values.
Thus: apply your shortest-path algorithm, using -log(r(u,v)) as the lengths of the edges.

Algo to find the maximum-sized matching of a graph that has the maximum sum of edge weight? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an app where people can give marks to each other, out of ten points. At midnight, each day, I would like to compute a "match" for each member. I would like to make everyone as much happy as possible, in average.
So at the midnight, I have an oriented graph like so :
1 -> 2 : 7.5 // P1 give a 7.5/10 to P2
1 -> 3 : 5
1 -> 4 : 9
2 -> 3 : 6
2 -> 1 : 4
etc.
To make things more simple let's say that if P1 give P2 a 5 and P2 give P1 a 7, the match P1 - P2 will have a weight of 5 + 7 - (7-5)/2 = 11 (I substract the difference because, for a same sum of grades, it's better if they are close to each other, that is, a (7/10 - 7/10) is a better match than a (10/10 - 4/10)).
So with this done, we have a non-oriented graph. Mathematically speaking, for my purpose, I think that I need to find an algorithm that finds, among all the maximum-sized matchings that have this graph, the one that has the maximum weight sum. Does such an algorithm exist ?
I've already looked into "Mariage stable problem" and "assignment problem" but these are for graph that can be divided in 2 classes (men/women, men/task ..)
A way to do that is to modify your graph and then find a maximum weight matching on it.
I need to find an algorithm that finds, among all the maximum-sized matchings that have this graph, the one that has the maximum weight sum. Does such an algorithm exist ?
Let's consider your graph G = (V, E, w) where w is your weight function. Let's denote by n the size of V, i.e the number of vertices in your graph, and by M the maximum weight among the edges.
Then, all you have to do is to define w' in this way: for any edge e of E, w'(e) = w(e) + n*M.
In this case, a maximum weight matching on G' = (V, E, w') corresponds to a matching of maximum size in G = (V, E, w) that also has a maximum weight.

Correctness of algorithm for finding diameter of graph [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have heard of algorithm of finding diameter of unweighted graph using the following algorithm:
Algorithm (start_vertex):
find out the set of vertices S having maximum value of
shortest distance from start_vertex.
ans = 0;
for each vertex v in S
temp = 0;
for each vertex u in graph G:
temp = max(temp, shortest distance between u and v).
ans = temp;
return ans;
This algorithm works in linear time for finding diameter of graph. Can anyone prove its correctness or prove it is false
A counter-example:
Obviously, graph diameter is 4 (distance from A to C),
but with starting vertex B you will have S = {D},
and all vertices are within range 3 from D.
This graph contains 7 vertices.
You can add extra vertices (inside the triangle) without breaking the counter-example to get a graph of size 8, 9, ...
Here is a counter-example:
Obviously, the graph has diameter 16 (from c to d).
If you start at a, the set S contains of solely b. The vertex with maximum minimal distance to b is e. Hence, the algorithm would return the graph diameter 11.

Dijkstra's algorithm concepts [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am just learning Dijkstra algorithm and just a little bit confuse in this
If min(A,B) = x;
min(A,C) = y;
min(B,C) = must be x-y;
Please justify it or i am wrong?
Okay here's what you meant to say:
I will be referring to a directed non-negative weight graph in all of this.
The shortest path problem:
For a digraph G and a node r in V and a real cost vector (c_e:e in E) (I wish we had LaTeX here)
we wish to find:
for each v in V a dipath from r to v of least cost (supposing it exists)
Here's the gist of what you want:
suppose we know there's a path from r to v of cost y_v for each v in V, and we find an edge vw in E satisfying y_v + c_vw < y_w
Since appending vw to the dipath to v (to get a path to w) gives a path of length y_v+c_vw
A least cost dipath satisfies:
y_v+c_vw >= y_w for all vw in E
We call such a y vector a "feasible potential"
Proposition: y_v is minimal
Let y be a feasible potential, and let P be a dipath from r to v, then it follows c(P) >= y_v
Proof:
c(P) = sum c_ei (the the ith edge in the path's cost)
Recall that a feasible potential statisfies y_v + c_vw >= y_w
So c_vw >= y_w - y_v this is what you have
Thus
c(P) >= sum (y_vi-y_v{i-1}) (the cost to the ith item take the cost of the previous one)
if you write it as sum (-y_v{i-1} + y_vi) then expand the sum: (y_v0 = 0 obviously)
-y_v0+y_v1 -y_v1 + y_v2 - .... -y_v{k-2} + y_v{k-1} -y_v{k-1} + y_vk
you see all the terms cancel out, giving:
c(P) >= y_vk - y_v0 = y_vk
Thus we have shown c(P) >= y_vk
It is wrong, think about any equilateral triangle, the difference of two sides is 0 and the length of the third size is not.

push flow relabel algorithm [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
A valid labelling of the vertices in V wrt. a preflow x is a function d[.] : V -> Z satisfying:
d[s] = n ^ d[t] = 0
for all (v,w) belong to E : d[v] <= d[w] + 1
supposed we have 4 verticies including (s and t)
then we have d[s] = 4
according to valid labeling we should have d[v] <= d[w]+1, but for edges which are coming from 's', it is not
valid because 4 <= 1 is false. Is this logic is not only source?
Am I understading it right? Please correct me.
Thanks for your time and help
Your definition of a valid labelling is close, but not quite correct.
You claim that d[v] <= d[w] + 1 for all (v,w) belonging to E.
However, this actually only needs to be true for all (v,w) belonging to R, where R is a residual edge.
A residual edge is an edge where the current flow is less than the capacity on the edge.
There is a good explanation at topcoder.
Consider this diagram:
In the labels on the edges (such as 2/3) the first number gives the current flow, and the second number gives the capacity of the edge.
The numbers on the nodes give the height function d for each node.
The green edges are the residual edges because they have spare capacity.
So to check the height constraint we only need to check the S->A edge, and the B->T edge.

Resources