4.6 Data Structures for relations and digraphs
Data can be stored in a list known as an array Data can be stored in a list known as an array. The locations of an array are consecutively numbered in a computer memory.
Example: Array by the name of A with 5 data items: A [1] Data 1 Index Location A [1] Data 1 A [2] Data 2 A [3] Data 3 A [4] Data 4 A [5] Data 5 With this method, it would be difficult to add an item between D2 and D43. D1 D2 D3 D4 D5
A linked list includes a pointer Data Pointer Index Array Array Location A [1] [2] [3] [4] [5] The pointer tells us what location to go to next. Start at D1 location at A [2]. P[2] points to location [3]. D2 is in location A [3]. D4 4 D1 3 D2 5 D5 D3 1
Location P[3] points to location A[5]. D3 is in location A [5]. When you get to A[4], P[4] contents is 0 which means you are done. We can put the data in any order. We have to go thru the links to access the data we want.
If we have the following digraph: We see that we have 10 edges. We can conclude that we have 10 pairs. R = {(1,2),(1,6),(1,3),(2,1),(2,3),(3,5),(3,4),(3,6),(5,4),(6,1)} 2 2 1 8 5 7 4 3 1 3 5 10 6 9 6 4
An array called VERT for Vertex contains a pointer for every vertex in the digraph. The digraph has 6 verticies, so the VERT array will have 6 locations. The ordered pairs of the digraph will be called TAIL to represent the beginning vertex. The HEAD array will represent the ending vertex. The NEXT array will be a pointer array to get all of the pairs for a given vertex.
TAIL HEAD NEXT [1] [2] [3] [4] VERT [5] [1] [6] [2] [7] [3] [8] [4] [9] [5] [10] [6] 5 8
TAIL HEAD NEXT [1] [2] [3] VERT [4] [1] [5] [2] [6] [3] [7] [4] [8] [5] [9] [6] [10] 2 3 2 1 2 3 5 7 9 5 4 3 4 3 4 6 3 6 6 1 5 1 6 10 8 1 3 1
VERT tells us which location to point to for our pairs. First we go to TAIL location [9]. TAIL [9] = 1 and HEAD [9] = 6. In our NEXT location [9] we point to another relation for vertex 1. We are directed to TAIL[10] where we see the TAIL, HEAD pair of 1,3We are then pointed to NEXT [10] to location [1] where we find TAIL,HEAD pari (1,2). We are done with vertex 1 so, in the NEXT [1] we have a 0 which indicates that we need to go to the next VERT entry, which happens to be 3. So we go to location [3] of TAIL and we fin the TAIL,HEAD pair (2,1)
1st look at vertex position [1] edge (1,6) (1,3) (1,2) 0 done with edges leaving vertex 1 Next look at vertex position [2] edge (2,1) (2,3) 0 done with edges leaving vertex 2 Next look at vertex position [3] edge (3,4) (3,5) (3,6) 0 done with edges leaving vertex 3
Next, look at vertex position [4] It is 0 so done with edges leaving vertex 4. Next look at vertex position [5] edge (5,4) 0 done with edges leaving vertex 5 Last look at vertex 6 edge (6,1) 0 done with edges leaving vertex 6