CHAPTER 1 BASIC CONCEPT · 2016. 10. 20. · d Row-Major: Column-Major: name m row, n column,...
Transcript of CHAPTER 1 BASIC CONCEPT · 2016. 10. 20. · d Row-Major: Column-Major: name m row, n column,...
NCUE CSIE Wireless Communications and Networking Laboratory
1
CHAPTER 2
ARRAYS
NCUE CSIE Wireless Communications and Networking Laboratory
Arrays
Array A
1 2 3 ……. n
2
Definition:
(1)A set of index and value
(2)Using consecutive memory
(3)Support “Random Access ” and “Sequential Access”
(4) Insert/Delete an element: O(n)
NCUE CSIE Wireless Communications and Networking Laboratory
int list[5], *plist[5];
list[5]: five integers
list[0], list[1], list[2], list[3], list[4]
*plist[5]: five pointers to integers
plist[0], plist[1], plist[2], plist[3], plist[4]
implementation of 1-D array
list[0] base address =
list[1] + sizeof(int)
list[2] + 2*sizeof(int)
list[3] + 3*sizeof(int)
list[4] + 4*size(int) 3
Arrays in C
0l
0l0l
0l
0l
NCUE CSIE Wireless Communications and Networking Laboratory
Compare int *list1 and int list2[5] in C.
Same: list1 and list2 are pointers.
Difference: list2 reserves five locations.
Notations:
list2 - a pointer to list2[0]
(list2 + i) - a pointer to list2[i]=(&list2[i])
4
Arrays in C (Continued)
NCUE CSIE Wireless Communications and Networking Laboratory
Addressing (1-Dimension Arrays)
Declare: A[1~n]
suppose : start address : size of each element
Then
name
Start index ended index
size=n
0l
A
1 2 3 4 5 ….. n
diliA )1(][ 0
d
5
NCUE CSIE Wireless Communications and Networking Laboratory
Addressing (1-Dimension Arrays)
(General case)
Declare: A[l~u]
suppose : start address : size of each element
Then
name
Start index ended index
size=u-l+1
0l
A
l l+1 l+2 l+3 l+4 ….. u
dliliA )(][ 0
d
6
NCUE CSIE Wireless Communications and Networking Laboratory
Addressing (2-Dimension Arrays)
Declare: A[1~m, 1~n] suppose : start address : size of each element Row-Major: Column-Major:
name m row, n column, size=m*n
0l
djniljiA ))1()1((],[ 0
dimjljiA ))1()1((],[ 0
d
7
NCUE CSIE Wireless Communications and Networking Laboratory
Addressing (2-Dimension Arrays)
(General case)
Declare: A[l1~u1, l2~u2] suppose : start address : size of each element Row-Major: Column-Major:
name (u1-l1+1) row, (u2-l2+1) column
0l
dljluliljiA ))()1()((],[ 22210
dliluljljiA ))()1()((],[ 11120
d
8
NCUE CSIE Wireless Communications and Networking Laboratory
Addressing (3-Dimension Arrays)
Declare: A[1~u1, 1~u2, 1-u3] suppose : start address : size of each element Row-Major: Column-Major:
0l
dkujuuilkjiA ))1())1(())1(((],,[ 3320
diujuuklkjiA ))1())1(())1(((],,[ 1120
2u
3u1u
d
9
NCUE CSIE Wireless Communications and Networking Laboratory
Addressing (3-Dimension Arrays)
(General case)
Declare: A[l1~u1, l2~u2, l3-u3] suppose : start address : size of each element Row-Major: Column-Major:
0l
)1()1()((],,[ 332210 lululilkjiA
122 lu
133 lu 111 lu
Row-major
column-major
dlklulj ))()1()( 3332
)1()1()((],,[ 112230 lululklkjiA
dlilulj ))()1()( 1112
d
10
NCUE CSIE Wireless Communications and Networking Laboratory
Addressing (n-Dimension Arrays)
Declare: A[1~u1, 1~u2,… 1-un] suppose : start address : size of each element Row-Major:
0l
)...)1(((]...,[ 321021 nn uuualaaaA
)...)1(( 432 nuuua
)...)1(( 543 nuuua
dan ))1(
Row-major
d
11
NCUE CSIE Wireless Communications and Networking Laboratory
Addressing (n-Dimension Arrays)
Declare: A[1~u1, 1~u2,… 1-un] suppose : start address : size of each element Column-Major:
0l
)...)1(((]...,[ 121021 uuualaaaA nnnn
)...)1(( 1321 uuua nnn
)...)1(( 1432 uuua nnn
da ))1( 1
column-major
d
12
NCUE CSIE Wireless Communications and Networking Laboratory
0002800
0000091
000000
006000
0003110
150220015
col1 col2 col3 col4 col5 col6
row1
row2
row3
row4
row5
row6
(a) (b)
8/36 15/15
sparse matrix data structure?
Sparse Martix
13
5*3 6*6
NCUE CSIE Wireless Communications and Networking Laboratory
Sparse Martix
14
Represent:
(1) Represented by a two-dimensional array
→ Sparse matrix wastes space
(2) 3-Tuple (Triples)
→ Each element is characterized by <row, col, value>
(3) Double Link-list
→We will discuss it in chapter 4
NCUE CSIE Wireless Communications and Networking Laboratory
Triples
row col value
[0] 6 6 8
[1] 1 1 15
[2] 1 4 22
[3] 1 6 -15
[4] 2 2 11
[5] 2 3 3
[6] 3 4 -6
[7] 5 1 91
[8] 6 3 28
0002800
0000091
000000
006000
0003110
150220015
# of nonzero terms = k # of columns = n
# of rows = m
row1
row2
row3
row4
row5
row6
col1 col2 col3 col4 col5 col6
Row-major
15
NCUE CSIE Wireless Communications and Networking Laboratory
Transposing a Matrix
t
mnnm AA t
jiij AA
Ex)
23
81
70
287
310
16
NCUE CSIE Wireless Communications and Networking Laboratory
Transposing a Matrix (Algorithm 1)
For i=0 to k put A[i, j, value] into At[j, i, value] End
row col value [0] 6 6 8 [1] 1 1 15 [2] 4 1 22 [3] 6 1 -15 [4] 2 2 11 [5] 3 2 3 [6] 4 3 -6 [7] 1 5 91 [8] 3 6 28
It’s not row-major
17
NCUE CSIE Wireless Communications and Networking Laboratory
row col value [0] 6 6 8 [1] 1 1 15 [2] 1 5 91 [3] 2 2 11 [4] 3 2 3 [5] 3 6 28 [6] 4 1 22 [7] 4 3 -6 [8] 6 1 -15
Transposing a Matrix (Algorithm 2)
For j=1 to n do For i=1 to k do if (A[i].col=j) then put A[i, j, value] into At[j, i, value] End End
→ when it’s not “sparse” anymore… 18
Time Complexity: O(n k)
where k m n
NCUE CSIE Wireless Communications and Networking Laboratory
Compared with matrix
transport using 2-D array
For i=1 to m do For j=1 to n do put A[i, j] into At[j, i] End End
0002800
0000091
000000
006000
0003110
150220015
Time Complexity: O(m n)
19
NCUE CSIE Wireless Communications and Networking Laboratory
Transposing a Matrix (Algorithm 3)
Step1: For i=1 to k do column_element[A[i].col]++ End
Step2: For i=2 to n do row_start[i]=row_start[i-1]+column_element[i-1] End Step3: For i=1 to k do At[row_start[A[i].col]]=A[i] row_start[A[i].col]++ End
Time Complexity: O(k+n) or O(Max(k, n))
2 1 2 2 0 1 column_element
1 2 3 4 5 6
1 3 4 6 8 8 Row_start
1 2 3 4 5 6
20
NCUE CSIE Wireless Communications and Networking Laboratory
Lower Triangular Matrix (row-major)
0
nn
Store: count=1 For i=1 to n do For j=1 to i do B[count]=A[i, j] count++ End End
B
1 2 3 4 5 ….. n(n+1)/2
(1,1) (2,1) (2,2) (3,1) (3,2) (n,n)
Access: if (i<j) then return 0 else return B[k]
jii
k
2
)1(
An n, Aij=0 if i<j
21
NCUE CSIE Wireless Communications and Networking Laboratory
Lower Triangular Matrix (column-major)
An n, Aij=0 if i<j
B
1 2 3 4 5 ….. n(n+1)/2
(1,1) (2,1) (3,1) (4,1) (n,n)
0
ijj
jn
ijjn
jijjn
2
)1()1(
2
)1)(2(
)1(2
)1)(22(
Access:
22
NCUE CSIE Wireless Communications and Networking Laboratory
Upper Triangular Matrix
Row-major:
Column-major:
jii
ink
2
)1()1(
ijj
k
2
)1(
23
NCUE CSIE Wireless Communications and Networking Laboratory
Symmetric Matrix
An n, Aij= Aji
24
Definition:
Another ex.: Band Matrix An,a,b
EX:
NCUE CSIE Wireless Communications and Networking Laboratory
A[-4~3, -3~2], l0=100, d=1 (a) If Row-major,then A[1, 1]=? (b) If Column-major,then A[1, 1]=?
Question:
25
NCUE CSIE Wireless Communications and Networking Laboratory
A[-2~7, -4~10, -2~1, -3~2, 1~10], l0=38, d=8 (a) If Row-major,then A[0, 8, 0, 1, 8]=?
Question:
26
NCUE CSIE Wireless Communications and Networking Laboratory
Suppose there is an integer array M[5, 8], the address of M[0, 0], M[0, 3], and M[1, 2] are 100, 106, and 120 respectively. What is the address of M[4, 5]
Question:
27
NCUE CSIE Wireless Communications and Networking Laboratory
Assume in a byte machine. A is an array declared as A[-1~m, 2~n], and each element occupied 3 bytes. The address of A[3, 5] is at 180, and A[5, 3] is at 138 (1) Find the address of the element A[-1, 2] (2) Find the value of m or n
Question:
28
NCUE CSIE Wireless Communications and Networking Laboratory
A[3, 3]=121, A[6, 4]=159, d=1, then A[4, 5]=?
Question:
29
NCUE CSIE Wireless Communications and Networking Laboratory
A[1, 1]=2, A[2, 3]=18, A[3, 2]=28, then A[4, 5]=?
Question:
30
NCUE CSIE Wireless Communications and Networking Laboratory
Reference
31
Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed
〝Fundamentals of Data Structures in C〞, W. H. Freeman & Co
Ltd, 1992.
Ellis Horowitz, Sartaj Sahni, and Dinesh Mehta
〝Fundamentals of Data Structures in C++〞 Silicon Pr, 2006
Richard F.Gilberg, Behrouz A. Forouzan,
〝Data Structures: A Pseudocode Approach with C〞, S
Baker & Taylor Books, 2004
Fred Buckley, and Marty Lewinter 〝A Friendly Introduction to Graph
Theory〞 Prentice Hall, 2002
〝資料結構-使用C語言〞蘇維雅譯,松崗,2004
〝資料結構-使用C語言〞 蔡明志編著,全華,2004
〝資料結構(含精選試題)〞洪逸編著,鼎茂,2005