Professional Documents
Culture Documents
Nagindas Khandwala College of Commerce, Arts & Management Studies & Shantaben Nagindaskhandwala College of Science Malad (W), Mumbai - 64
Nagindas Khandwala College of Commerce, Arts & Management Studies & Shantaben Nagindaskhandwala College of Science Malad (W), Mumbai - 64
Nagindas Khandwala College of Commerce, Arts & Management Studies & Shantaben Nagindaskhandwala College of Science Malad (W), Mumbai - 64
AUTONOMOUS INSTITUTION
(Affiliated To University Of Mumbai)
CERTIFICATE
This is certified to be a bonafide record of practical work done by the above student in the
college laboratory for the course DESIGN AND ANALYSIS OF
ALGORITHM(2142UISDA) for the partial fulfilment of FourthSemester of BSc IT during
the academic year 2021-22.
The journal work is the original study work that has been duly approved in the year 2021-22
by the undersigned.
N
DATE TITLE Page No SIGN
O
1a. Implement Insertion
Sort 1b. Implement Bubble
1. 4-12-2021 Sort 1c. Implement 1
Selection Sort 1d.
Implement Quick Sort 1e.
Implement Merge Sort
Implement Knapsack problem using
2. 11-12-2021 7
Dynamic Programming
Theory:- Insertion sort is a simple sorting algorithm that works the way we sort
playing cards in our hands.
Code:-
Output:-
1
B)
Aim:- Implement Bubble Sort.
Theory:- Bubble Sort is the simplest sorting algorithm that works by repeatedly
swapping the adjacent elements if they are in wrong order.
Code:-
Output:-
2
C)
Aim:- Implement Selection Sort.
Theory:- The selection sort algorithm sorts an array by repeatedly finding the
minimum element (considering ascending order) from unsorted part and putting
it at the beginning. The algorithm maintains two subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element (considering ascending
order) from the unsorted subarray is picked and moved to the sorted subarray.
Code:-
Output:-
3
D)
Aim:- Implement Quick Sort.
The key process in quickSort is partition(). Target of partitions is, given an array and
an element x of array as pivot, put x at its correct position in sorted array and put all
smaller elements (smaller than x) before x, and put all greater elements (greater
than x) after x. All this should be done in linear time.
Code:-
4
Output:-
E)
Aim:- Implement Merge Sort.
Theory:- Merge Sort is a Divide and Conquer algorithm. It divides input array
intwo halves, calls itself for the two halves and then merges the two sorted halves.
The merge() function is used for merging two halves. The merge(arr, l,m, r) is key
process that assumes that arr[l..m] and arr[m+1..r] are sorted andmerges the two
sorted sub-arrays into one.
Code:-
5
Output:-
6
Practical 2
Aim:- Implement Knapsack problem using Dynamic Programming.
Theory:- Here knapsack is like a container or a bag. Suppose we have given some
items which have some weights or profits. We have to put some items in the
knapsack in such a way total value produces a maximum profit.
For example, the weight of the container is 20 kg. We have to select the items in
such a way that the sum of the weight of items should be either smaller than or
equal to the weight of the container, and the profit should be maximum.
Code:-
Output:-
7
Practical 3
Aim:- From a given vertex in a weighted connected graph, find shortest paths to
other vertices using Dijkstra’s algorithm.
Theory:- Given a directed graph and a source vertex in the graph, the task is to find
the shortest distance and path from source to target vertex in the given graph
where edges are weighted (non-negative) and directed from parent vertex to source
vertices.
Approach:
2. Assign zero distance value to source vertex and infinity distance value to all
other vertices.
4. For current vertex, consider all of its unvisited children and calculate their
tentative distances through the current. (distance of current + weight of the
corresponding edge) Compare the newly calculated distance to the current assigned
value (can be infinity for some vertices) and assign the smaller one.
5. After considering all the unvisited children of the current vertex, mark the
current as visited and remove it from the unvisited set.
6. Similarly, continue for all the vertex until all the nodes are visited.
Code:-
8
Output:-
9
Practical 4
Aim:- Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal’s
algorithm.
Theory:- Given a connected and undirected graph, a spanning tree of that graph is
a subgraph that is a tree and connects all the vertices together. A single graph can
have many different spanning trees. A minimum spanning tree (MST) or minimum
weight spanning tree for a weighted, connected, undirected graph is a spanning tree
with a weight less than or equal to the weight of every other spanning tree. The
weight of a spanning tree is the sum of weights given to each edge of the spanning
tree.
A minimum spanning tree has (V – 1) edges where V is the number of vertices in the
given graph.
Code:-
10
Output:-
11
Practical 5
Aim:- Perform various tree traversal algorithms for a given tree.
Theory:- Unlike linear data structures (Array, Linked List, Queues, Stacks, etc)
which have only one logical way to traverse them, trees can be traversed in
different ways. Following are the generally used ways for traversing trees.
• Uses of Inorder
In the case of binary search trees (BST), Inorder traversal gives nodes in non-
decreasing order. To get nodes of BST in non-increasing order, a variation of Inorder
traversal where Inorder traversal s reversed can be used.
• Uses of Preorder
Preorder traversal is used to create a copy of the tree. Preorder traversal is also
used to get prefix expression on an expression tree.
• Uses of Postorder
Postorder traversal is used to delete the tree.Postorder traversal is also useful to get
the postfix expression of an expression tree.Example: Postorder traversal for the
above-given figure is 4 5 2 3 1.
Code:-
12
Output:-
13
Practical 6
Aim:- Print all the nodes reachable from a given starting node in a digraph using
both BFS and dfs methods.
Complexity: BFS has the same efficiency as DFS: it is Θ (V2) for Adjacency matrix
representation and Θ (V+E) for Adjacency linked list representation.
2. Depth First Traversal (or Search) for a graph is similar to Depth First Traversal
of a tree. The only catch here is, unlike trees, graphs may contain cycles (a node may
be visited twice). To avoid processing a node more than once, use a boolean visited
array.
root or any arbitrary node and mark the node and move to the adjacent unmarked
node and continue this loop until there is no unmarked adjacent node. Then
backtrack and check for other unmarked nodes and traverse them. Finally, print the
nodes in the path.
• Algorithm:
Create a recursive function that takes the index of the node and a visited array.
Traverse all the adjacent and unmarked nodes and call the recursive function with
the index of the adjacent node.
14
Code:
15
16
Output:-
17
Practical 7
Aim:- Implement any scheme to find the optimal solution for the Traveling
Salesperson problem and then solve the same problem instance using any
approximation algorithm and determine the error in the approximation.
Theory:-
Algorithm
2. Find out the shortest edge connecting current vertex and an unvisited vertex
V.
4. Mark V as visited.
6. Go to step 2.
Code:-
18
Output:-
19
Practical 8
Aim:- Find Minimum Cost Spanning Tree of a given undirected graph usingPrim’s
algorithm.
A group of edges that connects two set of vertices in a graph is called cut in graph
theory. So, at every step of Prim’s algorithm, we find a cut (of two sets, one contains
the vertices already included in MST and other contains rest of the vertices), pick
the minimum weight edge from the cut and include this vertex to MST Set (the set
that contains already included vertices).
How does Prim’s Algorithm Work? The idea behind Prim’s algorithm is simple, a
spanning tree means all vertices must be connected. So the two disjoint subsets
(discussed above) of vertices must be connected to make a Spanning Tree. And they
must be connected with the minimum weight edge to make it a Minimum Spanning
Tree.
Algorithm
1) Create a set mstSet that keeps track of vertices already included in MST.
2) Assign a key value to all vertices in the input graph. Initialize all key values as
INFINITE. Assign key value as 0 for the first vertex so that it is picked first.
a) Pick a vertex u which is not there in mstSet and has minimum key value.
b) Include u to mstSet.
c) Update key value of all adjacent vertices of u. To update the key values, iterate
through all adjacent vertices. For every adjacent vertex v, if weight of edge u-v is
less than the previous key value of v, update the key value as weight of u-v
20
The idea of using key values is to pick the minimum weight edge from cut. The key
values are used only for vertices which are not yet included in MST, the key value
for these vertices indicate the minimum weight edges connecting them to the set of
vertices included in MST.
Code:-
21
Output:-
22
Practical 9
Aim:- Implement N Queen's problem usingBacktracking.
Algorithm:
The idea is to place queens one by one in different columns, starting from the
leftmost column. When we place a queen in a column, we check for clashes with
already placed queens. In the current column, if we find a row for which there is no
clash, we mark this row and column as part of the solution. If we do not find such a
row due to clashes then we backtrack and return false.
Code:-
23
Output:-
24