Thursday, November 26, 2015

Role of Data Structures

Role of Data Structures

Multiple algorithms can be designed to solve a particular problem. However, the algorithms may differ in the extent of efficiency to which they can solve the problem. In such a situation, an algorithm that provides maximum efficiency should be used for solving the problem. Here, efficiency means that the algorithm should work in minimal time and use minimal memory. One of the basic techniques for improving the efficiency of algorithms is to structure the data that they operate on
in such a way that the resulting operations can be efficiently performed. The way in which the various data elements are organized in memory, with respect to each other, is called a data structure. Data can be organized in many different ways. Therefore, you can create as many data structures as you want. However, there are some standard data structures that have proved useful over the years. These include arrays, linked lists, stacks, queues, and trees. You will learn more about these data structures in the subsequent chapters. All these data structures are designed to hold a collection of data items. However, the difference lies in the way in which the data items are arranged with respect to each other and the operations that they allow. As the data items are arranged in different ways, some data structures prove to be more efficient than others to solve a given problem. Suppose you have to write an algorithm that enables a printer to serve the requests of multiple users on a first- come-first-served basis. In this case, using a data structure that stores and retrieves the requests in the order of their arrival would be more efficient than a data structure that stores and retrieves the requests in a random order. In addition, to improve the efficiency of an algorithm, the use of appropriate data structures is required. It also allows you to overcome the following programming challenges:

  1. Simplifying complex problems 
  2. Creating standard and reusable code components
  3.  Creating programs that are easy to understand and maintain 
To understand the use of an appropriate data structure, which helps in simplifying the solution to a problem, let us consider an example where you have to find the maximum value in a set of 50 numbers. In such a case, you can either use 50 variables or use a data structure, such as an array of size 50, to store the numbers. When 50 different variables are used to store the numbers, the following algorithm can be used to determine the maximum value among the numbers: 


  • Accept 50 numbers and store them in num1, num2, num3, ..., num50.
  •  Set max = num1.
  •  If num2 > max then: max = num2 
  • If num3 > max then: max = num3 
  • If num4 > max then: max = num4 
  • . .
  •  If num50 > max then: max = num50
  •  Display max.
On the other hand, when an array of size 50 is used, the following algorithm can be used to determine the maximum value among the elements in an array:

  • Set max = num[0].
  •  Repeat step 3 varying i from 1 to 49. 
  • If num[i] > max then: max = num[i] 
  • Display max.

From the preceding two algorithms, it can be seen that the algorithm that is using an array manipulates memory more efficiently than the algorithm that is using 50 variables. In addition, the algorithm using an array involves few steps, and is therefore, easier to understand and implement as compared to the algorithm that uses 50 variables. Data structures also enable the creation of reusable code components. Suppose you have created a class to implement a data structure that stores and retrieves requests in the order of their arrival. Once the class is created, the same class can be used in several different applications that need to serve the requests of multiple users on a first-come-first-served basis. This means that a data structure, once implemented, can be used as a standard component to provide standard solutions to a specific set of problems. The use of standard components helps to simplify the maintenance process. This is because the standard components are time-tested, and therefore, do not need much maintenance.

No comments:

Post a Comment