1) The document discusses linear arrays and their representation in memory. A linear array stores elements in consecutive memory locations.
2) The number of elements in an array is given by the length formula: Length = Upper Bound - Lower Bound + 1. The address of each element can be calculated using the base address of the array.
3) Common array operations like traversing, inserting, and deleting elements are discussed along with algorithms to perform each operation. Representing polynomials and sparse matrices using arrays is also covered.
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
Chapter-3 Linear Data Structure - Arrays
1. Chapter-3
Linear Data Structure - Arrays
MRS.SOWMYA JYOTHI, SDMCBM, MANGALORE
Reference: Data Structures with C by Seymour Lipschutz,
Schaum’s Outlines Series.
2. INTRODUCTION
• Data structures are classified as either linear or nonlinear.
• A data structure is said to be linear, if its elements form a sequence.
There are two basic ways of representing linear structures in memory.
1. Using arrays.
2. Using linked lists.
3. • LINEAR ARRAYS
A linear array is a list of a finite number N of homogeneous data elements (i.e.
data elements of the same type) such that:
a) The elements of the array are referred respectively by an index set consisting
of n consecutive numbers
b) The elements of the array are stored respectively in successive memory
locations.
The number n of elements is called the length or size of the array. If not explicitly
stated, we will assume the index set consists of the integers 1, 2, …. N.
In general, the length or the number of data elements of the array can be
obtained from the index set by the formula
Length=UB – LB + 1
Where UB is the largest index, called the upper bound, and LB is the smallest
index, called the lower bound of the array. (Length= UB when LB=1).
4. The elements of an array A may be denoted by the subscript notation
A1, A2, A3, ….. An
OR by the bracket notation A[1], A[2], A[3], ….. A[n]
The number K in A[K] is called a subscript or an index and A[K] is called
a subscripted variable.
5. Example
• Let DATA be a 6-element linear array of integers such that DATA[1]=247
DATA[2]=56 DATA[3]=429 DAT[4]=135 DATA[5]=87 DATA[6]=156.
• We will denote such an array by DATA: 247, 56, 429, 135, 87, 156
6. Problems:
1) Consider the linear array LA(1:10).
Find the number of elements in an array.
Ans: The number of elements is equal to the length; hence the
formula
Length= UB – LB + 1
Length(LA) = 10 – 1 + 1
= 10
7. 2) Consider the linear array AA(5:50), BB(-5:10) and CC(18). Find
the number of elements in each array.
Ans:
Length= UB – LB + 1
a) Length(AA) = 50 – 5 + 1
= 46
b) Length(BB) = 10 – (-5) + 1
= 16
c) Length(CC) = 18 – 1 + 1
= 18
8. Abstract Data Types (ADT)
• An abstract data type refers to a set of data values and associated
operations that are specified accurately, independent of
implementation.
• With an ADT, it is possible to know what a data type does, but how it
does is hidden.
• An ADT can further be defined as a data declaration packaged
together with the operations that are meaningful for the data. User
may not have the knowledge of data structure.
9. ARRAY AS ADT
• An array is a fundamental Abstract Data Type. An array is pre-
defined set which has a sequence of cells where we can store
different types of elements.
• Consider the following example: object (A, N). Here an array A is
created which can store N number of items in it.
• A[i] is an item in the array stored in its ith position.
10. • REPRESENTATION OF LINEAR ARRAYS IN MEMORY
• Let LA be a linear array in the memory of the computer. The notation
LOC(LA[K])=address of the element LA[K] of the array LA.
• Let LA[10]={5, 6, 12, 4, 15, 45, 87, 1, 9, 13} and Base address be 100.
11. • The elements of linear array LA are stored in successive
memory locations.
• Computer does not need to keep track of the address of
every element of LA, but it needs the base address Base(LA).
• Using the address of Base(LA), the computer calculates the
address of any element of LA by the following formula:
• LOC(LA[K]) = Base(LA) + w(K – Lower Bound)
Where w is number of words per memory cell for the array LA
12. Problems:
1) Consider the linear array AA(1:10).
Suppose Base(AA)=100 and w=4 words per memory cell
for AA.
Find the address of LA[5].
Ans:
LOC(AA[K]) = Base(AA) + w(K-LB)
LOC(AA[5]) = 100 + 4 (5-1)
= 100 + 16
= 116
13. 2) Consider the linear array AA(5:50).
Suppose Base(AA)=300 and w=4 words per memory cell for AA.
Find the address of a) AA[15], b) AA[35] and c) AA[55].
LOC(AA[K]) = Base(AA) + w(K-LB)
a) LOC(AA[15]) = 300 + 4 (15-5)
= 300 + 40
= 340
b) LOC(AA[35]) = 300 + 4 (35-5)
= 300 + 120
= 420
c) AA[55] is not an element of AA, since 55 exceeds UB=50.
14. TRAVERSING A LINEAR ARRAY
• Let LA be the collection of data elements stored in the memory of the
computer. Suppose we want to print the contents of each element of
LA or suppose we want to count the number of elements of LA with
each given property.
• This can be accomplished by traversing A, that is, by accessing and
processing each element of the array exactly once is called
traversing.
15. • Algorithm 4.1: (Traverse a Linear Array) Here LA is a Linear array with
lower boundary LB and upper boundary UB. This algorithm traverses
LA applying an operation Process to each element of LA.
1. [Initialize counter] Set K:=LB.
2. Repeat Steps 3 and 4 while K≤UB.
3. [Visit element] Apply PROCESS to LA[K].
4. [Increase counter] Set K:=K+1.
[End of Step 2 loop]
5. Exit.
16. The alternate algorithm for traversing (using for loop) is:
Algorithm 4.1: (Traverse a Linear Array) This algorithm traverse a linear
array LA with lower bound LB and upper bound UB.
1. Repeat for K=LB to UB
Apply PROCESS to LA[K].
[End of loop]
2. Exit.
17. Problems:
1) Suppose a 5-element array A contains the values {25, 32, 65, 43, 78}.
Find the values of the given arrays after each loop.
a) Repeat for K = 1 to 4:
Set AB[K + 1] := A[K]
[End of loop]
Ans:
First AB[2] := A[1] sets AB[2]=25, the value of A[1]
Then AB[3] := A[2] sets AB[3]=32, the value of A[2]
Then AB[4] := A[3] sets AB[4]=65, the value of A[3]
Then AB[5] := A[4] sets AB[5]=43, the value of A[4]
18. Suppose a 5-element array A contains the values {25, 32, 65, 43, 78}.
b) Repeat for K = 4 to 1 by -1:
Set A[K + 1] := A[K]
[End of loop]
Ans:
First A[5] := A[4] sets A[5]=78
Then A[4] := A[3] sets A[4]=43
Then A[3] := A[2] sets A[3]=65
Then A[2] := A[1] sets A[2]=32
19. INSERTING
• Let LA be a collection of data elements in the memory of the
computer.
• “Inserting” refers to the operation of adding another
element to the collection LA.
• Always Insertion is done at the last.
• Suppose an element is to be inserted in the middle of the
array. Then, half of the elements must be moved downwards
to new locations to accommodate the new element and
keep the order of the other elements.
20. Algorithm 4.2: (Inserting into a Linear Array) INSERT (LA, N, K, ITEM) Here
LA is a linear array with N elements and K is a positive integer such that
K<=N. the algorithm inserts an element ITEM into Kth position in LA.
1. [Initialize counter] Set J:=N
2. Repeat Steps 3 and 4 while J>=K
3. [Move Jth element downward] Set LA[J+1]:=LA[J]
4. [Decrease the counter] Set J:=J-1.
[End of Step 2 Loop]
5. [Insert the element] Set LA[K]:=ITEM
6. [Reset N] Set N:=N+1
7. Exit
22. DELETING
• Let LA be a collection of data elements in the memory of the
computer.
• “Deleting” refers to the operation of removing one of the
elements from LA.
• Deleting an element at the end of an array presents no
difficulties, but deleting an element somewhere in the
middle of the array would require that each subsequent
element be moved one location upward in order to “fill up”
the array.
• The following algorithm deletes the Kth element from the
linear array LA and assigns it to the variable ITEM.
23. Algorithm 4.3: (Deleting from a Linear Array)
DELETE (LA, N, K, ITEM). Here LA is a linear array with N
elements and K is a positive integer such that K<=N. The
algorithm deletes Kth element from LA.
1. Set ITEM:=LA[K]
2. Repeat For J=K to N-1
[Move J+1st element upward] Set LA[J]:=LA[J+1]
[End of Loop]
3. [Reset the number N of elements in LA] Set N:=N-1.
4. Exit
25. REPRESENTATION OF POLYNOMIALS USING ARRAYS
•Sometimes it may be necessary to evaluate several
polynomial expressions to perform basic arithmetic
operations. The polynomial expressions can be stored
in arrays. All the elements of an array has two values
namely coefficient and exponent.
• Once a polynomial is built, it can be used to perform
any arithmetic operations.
• A single dimensional array is used to represent a
single variable polynomial of degree n. the index can
be considered as exponent and the coefficients can be
stored at that particular location.
26.
27.
28. SPARSE MATRIX
• A matrix with a relatively high proportion of zero entries is called
sparse matrix. Two general types of n-square sparse matrices
which occur in various applications are shown below.
1. The matrix where all the entries above the main diagonal are
zeros, non-zero entries may occur below the main diagonal is
called lower triangular matrix.
2. The matrix where all the entries below the main diagonal are
zeros, non-zero entries may occur above the main diagonal is
called upper triangular matrix.
3. The matrix where non-zero entries occur on the diagonal or on
elements immediately above or below the diagonal, is called
tri-diagonal matrix.