60 likes | 194 Views
Graphs. Computing an Adjacency Matrix D. J. Foreman. The Graph & it's Adj 1. A. To. B. D. C. From. F. E. Algorithm for Computing Adj 2. If Adj[i,1] && Adj[1,j] == 1 path of length 2 from i to j through node 1 Therefore, If ANY Adj[i,n] && Adj[n,j] == 1, for all n, then
E N D
Graphs Computing an Adjacency Matrix D. J. Foreman
The Graph & it's Adj1 A To B D C From F E
Algorithm for Computing Adj2 • If Adj[i,1] && Adj[1,j] == 1 • path of length 2 from i to j throughnode 1 • Therefore, • If ANY Adj[i,n] && Adj[n,j] == 1, for all n, then • a path Length=2 from i to j. • Thus, for [i, j] compute: • Adj[i,1] && Adj[1,j] || Adj[i,2] && Adj[2,j] || … || Adj[i,n] && Adj[n,j] • Repeat for all i and j
Computation for 1 pair (A to B) • Let A-F be replaced by 1-6 • Find a length 2 path from i=1 to j=2 • adj[1,1] && adj[1,2]=0 && 1=>0 • adj[1,2] && adj[2,2]=1 && 0=>0 • adj[1,3] && adj[3,2]=1 && 1=>1 (13 2) • adj[1,4] && adj[4,2]=1 && 0=>0 • adj[1,5] && adj[5,2]=0 && 0=>0 • adj[1,6] && adj[6,2]=0 && 1=>0 • Now "or" these together Adj[1,2] • So a path of length 2 from A to B (through C) See the red edges in previous slide
Adj[i,1] && Adj[1,j] || Adj[i,2] && Adj[2,j] || … || Adj[i,n] && Adj[n,j] Adj[1] Adj[2]
Key Points • Adj (by itself) only tells if a path EXISTS • Does NOT tell what the path IS • Adj1 * Adj2 Adj3 (path of length 3) • Computations describe edges in path • Same as regular matrix multiply, but replace • * with && • + with ||