Advance Algorithm Analysis Assignment 02| ZAIN ABBAS
Comparison of Common Algorithms
Introduction:
In this assignment, we will explore and compare various common algorithms used in
computer science. We will discuss their time complexity, computational cost, and
stability. Understanding these key characteristics is crucial for selecting the most
appropriate algorithm for specific tasks.
Main Body
A. Sorting Algorithms:
1. Linear Search:
Time Complexity: O(n)
Computational Cost: Low
Stability: Stable
2. Binary Search:
Time Complexity: O(log n)
Computational Cost: Low
Stability: Stable
B. Sorting Algorithms:
1. Bubble Sort:
Time Complexity: O(n^2)
Computational Cost: Moderate
Stability: Stable
2. Selection Sort:
Time Complexity: O(n^2)
Computational Cost: Moderate
Stability: Unstable
3. Quick Sort:
Time Complexity: O(n^2) - O(n log n)
Computational Cost: High
Stability: Unstable
4. Merge Sort:
Time Complexity: O(n log n)
Computational Cost: High
Stability: Stable
5. Insertion Sort:
Time Complexity: O(n^2)
Computational Cost: Moderate
Stability: Stable
Advance Algorithm Analysis Assignment 02| ZAIN ABBAS
C. Graph Algorithms:
1. Breadth-First Search (BFS):
Time Complexity: O(V + E)
Computational Cost: High
Stability:
it is a graph traversal algorithm used to explore and search for
nodes in a graph. The concept of stability does not apply to BFS
because BFS does not perform any sorting or rearrangement of
elements
2. Depth-First Search (DFS):
Time Complexity: O(V + E)
Computational Cost: High
Stability: DFS doesn't make comparisons between elements; it
simply explores the graph by following edges. It doesn't involve
ordering or comparing elements based on their values.
3. Dijkstra's Algorithm:
Time Complexity: O(V^2) or O(E log V)
Computational Cost: High
Stability: Dijkstra's Algorithm has a specific goal of finding the
shortest path in a weighted graph, which involves finding the
minimum distance from a source node to all other nodes.
D. Other Algorithms:
1. Fibonacci Sequence:
Time Complexity: O(2^n)
Computational Cost: High
Stability: Fibonacci Sequence is a mathematical sequence defined
by a recursive mathematical formula where each term is the sum of
the two preceding ones (e.g., 0, 1, 1, 2, 3, 5, 8, 13, ...). It is not an
algorithm in the sense that it doesn't involve a step-by-step
procedure for solving a computational problem or manipulating
data.
2. Knapsack Problem:
Time Complexity: O(nW)
Computational Cost: High
Stability: Knapsack Problem falls into the category of optimization
problems, where the goal is to find the best solution among a set of
possible solutions.
3. Huffman Coding:
Time Complexity: O(n log n)
Computational Cost: High
Stability: Stable
Advance Algorithm Analysis Assignment 02| ZAIN ABBAS
Comparison Table
Table 1.1
Category Algorithm Time Complexity Computational Cost Stability
Searching Linear Search O(n)
Low Stable
Algorithms Binary Search O(log n)
Bubble Sort O(n^2) Moderate Stable
Selection Sort O(n^2) Moderate Unstable
Sorting
Quick Sort O(n^2) - O(n log n) High Unstable
Algorithms
Merge Sort O(n log n) High Stable
Insertion Sort O(n^2) Moderate Stable
Breadth-First Search (BFS) O(V + E)
Graph Stable
Depth-First Search (DFS) O(V + E) High
Algorithms
Dijkstra's Algorithm O(V^2) or O(E log V)
Fibonacci Sequence O(2^n) N/A
Other
Knapsack Problem O(nW) High N/A
Algorithms
Huffman Coding O(n log n) Stable
I. Time complexity is given in big O notation.
II. Computational cost is a qualitative measure of the computational resources
required, with "Low," "Moderate," and "High" indicating relative levels of resource
consumption.
III. Stability refers to whether the algorithm maintains the relative order of equal
elements in the input data. "Stable" means it preserves the order, while
"Unstable" means it may not preserve the order.
IV. For algorithms like BFS, DFS, and Dijkstra's algorithm, stability doesn't apply in
the same way it does to sorting algorithms because they are typically used for
different types of problems.
Conclusion: In conclusion, the choice of algorithm can significantly impact the efficiency
and stability of various computational tasks. It is essential to consider factors like time
complexity, computational cost, and stability when selecting the most appropriate
algorithm for a specific problem. While some algorithms excel in sorting data efficiently,
others are designed for graph traversal or optimization problems. Understanding these
characteristics is essential for making informed decisions in algorithm design and
selection.
Submitted By: Zain Abbas