* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download 31GraphsDigraphsADT
System of linear equations wikipedia , lookup
Linear least squares (mathematics) wikipedia , lookup
Rotation matrix wikipedia , lookup
Eigenvalues and eigenvectors wikipedia , lookup
Principal component analysis wikipedia , lookup
Determinant wikipedia , lookup
Four-vector wikipedia , lookup
Jordan normal form wikipedia , lookup
Matrix (mathematics) wikipedia , lookup
Singular-value decomposition wikipedia , lookup
Non-negative matrix factorization wikipedia , lookup
Orthogonal matrix wikipedia , lookup
Perron–Frobenius theorem wikipedia , lookup
Matrix calculus wikipedia , lookup
Cayley–Hamilton theorem wikipedia , lookup
FUNDAMENTALS: DATA STRUCTURES & ALGORITHMS SPECIFICATION OF THE ADT – GRAPH/DIGRAPH Additional definitions Subgraph Connected Adjacency Spanning Tree a graph containing a subset of nodes and edges contains a path between every pair of nodes if there is an edge between the two nodes a graph with sufficient edges removed to eliminate cycles Name Elements Structure - Graph or Digraph - set of instances from a GraphNode ADT - each instance of GraphNode connected to zero or more other GraphNode objects determined by the set of edges. Domain Sets G – set of all ordered pairs <n, e> (see earlier definition for a graph) N – set of all GraphNode objects E – set of ordered pairs of GraphNode references from the set N K – set of instances of KeyNode ADT Operations create: g insert: g, n g (uses ‘traverse’ to locate position in the tree) Also delete, update and retrieve operations traverse: g, k n ( several methods of maintaining the graph structure; depth first, breadth first, priority first, spanning tree) path: g e (possibilities: between two nodes, all paths, shortest paths etc.) ______________________________ Representation and Implementation a)Matrix Representation Matrices are of considerable use in representing graphs/digraphs, particularly static graphs. Assuming digraph G = <N, E> where N = set of nodes { n1, n2, n3, . . . nm } and E the set ordered pairs from N then: ADJACENCY MATRIX (A) PRECEDENCE MATRIX (P) aij = 1 if < ni, nj > E pij = 1 if ni precedes nj aij = 0 if < ni, nj > E pij = 0 if ni does not precede nj PATH MATRIX (L) lij = 1 if a path exists between ni and nj lij = 0 if no path exists between ni and nj Example 1 2 O6 O O 5 O O SUCCEDENCE MATRIX (S) sij = 1 if ni succedes nj sij = 0 if ni does not succede nj A = O 010000 001100 000100 110000 001000 000000 L = 1 11100 111100 111100 111100 111110 000000 Relationship between the Adjacency matrix and the Path matrix for a digraph Suppose A is the adjacency matrix. Let matrix B = Ak Then bij is the total number of distinct sequences < n1, . .> . . . . . . <. . , nj > that: i) have length k ii) correspond to paths in the digraph Proof: For k = 1 then B = A and the theorem is true (through the definition of the Adjacency matrix A) Assume true for some k = h and let matrix C = Ah For a given i, j and k we have: cik = sequences of length h < n1, . . .> . . . . . . . . . . . . . . . . . <. . . , nk > = sequences of length h+1 < n1, . . .> . . . . . . . . . . . . . . . . . <. . . , nk >, < nk, nj > i.e. cik . akj = cik if akj is an arc (sequence of one or more edges), otherwise it is 0 Hence total sequences of length h+1 = < n1, . . .> . . . . . . . . . . . . . . . . . <. . . , n j > n = cik . akj i.e. the (i,j)th element of the matrix Ah+1 Corollaries i) If Ak = 0 for some k <= n then the digraph contains no cycles i.e. is acyclic. ii) If L is the path matrix of a digraph and Q = A + A 2 + A3 + A4 . . . . . . . . . . . + An then: lij = 1 if and only if qij is not zero. If boolean matrix operations are used (see later) i.e. Q = A A2 A3 A4 . . . . . . . . . . An where Ak = Ak-1 ^ A then Q = L OPERATIONS ON MATRIX REPRESENTATIONS FOR DIGRAPHS The Adjacency to Path matrix for a Digraph If A = Adjacency matrix of size n. If B = Ak (A A . . .) then bij = 0 reflects a distinct path in the digraph of length k. a) If Ak = 0 for some k <= n then the digraph is acyclic. b) Boolean operations used: C=AA cij = (a ik akj ) k=1 D=AA c) dij = aij aij The path matrix: P = A A2 A3 A4 . . . . . . . . . . . . . An Example o1 A o2 = 0 1 1 1 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 o3 A2 = o5 4o o6 A3 = 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 A4 = 0 0 0 0 0 0 Since the Path matrix P is equivalent to the Adjacency matrix A, then P2 contains the second precedents to each node, P3 the third precedents etc.etc. Representation and Implementation of Graphs Matrix Representation Matrices are of considerable use in representing graphs/digraphs, particularly static graphs. Assuming digraph G = <N, E> where N = set of nodes { n1, n2, n3, . . . nm } and E the set ordered pairs from N then: ADJACENCY MATRIX (A) aij = 1 if < ni, nj > E aij = 0 if < ni, nj > E nj PRECEDENCE MATRIX (P) pij = 1 if ni precedes nj pij = 0 if ni does not precede PATH MATRIX (L) SUCCEDENCE MATRIX (S) lij = 1 if a path exists between ni and nj sij = 1 if ni succedes nj lij = 0 if no path exists between ni and nj sij = 0 if ni does not succede nj Example 1 O 2 O O6 5 O O 4 O 3 A = 010000 001100 000100 110000 001010 000000 L = 111100 111100 111100 111100 111110 000000