Docsity

Understanding Algorithms and Time Complexity: Data Structures and Efficiency, Study notes of System Analysis and Design

Partial preview of the text

Download Understanding Algorithms and Time Complexity: Data Structures and Efficiency and more Study notes System Analysis and Design in PDF only on Docsity!

Introduction and

Review

DIPLOMA IN INFORMATION TECHNOLOGY Data Structures & Algorithms (DIT2113)

  • An algorithm is a finite set of instructions or logic, written in order, to accomplish a certain predefined task.
  • Algorithm is not the complete code or program, it is just the core logic(solution) of a problem, which can be expressed either as an informal high level description as pseudocode or using a flowchart.
  • Every Algorithm must satisfy the following properties:
    1. Input: There should be 0 or more inputs supplied externally to the algorithm.
    2. Output: There should be at least 1 output obtained.
    3. Definiteness: Every step of the algorithm should be clear and well defined.
    4. Finiteness: The algorithm should have finite number of steps.
    5. Correctness: Every step of the algorithm must generate a correct output.

What is Algorithms?

  • An algorithm is said to be efficient and fast, if it takes

less time to execute and consumes less memory

space.

  • The performance of an algorithm is measured on the

basis of following properties :

  1. Space Complexity
  2. Time Complexity

What is Algorithms?

  • If we want to go from city "A" to city "B". There can be many ways of doing this such by flight, by bus, by train and also by cycle. Depending on the availability and convenience we choose the one which suits us.
  • Similarly, in Computer Science there can be multiple algorithms exist for solving the same problem (for example, sorting problem has lot of algorithms like insertion sort, selection sort, quick sort and many more).
  • Algorithm analysis helps us determining which of them is efficient in terms of time and space consumed.

Algorithmic Analysis

  • Time Complexity is a way to represent the amount of time required by the program to run till its completion. It's generally a good practice to try to keep the time required minimum, so that our algorithm completes it's execution in the minimum time possible.
  • For any defined problem, there can be N number of solution. If we have a problem and discuss about the problem with all friends, they will all suggest us different solutions. And we are the one who have to decide which solution is the best based on the circumstances. Similarly for any problem which must be solved using a program, there can be infinite number of solutions.

Time Complexity

  • The time complexity of algorithms is most commonly expressed using the Big O notation. It's an asymptotic notation to represent the time complexity.
  • Time Complexity is most commonly estimated by counting the number of elementary steps performed by any algorithm to finish execution.
  • Most common metric for calculating time complexity is Big O notation. This removes all constant factors so that the running time can be estimated in relation to N, as N approaches infinity.

Time Complexity

  • O (1) : constant
  • O (log n) : logarithmic
  • O (n) : linear
  • O (n log n) : linearithmic
  • O (n

) : quadratic

  • O (n

) : cubic

  • O ( n ) : exponential Some Common Functions which Describe Growth Rates (in order)
  • In general you can think of it like this :
  • Above we have a single statement. Its Time Complexity will be Constant. The running time of the statement will not change in relation to N.
  • The time complexity for the above algorithm will be Linear. The running time of the loop is directly proportional to N. When N doubles, so does the running time.

Time Complexity

11

  • This is an algorithm to break a set of numbers into halves, to search a particular field. Now, this algorithm will have a Logarithmic Time Complexity. The running time of the algorithm is proportional to the number of times N can be divided by 2(N is high-low here). This is because the algorithm divides the working area in half with each iteration.

Time Complexity

  • Taking the previous algorithm forward, above we have a small logic of Quick Sort. Now in Quick Sort, we divide the list into halves every time, but we repeat the iteration N times (where N is the size of list). Hence time complexity will be N*log( N ). The running time consists of N loops (iterative or recursive) that are logarithmic, thus the algorithm is a combination of linear and logarithmic (linearithmic).

Time Complexity

  • O(expression) is the set of functions that grow slower than or at the same rate as expression. It indicates the maximum required by an algorithm for all input values. It represents the worst case of an algorithm's time complexity.
  • Omega(expression) is the set of functions that grow faster than or at the same rate as expression. It indicates the minimum time required by an algorithm for all input values. It represents the best case of an algorithm's time complexity.
  • Theta(expression) consist of all the functions that lie in both O(expression) and Omega(expression). It indicates the average bound of an algorithm. It represents the average case of an algorithm's time complexity.

Understanding Notations of

Time Complexity

16

  • Data Structure is a way of collecting and organizing data in such a way that we can perform operations on these data in an effective way.
  • Data Structures is about rendering data elements in terms of some relationship, for better organization and storage.
  • For example, we have some data which has, player’s name “Danial" and age 20. Here “Danial" is of String data type and 20 is of integer data type. We can organize this data as a record like Player record, which will have both player's name and age in it. Now we can collect and store player's records in a file or database as a data structure. For example: “Haziq" 22, "Gugunan" 28, “Seng" 25.

What is Data Structure?

  • Anything that can store data can be called as a data structure, hence integer, float, Boolean, char, etc. are all data structures. They are known as Primitive Data Structures.
  • Then it also has some complex Data Structures, which are used to store large and connected data. Some example of Abstract Data Structure are : − Linked List − Array − Tree − Graph − Stack − Queue
  • All these data structures allow us to perform different operations on data. We select these data structures based on which type of operation is required.

Basic Types of Data Structure

19

  • There are basic structures and directly operated

upon by the machine instructions.

  • In general, there are different representation on

different computers.

  • Integer, Floating-point number, Character

constants, string constants, pointers etc., fall in this

category.

Primitive Data Structure