DSU Unit 1 Notes Final
DSU Unit 1 Notes Final
- Data : Data is a collection of information in raw form, when it becomes meaningful after processing
then it is called Information.
- Data type : - Data type is a term which refers to the type / kind of the data that variable can hold/ store.
- e.g in C programming data types are int, float, char double etc.
- Data object : - Data object is a term refers to the Set of elements, such a set may finite or infinite.
- e.g. set of students studying in second year is a finite set, set of natural numbers is an infinite set.
- For writing an efficient algorithm, proper data structures should be selected so that,
- Data structures and algorithm are inter related to each other for efficient problem solving.
- Algorithms manipulate the data in these structures in various ways, such as inserting a new data element,
searching for a particular element, or sorting the elements.
- Thus the study of data structure create and identify the best data structure and select the most
suitable algorithm to execute on this data structure for efficient operation.
- E.g. In sequential data structure using array data accessing is very easy but modifications are difficult.
but If we implement the sequential data structure using linked list then modification become very easy
but accessing the large list become inefficient.
- Thus we need to study data structures so that we can select the best suitable data structure and
algorithm to solve the problem.
- The non-primitive data structures are highly developed complex data structures.
- Non-primitive data structures are derived from the primitive data structures.
- The non-primitive data structures emphasize on structuring of a group of homogeneous (same type)
or heterogeneous (different types) data items.
- Examples of non-primitive data structure in 'C' Language are arrays, structures, Union, linked list,
queue, tree, graph.
For example:
int a[10];
char b[10];
int arrl[5] = {10,20,30,40,50};
1. Array:
- Linked list is alternative approach of arrays. If we are not aware of upper limit of number of elements in
advance then linked list is one of the options.
- In this organization, data can be placed anywhere in memory and these data are linked with each other
using pointer field. This pointer field stores the address of next element in the list.
- Inked list is collection of nodes where each node consists of two parts i.e. data and pointer to next node.
- The first node in linked list is called head/start node which points to the first data element in list and the
last node contains null pointer as shown in Fig above.
3. Stack : -
- A stack is a data structure in which all the insertion and deletion are performed at one end which is
known as Top of the stack.
- The insertion operation in stack is known as push and deletion operation in stack is known as pop as
shown in Fig. below.
- stack follows LIFO (Last In First out) mechanism. The real life examples of stacks are, plates on a tray
and stack of coins etc.
4. Queue : -
2. Accessing the Array supports random access Linked list supports sequential access
element hence nth element can be accessed hence to access nth element of a linked list,
directly by specifying its index. one has to travel entire list.
3. Memory Elements are stored in contiguous New elements can be stored anywhere in
location memory location or consecutive the memory.
manner in the memory.
4. Operations In array, insertion and deletion Insertion and deletion operations are fast in
operation takes more time, as the linked list.
memory locations are consecutive and
fixed.
5. Memory Memory is allocated as soon as the array Memory is allocated at runtime, as and
allocation is declared, at compile time. It is also when a new node is added. It is also known
known as static memory allocation. as dynamic memory allocation.
1. Tree : -
- Tree maintains a hierarchical structure on collection of data items.
- A tree is collection of, one or more nodes such that there is a specially node calied root and remaining
nodes can be partitioned into n (n>=0) disjoint subsets.
- Each disjoint set is a tree as shown in Fig.
- Graph is collection of vertices (nodes) and edges (arcs) where each edge in it is specified by a pair of
vertices.
- The graph can be directed graph or undirected graph as shown in Fig below.
Operations on
Data Structures
- Creation: To create a new data structure, this operation is to allocate memory at compile time or run
time for data.
- Destroy: To delete data structure or to destroy a data structure, this operation is to deallocates memory
which is already allocated with help of malloc() or calloc() function.
- Selection: As per application or requirement, one wants to work on some specific data and other wants
to work on entire data available in data structure. Selection is used by programmers to access the data
within data structures.
- Update: This operation is used to edit or change the data within the data structure. This operation
updates or alters or changes the data.
Algorithm:-
Definition :- An algorithm is a step by step method of solving a problem. They form the foundation of writing a
program.
Properties/Criteria/Characteristics of an algorithm : -
1. Input – input data supplied externally.
2. Output – Result of the program.
3. Definiteness – Each instruction must be clear and distinct.
4. Finiteness – it means algorithm must stop/terminates after finite number of steps.
5. Effectiveness – Effective means should be considered as principle and should be executing in finite
time, also it should be feasible to convert the algorithm into computer program.
3. Finding the exact algorithms complexity is difficult because the algorithmic complexity depends on
many factors as :
o Number if input or output.
o The algorithm used to calculate instruction.
o The software on which the algorithm executed.
o The hardware.
o Machine instruction.
4. In complexity analysis, only the dominant term is retained OR we consider only the most significant
term.
e.g. if an algorithm requires 2n3+logn+4 operations, its order is said to be O(n3) since 2 n3 is the
dominant term.
5. Constants and scaling factors are ignored since we are concerned only about asymptotic behavior.
Time complexity :-
- Time complexity gives total time required by the program to run till its completion.
- This is calculated by counting the number of elementary steps OR the dominant term OR we
consider only the most significant term performed by any algorithm to finish execution.
- The time complexity of algorithms is most commonly expressed using the big O notation.
- It's an asymptotic notation to represent the time complexity.
- As the algorithm's performance may vary with different types of input data, hence for an algorithm we
usually use the worst-case Time complexity because that is the maximum time taken for any input size.
- E.g. Algorithm A : a = a + 1.
- Above statement is independent and not contained in any loop thus executed only once and the
frequency count for this algorithm is 1.
int i=1
loop ( i< =n )
pnint i
i=i+1
int i=1
loop ( i< =n )
{
x=x+1
i=i+1
}
- Result is exactly same as above.
Input size O(1) O (log n) O(n) O(n log n) O(n2) O(n3) O(2n)
1 1 0 1 0 1 1 2
2 1 1 2 2 4 8 4
4 1 2 4 8 16 64 16
8 1 3 8 24 64 512 256
18 1 4 16 64 256 4096 65535
32 1 5 32 160 1024 32768 2147483648
Space Complexity :-
- The space complexity of an algorithm is the amount of memory that it needs to run to completion.
- The space needed by the program is the sum of following components:
1. Fixed/Constant Space Requirement : - This includes the instruction space for simple variables,
fixed size structured variables and constants.
2. Variable/Linear Space Requirement - : These consist of space needed by structured variables
whose size depends on particular instance of variables. It also includes the additional space
required when the function uses recursion.
- In C programming language following is the space requirement for different primitive data type:
2 bytes to store Integer value,
4 bytes to store Floating Point value,
1 byte to store Character value,
8 bytes to store Double value
1. Best Case: Fastest time to complete, with optimal inputs chosen. Best case for 'searching' algorithm
is element found at first position.it is denoted by Big - Omega (Ω).
2. Worst Case: Slowest time to complete Worst case for searching algorithm is element found at last
position or not found at all. Here, we need to do N comparison so time complexity of searching
algorithm is O (N) in worst case. Denoted by Big - Oh (O) notation.
3. Average Case: Average time to complete with random input chosen. Average case for searching
algorithm is element found in between the list. Here we need to do N/2 comparison. It is denoted by
Big – Theta (θ).
- Big - Oh (O) : -
- Big O is the formal method of expressing the upper bound of an algorithm's running time. It is the
measure of the longest amount of time required for the algorithm to complete.
- Commonly written as O, is an Asymptotic Notation for the worst case, or ceiling of growth for a given
function.
[Any one example from the above can be given for big-O]
Big-Omega
- Big-Omega, commonly written as Ω, is an Asymptotic Notation for the Best case for a given function.
- It provides us with an asymptotic lower bound for the growth rate of runtime of an algorithm.
- It is measure of the fastest amount of time required for the algorithm to complete.
- Definition -- Consider function f(n) and g(n) is the most significant term.
- Then If f(n) >= C g(n) for all n >= 1, C > 0, if this condition is true,
- Then we can represent f(n) is Ω(g(n)). i.e. f(n) = Ω(g(n)).
- In this algorithm after a particular input ‘n’ , Cg(n) is less than f(n) always, this indicated the algorithm
lower bound, or best case or fastest execution time.
- In above graph after a particular input value n0, always C1 g(n) is less than f(n) and C2 g(n) is greater than f(n) which
indicates the algorithm's average bound.
- Example : - Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as Θ(g(n))
- then it must satisfy C1 g(n) <= f(n) >= C2 g(n) for all values of C1, C2 > 0 and n>= 1