ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3138Data Structures
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3138

    Design and Analysis of Algorithms

    Progress0 / 53 done
    Topics53
    1Introduction to Algorithm Design2Data Structures3Efficiency in Algorithms4Analysis of Algorithms5Mathematical Review6Mathematical Analysis of Algorithms7Types of Functions8Order of Growth9Asymptotic Notations10Sorting Algorithms11Selection Sort Algorithm12Example and Analysis of Selection Sort13Insertion Sort Algorithm14Divide and Conquer Algorithms15Merge Sort Algorithm16Quick Sort Algorithm17Bucket Sort Algorithm18Radix Sort Algorithm19Counting Sort Algorithm20Heap Sort Basics21Heap Algorithms22Heap Properties and Examples23Heap Operations24Heap Sort Algorithm Analysis25Heap Insertion and Deletion26Tree Based Algorithms and Hashing27Red Black Tree Basics28Binary Search Tree Basics29Tree Searching Algorithms30Analysis of Tree Searching Algorithms31Analysis of Insertion and Deletion in BST32Hashing Basics33Examples of Hash Functions34Analysis of Collision Resolution Techniques35Dynamic Programming360-1 Knapsack Problem37Fractional Knapsack Problem38Longest Common Subsequence39Shortest Path Finding40Matrix Chain Multiplication41Assembly Line Chain Problem42Greedy Algorithms43Prim's Algorithm44Kruskal's Algorithm45Dijkstra's Algorithm46Huffman Coding47NP-Completeness48Polynomial Time Verification49Reducibility50NP-Completeness Proofs51Randomized Algorithms52Particle Swarm Optimization53Genetic Algorithms
    7 min read
    Topic 2 of 53
    Topic Study Note Illustration
    02

    Data Structures

    A data structure is a way of organizing and storing data so that we can perform operations on it efficiently. It defines a particular way of storing...

    Data Structures

    What are Data Structures?

    A data structure is a way of organizing and storing data so that we can perform operations on it efficiently. It defines a particular way of storing data that allows access to the data, manipulation of the data, and operations like searching, inserting, deleting, and updating.

    In simpler terms, think of a data structure as a container or an organization method for the data that you work with in your program. Just like in a physical warehouse, where you need to decide how to store boxes, shelves, and items to make retrieval easy, in computer science, data structures help in organizing data to make tasks like searching, insertion, and deletion easier and more efficient.


    Types of Data Structures

    Data structures can be broadly classified into two categories:

    1. Primitive Data Structures
    2. Non-Primitive Data Structures

    1. Primitive Data Structures

    Primitive data structures are the basic types of data that are directly supported by programming languages. These include:

    • Integers: Whole numbers, both positive and negative.
    • Floats: Numbers with decimal points.
    • Characters: Single letters or symbols (e.g., 'a', 'B', '1').
    • Booleans: True or false values.

    These are the fundamental building blocks that can be used to construct more complex data structures.


    2. Non-Primitive Data Structures

    Non-primitive data structures are more complex and can store collections of data. They are further classified into:

    • Linear Data Structures
    • Non-linear Data Structures
    Linear Data Structures

    In linear data structures, elements are stored sequentially, and each element is connected to its previous and next elements (except the first and last element). You can traverse the structure from one element to the next in a linear manner.

    • Arrays
    • Linked Lists
    • Stacks
    • Queues

    Let's explore each of these in detail.


    Linear Data Structures

    1. Arrays

    An array is a collection of elements of the same type stored at contiguous memory locations. Arrays are indexed, meaning each element in the array has a unique index or position.

    • Example: int arr[] = {1, 2, 3, 4, 5};

    • Operations:

      • Accessing an element: Direct access using index (arr[2] gives 3).
      • Insertion and Deletion: Inserting or deleting elements may require shifting elements to maintain contiguous memory, which can be inefficient.
    • Time Complexity:

      • Access: O(1) (constant time)
      • Insertion/Deletion: O(n) (linear time) in the worst case, because elements might need to be shifted.

    2. Linked Lists

    A linked list is a collection of nodes, where each node contains two parts: the data and a reference (or pointer) to the next node in the list. Linked lists are dynamic, meaning they don’t require contiguous memory like arrays.

    • Example: Node 1 -> Node 2 -> Node 3 -> NULL

    • Operations:

      • Insertion/Deletion: Easy to insert or delete an element at the beginning or middle of the list.
      • Searching: Linear time complexity for searching as you need to traverse each node.
    • Types of Linked Lists:

      • Singly Linked List: Each node points to the next node.
      • Doubly Linked List: Each node points both to the next and the previous node.
      • Circular Linked List: The last node points back to the first node.
    • Time Complexity:

      • Insertion/Deletion: O(1) (if at the beginning of the list)
      • Searching: O(n) (since you may have to traverse the entire list)

    3. Stacks

    A stack is a collection that follows the LIFO (Last In, First Out) principle. The last element added to the stack is the first one to be removed. It can be imagined like a stack of plates where the last plate added is the first to be taken off.

    • Operations:

      • Push: Add an element to the top.
      • Pop: Remove the top element.
      • Peek: View the top element without removing it.
    • Example: Imagine a stack of plates, where the plate added last is the first plate you take off.

    • Applications:

      • Undo operations in text editors
      • Expression evaluation (like converting infix to postfix notation)
    • Time Complexity:

      • Push/Pop/Peek: O(1) (constant time)

    4. Queues

    A queue is a collection that follows the FIFO (First In, First Out) principle. The first element added to the queue is the first one to be removed. Think of it like a line at a grocery store: the first person in line is the first to be served.

    • Operations:

      • Enqueue: Add an element to the back.
      • Dequeue: Remove an element from the front.
      • Peek: View the front element without removing it.
    • Example: In a queue, the first item added is the first to be removed.

    • Applications:

      • Managing tasks in a printer queue
      • Breadth-first search (BFS) in graph algorithms
    • Time Complexity:

      • Enqueue/Dequeue/Peek: O(1) (constant time)

    Non-Linear Data Structures

    Non-linear data structures don't store data in a linear sequence. They are more complex and allow for hierarchical relationships between elements.

    1. Trees

    A tree is a hierarchical data structure with a root node and child nodes. Each node has a value and a list of references to its child nodes. Trees are used to represent hierarchical relationships, like organizational structures or file systems.

    • Example: A binary tree, where each node has at most two children (left and right).

    • Operations:

      • Insertion/Deletion: Based on the tree's structure (e.g., binary search trees or AVL trees).
      • Searching: Often faster than linear data structures due to hierarchical organization.
    • Time Complexity:

      • For balanced trees: O(log n) for searching, insertion, and deletion.

    2. Graphs

    A graph is a collection of nodes (also called vertices) and edges that connect pairs of nodes. Graphs are used to represent relationships between objects, such as social networks or roadmaps.

    • Types of Graphs:

      • Directed Graph (Digraph): Edges have a direction.
      • Undirected Graph: Edges do not have direction.
      • Weighted Graph: Each edge has a weight or cost associated with it.
    • Operations:

      • Searching: Depth-First Search (DFS) and Breadth-First Search (BFS).
      • Shortest path: Dijkstra’s or Bellman-Ford algorithm.
    • Time Complexity:

      • For adjacency matrix representation: O(V^2) for some operations.
      • For adjacency list representation: O(V + E) for traversal.

    Conclusion

    Data structures are fundamental building blocks in computer science. Understanding how different data structures work helps in choosing the right one for your problem, ensuring that operations like searching, insertion, and deletion can be performed efficiently. Each data structure has its strengths and weaknesses, and choosing the appropriate one depends on the type of problem you're solving and the operations you'll perform most often.

    Here's a quick summary of the major data structures:

    Data Structure Type Key Operations Time Complexity (Average)
    Array Linear Access, Insert, Delete Access: O(1), Insert/Delete: O(n)
    Linked List Linear Insert, Delete, Traverse Insert/Delete: O(1), Search: O(n)
    Stack Linear Push, Pop, Peek Push/Pop/Peek: O(1)
    Queue Linear Enqueue, Dequeue, Peek Enqueue/Dequeue/Peek: O(1)
    Tree Non-linear Insert, Search, Delete Balanced Tree: O(log n)
    Graph Non-linear Search, Add/Remove Edge Adjacency List: O(V+E)
    ← Previous topic 1
    Introduction to Algorithm Design
    Next topic 3 →
    Efficiency in Algorithms

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.1kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Design and Analysis of Algorithms