Chapter 01 - Intro to DSA (1)
Chapter 01 - Intro to DSA (1)
• char • Class
• Function
• int •
• Array Structure
• boolean • Pointer • Union
• float • Enumeration
• double • Typedef
• void
• wide character
(wchar_t)
• The data structures are the building blocks of a program and hence the
selection of a particular data structure stresses on
• The data structures must be rich enough in structure to reflect the relationship existing
between the data, and
• The structure should be simple so that we can process data effectively whenever
required.
• Data Structure can be used for the following purpose:
• Organizing the data – How data items are organized in the main memory?
• Accessing methods – How data items can be accessed?
• Specifying the degree of associativity – How data items are interrelated?
• Processing alternatives for data – How many different ways are there in which these
data items can be processed?
Creation Allocation of memory for the data structure, the creation of data structure may take
place either during compile-time or during run-time.
Searching Find the location of the key value within the data structure.
Sorting Arranging all the data items in a data structure either in ascending or in descending
order or in lexicographical order (for Strings).
Merging
March 2023 Combining the data items
DATAof two different
STRUCTURE sorted lists into a single sorted list.
AND ALGORITHM 21
Overview of Different Data Structures
• Array
• An array is a collection of the same type of data items, which are stored in consecutive
memory locations under a common name.
• In arrays, there is always a fixed relationship between the addresses of two consecutive
elements as all the items of an array must be stored contiguously.
• Stack
• A stack is a collection of elements into which new elements may be inserted and from
which elements may be deleted only at one end called the top of the stack.
• All the insertion and deletion in a stack is done from the top of the stack, the last added
element will be first to be removed from the stack.
• Stack is also called Last-In-First-Out (LIFO) data structure.
• Tree
• The tree is a non-linear data structure.
• A Tree may be defined as a non-empty finite set of nodes, such that
i. There is a specially designated node called the root.
ii. The remaining nodes are partitioned into zero or more disjoint trees T1, T2, … Tn are called the
subtrees of the root R.
• Graph
• The graph is another non-linear data structure.
• A Graph G is defined as an ordered set G = (V, E), consists of finite non-empty set of
objects V, where V (G) = {v1, v2, v3, … vn} called vertices (or nodes or points) and
another set E where E (G) = {e1, e2, e3, …em} whose elements are called edges, that
connects these vertices.
• Algorithm: Convert(C)
[The variable C is used as Celsius value and F is used as Fahrenheit value]
1. Read C
2. Set F = 1.8 * C + 32
3. Print F
4. Return
• The performance of the algorithm can be measured on the scales of time and
space.
• Time means, we are looking for the fastest algorithm for the problem.
• Space means we are looking for an algorithm that consumes or needs minimum
memory space for its execution.
• When performance is measured in terms of space, it is known as space
complexity
• when performance is measured in terms of time, it is termed as time
complexity.
• If f(n) and g(n) are functions defined for positive integers, then we write
f(n) = O(g(n)) if there exist positive constants n0 and c such that
|f(n)|≤ c| g(n |, for all n ≥ n0.
• In general, O(g(n)) = {f(n): there exist positive constants c and n0 such that
0 ≤ f(n) ≤ cg(n) for all n, where n ≥ n0}.
• That is the right of n0, the value of f (n) always lies on or below cg (n).
• This notation gives an upper bound for a function to within a constant factor.
• Example 1:
• Suppose f (n) = 3n + 2
• We can write f (n) = 3n + 2 ≥ 3n, for all n ≥ 1, here g (n) = n, c = 3, n0 = 1
• Hence, we can write f (n) = Ω (g (n)) = Ω (n)
• Example 2:
• Suppose f (n) = 10n2 + 4n + 2
• We can write f(n)=10n2 +4n+2≥10n2, for all n≥1, here g(n)=n2, c=10, n0=5
• Hence, we can write f (n) = Ω (g (n)) = Ω (n2)
• Example 3:
• Suppose f (n) = 2n3 + n2 + 2n
• We can write f (n) = 2n3 + n2 +2n ≥ 2n3, for all n ≥ 1, here c = 2, n0 = 1
• Hence, we can write f (n) = Ω (g (n)) = Ω (n3)
• Example:
Suppose f (n) =2n3 + 3n2 + n + 10
we can write f (n) = o (g (n)) = o (n4)
March 2023 DATA STRUCTURE AND ALGORITHM 55
ω (little omega)-Notation
• Little-ω notation represents a loose bounding version of Big-Ω;
• g (n) is a loose lower boundary of the function f (n);
• it bounds from the bottom, but not from the top.
• For non-negative functions, f(n) and g(n), f(n) is little omega of g(n) if and only if
f(n) = Ω(g(n)), but f(n) ≠ Θ(g(n)).
• It is denoted as "f(n) = ω(g(n))".
• We can write f (n) = ω (g (n)) iff
• Example:
• Suppose f (n) = 4n3 + 2n + 3
• we can write f (n) = ω (g (n)) = ω (n2)
n g n g 2
1 0 0 1 1 2
2 1 2 4 8 4
4 2 8 16 64 16
8 3 24 64 512 256
16 4 64 256 4096 65536
32 5 160 1024 32768 4294967296
Table 1.1: Values for different growth functions
• Order of asymptotic behavior of the functions from the above list, we can compare
among time complexities:
O (1) < O () < O (n) < O (n) < O (nk) < O (kn) < O (n!)
3. for loops
4. Nested loops
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
sequence of statements
}
}
• The outer loop executes N times. Every time the outer loop executes, the inner loop
executes M times. As a result, the statements in the inner loop execute a total of N *
M times.
• Thus, the complexity is O(N * M).
2. How would the complexity change if the second loop went to N instead of
M? A nested loop followed by a non-nested loop:
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) { Answer:
sequence of statements 1. The first set of nested loops is O(N2)
} 2. The second loop is O(N).
} 3. This is O(max(N2,N)) which is O(N2).
3. A nested loop in which the number of times the inner loop executes
depends on the value of the outer loop index:
Answer:
for (i = 0; i < N; i++) { • When i is 0 the inner loop executes N times.
for (j = i; j < N; j++) { • When i is 1 the inner loop executes N-1
sequence of statements times.
} • In the last iteration of the outer loop when i
} is N-1 the inner loop executes 1 time.
• The number of times the inner loop
statements execute is N + N-1 + ... + 2 +1.
• This sum is N(N+1)/2 and gives O(N2)
4. For each of the following loops with a method call, determine the overall
complexity. As above, assume that method f takes constant time, and
that method g takes time linear in the value of its parameter.
a) for (j = 0; j < N; j++) f(j);
b) for (j = 0; j < N; j++) g(j);
c) for (j = 0; j < N; j++) g(k);
Answer:
a) Each call to f(j) is O(1). The loop executes N times so it is N x O(1) or O(N).
b) The first time the loop executes j is 0 and g(0) takes “no operations.” The next time j is 1
and g(1) takes 1 operations. The last time the loop executes j is N-1 and g(N-1) takes
N-1 operations. The total work is the sum of the first N-1 numbers and is O(N2).
c) Each time through the loop g(k) takes k operations and the loop executes N times. Since
you do not know the relative size of k and N, the overall complexity is O(N x k).
March 2023 DATA STRUCTURE AND ALGORITHM 74
March 2023 DATA STRUCTURE AND ALGORITHM 75
Exercise
• Compute the complexity of the following fragment of algorithms?
a b sum = 0;
sum = 0;
for (i=0; i<3; i++) for (i=0; i<n*n; i++)
for (j=0; j<n; j++) sum++;
sum++;
C sum = 0; d for(i=1;i<=n;i++){
for (i = 0; i < n; i++){ for(j=1;j<=2*i;j++){
if (is_even(i)) { k=j;
for (j = 0; j < n; j++) while(k>=0)
sum++; k=k-1;
} }
else }
sum = sum + n;
}