Aoa Lab Manual Sem IV 2024-25
Aoa Lab Manual Sem IV 2024-25
INSTITUTE MISSION
VISION OF DEPARTMENT
To create proficient graduates with technical knowledge and social value.
MISSION OF DEPARTMENT
To improvise teaching and learning process through modern tool
usage.
has performed and successfully completed all the practicals in the subject of
______________________________________________ for the academic year 2021 to 2022 as
prescribed by University of Mumbai.
DATE :- ____________.
Lab Objectives:
3 Strengthen the ability to identify and apply the suitable algorithm for the given real-
world problem.
Lab Outcomes: At the end of the course, the students will be able to
3 Demonstrate real world scenarios like resource allocation using knapsack algorithm
Description
1 Introduction
2. Finding Minimum and Maximum, Merge sort, Quick sort, Binary search
1
0/1 knapsack
5. N-queens problem
1 Sum of subsets
Graph coloring
Term Work:
Journal must include at least 2 assignments on content of theory and practical of “Analysis of
Algorithms”
The final certification and acceptance of term work ensures that satisfactory performance of
laboratory work and minimum passing marks in term work.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex engineering
activities with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological change.
Department of Computer Engineering
CSL401.2 Apply sorting algorithms like quick sort and merge sort for information
searching.
CSL401.3 Demonstrate real world scenarios like resource allocation using knapsack
algorithm
CSL401.4 Apply greedy approach to solve different problems to find optimal solution
Understand
Understand
experiment
experiment but Improper No
Understanding and drawn
5 conclusion less conclusion conclusion
correct
(R2) accurate
conclusion (3-2) (2-0)
(4-3)
(5-4)
Submission
Submission Submission
Punctuality and Submission after three
within a after two
discipline 5 after a week weeks or
week weeks
more
(R3) (3-2)
(5-4) (3-2)
(2-0)
TABLE OF CONTENTS
Date of Date of
Sr. Pg
Name of Experiment Grade Sign
No No
Performance Submission
11. Assignment
(A+B)
__________________________________________________________
_____
Lab Outcome :-
__________________________________________________________
_____
__________________________________________________________
_____
______________________________________
Practical Incharge
EXPERIMENT NO.: 1
2. LEARNING OBJECTIVES:
• To understand the insertion sort method of sorting
3. AIM:
• Sort a given set of n integer elements using Insertion Sort method.
• The elements can be read from a file or can be generated using the
random number generator.
• Demonstrate using C how the sorting method works
4.THEORY :
Insertion sort is a simple sorting algorithm that works similar to the way you sort
playing cards in your hands. The array is virtually split into a sorted and an unsorted
part. Values from the unsorted part are picked and placed at the correct position in
the sorted part.
5.Algorithm
To sort an array of size n in ascending order:
1: Iterate from arr[1] to arr[n] over the array.
2: Compare the current element (key) to its predecessor.
3: If the key element is smaller than its predecessor, compare it to the elements
before. Move the greater elements one position up to make space for the swapped
element.
Example:
Another Example:
12, 11, 13, 5, 6
Let us loop for i = 1 (second element of the array) to 4 (last element of the array)
i = 1. Since 11 is smaller than 12, move 12 and insert 11 before 12
11, 12, 13, 5, 6
i = 2. 13 will remain at its position as all elements in A[0..I-1] are smaller than 13
11, 12, 13, 5, 6
i = 3. 5 will move to the beginning and all other elements from 11 to 13 will move
one position ahead of their current position.
5, 11, 12, 13, 6
i = 4. 6 will move to position after 5, and elements from 11 to 13 will move one
position ahead of their current position.
5, 6, 11, 12, 13
6. PROGRAM:
7. OUTPUT:
8. RESULTS
9. LEARNING OUTCOMES:
Students are able to implement Insertion sort method of sorting which is based on
simple approach.
11. REMARKS:
EXPERIMENT NO.________
__________________________________________________________
Lab Outcome :-
__________________________________________________________
__________________________________________________________
______________________________________
Practical Incharge
EXPERIMENT NO.: 2
2. LEARNING OBJECTIVES:
● To study and implement selection sort algorithm.
3. AIM:
● To write a program to implement Selection Sort.
Shivajirao S
Jondhale College of
Engineering,
Dombivli (E)
Department of
Computer
Engineering
AOA Lab/ IV 1
Experiment
Number 1
Aim:
Shivajirao S
Jondhale College of
Engineering,
Dombivli (E)
Department of
Computer
Engineering
AOA Lab/ IV 1
Experiment
Number 1
Aim
4.THEORY:
The algorithm divides the input list into two parts: the sublist of items already
sorted,which is built up from left to right at the front (left) of the list, and the sublist
of items remaining to be sorted that occupy the rest of the list. Initially, the sorted
sublist is empty and the unsorted sublist is the entire input list. The algorithm
proceeds byfinding the smallest (or largest, depending on sorting order) element in
the unsorted sublist, exchanging it with the leftmost unsorted element (putting it in
sorted order),and moving the sublist boundaries one element to the right.
5.ALGORITHM
7.OUTPUT:
8.RESULT:
9. LEARNING OUTCOMES:
Students are able to implement Selection sort method of sorting which is based on
simple approach.
10. APPLICATION AREAS:
● The selection sort is used when a small list is to be sorted
11.REMARKS:
EXPERIMENT NO.________
__________________________________________________________
Lab Outcome :-
__________________________________________________________
__________________________________________________________
______________________________________
Practical Incharge
EXPERIMENT NO.: 3
2. LEARNING OBJECTIVES:
• To understand the merge sort method of sorting which is based on
divide and conquer approach.
3. AIM:
• Sort a given set of n integer elements using Merge Sort method
• The elements can be read from a file or can be generated using the
random number generator.
• Demonstrate using C how the divide-and- conquer method works
4.THEORY:
Merge sort is an application of divide and conquer technique which sorts a given
array by dividing into two halves and sorting each of them recursively and then
merging the two smaller sorted arrays into a single sorted one.
Example:
5.Algorithm:
MERGE(A, p, q,r)
//Merges A[p . . q] and A[q + 1 . .r].
1 n1 ← q − p + 1; n2 ← r − q
2 create array L[1 . . n1 + 1] and R[1 . . n2 + 1]
3 for i ← 1 to n1 do L[i] ← A[p + i − 1]
4 for j ← 1 to n2 do R[ j] ← A[q + j]
5 L[n1 + 1] ← ∞; R[n2 + 1] ← ∞
6 i ← 1, j ← 1
7 for k ← p to r do
8 if L[i] <=R[ j] then A[k] ← L[ j]; i++ ;
9 else A[k] ← R[ j]; j++
MERGE-SORT(A, p,r)
Sorts A[p . .r].
1. if p < r then
2. q ← b(p + 1)/2c
3. MERGE-SORT(A, p, q);
4. MERGE-SORT(A, q + 1,r)
5.MERGE(A, p, q,r);
6.PROGRAM
7.OUTPUT
8. RESULTS
9. LEARNING OUTCOMES:
Students are able to implement merge sort method of sorting which is based on
divide and conquer approach.
11.REMARKS:
EXPERIMENT NO.________
__________________________________________________________
Lab Outcome :-
__________________________________________________________
__________________________________________________________
______________________________________
Practical Incharge
EXPERIMENT NO. 4
2. LEARNING OBJECTIVES:
• To understand and apply the binary search algorithm for searching a sorted
list
3. AIM:
• In binary search, on a sorted array of numbers, we divide (decrease) the
array of numbers(search space), conquer the sub problems by recursively
looking at the middle point for our target and splitting until we find our target
the base case condition. Note binary search works on a sorted collection of
elements.
• Demonstrate using C/Java/Python how the divide-and- conquer method works
4. THEORY:
Divide and Conquer is probably the best known general algorithm design technique.
A divide and conquer algorithm works by recursively breaking down a problem into
two or more sub-problems of the same (or related) type, until these become simple
enough to be solved directly. The solutions to the sub-problems are then combined to
give a solution to the original problem.
Given a sorted array arr[] of n elements, write a function to search a given element x
in arr[].
A simple approach is to do a linear search. The time complexity of the above
algorithm is O(n). Another approach to perform the same task is using Binary
Search.
Binary Search: Search a sorted array by repeatedly dividing the search interval in
half. Begin with an interval covering the whole array. If the value of the search key is
less than the item in the middle of the interval, narrow the interval to the lower half.
Otherwise, narrow it to the upper half. Repeatedly check until the value is found or
the interval is empty.
Example :
5. Algorithm:
6.PROGRAM
7.OUTPUT:
8. RESULTS
9. LEARNING OUTCOMES:
Students are able to implement binary search method of sorting which is based on
divide and conquer approach.
11. REMARKS:
EXPERIMENT NO.________
__________________________________________________________
Lab Outcome:-
__________________________________________________________
__________________________________________________________
______________________________________
Practical Incharge
EXPERIMENT NO.: 5
2. LEARNING OBJECTIVES:
• To apply greedy method
3. AIM:
Implement, the Fractional Knapsack problem using Greedy method.
4. THEORY:
Given a knapsack with maximum capacity W,and a set S consisting of N items.
Each item i has some weight wi and benefit value bi (all wi and W are integer
values)
Problem: How to pack the knapsack to achieve maximum total value of packed
items? i.e.,to find
max∑bisubject to∑wi≤W
i∈T i∈T
The problem is called a “0-1” problem, because each item must be entirely
accepted or rejected .There are different ways to solve 0/1 Knapsack Problem
using Greedy Method.
5. Algorithm:
Greedy Knapsack(m,n)
//p[1..n]and w[1..n] contains the profits and weights respectively of the n objects
//ordered such that p[i]/w[i]>=p[i+1/w[i+1]
//m is the Knapsack size and x[1:n]is the solution vector
{
for i:= 1 to n do x[i] := 0.0; //Initialize x
U := m;
for i:= 1 to n do x[i] := 0.0;
{
if w[i]>U then break;
x[i] := 1; U := U-w[i]
}
if(i<=n) then x[i] := U/w[i];
}
7. LEARNING OUTCOMES:
● Students are able to implement fractional knapsack problem using greedy
programming approach
8. APPLICATION AREAS:
● Hard-real time systems-cache memory management, dynamic storage
management.
8. REMARKS:
EXPERIMENT NO.________
__________________________________________________________
Lab Outcome:-
__________________________________________________________
__________________________________________________________
______________________________________
Practical Incharge
EXPERIMENT NO.: 6
2. LEARNING OBJECTIVES:
• To understand the minimum cost spanning tree.
3. AIM: To find minimum cost spanning tree of a given undirected graph using
Prim’s and Kruskal’s algorithm
4. THEORY:
Kruskal's algorithm finds the minimum spanning tree for a weighted connected graph
G=(V,E) to get an acyclic subgraph with |V|-1 edges for which the sum of edge
weights is the smallest. Consequently the algorithm constructs the minimum
spanning tree as an expanding sequence of subgraphs, which are always acyclic but
are not necessarily connected on the intermediate stages of algorithm.
The algorithm begins by sorting the graph’s edges in non decreasing order of their
weights. Kruskal's algorithm finds the minimum spanning tree for a weighted
connected graph G=(V,E) to Then starting with the empty subgraph, it scans the
sorted list adding the next edge on the list to the current subgraph if such an inclusion
does not create a cycle and simply skipping the edge otherwise.
Prim’s Algorithm is preferred when the graph is dense i.e. when there are large
number of edges in the graph like E = O(V 2) because we do not have to pay much
attention to the cycles by adding an edge as we primarily deal with the vertices in
Prim’s Algorithm.
5.Algorithm:
Kruskal(G,w)
// Kruskal’s algorithm for constructing a minimum spanning tree
//Input: A weighted connected graph G=(V,E)
//Output: ET,the set of edges composing a minimum spanning tree of G
{
Sort E in non decreasing order of the edge weights w(ei1)<=…….>= w(ei |E|))
ET ← ∅ ; ecounter ← 0 //Initialize the set of tree edges and its size
k ← 0 //initialize the number of
processed edges while ecounter <|
V|-1 do
{
k ← k+1
if ET U {eik} is acyclic
ET ← ET U {eik}; ecounter ← ecounter+1
}
return ET
}
Prim(G,w,r)
// Prim’s algorithm for constructing a minimum spanning tree
//Input: A weighted connected graph G=(V,E)
//Output: ET,the set of edges composing a minimum spanning tree of G
{
for each u € G,V
u,key = ∞
u,π = NIL
r.key = 0
Q = G.V
while Q ≠ NULL
u = EXTRACT-MIN(Q)
for each v € G.Adj[u]
if v € Q and w(u,v)<v.key
v. π = u
v,key = w(u,v)
}
7. APPLICATION AREAS:
• Clustering Analysis
6. LEARNING OUTCOMES:
Students are able to implement program to find minimum cost spanning tree using
Kruskal and Prim’s algorithm.
9. REMARKS:
EXPERIMENT NO.________
__________________________________________________________
Lab Outcome:-
____________________________________________________
__________________________________________________________
______________________________________
Practical Incharge
EXPERIMENT NO.: 7
2. LEARNING OBJECTIVES:
• Learn about Single Shortest Path Problem is to find the distances from source
vertex to all other vertices in a given weighted connected graph((directed or
undirected).
• Learn about Dijkstra’s algorithm for Single Source Shortest Paths Problem.
3. AIM:
Write C/Java/Python programs to Implement Single Source Shortest Paths
problem using Dijkstra’s algorithm.
4. THEORY
Dijkstra’s algorithm finds a shortest path tree from a single source node, by building
a set of nodes that have minimum distance from the source.
5. Algorithm
1. While QQ is not empty, pop the node vv, that is not already in SS,
from QQ with the smallest distdist (vv). In the first run, source node ss will be
chosen because distdist(ss) was initialized to 0. In the next run, the next node
with the smallest distdist value is chosen.
2. Add node vv to SS, to indicate that vv has been visited
3. Update distdist values of adjacent nodes of the current node vv as follows: for
each new adjacent node uu,
7. LEARNING OUTCOMES:
• Students are able to develop program based on Dijkstra’s algorithm for
Single Source Shortest Paths Problem.
9. APPLICATION AREAS:
• Shortest path algorithms are applied to automatically find directions
between physical locations, such as driving directions on web mapping
websites like google maps.
• Other applications include robotics, VLSI design
9. REMARKS:
EXPERIMENT NO.________
__________________________________________________________
Lab Outcome:-
____________________________________________________
__________________________________________________________
______________________________________
Practical Incharge
EXPERIMENT NO.: 8
2. LEARNING OBJECTIVES:
• Learn about Job Sequencing Problem with Deadline Problem
3. AIM:
Write C/Java/Python programs to Implement Sequencing Problem with Deadline
Problem.
6. THEORY
This problem consists of n jobs each associated with a deadline and profit and our
objective is to earn maximum profit. We will earn profit only when job is
completed on or before deadline. We assume that each job will take unit time to
complete.
● In this problem we have n jobs j1, j2, … jn each has an associated deadline
d1, d2, … dn and profit p1, p2, ... pn.
● Profit will only be awarded or earned if the job is completed on or before
the deadline.
● We assume that each job takes unit time to complete.
● The objective is to earn maximum profit when only one job can be
scheduled or processed at any given time.
Consider the following 5 jobs and their associated deadline and profit.
index 1 2 3 4 5
JOB j1 j2 j3 j4 j5
DEADLINE 2 1 3 2 1
PROFIT 60 100 20 40 20
index 1 2 3 4 5
JOB j2 j1 j4 j3 j5
DEADLINE 1 2 2 3 1
PROFIT 100 60 40 20 20
As dmax = 3 so we will have THREE slots to keep track of free time slots. Set the
time slot status to EMPTY
time slot 1 2 3
If we look at job j2, it has a deadline 1. This means we have to complete job j2 in
time slot 1 if we want to earn its profit.
7. Algorithm
for i = 1 to n do
Set k = min(dmax, DEADLINE(i)) //where DEADLINE(i) denotes deadline of
ith job
while k >= 1 do
if timeslot[k] is EMPTY then
timeslot[k] = job(i)
break
endif
Set k = k - 1
endwhile
endfor
9. REMARKS:
EXPERIMENT NO.________
__________________________________________________________
Lab Outcome:-
____________________________________________________
__________________________________________________________
______________________________________
Practical Incharge
EXPERIMENT NO.: 9
2. LEARNING OBJECTIVES:
• Learn about Single Shortest Path Problem is to find the distances from source
vertex to all other vertices in a given weighted connected graph((directed or
undirected).
• Learn about Bellman Ford’s algorithm for Single Source Shortest Paths
Problem.
3. AIM:
Write C/Java/Python programs to Implement Single Source Shortest Paths
problem using Bellman Ford’s algorithm.
4.THEORY
The Bellman-Ford algorithm solves the single-source shortest-paths problem in the
general case in which edge weights may be negative. Given a weighted, directed
graph G (V, E) with source s and weight function W:E R, the Bellman-Ford
algorithm returns a boolean value indicating whether or not there is a negative-
weight cycle that is reachable from the source. If there is such a cycle, the
algorithm indicates that no solution exists. If there is no such cycle, the algorithm
produces the shortest paths and their weights. The algorithm relaxes edges,
progressively decreasing an estimate :d on the weight of a shortest path from the
source s to each vertex v € V until it achieves the actual shortest-path weight
δ(s,v).The algorithm returns TRUE if and only if the graph contains no negative-
weight cycles that are reachable from the source.
5.Algorithm
Bellman-Ford algorithm(G,w,s)
INITIALIZE-SINGLE-SOURCE(G,s)
d[s] ← 0
for each v ∈ V – {s}
do d[v] ← ∞
for i ← 1 to |V| – 1
do for each edge (u, v) ∈ E
do if d[v] > d[u] + w(u, v)
then d[v] ← d[u] + w(u, v)
for each edge (u, v) ∈ E
do if d[v] > d[u] + w(u, v)
then report that a negative-weight cycle exists
6.PROGRAM:
7.OUTPUT
8. RESULTS
9. LEARNING OUTCOMES:
• Students are able to develop program based on Bellman Ford’s
algorithm for Single Source Shortest Paths Problem.
9.APPLICATION AREAS:
• Shortest path algorithms are applied to automatically find directions
between physical locations, such as driving directions on web mapping
websites like google maps.
• Other applications include robotics, VLSI design
10. REMARKS:
EXPERIMENT NO.________
Aim of the Experiment:-
__________________________________________________________
__________________________________________________________
Lab Outcome:-
__________________________________________________________
__________________________________________________________
______________________________________
Practical Incharge
EXPERIMENT NO. 10
2. LEARNING OBJECTIVES:
• To understand the Travelling Salesman Problem.
4. THEORY:
In the TSP, given a set of cities and the distance between each pair of cities, a
salesman needs to choose the shortest path to visit every city exactly once and return
to the city from where he started.
Let’s take an example to understand the TSP in more detail:
Here, the nodes represent cities, and the values in each edge denote the distance
between one city to another. Let’s assume a salesman starts the journey from the
city . According to TSP, the salesman needs to travel all the cities exactly once and
get back to the city by choosing the shortest path. Here the shortest path means the
sum of the distance between each city travelled by the salesman, and it should be less
than any other path.
With only cities, the problem already looks quite complex. As the graph in the
example is a complete graph, from each city, the salesman can reach any other cities
in the graph. From each city, the salesman needs to choose a route so that he doesn’t
have to revisit any city, and the total distance travelled should be minimum.
5.Algorithm:
6. RESULTS & CONCLUSIONS:
7.LEARNING OUTCOMES:
Students are able to understand and implement Travelling Salesman Problem
8.REMARKS: