Algorithms Fundamentals: Properties and Structograms
This article is a concept explanation about algorithms and their fundamental properties.
In a Nutshell
An algorithm is a finite sequence of well-defined instructions to solve a specific problem or perform a computation.
Algorithm Properties
Essential Characteristics
- Finiteness: Algorithm must terminate after a finite number of steps
- Definiteness: Each step must be precisely defined and unambiguous
- Input: Algorithm has zero or more well-defined inputs
- Output: Algorithm produces one or more well-defined outputs
- Effectiveness: Each step must be basic enough to be carried out
Algorithm Quality Criteria
- Correctness: Produces correct output for all valid inputs
- Efficiency: Uses minimal time and memory resources
- Readability: Easy to understand and maintain
- Robustness: Handles invalid inputs gracefully
- Scalability: Performs well with increasing input size
Structograms (Flowcharts)
Structograms are graphical representations of algorithms using standardized symbols.
Basic Flowchart Symbols
┌─────────────┐ Start/End
│ OVAL │
└─────────────┘
┌─────────────┐ Process/Operation
│ RECTANGLE │
└─────────────┘
┌─────────────┐ Decision/Condition
│ DIAMOND │
└─────────────┘
┌─────────────┐ Input/Output
│ PARALLELOGRAM│
└─────────────┘
┌─────────────┐ Connector
│ CIRCLE │
└─────────────┘
Example: Finding Maximum Number
┌─────────┐
│ START │
└─────────┘
│
▼
┌─────────────┐
│ Input n[] │
│ Set max=n[0]│
└─────────────┘
│
▼
┌─────────────┐
│ i = 1 │
└─────────────┘
│
▼
┌─────────────┐
│ i < n.length │
└─────────────┘
YES │ NO
▼ ▼
┌─────────────┐
│ max > n[i]? │
└─────────────┘
YES │ NO
▼ ▼
┌─────────────┐
│ max = n[i] │
└─────────────┘
│
▼
┌─────────────┐
│ i = i + 1 │
└─────────────┘
│
▼
┌─────────────┐
│ Output max │
└─────────────┘
│
▼
┌─────────┐
│ END │
└─────────┘
Algorithm Examples
Linear Search
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i # Found at index i
return -1 # Not found
Binary Search
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid # Found at index mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1 # Not found
Bubble Sort
def bubble_sort(arr):
n = len(arr)
for i in range(n):
swapped = False
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
if not swapped:
break # Array is sorted
return arr
Algorithm Analysis
Time Complexity
Time complexity measures how runtime grows with input size.
| Complexity | Description | Example |
|---|---|---|
| O(1) | Constant time | Array access |
| O(log n) | Logarithmic | Binary search |
| O(n) | Linear | Linear search |
| O(n log n) | Linearithmic | Merge sort |
| O(n²) | Quadratic | Bubble sort |
| O(2ⁿ) | Exponential | Brute force |
Space Complexity
Space complexity measures memory usage relative to input size.
- O(1): Constant space - uses fixed amount of memory
- O(n): Linear space - memory grows proportionally to input
- O(n²): Quadratic space - memory grows quadratically
Algorithm Design Techniques
1. Divide and Conquer
- Break problem into smaller subproblems
- Solve subproblems recursively
- Combine solutions
Example: Merge Sort, Quick Sort
2. Greedy Algorithms
- Make locally optimal choice at each step
- Hope to find global optimum
Example: Dijkstra’s Algorithm, Huffman Coding
3. Dynamic Programming
- Break problem into overlapping subproblems
- Store results of subproblems
Example: Fibonacci Sequence, Knapsack Problem
4. Backtracking
- Try all possible solutions
- Abandon partial solutions that don’t work
Example: N-Queens Problem, Sudoku Solver
Best Practices
- Clear problem definition: Understand requirements completely
- Choose appropriate data structures: Select structures that optimize your algorithm
- Handle edge cases: Consider empty inputs, single elements, etc.
- Test thoroughly: Verify with various input scenarios
- Document your algorithm: Explain logic and complexity
Common Mistakes to Avoid
- Off-by-one errors: Incorrect loop boundaries
- Infinite loops: Missing termination conditions
- Memory leaks: Not releasing allocated memory
- Incorrect assumptions: Assuming sorted input when not guaranteed
- Poor variable naming: Using unclear variable names
Learning Resources
- Books: Introduction to Algorithms by Cormen et al.
- Online: Khan Academy, Coursera algorithms courses
- Practice: LeetCode, HackerRank, Codeforces
- Visualization: Algorithm visualizers online
Next Steps
After mastering fundamentals:
- Study advanced algorithms
- Learn data structures in depth
- Practice competitive programming
- Implement algorithms from scratch
- Analyze real-world algorithm applications