International Open University
International Open University
STUDENT NAME
MOH’D KHAMIS SONGORO
STUDENT ID
ST10196482
COURSE CODE:
CMP 202
QUESTION:
QUESTION 1
Contents
Part i) Queues and Their Operations: ..................................................................................................... 3
Introduction to Queues: ..................................................................................................................... 3
Key Operations on Queues: ................................................................................................................ 3
Enqueue (Insertion): ....................................................................................................................... 3
Dequeue (Deletion): ........................................................................................................................ 3
Front (Peek): ................................................................................................................................... 3
Size: ................................................................................................................................................. 3
Part ii) Sorting Algorithms: ...................................................................................................................... 4
Part iii) Dijkstra's Algorithm: ................................................................................................................... 6
References ............................................................................................................................................ 10
2
Part i) Queues and Their Operations:
Introduction to Queues:
Queues are fundamental data structures that play a crucial role in various computing
applications. They follow the First In, First Out (FIFO) principle, mirroring real-world
scenarios like standing in a queue at a ticket counter. Understanding queues is essential for
designing efficient algorithms and managing data in computer systems.
Dequeue (Deletion):
Dequeueing removes the element from the front of the queue, representing the completion of
a task or service. Just like the person at the front of the line is served first, the element at the
front of the queue is processed next.
Front (Peek):
The front operation retrieves the element at the front without removing it. This operation is
useful for inspecting the next element to be processed without altering the queue's structure.
Size:
The Size operation returns the number of elements currently present in the queue. It provides
insights into the workload and processing requirements.
#include <iostream>
#include <queue>
int main() {
// Creating a queue
queue<int> queYangu;
3
// Enqueue elements
queYangu.push(10);
queYangu.push(20);
queYangu.push(30);
return 0;
}
In this C++ program, we've used the queue class from the Standard Template Library (STL)
to demonstrate the basic operations of a queue.
Algorithm Explanation:
The algorithm iterates through the array, considering one element at a time. It builds a sorted
sequence by shifting elements as needed to find the correct position for each element.
4
for i = 1 to n-1
key = arr[i]
j=i-1
arr[j + 1] = key
Applications:
Insertion sort is not the most efficient for large datasets but performs well for small datasets
or nearly sorted data. It's often used in scenarios where the input size is expected to be small
or when the array is already partially sorted.
Merge Sort:
Overview:
Merge sort is a divide-and-conquer algorithm known for its stability and consistent
performance. It efficiently handles large datasets by dividing the array into two halves,
recursively sorting each half, and then merging them.
Algorithm Explanation:
mergeSort(arr[], l, r)
if r > l
1. Find the middle point to divide the array into two halves:
middle m = l + (r-l)/2
2. Call mergeSort for the first half:
mergeSort(arr, l, m)
3. Call mergeSort for the second half:
mergeSort(arr, m+1, r)
4. Merge the two halves sorted in steps 2 and 3:
5
merge(arr, l, m, r)
Applications:
Merge sort is widely used for its stability and predictable performance. It is the algorithm of
choice for external sorting and is often used as the standard algorithm for sorting linked lists.
Complexity Analysis:
Insertion Sort:
Time complexity: O(n^2) in the worst case.
Algorithm Steps:
Initialization:
6
Repeat step 2 until all vertices are visited. The final distances represent the shortest paths
from the source to all other vertices.
C++ Program Example:
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
pq.push(make_pair(0, src));
dist[src] = 0;
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
7
}
cout << "Shortest distances from source " << src << ":\n";
for (int i = 0; i < V; ++i)
cout << "To " << i << ": " << dist[i] << endl;
}
int main() {
vector<vector<int>> graph = {
{0, 1, 4, 0, 0, 0},
{1, 0, 4, 2, 7, 0},
{4, 4, 0, 3, 5, 0},
{0, 2, 3, 0, 4, 6},
{0, 7, 5, 4, 0, 7},
{0, 0, 0, 6, 7, 0}
};
dijkstra(graph, 0);
return 0;
}
Explanation:
1. Initialization:
A priority queue (pq) is used to efficiently select the vertex with the minimum distance.
The dist array maintains the shortest distance from the source to each vertex, initialized to
INT_MAX for all vertices except the source.
2. Iteration:
The algorithm iterates until all vertices are visited.
In each iteration, the vertex with the minimum distance (u) is selected from the priority
queue.
For each neighbor of u, if a shorter path is found, update its distance and enqueue it.
8
3. Result:
After all vertices are visited, the dist array contains the shortest distances from the source to
all other vertices.
Applications:
Dijkstra's algorithm is widely used in various applications such as network routing protocols,
GPS systems, and robotics for path planning.
Conclusion:
These all concepts are foundational to computer science and are crucial for understanding and
implementing more advanced algorithms and data structures. By mastering these fundamental
principles, one can build a solid foundation for solving a wide range of computational
problems.
9
References
1. Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2009). Introduction to
Algorithms (3rd ed.). The MIT Press. Cambridge, MA.
2. Weiss, M. A. (2014). Data Structures and Algorithm Analysis in Java (3rd ed.).
Pearson. Boston, MA.
3. Goodrich, M. T., Tamassia, R., & Goldwasser, M. H. (2014). Data Structures and
Algorithms in Java (6th ed.). John Wiley & Sons. Hoboken, NJ.
4. Sedgewick, R., & Wayne, K. (2011). Algorithms (4th ed.). Addison-Wesley. Boston,
MA.
5. Lafore, R. (2002). Data Structures and Algorithms in Java (2nd ed.). Sams
Publishing. Indianapolis, IN.
10