unable to comprehend prims algorithm - pseudocode

Please help in understanding prims algo pseudocode(as it is in coreman and wiki)
Prim's algorithm.
MST-PRIM (G, w, r) {
for each u ∈ G.V
u.key = ∞
u.parent = NIL
r.key = 0
Q = G.V
while (Q ≠ ø)
//1
u = Extract-Min(Q)
for each v ∈ G.Adj[u]
if (v ∈ Q) and w(u,v) < v.key
v.parent = u
v.key = w(u,v)}
i am able to understand till 1 or while loop that r.key=0 ensure that neighours or adjacents of root are scanned first,
but as u already belongs to Q(queue of nodes till now not included in prims minimum spanning tree) and v also in Q,will not help in generating prims MST.
also both coreman and thus wiki states
1. A = { (v, v.parent) : v ∈ V - {r} - Q }.
2. The vertices already placed into the minimum spanning tree are those in V−Q.
3. For all vertices v ∈ Q, if v.parent ≠ NIL, then v.key < ∞ and v.key is the weight of a light edge
Prior to each iteration of the while loop of lines 6–11,
(v, v.parent) connecting v ::to some vertex already placed into the minimum spanning tree.
as A is our MST then how 1. will help as v is already been included in our MST (as shown by v ∈ V - {r} - Q ) why then it should be included.

For the part that you have doubts:
u = Extract-Min(Q)
for each v ∈ G.Adj[u]
if (v ∈ Q) and w(u,v) < v.key
v.parent = u
v.key = w(u,v)
"For each vertex v, the attribute v:key is the minimum weight of any edge connecting to a vertex in the tree; by convention, key = ∞ if there is no such edge." (http://en.wikipedia.org/wiki/Prim's_algorithm)
Therefore, u = Extract-Min(Q) will get the vertex with the minimum key.
for each v ∈ G.Adj[u] will find all the neighbors of u.
if (v ∈ Q) and w(u,v) < v.key condition to eliminate cycle and check if path should be updated.
Then the following lines of code update the neighbors edges.
v.parent = u
v.key = w(u,v)
"Prior to each iteration of the while loop of lines 6–11,
1. A = { (v, v.parent) : v ∈ V - {r} - Q }. " (http://en.wikipedia.org/wiki/Prim's_algorithm)
Based on the above statement, before the while loop A is empty as Q = G.V! After the while loop you will get A contains all the vertices that form the MST. Each vertex v in A has a parent (v.parent). For root r, its parent is NIL. Root r is excluded due to the statement V - {r} but it exists in A thanks to its children in the form of v.parent.
Therefore in this link http://en.wikipedia.org/wiki/Prim's_algorithm , it states that: "2. The vertices already placed into the minimum spanning tree are those in V−Q."
and "When the algorithm terminates, the min-priority queue Q is empty; the minimum spanning tree A for G is thus A = { (v, v.parent) : v ∈ V - {r} }."

Related

In SPFA Shortest Path Faster Algorithm why does it have to check if current vertex is in queue before adding it to queue?

procedure Shortest-Path-Faster-Algorithm(G, s)
1 for each vertex v ≠ s in V(G)
2 d(v) := ∞
3 d(s) := 0
4 push s into Q
5 while Q is not empty do
6 u := poll Q
7 for each edge (u, v) in E(G) do
8 if d(u) + w(u, v) < d(v) then
9 d(v) := d(u) + w(u, v)
10 if v is not in Q then
11 push v into Q
In line 10, it checks if v is in Q. Why do we need this step?
When a node gets updated more than once in one iteration, we only need to process it once in the next iteration of the algorithm.
For example, let's say we have three nodes a,b,c and two edges a->c with weight 5 and b->c with weight 2, let the current distnaces be d[a] = 5, d[b] = 6, d[c] = infinity. When we process the edge a->c, the value of d[c] gets updated with 5 + 5 = 10, and pushed into the queue. Then when we process edge b->c the new value of d[c] is 6 + 2 = 8 which is less than current d[c] = 10 so d[c] = 8 now, but c is already in the queue so there is no need to push it again, and the older value of d[c] = 10 is not required anymore in the next iteration.
Intuitively, you can think of SPFA as a variation on the Bellman-Ford algorithm. As a reminder, Bellman-Ford works like this. Here, d[v] denotes our current guess of the distance from the start node s to node v, and dist(u, v) denotes the length of edge (u, v):
set d[v] = infinity for all nodes v
set d[s] = 0
for l = 1 to n-1, where n is the number of nodes:
for each node u:
for each node v adjacent to u:
if d[u] + dist(u, v) < d[v]:
set d[v] = d[u] + dist(u, v)
The idea is that the loop on l guarantees that at the start of iteration l, the value of d[v] is the length of the shortest path from s to v using at most l edges.
The optimization SPFA uses works by noticing that the inner loop on u will not have any effect unless d[u] changed in the previous iteration of the outer loop. We therefore can save some work by rewriting Bellman-Ford as follows:
set d[v] = infinity for all nodes v
set d[s] = 0
for l = 1 to n-1, where n is the number of nodes:
for each node u where d[u] changed:
for each node v adjacent to u:
if d[u] + dist(u, v) < d[v]:
set d[v] = d[u] + dist(u, v)
Now, how should we implement this idea? To loop over all nodes that were updated on the previous iteration, and no others, we can maintain a queue containing the nodes we need to process on the next iteration. That queue shouldn’t contain any duplicates, since we just need to know whether we do or don’t process it next time. That gives this code:
set d[v] = infinity for all nodes v
set d[s] = 0
set q = {s}
for l = 1 to n-1, where n is the number of nodes:
set q’ = {}
for each node u in q:
for each node v adjacent to u:
if d[u] + dist(u, v) < d[v]:
set d[v] = d[u] + dist(u, v)
add v to q’ if it’s not already there
set q = q’
The last step to make this look like the final version of SPFA is to unify the roles of q’ and q. We can do that under the assumption that there are no negative cycles as follows:
set d[v] = infinity for all nodes v
set d[s] = 0
set q = {s}
while q isn’t empty;
remove a node u from q
for each node v adjacent to u:
if d[u] + dist(u, v) < d[v]:
set d[v] = d[u] + dist(u, v)
add v to q if it’s not already there

running time variation Prim

I wanted to find the "minimum spanning tree", given a set A with edges that must be in the "minimum spanning tree" (so it's not a real minimum spanning tree, but given A, it has the least sum of weights). So the "minimum spanning tree" must definitely contain all edges of A. I made some modifications to Prim's algorithm which can be found below. I then wanted to find the running time of this algorithm, however, I'm having trouble with finding the running time to check wheter the intersection of two sets is empty.
Could somebody please help me? And what would be to total running time then? I already put the running time for each step next to that step, except for "?".
Notation clarification:
δ(W) = {{v ,w} ∈ E : w ∈ W, v ∈ V\W} for W ⊂ V
algorithm:
1. T = ∅, W = {v} for some v ∈ V O(1)
2. While W ≠ V n iterations
If (A ∩ δ(W) ≠ ∅) do ?
Take e = {v,w} ∈ (A ∩ δ(W)) O(1)
T = T ∪ {e} O(1)
W = W ∪ {v,w } O(1)
Else
Find e = {v,w } ∈ δ(W) s.t. ce ≤ cf ∀ f ∈ δ(W) O(m)
T = T ∪ {e} O(1)
W = W ∪ {v,w } O(1)
End while

shortest Path in directed graph G

I had an exam yesterday and I would like to check if I was answering correctly on one of the questions.
The question:
G = (V, E, w) is a directed, simple graph (V: set of vertices, E: set of edges, w: non-negative weight function). There is a non-empty subset of G denoted E(red).
A path p in G will be called n-red if there are n red edges on p. d_red(u, v) will be the lightest path from vertex u to vertex v that is at least 1-red. If all paths from u to v are 0-red, d_red(u, v) = Infinity.
The weight of a path p is the sum of all edges that are part of p.
Input:
G = (V, E, w)
s, t that are elements of V
f_red: E -> { true, false }
f_red(red edge) = true
f_red(non-red edge) = false
Output:
d_red(s, t) (the lightest path that includes at least one red edge).
Runtime Constraint: O(V log V + E)
In a few words, my solution was to use Dijkstra's algorithm. A Boolean variable that is initially false is used to keep track of whether at least one red edge has been encountered. This is checked for every iteration with f_red and the variable is set to true if f_red(current edge) = true. If the variable is still false at the end, return d_red(u, v) = Infinity.
What do you think about that?

Find a shortest path for a directed graph

There is a directed graph G = [V ; E] with edge weights w(u, v) for (u, v) ∈ E.
Suppose the values for {d[v], π[v]}; v ∈ V and claims
that these are the length of the shortest path and the predecessor node in
it for v ∈ V , how could I verify if this statement is true or false that does not solve the entire shortest path problem from scratch? This is an problem I met with not many ideas in my head ..
The problem is a bit unclear, but to clarify:
There's a node s in your graph, and that for each vertex v:
for v != s, pi[v] is intended to be a node adjacent to v that's on a shortest path from v to s.
d[v] is intended to store the shortest distance from v to s.
The problem is to verify, given a pi, d, that they legitimately contain back-edges and minimal distances.
An easily implemented condition that verifies this is as follows:
For each vertex v
Either:
v = s and d[v] = 0
Or:
d[pi[v]] = d[v] - 1
d[u] >= d[v] - 1 for each u adjacent to v
pi[v] is adjacent to v
This check runs in O(V + E) time.

Analysis of BFS

I have the following BFS function from Cormen.
Definition of the shortest-path distance path(s,v) from s to v as the minimum number of edges in any path from vertex s to vertex v, or else if there is no path from s to v. A path of length path(s,v) from s to v is said to be a shortest path from s to v.
Following is lemma given
Let G = (V,E) be a directed or undirected graph, and let s belongs to V be an arbitrary vertex. Then, for any edge (u, v) E,
path(s,v) <= path(s,u) + 1 .
My question is why we have to have <= in above formula, i taught "=" is ok, can any one tell me one scenrio why we require <= ?
Below is BFS algorithm
Leema 2:
Let G = (V,E) be a directed or undirected graph, and suppose that BFS is run on G from a given source vertex s belongs to V. Then upon termination, for each vertex v belongs to V, the value d[v] computed by BFS satisfies d[v] >= path (s, v).
Proof:
We use induction on the number of times a vertex is placed in the queue Q. Our inductive hypothesis is that d[v] >= path(s,v) for all v belongs to V.
The basis of the induction is the situation immediately after s is placed in Q in line 8 of BFS.
The inductive hypothesis holds here, because d[s] = 0 = path(s, s) and d[v] = path (s, v) for all v belongs to V - {s}.
My question is what does author mean by "We use induction on the number of times a vertex is placed in the queue Q" ? and how it is related to inductive hypothesis?
Thanks!
BFS(G,s)
1 for each vertex u V[G] - {s}
2 do color[u] WHITE
3 d[u]
4 [u] NIL
5 color[s] GRAY
6 d[s] 0
7 [s] NIL
8 Q {s}
9 while Q
10 do u head[Q]
11 for each v Adj[u]
12 do if color[v] = WHITE
13 then color[v] GRAY
14 d[v] d[u] + 1
15 [v] u
16 ENQUEUE(Q,v)
17 DEQUEUE(Q)
18 color[u] BLACK
For your first question, consider a complete graph with only three vertices. In this graph is it true that path(s,v) = path(s,u) + 1 ?

Resources