0% found this document useful (0 votes)
28 views

International Open University

This document discusses queues, sorting algorithms, and Dijkstra's algorithm. It provides an introduction and explanation of key queue operations like enqueue, dequeue, front, and size. It also describes insertion sort and merge sort algorithms, including their time and space complexities. Finally, it outlines Dijkstra's algorithm for finding shortest paths in graphs, providing pseudocode and a C++ implementation example.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

International Open University

This document discusses queues, sorting algorithms, and Dijkstra's algorithm. It provides an introduction and explanation of key queue operations like enqueue, dequeue, front, and size. It also describes insertion sort and merge sort algorithms, including their time and space complexities. Finally, it outlines Dijkstra's algorithm for finding shortest paths in graphs, providing pseudocode and a C++ implementation example.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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.

Key Operations on Queues:


Enqueue (Insertion):
Enqueueing is the process of adding an element to the end of the queue. This operation is
analogous to a person joining the back of a physical queue. In computing, it is used for tasks
like print spooling and managing requests.

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.

C++ Program Illustration:

#include <iostream>
#include <queue>

using namespace std;

int main() {
// Creating a queue
queue<int> queYangu;

3
// Enqueue elements
queYangu.push(10);
queYangu.push(20);
queYangu.push(30);

// Display front and dequeue


cout << "Front: " << queYangu.front() << endl;
queYangu.pop();

// Display new front


cout << "Front after dequeue: " << queYangu.front() << endl;

// Check if the queue is empty


cout << "Is Queue empty? " << (queYangu.empty() ? "Yes" : "No") << endl;

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.

Part ii) Sorting Algorithms:


Insertion Sort:
Overview:
Insertion sort is a simple sorting algorithm that builds the final sorted array one element at a
time. It is particularly efficient for small datasets and has applications in scenarios where the
array is almost sorted.

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

// Move elements of arr[0..i-1] that are greater than key


// to one position ahead of their current position
while j >= 0 and arr[j] > key
arr[j + 1] = arr[j]
j=j-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.

Space complexity: O(1).


Merge Sort:

Time complexity: O(n log n) in the worst case.


Space complexity: O(n).

Part iii) Dijkstra's Algorithm:


Introduction to Dijkstra's Algorithm:
Dijkstra's algorithm is a versatile algorithm used for finding the shortest paths between nodes
in a graph. It operates efficiently when all edge weights are non-negative. This algorithm is
widely applied in network routing protocols, transportation networks, and robotics.

Algorithm Steps:
Initialization:

Create a set of unvisited vertices.


Set the initial distance to infinity for all vertices except the source (set it to 0).
Iteration:

While the set of unvisited vertices is not empty:


Select the vertex with the minimum distance from the source.
For the selected vertex, update the distance of its neighbors if a shorter path is found.
Result:

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>

using namespace std;

#define V 6 // Number of vertices in the graph

void dijkstra(vector<vector<int>>& graph, int src) {


priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
vector<int> dist(V, INT_MAX);

pq.push(make_pair(0, src));
dist[src] = 0;

while (!pq.empty()) {
int u = pq.top().second;
pq.pop();

for (int v = 0; v < V; ++v) {


int weight = graph[u][v];
if (weight != 0 && dist[u] != INT_MAX && dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
pq.push(make_pair(dist[v], v));
}
}

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

You might also like