15Cs201J-Data Structures: Unit-I
15Cs201J-Data Structures: Unit-I
15Cs201J-Data Structures: Unit-I
UNIT-I
INTRODUCTION
TO
DATA STRUCTURES
• Subset of Data
• Manipulated raw data
• Organizing in a structure
1. Traversing: Accessing each record exactly once so that certain items in the
2. Searching: Finding the location of the record with a given key value.
6. Merging: Combing the records in two different sorted files into a single
Problem
Algorithm
1. Finiteness
Terminates after a finite number of steps
2. Definiteness
Clear, rigorously (Exact and Accurate) and unambiguously
specified
3. Input
valid inputs are clearly specified
4. Clearly specified/expected output
can be proved to produce the correct output given a valid input
5. Effectiveness
Steps are sufficiently simple and basic
c × g(n)
t(n)
Doesn’t
matter
n
n0
t(n) є O(g(n))
DEPARTMENT OF SOFTWARE ENGINEERING
Ω-Notation (contd.)
• Definition: A function t(n) is said to be in
Ω(g(n)) denoted t(n) є Ω(g(n)), if t(n) is
bounded below by some positive constant
multiple of g(n) for all sufficiently large n.
• If we can find +ve constants c and n0 such
that
t(n) ≥ c × g(n) for all n ≥ n0
t(n)
c × g(n)
Doesn’t
matter
n
n0
t(n) є Ω(g(n))
c1 × g(n)
t(n)
c2 × g(n)
Doesn’t
matter
n
n0
t(n) є Θ(g(n))
1. Time Complexity
2. Space Complexity
– A fixed part: needed for instruction space (Space for the code), simple
variable space, constants space etc. c (Ignore c during calculation)
– A variable part: dependent on a particular instance of input and output
data. Sp(instance)
• T(P) = c + tp(instance)
• Time required T(P) to run a program P also
consists of two components:
– A fixed part: compile time which is independent of
the problem instance c. (Ignore c during
calculation)
– A variable part: run time which depends on the
problem instance tp(instance)
Algorithm Sum(a[],n)
{
S = 0.0; 1
for i=1 to n do n
s = s+a[i]; n
return s; 1
}
2n + 2=O(n)
DEPARTMENT OF SOFTWARE ENGINEERING
Example-2
Algorithm Sum(a[],n,m)
{
for i=1 to n do; n
for j=1 to m do nm
s = s+a[i][j]; nm
return s; 1
}
2nm + n + 1
=O(nm)
DEPARTMENT OF SOFTWARE ENGINEERING
Iterative - Example
1) A()
{
int i;
for(i=1 to n)
printf (“abc”);
}
Soln: O(n)
Input size: n
Basic operation: A[i] = A[j]
Does C(n) depend on type of input?
UniqueElements (contd.)
fori<- 0 to n-2 do
for j <- i+1 to n-1 do
if A[i] = A[j]
return false
return true
Cworst(n) =
Harmonic Series:
Others:
Example-1
Factorial
ALGORITHM F(n)
// Output: n! Input size: n
Basic operation: ×
if n = 0
return 1 M(n) = M(n-1) + 1 for n > 0
else
to compute
return F(n-1)xn F(n-1) to multiply
n and F(n-1)
Analysis Of Recursive Factorial method
Example1: Form and solve the recurrence relation for the running time
of factorial method and hence determine its big-O complexity:
long factorial (int n) {
if (n == 0)
return 1;
else
return n * factorial (n – 1);
}
T(0) = c (1)
T(n) = b + T(n - 1) (2)
= b + b + T(n - 2) by subtituting T(n – 1) in (2)
= b +b +b + T(n - 3) by substituting T(n – 2) in (2)
…
= kb + T(n - k)
The base case is reached when n – k = 0 k = n, we then have:
T(n) = nb + T(n - n)
= bn + T(0)
= bn + c
Therefore the method factorial is O(n)
53
Analysis Of Recursive Binary Search
• The recurrence relation for the running time of the method is:
T(1) = a if n = 1 (one element array)
T(n) = T(n / 2) + b if n > 1
Analysis Of Recursive Binary Search (Cont’d)
Without loss of generality, assume n, the problem size, is a multiple of 2, i.e., n = 2k
Expanding:
T(1) = a (1)
T(n) = T(n / 2) + b (2)
= [T(n / 22) + b] + b = T (n / 22) + 2b by substituting T(n/2) in (2)
= [T(n / 23) + b] + 2b = T(n / 23) + 3b by substituting T(n/22) in (2)
= ……..
= T( n / 2k) + kb
55
Analysis Of Recursive Fibonacci
long fibonacci (int n) { // Recursively calculates Fibonacci number
if( n == 1 || n == 2)
return 1;
else
return fibonacci(n – 1) + fibonacci(n – 2);
}
T(n) = c if n = 1 or n = 2 (1)
T(n) = T(n – 1) + T(n – 2) + b if n > 2 (2)
Example:
Write an algorithm for finding the location of the largest element of an array Data.
Assumptions:
1. All keys are equally likely in a search
2. We always search for a key that is in the array
Example:
• We have an array of 10 records.
• If search for the first record, then it requires 1 array
access; if the second, then 2 array accesses. etc.
The average of all these searches is:
(1+2+3+4+5+6+7+8+9+10)/10 = 5.5
=(1+2+…+n)/n
= n(n+1)/2n
= (n+1)/2
if no match then
look left (if need smaller))
or right (if greater)
else
Look to the right
DEPARTMENT OF SOFTWARE ENGINEERING
Looking Left
• Use indices “first” and “last” to keep track of
where we are looking
• Move left by setting last = middle – 1
7 12 42 59 71 86 104 212
F L M L
7 12 42 59 71 86 104 212
F M F L
7 12 42 59 71 86 104 212
F M L
Looking for 42
7 12 42 59 71 86 104 212
F M L
Looking for 42
7 12 42 59 71 86 104 212
F
M
L
42 found – in 3 comparisons
7 12 42 59 71 86 104 212
F M L
Looking for 89
7 12 42 59 71 86 104 212
F M L
Looking for 89
7 12 42 59 71 86 104 212
F L
M
Looking for 89
7 12 42 59 71 86 104 212
L F
Best Case:
1 comparison
1 7 9 12 33 42 59 76 81 84 91 92 93 99
Target: 59
DEPARTMENT OF SOFTWARE ENGINEERING
Binary Search Analysis: Worst Case
………… 1 comparison
N/8
.
.
.
………… 1 comparison
1
DEPARTMENT OF SOFTWARE ENGINEERING
Analysis of Linear and Binary Search
2 8 4 9 3 6
2 8 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 4 8 9 6
3
2 4 8 9 6
3
2 3 4 8 9 6
6
2 3 4 8 6
6
2 3 4 6 8
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
2 3 4 6 8 9 done
Insert
While loop runs for (n-1) times and the cond is false.
Hence (n-1) times execution is done.
So the order is O(n-1)=O(n)
DEPARTMENT OF SOFTWARE ENGINEERING
Worst-Case Analysis
Elements 6 54 3 2 1
In Ist pass= 1 comparison,1 swap
In 2nd pass= 2 comparison,2 swap
In 3rd pass= 3 comparison,3 swap
……
We have (n-1) passes=5 passes (i.e) 1 + 2 + 3 + 4 + 5
(n-5)+(n-4)+(n-3)+(n-2)+(n-1)
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
42 Swap 42
77 77 35 12 101 5
1 2 3 4 5 6
42 7735 Swap 35
77 12 101 5
1 2 3 4 5 6
42 35 12 Swap 12
77 77 101 5
1 2 3 4 5 6
42 35 12 77 101 5
No need to swap
1 2 3 4 5 6
42 35 12 77 5 Swap 101
101 5
1 2 3 4 5 6
42 35 12 77 5 101
loop
exitif(index > last_compare_at)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop
DEPARTMENT OF SOFTWARE ENGINEERING
Items of Interest
• Notice that only the largest value is correctly
placed
• All other values are still out of order
• So we need to repeat this process
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
N-1
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
1 2 3 4 5 6
5 12 35 42 77 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
loop
exitif(to_do = 0)
index <- 1
loop
exitif(index > to_do)
if(A[index] > A[index + 1]) then
Outer loop
Inner loop
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop
to_do <- to_do - 1
endloop
endprocedure // Bubblesort