Docsity

Data Structures and Algorithms: Comprehensive Notes for Efficient Processing, Summaries of Technology

Partial preview of the text

Download Data Structures and Algorithms: Comprehensive Notes for Efficient Processing and more Summaries Technology in PDF only on Docsity!

DATA STRUCTURES AND

ALGORITHMS NOTES

2024 LEC DAISY OINDI KEMUNTO KCA UNIVERSITY |

UNIT TITLE: DATA STRUCTURES AND ALGORITHMS

UNIT CODE: DIT 406

LECTURER NAME: OINDI DAISY KEMUNTO

EMAIL: [email protected] COURSE OUTLINE; Simple and abstract data types. Application and implementation of: Lists Stack Queue Trees graph Sorting algorithms Searching algorithms Merge algorithms Computer storage management

Characteristics of a Data Structure

  1. Correctness โˆ’ Data structure implementation should implement its interface correctly. (Avoid errors as much as possible)
  2. Time Complexity โˆ’ The running time or the execution time of operations of the data structure must be as small as possible. (The idea is to save on time)
  3. Space Complexity โˆ’ Memory usage of a data structure operation should be as little as possible. ๏ƒ˜ Is There a Need for Data Structures? ๏ƒ˜ ๏ƒ˜ The fact that we are taking our time to study, it clearly states the need Need for Data Structure ๏ƒ˜ As applications are getting complex and data-rich, there are three common problems that applications face nowadays.
  4. Data Search โˆ’ Consider an inventory of 1 million-(106) items of a store. If the application is to search for an item, it has to search an item in 1 million-(106) items every time slowing down the search. As data grows, the search will become slower.
  5. Processor speed โˆ’ Processor speed although very high, falls limited if the data grows to billion records.
  6. Multiple requests โˆ’ As thousands of users can search data simultaneously on a web server, even the fast server fails while searching the data. Data can be organized in a data

structure in such a way that all items may not be required to be searched, and the required data can be searched almost instantly. ๏ƒ˜ It is important to note the essence of time in dealing with data structure. This brings us to compare execution times Execution Time Cases ๏ƒ˜ Execution Time Case Three cases are usually used to compare various data structures' execution time relatively.

  1. Worst Case โˆ’ This is the scenario where a particular data structure operation takes the maximum time it can take. If an operation's worst-case time is ฦ’(n) then this operation will not take more than ฦ’(n) time where ฦ’(n) represents the function of n.
  2. Average Case โˆ’ This is the scenario depicting the average execution time of an operation of a data structure. If an operation takes ฦ’(n) time in execution, then m operations will take mฦ’(n) time.
  3. Best Case โˆ’ This is the scenario depicting the least possible execution time of an operation of a data structure. If an operation takes ฦ’(n) time in execution, then the actual operation may take time as the random number which would be maximum as ฦ’(n). Terminologies you might come across Basic Terminologies

๏ƒ˜ An algorithm should have the following characteristics

  1. Unambiguous โˆ’Each of its steps (or phases), and their inputs/outputs should be clear and must lead to only one meaning.
  2. Input โˆ’ An algorithm should have 0 or more well-defined inputs.
  3. Output โˆ’ An algorithm should have 1 or more well-defined outputs, and should match the desired output.
  4. Finiteness โˆ’ Algorithms must terminate after a finite number of steps.
  5. Feasibility โˆ’ This should be feasible with the available resources.
  6. Independent โˆ’ An algorithm should have step-by-step directions, which should be independent of any programming code. ๏ƒผ It should be efficient both in terms of memory and time. IMPORTANCE OF ALGORITHMS ๏ƒ˜ It helps in enhancing the thinking process. They are like brain stimulants that will give a boost to our thinking process. ๏ƒ˜ It helps in solving many problems in computer science, computational biology, and economics. ๏ƒ˜ Without the knowledge of algorithms, we can become a coder but not a programmer. ๏ƒ˜ A good understanding of algorithms will help us to get a job. There is an immense demand of good programmers in the software industry who can analyze the problem well.

๏ƒ˜ Genetic algorithms and randomized approach will help us to retain that job in the changing market. WAYS OF WRITING AN ALGORITHM ๏ƒ˜ There are three basic ways of writing algorithms in programming. ๏ƒ˜ They include:

  1. English like algorithm
  2. Flowcharts
  3. pseudocodes Data Types **_We have two different types of data types
  4. Built-in Data Type_** ๏ƒ˜ Those data types for which a language has built-in support are known as Built-in Data types. For example, most of the languages provide the following built-in data types. ๏‚ท Integers ๏‚ท Boolean (true, false) ๏‚ท Floating (Decimal numbers) ๏‚ท Character and Strings 2. Derived Data Type

4. The amount of time required by an algorithm to perform Arithmetic operations, logical operations, return value and assignment operations, etc., 5. Input data N/B; When we calculate the time complexity of an algorithm, we consider only input data and ignore the remaining things, as they are machine-dependent. ๏ƒผ Calculating the Time Complexity of an algorithm based on the system configuration is a very difficult task because the configuration changes from one system to another system. To solve this problem, we must assume a model machine with a specific configuration. So that, we can be able to calculate generalized time complexity according to that model machine. Example; Let us assume we have a machine with the following configuration;

  1. It is a Single processor machine.
  2. It is a 32 bit Operating System machine
  3. It performs sequential execution
  4. It requires 1 unit of time for Arithmetic and Logical operations
  5. It requires 1 unit of time for Assignment and Return value
  6. It requires 1 unit of time for Read and Write operations.

Let us now Calculate the time complexity of an algorithm Example of a code; int sum(int a, int b) { return a+b; } Simply above, the code requires;

  1. 1 unit of time to calculate a+b
  2. 1 unit of time to return the value Totality, we need 2 units to complete execution. ๏ƒ˜ The above is a Constant Time Complexity. (A program that requires fixed amount of time for all input.) ๏ƒ˜ We say that because in the above, unit time does not change based on the input values of a and b. Linear Time Complexity; If the amount of time required by an algorithm is increased with the increase of input value. Example of a code int sum(int A[], int n) { int sum = 0, i; for(i = 0; i < n; i++) sum = sum + A[i]; return sum; } Let us break it down, Int sumofList(

COST REPETITION

No. of times

TOTAL

Total