CSE2208-Lab Manual
CSE2208-Lab Manual
LABORATORY MANUAL
Content Page
no.
Course Objective 2
Preferred Tools 2
Reference Books 2
Heap Sort 11
Course Objective
Course objectives for the course are -
Page | 1
2. Learning how to design the algorithms from scratch
3. Coding algorithms in programming language
4. Evaluating time and space complexity of algorithms
Preferred Tools
1. Codeblocks
2. NetBeans
Reference Books
a. Introduction to Algorithms (3rd Edition)
i. Thomas H. Cormen
ii. Charles E. Leiserson
iii. Ronald L. Rivest
iv. Clifford Stein
b. Classic Data Structures
i. Debasis Samanta
Lab 1 - STL
Objective: Objective of this topic is to learn about Standard Template Library of C++. This
knowledge will be required throughout the course to code easily.
Standard Template Library is a C++ library which contains some useful data structures like
Stack, Queue, Vector, Priority Queue, Map etc. They come very handy while coding graph
theory related codes.
Vector
Page | 2
Vectors are sequence container that can change size. Container is an object that holds data
of same type. Sequence containers store elements strictly in linear sequence.
Vector stores elements in contiguous memory locations and enables direct access to any
element using operator []. Unlike array, vector can shrink or expand as needed at run time.
The storage of the vector is handled automatically.
The vector operations that will be used frequently in this course are:
.push() - It is used to insert an element at the back of a vector.
.size() - It is used to find out the number of elements kept inside the vector.
Example: You are given a list of N numbers in a vector. Write a program to find out if
summation of all odd numbers in the vector is greater than summation of all even
numbers.
5 YES
12345 NO
4
1234
#include <stdio.h>
#include <vector>
int main() {
int n;
scanf(“%d”, &n);
vector<int> v;
Page | 3
int a;
scanf(“%d”, &a);
v.push_back(a);
}
Map
Map is dictionary like data structure. It is a sequence of (key, value) pair, where only single
value is associated with each unique key. It is often referred as associative array.
Example: You are given a list of N student names and their achieved numbers in final. You
have to query a student name. If the student name and her/his number has already been
taken as input then you will check if the number is >= 80. In that case print “A+”. Otherwise
print “Not A+”. If the student’s name has not been taken as input, print “NO STUDENT
RECORD AVAILABLE”. The sample IO is given below:
Page | 4
Sample Input Sample Output
#include <iostream>
#include <map>
using namespace std;
int main() {
int n;
scanf("%d", &n);
string query;
cin >> query;
if(mp[query]) {
if(mp[query] >= 80.0)
cout << "A+" << endl;
else
cout << "NOT A+" << endl;
}
else {
cout << "NO STUDENT RECORD AVAILABLE" << endl;
}
return 0;
Page | 5
}
Priority Queue
Priority queues are a type of container adaptors, specifically designed such that its first
element is always the greatest of the elements it contains, according to some strict weak
ordering criterion.
This context is similar to a heap, where elements can be inserted at any moment, and only
the max heap element can be retrieved (the one at the top in the priority queue).
Priority queues are implemented as container adaptors, which are classes that use an
encapsulated object of a specific container class as its underlying container, providing a
specific set of member functions to access its elements. Elements are popped from the
"back" of the specific container, which is known as the top of the priority queue.
Useful priority queue operations for this course are given below:
.empty() - Test whether container is empty
.top() - Access top element
.push() - Insert element
.pop() - Remove top element
Example: You are given a list of N numbers. You need to write a program such that the
numbers will be stored in ascending order in a priority queue.
5 11347
14317
Page | 6
#include <stdio.h>
#include <queue>
int main() {
int n;
scanf(“%d”, &n);
priority_queue<int> q;
while(!q.empty()) {
int u = q.top(); q.pop();
printf(“%d ”, -u);
}
}
Task:
1. Write a program to demonstrate usage of Vector.
2. Write a program to demonstrate usage of Map.
3. Write a program to demonstrate usage of Priority queue
Lab 2 - Quicksort and Merge sort
Objective: Objective of the topics is to learn about quick sort and merge sort algorithm and
which one to use when. We will also learn the advantages and disadvantages of each
sorting mechanism.
Quick Sort
Quick sort works in the following steps:
1. Finds a pivot (an arbitrary number)
2. Bring all numbers less than pivot to its left
3. Bring all numbers greater than pivot to its right
4. Keep doing that to left of pivot and right of pivot
Page | 7
Algorithm Quicksort(A, lo, hi):
1. if lo < hi then
2. p = Partition(A, lo, hi)
3. Quicksort(A, lo, p - 1 )
4. Quicksort(A, p + 1, hi)
Example: A sample input output for sorting a vector of N numbers using quick sort.
5 -20 1 4 7 41
4 1 7 -20 41
Merge Sort
Merge sort works in the following steps:
1. Keep breaking down main array in half until they are just single numbers
2. Keep merging the broken parts and sort them while merging
3. Get the original array in sorted order
Page | 8
The merge sort algorithm is given below:
Example: A sample input output for sorting a vector of N numbers in descending order
using merge sort.
5 747 741 11 11 -7
11 747 11 -7 741
Tasks:
1. Write a program to sort a list of numbers in ascending order using merge sort
2. Write a program to sort a list of numbers in ascending order using quicksort.
Page | 9
Lab 3 - Heap Sort
Objective: Objective of the topic is to learn about heap sort algorithm and advantage of
using it.
Heap sort is a sorting algorithm that runs as fast as Merge sort but doesn’t take any extra
space. It can be used using Max or Min heapify. Time complexity for heap sort is O(nlogn).
Page | 10
8. largest = r
9. if largest != i
10. exchange A[i] with A[largest]
11. MAX-HEAPIFY(A, largest)
Example: A sample input output for sorting a vector of N numbers using Heap sort.
5 -41 1 2 3 5
5 1 -41 2 3
Note:
● Time complexity of Heap sort is : O(nlogn)
Tasks:
1. Write a program to sort a list of numbers in ascending order using heap sort
Page | 11
Lab 4 - DFS, BFS
Objective: Objective of this class is to learn about traversal algorithms DFS and BFS. These 2 are
one of the basic graph algorithms which will also help getting familiar with graph input and
handling adjacency list.
Algorithm: DFS(u)
1. Color[u] = GREY
2. for each v in Adj[u]
3. if Color[v] = WHITE then
4. do DFS(v)
5. Color[v] = BLACK
Page | 12
The Breadth First Search algorithm is given below:
Color = 1D Array of size |V|
Adj = 2D List of |V| rows
Algorithm: BFS(s)
1. Color[s] = 1
2. ENQUEUE(Q, s)
3. while Q != EMPTY
4. u = DEQUEUE(Q)
5. for each v in Adj[u]
6. if Color[v] = 0 then
7. do ENQUEUE(Q, v)
8. Color[v] = 1
Example: You are given graph containing V vertices and E edges. You need to find the DFS and BFS
traversal for the graph. First line will contain V and E. Next E lines will contain the edges.
54 DFS: 5 4 3 2 1
12 BFS: 1 2 3 4 5
23
34
45
Notes:
● Time complexity of DFS is O(V + E)
● Time complexity of BFS is O(V + E)
Tasks:
1. Given a graph write a program to print the DFS traversal output.
2. Given a graph write a program to print the BFS traversal output.
Page | 13
Lab 5 - Topological Sort, Strongly Connected Component
Topological Sort
Objective: Suppose we are given a list of tasks where to work on a task b we need another
task a to be completed before. We need to order the tasks in such a way that no dependent
task will come before the task(s) on which it is dependent. To solve this type of problems,
we need to learn Topological Sorting.
Page | 14
Topological Sorting of vertices of a Directed Acyclic Graph is an ordering of the vertices
1, 2 , . . . , in such a way, that if there is an edge directed towards vertex from
vertex , then comes before . For example consider the graph given below:
1 2 4
3 5
To find out the topological sort order, we can use the following algorithm:
Algorithm: TOPOLOGICAL-SORT(G)
Notes:
● A topological sort for the above graph is: 1 2 3 4 5
● Multiple topological sort is possible for a single graph
● We can perform a topological sort in time 𝛩 (V + E), DFS search takes
𝛩(V + E) time and it takes O(1) time to insert each of the |V| vertices onto the front
of the linked list or to push in the stack.
Task:
1. Given a graph where some nodes are dependent on some other nodes, find a
topologically sorted order.
Page | 15
Strongly Connected Component (SCC)
To find out the strongly connected components, we can use the following algorithm:
Algorithm: STRONGLY-CONNECTED-COMPONENT(G)
Example - SCC: You are given a directed graph containing V vertices and E edges. The first
line of the input will contain V and E. The next E lines will contain 2 numbers. The
endpoints. The sample input output is given below:
69 2
12 123
Page | 16
23 456
31
16
36
64
45
56
35
Notes:
● According to the algorithm - the SCCs for the Graph mentioned above are: [1 ,2 ,3]
and [6, 5, 4]
● = (V, ), where = {(u, v) : (v, u) ∈ E}. That is, consists of the edges of G
with their directions reversed.
● Time to create is O(V + E)
Task:
1. Find out the number of SCCs of a directed graph. Print all the SCCs.
Objective: Suppose we are given a set of nodes which can be connected by some edges. It
takes certain amount of cost to create the edges. Our objective is to reduce the cost by
creating only the edges that are necessary. We also need to minimize the total cost of
weight. To solve this type of problems, we need Minimum Spanning Tree algorithm. This
topic covers MST Kruskal.
Page | 17
Kruskal Algorithm uses the following algorithm to find MST of a graph:
Example: You are given an undirected weighted graph. You have to find the MST using
Kruskal algorithm. First line of the input will contain the number of vertices V and the
number of edges E. The next E lines will contain the endpoints of each edge and their
weight. The output will only contain the edges of the MST.
57 121
121 235
137 253
235 452
244
253
452
356
Note:
● In Kruskal’s algorithm, most time consuming operation is sorting because the total
complexity of the Disjoint-Set operations will be O(ElogV), which is the overall Time
Complexity of the algorithm.
Tasks:
1. Find MST of a given graph using Kruskal Algorithm
Page | 18
Lab 7 - Minimum Spanning Tree (Prim’s)
Objective: Objective of this topic is to learn Prim’s algorithm to find MST of a graph.
Page | 19
11. key[v] ← w(u, v)
Notes:
● The time complexity of the Prim’s Algorithm is O((V+E)logV) because each vertex is
inserted in the priority queue only once and insertion in priority queue take
logarithmic time.
Tasks:
1. Find MST of a given graph using Prim’s Algorithm
Page | 20
Lab 9 - Dijkstra Algorithm
Objective: The objective of single source shortest path problem is to find paths between
source and all vertices in a graph such that the total distance is minimum. This topic covers
Dijkstra algorithm for finding shortest path.
Page | 21
Algorithm: DIJKSTRA(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. S←𝜙
3. Q ←V[G]
4. while Q != 𝜙
5. do u ←EXTRACT-MIN(Q)
6. S ←S U {u}
7. for each vertex v ∈ Adj[u]
8. do RELAX(u, v, w)
Example: You are given a directed weighted graph with V vertices and E edges. Each edge
will have the end points and the weight. First line of the input will contain V and E. The next
E lines will contain the edges. You need to find the distance of all nodes from Node 1 using
Dijkstra algorithm. The Dijkstra algorithm output for a sample input graph is given below:
Sample Input Sample Output
33 10
1 2 10 2 10
1 3 60 3 50
2 3 40
Note:
● Time Complexity of Dijkstra's Algorithm is O(V^2) but with min-priority queue it
drops down to O(V+ElogV).
Task:
1. Find out single source shortest path for a directed graph where nodes are non-
negative.
Lab 10 - Bellman-Ford Algorithm
Objective: The objective of single source shortest path problem is to find paths between
source and all vertices in a graph such that the total distance is minimum. In this topic, we
will be using BELLMAN-FORD algorithm.
The Bellman-Ford algorithm solves the single-source shortest-paths problem in the general
case in which edge weights may be negative.
Page | 22
2. for i ← 1 to |V[G]| − 1
3. do for each edge (u, v) ∈ E[G]
4. do RELAX(u, v, w)
5. for each edge (u, v) ∈ E[G]
6. do if d[v] > d[u] + w(u, v)
7. then return FALSE
8. return TRUE
5 5
6 6 2 4
s - s -
8 - 7 8 - 7
0 2 - 0 2 -
7 7
7 -
9 9
Notes:
● The Bellman-Ford algorithm runs in time O(VE)
Task:
1. Find out single source shortest path for a directed graph where nodes can be
negative.
Lab 11 - Floyd-Warshall Algorithm
Objective: The objective of all pair shortest path problem is to find paths between all pairs
of vertices in a graph such that the total distance is minimum.
Algorithm: FLOYD-WARSHALL(W)
1. n ← rows[W]
2. D(0) ← W
3. for k ← 1 to n
4. do for i ← 1 to n
5. do for j ← 1 to n
6. do ← min( 1 , 1 +
1
)
Page | 23
7. return D(n)
Example: You are given a directed weighted graph with V vertices and E edges. Each edge
will have the end points and the weight. First line of the input will contain V and E. The next
E lines will contain the edges. You need to find the distance of all nodes from every other
nodes using Floyd Warshall algorithm. The Floyd Warshall algorithm output for a sample
input graph is given below:
33 110
1 2 10 1 2 10
1 3 60 1 3 50
2 3 40 2 1 INF
220
2 3 40
3 1 INF
3 2 INF
330
Note:
● Time Complexity of Floyd Warshall Algorithm is O(V^3)
Task:
1. Find out all pair shortest path of a graph.
Objective: Objective of this topic is to learn about Greedy approach and when to use it.
A greedy algorithm, as the name suggests, always makes the choice that seems to be the
best at that moment. This means that it makes a locally-optimal choice in the hope that this
choice will lead to a globally-optimal solution.
Page | 24
Example - Activity Selection Problem: You are given a list of N tasks with their starting Si
and ending time Ei, where 1 <= i <= N. You need to find out the maximum number of tasks
you can complete such that no 2 tasks overlap. Given below is a sample input and output
which is generated using Greedy algorithm for Activity Selection problem.
Sample Input Sample Output
5 3
15
23
47
46
7 10
Example - Fractional Knapsack Problem: You are given N items and a Capacity CAP. Item
i (1 <= i <= N) has Wi weight and Ci cost. You need to find out the maximum cost you can
make such that total weight is less than CAP. You can take an item partially. A sample input
for the problem and output using Greedy approach is given below:
Sample Input Sample Output
35 7.5
34
77
76
Task:
● Write a program to demonstrate activity selection problem solution
Objective: Objective of this topic is to learn about DP and backtracking approach and why
are they necessary.
Page | 25
The intuition behind dynamic programming is that we trade space for time, i.e. to say that
instead of calculating all the states taking a lot of time but no space, we take up space to
store the results of all the sub-problems to save time later.
Example - Longest Common Subsequence (LCS): You are given 2 strings. You need to
find a longest common subsequence of them. A sample input output using Dynamic
Programming is given below:
ABCEFGH ABCEFH
ABHCEHFH
Example - 0-1 Knapsack Problem: You are given N items and a Capacity CAP. Item i (1 <=
i <= N) has Wi weight and Ci cost. You need to find out the maximum cost you can make
such that total weight is less than CAP. A sample input for the problem and output using
Dynamic programming is given below:
3 10 9
66
55
33
Backtracking
Backtracking is an algorithm for capturing some or all solutions to given computational
issues, especially for constraint satisfaction issues. The algorithm can only be used for
problems which can accept the concept of a “partial candidate solution” and allows a quick
test to see if the candidate solution can be a complete solution. Backtracking is considered
an important technique to solve constraint satisfaction issues and puzzles. It is also
Page | 26
considered a great technique for parsing and also forms the basis of many logic
programming languages.
Example: You are given an NxN chessboard. The task is to place N queens on that board
such that no 2 queens attack each other. A sample valid output using Backtrack algorithm
on a 4x4 chessboard is given below -
Task:
● Write a program to demonstrate 0-1 Knapsack problem solution
● Write a program to demonstrate N-Queens problem using Backtracking
Midterm Examination
There will be a 10 marks examination containing algorithm problems, simulations and multiple
choice questions.
Page | 27