× back Introduction Static vs Dynamic memory allocation Physical vs Logical DS Abstract Datatype Time complexity Space complexity Finding time complexity from program code.

Introduction

Introduction to Data Structure

Data structure

  • It can be defined as arrangement of collection of data items so that they can be utilized efficiently and operations on that data can be done efficiently.
  • The arrangement and operation on data take place inside the main memory and during the execution of a program.
  • During the execution of the program, how the program will manage data inside the main memory and perform the operations that is data structure.

How the program utilizes data and how they put the data in inside the main memory? ↓ ↓ ↓

Database

  • When data is larger in size or commercial data (used in businesses like banks, retail stores, etc.) they will have lot of data and they will have some organized data in the form of database table or relational data.

Data warehouse

  • We have talked about commercial data that is data used in businesses, so they will have huge amount of data and it will grow day by day.
  • The large size data may not be used daily like many year old data.
  • Commercial data can be categorized into two parts:
    1. Operational data → used daily.
    2. Legacy data (old/historical data) → data kept in storage and if required we can fetch the data and use it.
  • So, that historical data is kept on array of disk as it is historical data of any commercial firm, this storage is data warehouse.
  • For commerical firm this data warehouse are helpful for analyzing the business.
  • The algorithm written for analyzing the warehouse data are known as data mining algorithms.

Big data

  • The data avialable in internet like data about things, people and places, by analyzing this data, decisions are taken that is for management, governance, businesses analysis.
  • Study related to storing and utilizing that large size data is big data.

Static vs Dynamic Memory Allocation

Here we will discuss the following:

  1. About main memory
  2. How a program use memory
  3. Static allocation
  4. Dynamaic allocation

Main memory

How program uses Main memory?


Now we will discuss that if there are sequence of function calls then how the memory is allocated for these functions inside the stack?

How heap memory is utilized by a program?

  • Heap means piling things up.
  • Heap is used in two cases:
    1. If the things are properly organized like a tower.
    2. If the things are not organized.
  • Heap term can be used for both organized things and not organized things.
  • In main memory, heap is an unorganized memory.
  • Stack memory is organized.
  • Heap memory should be treated as a resource, means when required it should be utilized and when we don't require it we should release the memory.
  • Program cannot directly access heap memory so they use pointer to access heap memory.

Physical vs Logical Data Structures

Physical Data Structures

  • The two physical data structures are:
    1. Array
    2. Linked list
  • By combining array and linked list we can create various physical data structure.
  • The reason we call these physical data structure is because they defines how the memory is organized, how the memory is allocated.

  • These two DS are physical because they define how the memory should organized for storing the elements or data. So, these are more related to memory.

Logical Data Stuctures

  • List of logical DS:
    1. Stack
    2. Queues
    3. Trees
    4. Graph
    5. Hash table

Difference between Physical and Logical DS ↓

  • Stack: It works on a discipline called LIFO.
  • Queue: It works on a discipline called FIFO.
  • Trees and graph: These have hierarichal type structure.
  • Logical DS are actually used in applications.
  • To implement logical DS we either use either array or linkedlist.

Abstract Datatype

Let us take an example of list that is collection of elements.

Time complexity

Working with array and list.

  • In a list if we have 'n' elements and we are going through it just once then the time is 'n'. This 'n' is represented as a degree, O(n). [order of n]

Either from procedure or program code we can find time complexity.

                           
                           
                            for(i = 0; i < n; i++)
                            {
                                // searching 
                                // adding
                            }
                            // here 'i' goes from 0 to 'n' so it is O(n).
                           
                       

For each element in a list if there is again a comparision that means there are two for loop, one inside another with O(n2) time complexity.

                            
                            
                             for(i = 0; i < n; i++)
                             {
                                for(j = 0; j < n; j++)
                                {
                                    // processing
                                }
                             }
                            
                        
                            
                            
                             for(i = 0; i < n; i++)
                             {
                                for(j = i+1; j < n; j++)
                                {
                                    // processing
                                }
                             }
                             // O(n^2)
                            
                        

When a list is successively divide in two half until it reaches 1 that is represented as log2 n

                            
                            
                             for(i = n; i > 1; i = 1/2)
                             {
                                 // processing
                             }
                             // O(log n)
                            
                        

Matrix

  • When we are processing upon a matrix of dimension nxn then it will require n2 amount of time (if you are processing all the elements).
  • If you are processing single row or column then it take O(n) time complexity.
                           
                           
                            // matrix processing code

                            for(i = 0; i < n; i++)
                            {
                                for(j = 0; j < n; j++)
                                {
                                    // processing
                                }
                            }
                           
                       

Space complexity

Finding time complexity from program code.

                   
                   
                    void swap(x, y)
                    {
                        int t;
                        t = x; // 1
                        x = y; // 1
                        y = t;// 1, each statement take 1 unit of time as it is simple assignment. 
                    }
                    time f(n) = 1 + 1 + 1
                              = 3 (constant)
                              = O(1)
                   
               
                   
                   
                    int sum(int A[], int n)
                    {
                        int s, i;
                        s = 0; // 1 unit
                        for( i = 0; i < n; i++) // condition will be checked for n + 1 time and increment will be n time so total = n + 1 + n + 1 = 2(n + 1)
                        { // i = 0 assignment = 1 time
                            s = s + A[i]; // n 
                        }
                        return s; // 1
                    }
                    /* total = 1 + 2(n+1) + n + 1 
                             = 1 + 2n + 1 + n + 1
                             = 3n + 3
                             = O(n) as the degree of n is 1 */
                   
               
                   
                   
                   void Add(int n)
                   

int i, j; for(i = 0; i < n; i++) { for(j = 0; j < n; j++) { C[i][j] = A[i][j] + B[i][j]; } } // O(n^2)

                   
                   
                    fun1()
                    {
                        fun2();
                    }

                    fun2()
                    {
                        for(i = 0; i < n; i++)
                        {
                            // processing
                        }
                    }
                    // O(n)