L1-Linear Data Structure
L1-Linear Data Structure
What is Complexity?
The level of difficulty in solving mathematical
problems are measured by
The time
(time complexity)
memory space required
(space complexity)
Complexity Analysis
Algorithm analysis means predicting resources such as
computational time
memory
Worst case analysis
Provides an upper bound on running time
Average case analysis
Provides the expected running time
Very useful, but treat with care
Random (equally likely) inputs
Real-life inputs
Comment
Constant
O(1)
Log log
O(loglogN)
Extrapolation search
Logarithmic O(logN)
Linear
O(N)
N logN
O(NlogN)
Quadratic
O(N2)
Cubic
O(N3)
Polynomial time
Increasing cost
Name
y = x2 + 3x + 5, for x=1..10
y = x2 + 3x + 5, for x=1..20
Asymptotic Notations
Asymptotic Notations , O, , o,
We use to mean order exactly,
O to mean order at most,
to mean order at least,
o to mean tight upper bound,
to mean tight lower bound,
upper
bound
for
the
function f
c*g(n)
n0
Definition:
f(n)
f(n)
Running Time
O(g(n))
if
positive
Input size N
(g(n))
if
positive
c*g(n)
n0
Definition:
f(n)
f(n)
Running Time
or (n),
Input size N
c2*g(n)
Running Time
The
f(n)
c1*g(n)
n0
(g(n))
if
positive
Input size N
Relations Between , O,
Little-Oh Notation
o-notation is used to denote a upper bound that is not
asymptotically tight.
For a given function g n 0, denoted by o g n the set of functions,
f n
lim
0
n g n
Little-Omega Notation
Little- notation is used to denote a lower bound
that is not asymptotically tight.
f n
lim
n g n
Comparison of Functions
fg ab
f (n) = O(g(n)) a b
f (n) = (g(n)) a b
f (n) = (g(n)) a = b
f (n) = o(g(n)) a < b
f (n) = (g(n)) a > b
Review:
Other Asymptotic Notations
three types.
1.
Worst case
2.
Average Case
3.
Best Case
Time Complexity
Worst Case: It is the longest time that an algorithm will use
the algorithm will use over all instances of size n for a given
problem to produce a desired result. It depends on the
probability distribution of instances of the problem.
Best Case: It is the shortest time ( or least space ) that the
Method
Worst Case
Average Case
Selection sort
n2
n2
Insertion sort
n2
n2
Merge sort
n log n
n log n
Quick sort
n2
n log n
Space Complexity
Space Complexity of a program is the amount of memory
following:
A fixed amount of memory occupied by the space for the program code
and space occupied by the variables used in the program.
complexity
If memory available is large then we need not compensate
on time complexity
If speed of execution is not main concern and the memory
machine instructions.
In C++ language, the different primitive data structures are int,
machine instructions.
Arrays, linked lists, files etc., are some of non-primitive data
structures and are classified into linear data structures and nonlinear data structures.
Major operation
Traversing: Accessing
Major operation
Inserting : Adding a new element to the list
Deleting : Removing a element from the list
Sorting : Arranging the elements in some type of
add
delete
find
destroy
ARRAY
Linear Arrays
A linear array is a list of a finite number of n
Linear Arrays
The number n of elements is called the length or
Linear Arrays
Element of an array A may be denoted by
Subscript notation A1, A2, , . , An
Parenthesis notation A(1), A(2), . , A(n)
Bracket notation A[1], A[2], .. , A[n]
1000
1001
1002
1003
1004
1005
:
Computer Memory
array LA
The element of LA are stored in the successive memory
cells
Computer does not need to keep track of the address of
Example 1
Find the address for LA[6]
Each element of the array
occupy 1 byte
200
LA[0]
201
LA[1]
202
LA[2]
203
LA[3]
204
LA[4]
205
LA[5]
206
LA[6]
207
LA[7]
Example 2
Find the address for LA[15]
Each element of the array
occupy 2 bytes
200
LA[0]
201
202
LA[1]
203
204
LA[2]
205
206
LA[3]
207
LOC(LA[K]) = Base(LA) + w(K lower bound)
LOC(LA[15]) = 200 + 2(15 0) = 230
the
Linear Array
each
Linear Array
Linear Array
Linear Array
Linear Array
Linear Array
1. Repeat for K = LB to UB
Apply PROCESS to LA[K]
[End of Loop]
2. Exit
for( i=0;i<size;i++)
printf(%d,LA[i]);
Insertion
1
Brown
Brown
Davis
Davis
Johnson
Johnson
Smith
Smith
Wagner
Wagner
Ford
Insertion
1
Brown
Brown
Brown
Brown
Davis
Davis
Davis
Davis
Johnson
Johnson
Smith
Smith
Johnson
Ford
Wagner
Johnson
Wagner
4
5
Smith
Smith
Wagner
Wagner
Insertion Algorithm
INSERT (LA, N , K , ITEM) [LA is a linear array with N
Deletion
1
Brown
Brown
Davis
Davis
Ford
Ford
Johnson
Johnson
Smith
Smith
Taylor
Taylor
Wagner
Deletion
1
Brown
Davis
Ford
Ford
Johnson
Johnson
Johnson
Smith
Smith
Taylor
Smith
Taylor
Wagner
Taylor
Wagner
Wagner
Brown
Brown
Ford
Brown
Ford
Johnson
4
5
Smith
Taylor
Wagner
Deletion
1
Brown
Ford
Johnson
Smith
Taylor
Wagner
7
8
Deletion Algorithm
DELETE (LA, N , K , ITEM) [LA is a linear array with N
Multidimensional Array
One-Dimensional Array
Two-Dimensional Array
Three-Dimensional Array
Two-Dimensional Array
A Two-Dimensional m x n array A is a collection of
2D Arrays
The elements of a 2-dimensional array a is shown as
below
a[0][0]
a[1][0]
a[2][0]
a[0][1]
a[1][1]
a[2][1]
a[0][2]
a[1][2]
a[2][2]
a[0][3]
a[1][3]
a[2][3]
Rows of a 2D Array
a[0][0]
a[1][0]
a[2][0]
a[0][1]
a[1][1]
a[2][1]
a[0][2]
a[1][2]
a[2][2]
a[0][3]
a[1][3]
a[2][3]
row 0
row 1
row 2
Columns of a 2D Array
a[0][0]
a[1][0]
a[2][0]
a[0][1]
a[1][1]
a[2][1]
column 0
a[0][2]
a[1][2]
a[2][2]
a[0][3]
a[1][3]
a[2][3]
2D Array
Let A be a two-dimensional array m x n
The array A will be represented in the memory by
Fortran, MATLAB
Row by Row
Java
2D Array in Memory
A
Subscript
Subscript
(1,1)
(1,1)
(2,1) Column 1
(3,1)
(1,2)
(1,2)
(2,2)
Column 2
(1,3)
Column 3
(2,3)
(3,3)
(1,4)
(1,3)
(1,4)
(2,1)
(3,2)
Row 1
(2,2)
Row 2
(2,3)
(2,4)
Column 4
(2,4)
Column-Major
Order
(3,4)
(3,1)
(3,2)
(3,3)
Row-Major Order
Row 3
2D Array
Column-Major Order
Jth Row
Kth
Column
A[J][K]
Jth Row
Kth
Column
A[J][K]
2D Array Example
Consider a 25 x 4 array A.
Suppose the Base(A)=200 and w =2.
Suppose the programming stores 2D array using
row-major.
Compute LOC(A[12,3])
LOC(A[J,K]) = Base(A) + w[n(J-LB) + (K-LB)]
Multidimensional Array
An n-dimensional m1 x m2 x . x mn array B is a collection of
BK1,K2, ,Kn
or B[K1,K2,.,Kn]
Multidimensional Array
Let C be a n-dimensional array
The length Li of dimension i of C is the number of
Multidimensional Array
Address LOC(C[K1,K2, ., Kn]) of an arbitrary element of C
can be obtained as
Column-Major Order
Base(C) + w[((((ENLN-1 + EN-1)LN-2) +..+E3)L2+E2)L1+E1]
Row-Major Order
Base(C) + w[( ((E1L2 + E2)L3 + E3)L4 + .. +EN-1)LN +EN]
STACKS
Stacks
A Stack is defined as a special type of data structure where
items are inserted from one end called top of stack and
items are deleted from the same end.
Stack is organized as a Last In First Out(LIFO) data
structure.
Operations performed on Stacks are:
Insert an item into the stack (Store)
Delete an item from the stack (Retrieve)
Display the contents of the stack
Characteristics of a Stack
When you add an element to the stack, you say that you
push it on the stack (it is always done at the top), and if you
delete an element from a stack, you say that you pop it from
the stack (again from the top of the stack).
Since the last item pushed into the stack is always the first
Characteristics of a Stack
Since the definition of the stack provides for the insertion and
Operations on Stacks
Some of the typical operations performed on stacks are:
create (s)
S[1:6]
Pop(S)
Push(S,a) a
Pop(S) a
Pop(S)
Top=1
Pop(S)
Pop(S) pop(S)
Top=1
Push(S,a) a b c
Push(S,b)
Push(S,c) push(S,d)
Push(S,e) push(S,f)
Push(S,g) a
a
e
f
Pop(S)
Top=6
Top=0
e
Top=6 = n
Stack overflow
Pop(S)
Top=0 stack underflow
Top=5