DSA
Key concepts:
Data Structures
- Arrays: A collection of elements, each identified by an index. Fixed size and same data type.
- Linked Lists: A sequence of nodes, each containing data and a reference to the next node.
- Stacks: LIFO (Last In, First Out) structure where elements are added and removed from the top.
- Queues: FIFO (First In, First Out) structure where elements are added at the rear and removed from the front.
- Trees: Hierarchical structure with nodes, where each node has a value and references to child nodes. Common types include binary trees and binary search trees.
- Graphs: A set of nodes connected by edges. Can be directed or undirected.
- Hash Tables: Data structure that maps keys to values for efficient lookup.
Algorithms
-
Sorting Algorithms:
- Bubble Sort: Repeatedly swaps adjacent elements that are out of order.
- Selection Sort: Repeatedly selects the smallest remaining element and swaps it to the correct position.
- Insertion Sort: Builds the sorted array one element at a time.
- Merge Sort: Divides the array into halves, recursively sorts them, and merges the sorted halves.
- Quick Sort: Divides the array using a pivot, recursively sorts the partitions.
-
Searching Algorithms:
- Linear Search: Checks each element in the array until the target is found.
- Binary Search: Efficiently searches a sorted array by repeatedly dividing the search interval in half.
-
Graph Algorithms:
- Depth-First Search (DFS): Explores as far as possible along each branch before backtracking.
- Breadth-First Search (BFS): Explores all neighbors at the present depth before moving on to nodes at the next depth level.
- Dijkstra's Algorithm: Finds the shortest path between nodes in a graph.
- A Algorithm*: Uses heuristics to improve the efficiency of pathfinding.
-
Dynamic Programming: Solves problems by breaking them down into subproblems, solving each subproblem once, and storing their solutions.
- Fibonacci Sequence: Classic example of dynamic programming.
- Knapsack Problem: Optimizes the total value in a knapsack without exceeding the weight limit.
-
Greedy Algorithms: Makes locally optimal choices at each step with the hope of finding a global optimum.
- Huffman Coding: Uses greedy algorithms for lossless data compression.
Complexity Analysis
- Time Complexity: Measures the amount of time an algorithm takes to complete as a function of the length of the input.
- Big O Notation: Describes the upper bound of the time complexity (e.g., O(n), O(log n), O(n^2)).
- Space Complexity: Measures the amount of memory an algorithm uses as a function of the length of the input.
Advanced Concepts
-
Heaps: A special tree-based data structure that satisfies the heap property.
- Min Heap: Parent nodes are smaller than their child nodes.
- Max Heap: Parent nodes are larger than their child nodes.
-
Trie: A tree-like data structure that stores a dynamic set of strings, used for efficient retrieval.
-
Segment Trees: Used for storing intervals or segments and allows querying which of the stored segments contain a given point.
-
Union-Find: Also known as Disjoint Set Union (DSU), a data structure that keeps track of elements partitioned into disjoint subsets, supporting union and find operations efficiently.