Title: Data Representation Methods
1??????? ? ?????? ??
http//www.cise.ufl.edu/sahni/cop3530 ??????? ??
2???? ???
- ?????? ???? ??????? ???? ??????? ????? ?? ?? ???
??? - ???? ??? ???? ??????? ?????? ??? ????? ?? ????
??????? ?? ??? ( ????????...) - class Linear List
- int list-size
- Object Type Memberslist-size
- //Operations
- AddNewMember (Object Type m)
- RemoveMember(int n)
3????? ???? ??? ?? ?????
- ???? ?? ????? ?? ???? ????? elements ???? ????
?? ???
a
b
c
d
e
L (a, b, c, d, e) ??? i ?? ????? ?? ???
elementsi ???? ?? ????
4????? ???? ?? ??
5????? ?? ?? ????
6????? ?????
7????? ?????
size 5
- ??? i ?? ????? ?? ??? elementsi ???? ?? ????
- ?? ????? size????? ??????? ????? ????? ????
??????? ?? ???
8Add/Remove An Element
add(1,g)
9????? ???? ????
- ??? ????? ???? ?? ????? ???? ????? ????
- ?? ???? ?? ????? ?? ???? Object ??? ?? ???
- ????? ???? ?? ?? ??? Object ?????? ????
- ?? ??? ???? ??? ???? ??? ????? ( int, char,
float ,) ?? ??? ???? ?? ???? ???? ??? - Wrapper Classes ? ?????? ????
10?????? ?????? ????
- ????? ????? ???? ?? ????? ???? ????? ?? ???
?????? - ???? ????? ???? ?? ???? ?????? ?? ???? ?? ?????
???? ?????? ????? ????
11?????? ??? ?????
????? ????? ????? ?? ?????
newArray new Object15
12?????? ??? ?????
- ??? ????? ????? ??? ?? ?? ?? ??? ?? ????
13?????? ??? ?????
- ?? ????? ??? ????? ?? ????? ?? ????
- element newArray
element.length 15
14?????? ??? ?????? ????? ???? ????
- // create a new array of proper length and data
type - Object newArray new Object newLength
// copy all elements from old array into new one
System.arraycopy(element, 0, newArray,
0, element.length)
// rename array element newArray
15??? ????? ???? ???? ???? ?????
- ????? ??? ????? ?? ????? ????!
- ????? ?????? ??? ????? ????? ??? ?? theta(M) ? ??
?? ?? M ??? ?????? ??? ??? - ????? n ??? ?????? ??? ?? ????? ???? ?? ?????
theta(n2) ???
16????? ???? ????
newArray new char7
space needed 2 newLength 1
17??? ???? ??????? ??? ????
newArray new char12
????? ?????? n ??? ???? ?? ????? theta(n)
???? ????? ???? ???? 1.5 ??? ???? 3 ???
????
18??? ????? ???? ???? ???? ?????
- ??? ??? ???? ????? ?? ??? ???? ????
- new length c old length
- ?????? n ??? ????? ?? ?? ????? Theta(n) ?????
????
??? ??? ???? ??? ???? ????? ????? ????
new length c old length ?????? n ??? ?????
?? ?? ????? Theta(n2) ????? ????
19??? ????? ???? ???? ???? ?????
- ??? ??? ???? ????? ?? ??? ???? ????
- new length c old length
- ????? ???? ???? ????? ??? ??
- (1c) (old length)
- ??? ??? ???? ????? ??? ???? ????? ????? ?????
????? ???? ???? ????? ??? ?? - (old length) (old length c) 2 (old
length) c space.
20?????? ??? ???? ?? ????
- java.util.Vector array doubling
- java.util.ArrayList c 1.5
21 ????? ???? ????? ??? ?? ???? ?? Java, C, C
a
b
c
d
- 1-dimensional array x a, b, c, d
- map into contiguous memory locations
22????? ???? ???? ???? ??????? ??????? ???? Overhead
- space overhead 4 bytes for start
- 4 bytes for
x.length - 8 bytes
23????? ??? ?? ????
- ????? ????? ?? ????
- int a new int34
- may be shown as a table
- a00 a01 a02 a03
- a10 a11 a12 a13
- a20 a21 a22 a23
24?????? ????? ?? ????
- a00 a01 a02 a03
row 0 - a10 a11 a12 a13
row 1 - a20 a21 a22 a23
row 2
25???? ??? ????? ?? ????
- a00 a01 a02 a03
- a10 a11 a12 a13
- a20 a21 a22 a23
26 ????? ???? ????? ??? ?? ???? ?? Java, C, C
2-dimensional array x a, b, c, d e, f, g, h i,
j, k, l
- ????? ?? ????? ????? ?? ????? ??? ???? ???
- x row0, row1, row 2
- row 0 a,b, c, d
- row 1 e, f, g, h
- row 2 i, j, k, l
27 ????? ???? ????? ??? ?? ???? ?? Java, C, C
- x.length 3
- x0.length x1.length x2.length 4
28????? ???? ???? ????
- space overhead overhead for 4 1D arrays
- 4 8 bytes
- 32 bytes
- (number of rows
1) x 8 bytes
29 ????? ???? ????? ??? ?? ???? ?? Java, C, C
- ????? ?? ?? ????? ??
- ?????? ?? ????? ?? ??? ??? 3?4?4?4 ???? ???? ???
- ?? ??? ?????? ???? ??????? ????? ???? ? ?? ????
?????? ???? ??????? ????? ???? ???? ???
30????? ???? Row-Major Mapping
- ????????? 3 4 x
- a b c d
- e f g h
- i j k l
- ????? ??? ?? ????? ?????? ????? ?? ???
- ?????? ????? ?? ???? ?? ?? ?? ???? ? ?? ???? ??
????? ?? ??? ????? ???? ?? ????? - ????? ?? ????? y a, b, c, d, e, f, g, h,
i, j, k, l
???? ??? ?????????? ?????? ????? ?? ????? ????
???? ???
31 ????? xij
- ??? ???? x? ????? r ??? ? c????? ???
- ?? ??? c ??? ????
- ??? ?? ??? i ?? ????? i???? ???? ?????(???? ??
???) - ???????? xij?? ic j? ?? ????? 1 ????
????? ?? ???
32Space Overhead
- 4 bytes for start of 1D array
- 4 bytes for length of 1D array
- 4 bytes for c (number of columns)
- 12 bytes
- (number of rows length /c)
33Column-Major Mapping
- a b c d
- e f g h
- i j k l
- ????? ????? ?? ???? ? ???? ?? ???? ?? ?? ?? ????
? ?? ???? ?? ????? ?? ????? ?? ???? ???? ?? ????? - y a, e, i, b, f, j, c, g, k, d, h, l
- ???? ????? ???? ?? ????
34?????? Matrix
- ?????? ???? ????? ?? ?????? ?? ??? ????? ????.
????? ??? ????? ?? 1 ???? ?? ??? - a b c d row 1
- e f g h row 2
- i j k l row 3
- ?? x(i,j) ???? xij. ??????? ?? ???
- ?? ???? ?? ????? ?? ???? ???? ????? ??????
??????? ???
35????? ??????? ?? ????? ?? ???? ???? ????? ??????
- ???? ????? ????? ?? ???
- ????? ??? ???? ?? ????? ???? ?????? ?? ????? ???
? ??? ? ???????? ???? ???????? ??? ???? - ???? ??? ?????? ???? ???? ?? ???? ????? ??????
????? ???
36?????? ????
- 1 0 0 0
- 0 2 0 0
- 0 0 3 0
- 0 0 0 4
?????? ????? ??? ?? ????? ??? ??? ???? ?? ???
?????
- x(i,j) is on diagonal iff i j
- number of diagonal elements in an n x n matrix is
n - non diagonal elements are zero
- store diagonal only vs n2 whole
37?????? ????? ?????
- ?????? ????? ????? ?????? ????? ?? ??? ?? ?????
??? ???? ? ??? ?? ???? ???? ??? ?????
1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10
- ????? ????? ??? ??? ?????? ????? ????? 1 2
n n(n1)/2. - ????? ???? ????? ?? ???? ?? ??? ????
38????? ???? ?? ????? ?? ?? ????? ??
- ????? ?????? ?????? ????? ???? ?? ??? ??? ?????
???? ??????? ????
39????? ????? ??? ?????? ?? ????
- // declare a two-dimensional array variable
- // and allocate the desired number of rows
- int irregularArray new int
numberOfRows - // now allocate space for the elements in
each row - for (int i 0 i lt numberOfRows i)
- irregularArrayi new int sizei
- // use the array like any regular array
- irregularArray23 5
- irregularArray46 irregularArray23
2 - irregularArray11 3
40??????? ?? ?? ????? ?? ????
- ?? ??? ???? ???? ???? ???? ??????? ?? ??? ???
????? ??? ??? ?? ???? - 1 0 0 0
- 2 3 0 0
- 4 5 6 0
- 7 8 9 10
- 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
-
41????? ??? ij
- ????? row 1, row 2, row 3,
- ??? ?? ??? i ?????? 1, 2, , i-1 ???? ?????
- ??? ??? i ????? i. ???
- ????? ?????? ?? ??? ?? ??? i ???? ?????
- 1 2 3 i-1 i(i-1)/2
- ?? ??? (i,j) ?? ???? i(i-1)/2 j -1 ????? 1
???? ???? ????.
42?????? ???? Sparse Matrices
- ?????? ????????? ???? ?????
- ?????? ?????? ????? ?????? ?????
- sparse many elements are zero
- dense few elements are zero
- ???? ?????? ??? ????? ?????? ??? ????? ?????
- ?? ???? ?? ????? ?? ???? ???? ????? ???? ???????
??? - ??? ??? ?????? ??? ????? ?? ?????? ???
??????????? ???????
43?????? ???? ???? ?????? ?????
- ???? ???? ????? ??.
- ????????? ?? 1 ?? n? ????? ????? ??? ???
- flight(i,j) ???? ????? ????? ???????? ??????
??? ??? ??????????? - ??? ???? n 1000
- n x n array of list references gt 4 million bytes
- ??? ????? ????? ??????? 20,000 ????.
- ?????? 20000 ????? ????? ?? ???
44?????? ???? ???? ?????? ?????
- ?????? ????? ??.
- ????? ?? 1 ?? n ????? ????? ??? ???
- ????? ?????? ?? ???? i??? ???? web(i,j) j
- ?????? ??.
- authority page page that has many links to it
- hub page links to many authority pages
45?????? ????? ??
- n 2 billion (and growing by 1 million a day)
- n x n array of ints gt 16 1018 bytes (16 109
GB) - ?? ??? ????? ?? ???? ?? 10 ???? ???? ???? ????
- ?? ?? ?? ??? ????? ?? ??? ????? 10 ??? ??? ???
???? ???? - ???? ???? ???? ???? ??????? ????? ??? ??? ?????
??? ?? 20 billion x 4 bytes 80 billion bytes
(80 GB)
46????? ?????? ???? ???? ??????
- ????? ?? ???? ?? ???? ????
- ?? ??? ??? ??? ?? ?? ???? (row, column, value)
????? ?? ??? - ??? ?? ???? ?? ?? ???? ?? ????? ??? ?? ???? ??
???? ?????? ?? ?? ????? ???? ???
47????? ?? ??????? ?? ???? ???
- 0 0 3 0 4
- 0 0 5 7 0
- 0 0 0 0 0
- 0 2 6 0 0
48????? ?? ??????? ?? ???? ???
- row 1 1 2 2 4 4
- list column 3 5 3 4 2 3
- value 3 4 5 7 2 6
-
49????? ?? ?????? ?? ?? ???????
50?? ??????
- row 1 1 2 2 4 4
- list column 3 5 3 4 2 3
- value 3 4 5 7 2 6
51?? ??? ?? ?? ???? ???????
row1 (3, 3),
(5,4) row2 (3,5), (4,7) row3 row4
(2,2), (3,6)
-
- 0 0 3 0 4
- 0 0 5 7 0
- 0 0 0 0 0
- 0 2 6 0 0
52?????? ?? ?? ?????
53????? ?? ?? ?????? ?????
- 0 0 3 0 4
- 0 0 5 7 0
- 0 0 0 0 0
- 0 2 6 0 0
null
54????? ?? ??????? ?????? ???? ? ?????
55???? ?????
- 0 0 3 0 4
- 0 0 5 7 0
- 0 0 0 0 0
- 0 2 6 0 0
56???? ???? ??
- 0 0 3 0 4
- 0 0 5 7 0
- 0 0 0 0 0
- 0 2 6 0 0
57Orthogonal Lists
- 0 0 3 0 4
- 0 0 5 7 0
- 0 0 0 0 0
- 0 2 6 0 0
58????? ???? ????
- 500 x 500 matrix with 1994 nonzero elements
- 2D array 500 x 500 x 4 1million
bytes - Single Array List 3 x 1994 x 4 23,928 bytes
- One Chain Per Row 23928 500 x 4 25,928
59???? ????
- Matrix Transpose
- 500 x 500 matrix with 1994 nonzero elements
- 2D array 210 ms
- Single Array List 6 ms
- One Chain Per Row 12 ms
60???? ????
- Matrix Addition.
- 500 x 500 matrices with 1994 and 999 nonzero
elements - 2D array 880 ms
- Single Array List 18 ms
- One Chain Per Row 29 ms
61????? ????? ?????? ????
- ?? ADT ???? ????? ?????? ??? ???? ?? ??????? ??
???? ??? ??? ??????? ?? ?? ?? ?? ??? ??? ??? ??
?? ???? (row, column, value) ???? ???? ?? ???. - ??? ??? ?? ?????? ?? ???? ??? ??? ????? ?? ????
??? ?? ??????? - ????? ?? ??????? ds-cs-sparse ? ??
ds-math-sparse