Basic Concepts of Data Structures and Algorithm Analysis


Fundamental Concepts of Data Structures

  • Data structures are defined as a systematic way of organizing and storing data in a computer to ensure it can be utilized efficiently.

  • This fundamental concept in computer science represents the knowledge of how data is structured and organized within memory.

  • Beyond simple organization, data structures are used for processing, retrieving, and storing data through specialized formats.

  • Data consists of raw and unorganized facts that must be processed to become meaningful.

  • Structure is defined as a specific way of organizing these facts.

Classification of Data Structures

Data structures are broadly categorized into two main types: Primitive and Non-primitive.

  • Primitive Data Structures: These are the basic data types provided by programming languages. Examples include:

    • Integer

    • Float

    • Character

    • Boolean

  • Non-primitive Data Structures: These are more complex structures derived from primitive types, subdivided into linear and non-linear types.

    • Linear Data Structures:

      • Array

      • Stack

      • Queue

      • Linked List

    • Non-linear Data Structures:

      • Tree

      • Graph

      • Trie

      • Hash Table

Linear Data Structures

  • A linear data structure is one where data elements are arranged in a sequential or linear fashion. Each element (except the first and last) is attached to its previous and next adjacent elements.

Static Data Structures
  • In static structures, the size is fixed at the time of allocation. While contents can be modified, the memory space remains constant.

  • Example: Array - A collection of items of the same data type stored at contiguous memory locations.

Dynamic Data Structures
  • In dynamic structures, the size is not fixed and can be modified during run-time operations to facilitate change.

  • Stack: Follows the Last-In-First-Out (LIFO)(LIFO) principle, where the most recently added element is the first to be removed.

  • Queue: Follows the First-In-First-Out (FIFO)(FIFO) principle, where the first element added is the first to be removed.

  • Linked List: A chain of nodes where each node contains a data field and a reference (link) to the next node. Unlike arrays, elements are not stored in contiguous memory locations.

Non-Linear Data Structures

  • In non-linear structures, data elements are not placed sequentially and instead utilize hierarchical or complex relationship mappings.

  • Trees: A hierarchical structure where each element (node) is connected to one or more nodes in a parent-child relationship. These are used to represent systems like file directories or organizational charts.

  • Graphs: A collection of nodes (vertices) and edges that connect them. These are more flexible than trees and represent complex relationships such as social networks, transportation systems, or web pages.

Comparison of Linear and Non-Linear Data Structures

Feature

Linear Data Structure

Non-Linear Data Structure

Arrangement

Sequential

Hierarchical

Layers

Single layer

Multiple layers

Traversal

All elements can be traversed in one go

May require multiple runs depending on size and arrangement

Memory Efficiency

Inefficient for large datasets

Various efficient ways to use memory based on structure

Applications and Advantages of Data Structures

Applications
  • Databases: Organizing and storing data for efficient retrieval and manipulation.

  • Operating Systems: Managing system resources like memory and files.

  • Computer Graphics: Representing geometric shapes and graphical elements.

  • Artificial Intelligence: Representing knowledge and information within systems.

Advantages
  • Efficiency: Optimizes storage and retrieval performance for critical applications.

  • Flexibility: Allows for easy modification and manipulation of data organization.

  • Reusability: Structures can be used across multiple programs, reducing redundant code.

  • Maintainability: Well-designed structures make programs easier to understand and update over time.

Algorithms and Performance Analysis

  • An algorithm is a finite set of well-defined computational instructions designed to accomplish a particular task, transforming a set of inputs into a set of outputs.

  • Algorithms are independent of specific programming languages.

Criteria for Algorithms
  1. Input: Zero or more quantities supplied externally.

  2. Output: At least one quantity produced.

  3. Definiteness: Each instruction must be clear and unambiguous.

  4. Finiteness: The algorithm must terminate after a finite number of steps for all cases.

  5. Effectiveness: Each operation must be basic enough to be feasible (e.g., performed by a person with pencil and paper).

Theoretical Example: Making a Cup of Tea
  1. Start (get ingredients).

  2. Put teabag in cup.

  3. Fill kettle with water.

  4. Boil water.

  5. Pour boiled water into cup.

  6. Add milk.

  7. Add sugar.

  8. Stir the tea.

  9. Drink the tea.

  10. Stop.

Performance Evaluation Phases

  1. Priori Estimates: A machine-independent technique that determines the frequency count of each statement (how many times it executes) based on the algorithm logic rather than hardware or language.

    • Example: For a statement x=x+1x = x + 1, total time is the product of execution time for a single instance and the frequency of execution.

  2. Posteriori Testing: A machine-dependent technique that accounts for the specific characteristics of the hardware and language used to run the algorithm.

Complexity Analysis

To analyze an algorithm is to determine the resources required for execution, focused on Time and Storage Space.

Space Complexity
  • Space complexity is the amount of memory a program needs to run to completion.

  • The total space requirement S(P)S(P) is expressed as:     S(P)=c+Sp(I)S(P) = c + S_{p}(I)     where cc is a constant representing fixed space requirements and Sp(I)S_{p}(I) represents variable requirements.

  • Fixed Space Requirements: Includes instruction space, simple variables, and constants. These do not change based on input size.

  • Variable Space Requirements: Depends on instance II, including structured variables and additional space for recursion.

Recursive Space Example
  • Consider a function float rsum(float list[], int n) that sums a list recursively.

  • The compiler must save parameters, local variables, and return addresses for each call.

  • Data storage per recursive call assuming 4-byte units:

    • Parameter 1 (array pointer): 4 bytes

    • Parameter 2 (integer nn): 4 bytes

    • Return address: 4 bytes

    • Total per call: 12 bytes

  • If nn is the size of the array, Space Complexity = n×12n \times 12.

Time Complexity
  • The total time T(P)T(P) is the sum of compile time and run time. Analysis typically focuses on run (execution) time.

  • Worst-Case: The maximum value of f(n)f(n) for any input; provides an upper bound on resource requirements.

  • Average-Case: The expected value or average value of f(n)f(n).

  • Best-Case: The minimum possible value of f(n)f(n).

Calculation of Computation Time

Frequency Count (Step Count) Method
  • Step count is the number of times an instruction is executed.

  • Rules:

    • Comments and Declarations: Step count = 00

    • Return and Assignment statements: Step count = 11

    • Constants and low-order exponents are ignored when high-order exponents exist. For example, in 5n4+7n3+10n2+n+1005n^{4} + 7n^{3} + 10n^{2} + n + 100, the Time Complexity (TC) is O(n4)O(n^{4}).

Example: Summation Algorithm Step Count
int sum (int a[], int n) {
  s=0; // Count: 1
  for(i=0; i<n; i++) // Count: 1 + (n+1) + n
    s=s+a[i]; // Count: n
  return s; // Count: 1
}
  • Total Step Count: 1+1+(n+1)+n+n+1=3n+41 + 1 + (n+1) + n + n + 1 = 3n + 4

  • Complexity: O(n)O(n)

Algorithm Efficiency and Loops

Linear Loops
  • Efficiency is directly proportional to the number of iterations.

  • Single loop with factor nn: f(n)=nf(n) = n.

  • Loop updating by adding 2 (i+=2i += 2): f(n)=n2f(n) = \frac{n}{2}.

Logarithmic Loops
  • The loop-controlling variable is multiplied or divided in each iteration.

  • Example: for(i=1; i<1000; i*=2) or for(i=1000; i>=1; i/=2)

  • Efficiency: f(n)=log⁡(n)f(n) = \log(n).

Nested Loops
  • Efficiency is the product of inner loop iterations and outer loop iterations.

  • Linear Logarithmic Loop: Outer loop is linear (nn), inner loop is logarithmic (log⁡(n)\log(n)). Formula: f(n)=nlog⁡(n)f(n) = n \log(n).

  • Quadratic Loop: Both loops are linear. Formula: f(n)=n2f(n) = n^{2}.

  • Dependent Quadratic Loop: Inner loop iterations depend on the outer loop index (j≤ij \le i). The inner loop iterates an average of n+12\frac{n+1}{2} times. Formula: f(n)=nn+12f(n) = n \frac{n+1}{2}.

Asymptotic Notations

Big-O Notation (OO)
  • Represents the upper bound or the worst-case scenario. It describes the longest amount of time an algorithm could take.

  • Definition: f(n)=O(g(n))f(n) = O(g(n)) if and only if there exist positive constants cc and n0n_{0} such that f(n)≤c×g(n)f(n) \le c \times g(n) for all n≥n0n \ge n_{0}.

  • Examples:

    • 10=O(1)10 = O(1)

    • 2n3+1=O(n3)2n^{3} + 1 = O(n^{3})

    • 3n2+5=O(n2)3n^{2} + 5 = O(n^{2})

    • 2n3+3n2+5n−10=O(n3)2n^{3} + 3n^{2} + 5n - 10 = O(n^{3})

Omega Notation (Ω\Omega)
  • Represents a tight lower bound. The algorithm will never perform better than this value.

  • Definition: f(n)=Ω(g(n))f(n) = \Omega(g(n)) if and only if there exist positive constants cc and n0n_{0} such that 0≤c×g(n)≤f(n)0 \le c \times g(n) \le f(n) for all n≥n0n \ge n_{0}.

Theta Notation (Θ\Theta)
  • Represents an asymptotically tight bound (average case).

  • Definition: f(n)=Θ(g(n))f(n) = \Theta(g(n)) if and only if there exist positive constants c1c_{1}, c2c_{2}, and n0n_{0} such that c1×g(n)≤f(n)≤c2×g(n)c_{1} \times g(n) \le f(n) \le c_{2} \times g(n) for all n≥n0n \ge n_{0}.

Categories of Algorithm Complexity

  1. Constant Time: O(1)O(1)

  2. Linear Time: O(n)O(n)

  3. Logarithmic Time: O(log⁡(n))O(\log(n))

  4. Polynomial Time: O(nk)O(n^{k}) where k>1k > 1

  5. Exponential Time: O(2n)O(2^{n})

Growth of Operations for Functions of nn

nn

O(1)O(1)

O(log⁡(n))O(\log(n))

O(n)O(n)

O(nlog⁡(n))O(n \log(n))

O(n2)O(n^{2})

O(n3)O(n^{3})

1

1

0

1

0

1

1

2

1

1

2

2

4

8

4

1

2

4

8

16

64

8

1

3

8

24

64

512

16

1

4

16

64

256

4096