[ORIGINAL + translation] Introduction to algorithms (version 2) exercise 22.1-6: Universal sink)

Source: Internet
Author: User
Problem

 

If we use an adjacent matrix to store graphs, the running time of most graph algorithms is Ω (| v | 2) (V is the vertex set of a graph), but there are still some exceptions. For example, given a directed graph G's Adjacent matrix A, we can determine whether graph G contains a common sink point within the Ο (| v |) time, that is, a vertex with an inbound | v |-1 outbound degree of 0. Please provide such an algorithm.

 

Ideas

 

(Note: I have translated this section from the instructor manual. It is the same as my thinking, but the expression and logic are clearer .) If a [I, j] = 1, that is, (I, j) ε E (1 ≤ I ≤ | v |, 1 ≤ j ≤ | v |, E is the edge set of G. Therefore, if row I has an element of 1, vertex I cannot be a universal vertex. This also means that if I has an auto-cyclic edge, it cannot be a universal vertex. Assume that a [I, j] is 0, that is, (I, j) ∉ E, And I =j. In this case, vertex J cannot be a universal vertex, because either its inbound degree is strictly less than | v |-1, or it contains an auto-cyclic edge. Therefore, if column J has a non-diagonal element of 0, vertex J cannot be a universal sink point.

Therefore, this problem is equivalent to the following: Given a directed graph G's | v | × | v | adjacent matrix A, at O (| v |) determine whether there is an integer J (1 ≤ j ≤ | v |) within the time, so that for all I (1 ≤ I ≤ | v | and I =j) all have a [I,
J] = 1. For all K (1 ≤ k ≤ | v |), a [j, k] = 0. To put it more vividly, it is to judge whether there is such a "Ten" character in a: the "Ten" character is 0, the vertical column is all 1 (except the center of the word "10 ).

 

My Solutions

 

(Note: The following uses AIJ to represent a [I, j] and the left arrow to indicate the value assignment operation .)

To prove the correctness of the algorithm, we can use the following three Loop Invariant ):

Rows between 2nd and 8WhileWhen the loop starts iteration:

  1. I ≤ j;
  2. All elements in a [I, I .. J-1] are 0;
  3. In each column of the Child matrix A [1 .. I-1, 1 .. I-1], either the element on the diagonal line is 1 or a element above the diagonal line is 0.

The following is an argument for the three circular constants:

Initialization (first while iteration ):I = j = 1. Obviously, the three circular constants are true.

Maintainance ):HypothesisWhileAt the beginning of this iteration of a loop, all three cycle constants are true. If a [I, j] = 0, the value of I will remain unchanged, and the value of J will add 1, So I ≤ j will still be true; the increase of J makes a [I, j] =
0 is added to vector A [I, I .. so the second invariant will still be true at the beginning of the next iteration; Sub-matrix A [1 .. i-1, 1 .. i-1] will not change, so the third invariant will obviously continue to be true. If a [I, j] = 1 and I = J, I and j will add 1, therefore, at the beginning of the next iteration, I = j still exists (the first cyclic invariant will continue to exist), So I> J-1, and the second cyclic invariant is obviously true; child matrix A [1 .. i-1, 1 .. A row is added at the bottom of the I-1], a column is added to the right, and the element at the bottom of the newly added column (that is, the element on the diagonal line) is 1, therefore, the third circular invariant remains valid. If a [I,
J] = 1 and I <j, we will set the value of I to be equal to J, so like before, the first and second circular constants will still be valid; child matrix A [1 .. i-1, 1 .. j-I rows are added at the bottom of the I-1], J-I columns are added to the right, and the I row of each newly added column is 0 (because of the assumption at the beginning of this section ), because I <j, that is, the I-th row is on the diagonal line, the third circular invariant will remain valid at the beginning of the next iteration.

  Termination:WhenWhileAt the end of a loop, according to the arguments of Initialization and maintenance, the three circular constants are still valid. In this case, we have J = | v | + 1. If I = J, then according to the third circular invariant, in each column of the adjacent matrix A, either the element on the diagonal line is 1, or an element on the top of the diagonal line is 0, therefore, this integer J (1 ≤ j ≤ | v |) does not exist, so that for all I (1 ≤ I ≤ | v | and I =j) all have a [I,
J] = 1, for all K (1 ≤ k ≤ | v |) has a [j, k] = 0; that is, there is no such word "Ten. If I <j, then Similarly, according to the third circular invariant, 1 .. the I-1 is certainly not such an integer; and according to the second circular invariant, I + 1 .. | v | it cannot be such an integer. Therefore, I is the only integer that may meet the requirements. The statement after 8th rows in the algorithm is to check whether I meets the requirements.

InWhileIn each iteration of a loop, we increase the value of I or J, or the value of both. At the beginning of the loop, I = j = 1 while J = | v |WhileThe loop can be iterated 2 | v |-1 times at most (calculate the judgment of j ≤ | v | each time ). This process can be seen as moving a cursor from the upper left corner of a to the lower right corner. During each iteration, the cursor is closer to the bottom right corner. Two Algorithms after 8th rowsForMaximum loop iterations | v |
+ 1 time. Therefore, the running time of has-universal-sink is round (| v |), which is also equal to O (| v | ).

 

Instructor manual solution

 

(Note: the solution of the instructor's Manual is basically the same as that of mine, except that there are slight differences in details. In my algorithms, the cursor is always active in the diagonal and upper triangle areas of A, and the instructor's Manual algorithm does not. In addition, the description of the instructor manual seems to be easier to understand. My discussion may be too mathematical .)

First, implement a sub-process is-sink. This process determines whether a given vertex K is a universal vertex. If yes, true is returned; otherwise, false is returned:

 

Because the running time of this sub-process is O (| v |), if you want to judge whether graph G contains a common sink point in O (| v |) Time, we can only call O (1) for this sub-process.

At the same time, we can note that a directed graph can only contain one universal vertex at most. This is because if a vertex J is a universal sink point, then there is (I, j) ε E for all I = J, so that other vertex I cannot be a universal sink point.

The following algorithm uses the adjacent matrix A as the input. If a contains a universal vertex, the algorithm outputs the ID of a universal vertex. Otherwise, the message "no universal vertex" is output.

 

 

From the upper left corner of the adjacent matrix, the universal-sink moves the cursor to the right or downward position based on whether the current element a [I, j] is 0 or 1. When I or J is greater than | v |, iteration is stopped.

Similar to my solution, to prove the correctness of the universal-sink, we need to prove that whenWhileWhen the cycle ends, I is the only vertex that may be a universal vertex. The next call to is-sink is to check whether I meets the definition of a common sink.

FirstWhileThe I and j values are fixed at the end of the loop. All vertices that match 1 ≤ k <I cannot be common vertices. This is becauseWhileIn a loop, the only reason why the I value is increased is that the current element is 1.WhileWhen the loop ends, each row above line I contains at least one. According to the idea described at the beginning, if at least one element of row K is 1, then K cannot be a universal sink.

If I> | v | at loop termination, all vertices are excluded, so g cannot have a universal sink point. If I ≤ | v | at the end of a loop, we need to prove that the vertices with the serial number greater than I and smaller than or equal to | v | are not universal points. If I ≤ | v | at the end of the loop, then J> | v | must exist. This means that we have found 0 in each column. Looking back, if the K column has a zero value on the non-diagonal position, then the vertex K cannot be a universal sink point. In all
0 is found in the <k ≤ | v |) column.WhileIn the loop, we have never checked any row below row I, that is, we have not checked the elements on the diagonal lines of these column K, therefore, the 0 contained in the K column is in a non-diagonal position. Therefore, all vertex K that satisfies I <k ≤ | v | is not a universal sink point.

Therefore, all vertices smaller than I and all vertices larger than I are not universal points. The only common sink is I.WhileAfter the loop, the is-sink is called to check whether I is a universal sink.

Same as my algorithms,WhileIn each iteration of a loop, I or J will increase (but here I is increased, J is increased, and the increment is always 1, so in some cases, speed is estimated to be a little slower than my algorithm ). Therefore,WhileA loop can only iterate 2 | v |-1 times at most, and the time of each iteration is O (1). ThereforeWhileThe total time is O (| v | ). In addition, the time complexity is the is-Sink of O (| v |). the time complexity of the universal-sink is O (| v | ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.