Some Graph Algorithms

Some Graph Algorithms

page 1 of Section 3.1 CHAPTER 3 SOME GRAPH ALGORITHMS SECTION 3.1 WARSHALL'S ALGORITHM digraphs A graph with one—way edges is called a directed graph or a digraph. For the digraph in Fig 1, the adjacency matrix containsa1inrowB,colCto indicate edge BC from B to C. There'sa0inrowC,colBsince there is no edge CB from C to B. In general, the adjacency matrix of a digraph is not necessarily symmetric. An undirected graph can always be redrawn as a directed graph. Just replace each (two—way) edge by two one—way edges (Fig 2) ABCD D • C• A0101 B1011 C0010 • = P• A• • B P • Q • Q D0010 FIG 1 FIG 2 paths and cycles in a digraph In the underlying graph of Fig 1 (where the edges are not directed) there is a path between A and C; each vertex is reachable from the other. But in the digraph (where the edges are directed) there is a path from A to C but not from C to A. Similarly, in the underlying graph of Fig 1 there's a cycle ABDA. But in the digraph there is no cycle ABDA since there is no edge directed from D to A. the reachability matrix M™ If M is the adjacency matrix of a digraph then an entry of 1 in row i, col j indicates an edge vivj, i.e., a path from vi to vj with just one edge. In this section I'll extract from M a new matrix called the reachability matrix, denoted M™,in which an entry of 1 in row i, col j indicates a path (with one or more edges) from vi to vj, and an entry of 0 means no path at all. In other words, the reachability matrix indicates whether you can get from here to there. (Some books call M™ the transitive closure of M.) footnote It may seem odd but frequently you can't get from here to here. In Fig 1 there's a path from A to A, namely ABA, and there's a path from C to C, a loop, but there is no path from D to D. Maybe it's not so odd after all. If the vertices in a graph represent people and an edge from A to B means that B is the parent of A then there can't be a path like APQA,from A to itself, since that would make A her own great grandmother. Boolean arithmetic To compute the matrix M™ from the adjacency matrix you'll be dealing entirely with 0's and 1's and it will turn out that Boolean arithmetic will be pertinent. It differs from ordinary arithmetic only in that 1+1=1. In fact the new law1+1=1together with the old law1+0=1means that 1 + anything = 1 All sums in this section are intended to be Boolean. page 2 of Section 3.1 Warshall's algorithm for finding the reachability matrix M™ for a digraph Start with a digraph with n vertices. Here's the idea of the algorithm. Begin with the adjacency matrix M which indicates which pairs of vertices are directly connected. The first round will get a matrix M1 which indicates which pairs of vertices are connected by a path using v1 as the only possible intermediate point (i.e., connected by an edge from here to there or by a path from here to v1 to there). The next round gets matrix M2 which indicates which pairs of vertices are connected by a path allowing only v1 and v2 as possible intermediate points etc. In general here is the loop invariant (something that's true after every round): At round k, you will get a matrix Mk. Look at the entry say in row 3, col 5. (1) An entry of 1 means that there is a path from v3 to v5 with v1,..., vk as the only possible intermediates. An entry of 1 means that there is no path from v3 to v5 that uses only if only v1,..., vk as intermediates, Here's the algorithm for finding M™ starting with a digraph with n vertices and adjacency matrix M. Set M0 =M. To get M1, for every row in M0 that hasa1incol1,add(Boolean) row 1 to that row; i.e., look down col 1 and if there'sa1inarow, add row 1 to that row. To get M2, for every row in M1 witha1incol2,addrow2tothat row. In general, to go from Mk-1 to Mk, for every row in Mk-1 that hasa1incolk, add row k to that row. Continue until you have Mn. At the end of the algorithm, (2) M™ =Mn I'll do an example first and then show why the algorithm works, i.e., show why (1) and (2) hold. example 1 v5 • Start with a digraph with this adjacency matrix M. 00010 • • 10000 v1 v4 M= 01000 00011 v2 • 00000 • v3 That's M0. FIG 3 round 1 To go from M0 to M1. Look at col 1 in M0. The only row witha1incol1isrow2.Soaddrow1torow2 page 3 of Section 3.1 and leave the other rows alone. Note that the Boolean addition amounts to replacing 0's in row 2 by 1's if there is a corresponding 1 in row 1. 00010 10010 M1 = 01000 00011 00000 round 2 To go from M1 to M2. Look at col 2 in M1. The only row witha1incol2isrow3.Soaddrow2torow 3 and leave the other rows alone. 00010 10010 M2 = 11010 00011 00000 round 3 To go from M2 to M3. Look at col 3 of M2. There are no 1's in col 3 so leave M2 alone, i.e., M3 =M2. round 4 Look at col 4 of M3. The first four rows contain 1's in col 4 so add row 4 to each of the first four rows (note that adding a row to itself can't change it). 00011 10011 M4 = 11011 00011 00000 round 5 Look at col 5 in M4. The first four rows contain 1's in col 5 so add row 5 to them which doesn't change anything: M5 =M4.So 00011 10011 11011 M™ =M5 = 00011 00000 For instance, column 3 is all 0's so there are no paths to v3 from anywhere; row 5 is all 0's so there are no paths from v5 to anywhere. why (1) holds I'll use the graph from example 1 to illustrate the idea. Here's why (1) holds fork=1,i.e., for M1. As you go from M0 to M1 there are two ways in which entries of 1 appear in M1. (I) All the 1's in M0 are still there in M1 For example, the 1 in row 3, col 2 is a holdover. In M0 (and therefore in M1)it indicates an edge from v3 to v2, which you can call a path from v3 to v2 using no intermediates. page 4 of Section 3.1 (II) Some 0's in M0 might become 1's in M1 For example, the 0 in row 2, col 4 of M0 changed to a 1 because there wasa1in row 2, col 1 (this told you to add row 1 to row 2) and there wasa1inrow1,col4 (which got added to the 0 entry and changed it to a 1) (Fig 4) edge v2 to v1 edge v1 to v4 001 0 0 changes to a 1 in M 1 1000 0 0 0 10 M0 = 0 0 0011 00000 FIG4Seea0inM0 change toa1inM1 The 1 in row 2, col 1 (in M0) indicates that there is an edge from v2 to v1. The 1 in row 1, col 4 indicates that there is an edge from v1 to v4. Put those two edges together and you know there is a path from v2 to v4 using v1 as an intermediate, justifying the 1 in row 2, col 4 of M1. Allinall,a1inrowi,coljinM1 signals a path from vi to vj either using no intermediate (if it's type (I)) or using v1 as the only intermediate (if it's type (II)). And every such path is signaled like that in M1. Here's why (1) holds for M2. As you go from M1 to M2 there are two ways in which entries of 1 appear in M2. (I) All the 1's in M1 are still there in M2. For example the 1 in row 2, col 4 is a holdover. In M1, and now in M2, it indicates a path from v2 to v4 using v1 as a possible intermediate. (II) Some 0's in M1 might become 1's in M2. For example, the 0 in row 3, col 4 of M0 changed to a 1 because there wasa1in row 3, col 2 (this told you to add row 2 to row 3) and there wasa1inrow2,col4 (which got added to the 0 entry and changed it to a 1) (Fig 5) path v3 to v2 with v1 as the path v2 to v4 only possible intermediary 0 0010 1001 0 changes to a 1 in M 2 0 M1 = 1000 0 0011 00000 FIG5Seea0inM1 change toa1inM2 As I just proved, the 1 in row 3, col 2 (in M1) indicates that there is a path from v3 to v2 with v1 as the only possible intermediate. page 5 of Section 3.1 And the 1 in row 2, col 4 indicates that there is a path from v2 to v4 with v1 as the only possible intermediate.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    38 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us