DAA Practical Removed
DAA Practical Removed
6.11 Objective
Given a (directed/undirected) graph, design an algorithm and implement it using a program to
find if a path exists between two given vertices or not. (Hint: use DFS)
Input Format:
Input will be the graph in the form of adjacency matrix or adjacency list.
Source vertex number and destination vertex number is also provided as an input.
Output Format:
Output will be 'Yes Path Exists' if path exists, otherwise print 'No Such Path Exists'.
6.12 Output
6.21 Objective
Given a graph, design an algorithm and implement it using a program to find if a graph is
bipartite or not. (Hint: use BFS)
Input Format:
Input will be the graph in the form of adjacency matrix or adjacency list.
Output Format:
Output will be 'Yes Bipartite' if graph is bipartite, otherwise print 'Not Bipartite'.
6.22 Output
6.31 Objective
Given a directed graph, design an algorithm and implement it using a program to find whether
cycle exists in the graph or not.
Input Format:
Input will be the graph in the form of adjacency matrix or adjacency list.
Output Format:
Output will be 'Yes Cycle Exists' if cycle exists otherwise print 'No Cycle Exists'.
6.32 Output
Week 7
7.11 Objective
After end term examination, Akshay wants to party with his friends. All his friends are living as
paying guest and it has been decided to first gather at Akshay’s house and then move towards
party location. The problem is that no one knows the exact address of his house in the city.
Akshay as a computer science wizard knows how to apply his theory subjects in his real life and
came up with an amazing idea to help his friends. He draws a graph by looking in to location of
his house and his friends’ location (as a node in the graph) on a map. He wishes to find out
shortest distance and path covering that distance from each of his friend’s location to his house
and then whatsapp them this path so that they can reach his house in minimum time. Akshay has
developed the program that implements Dijkstra’s algorithm but not sure about correctness of
results. Can you also implement the same algorithm and verify the correctness of Akshay’s
results? (Hint: Print shortest path and distance from friends’ location to Akshay’s house)
Input Format:
Input will be the graph in the form of adjacency matrix or adjacency list.
Source vertex number is also provided as an input.
Output Format:
Output will contain V lines.
Each line will represent the whole path from destination vertex number to source vertex number
along with minimum path weigth.
Sample I/O Problem I and II:
Input: Output:
5 1:0
04100 231:3
00004 31:1
02040 431:3
00004 5231:7
00000
1
7.12 Output
7.21 Objective
Design an algorithm and implement it using a program to solve previous question's problem
using Bellman- Ford's shortest path algorithm.
Input Format:
Input will be the graph in the form of adjacency matrix or adjacency list.
Source vertex number is also provided as an input.
Output Format:
Output will contain V lines.
Each line will represent the whole path from destination vertex number to source vertex number
along with minimum path weigth.
7.22 Output
7.31 Objective
Given a directed graph with two vertices ( source and destination). Design an algorithm and
implement it using a program to find the weight of the shortest path from source to destination
with exactly k edges on the path.
Input Format:
First input line will obtain number of vertices V present in the graph.
Graph in the form of adjacency matrix or adjacency list is taken as an input in next V lines.
Next input line will obtain source and destination vertex number. Last
input line will obtain value k.
Output Format:
Output will be the weigth of shortest path from source to destination having exactly k edges.
If no path is available then print “no path of length k is available”.
7.32 Output
Week 8
8.11 Objective
Assume that a project of road construction to connect some cities is given to your friend. Map of
these cities and roads which will connect them (after construction) is provided to him in the form
of a graph. Certain amount of rupees is associated with construction of each road. Your friend
has to calculate the minimum budget required for this project. The budget should be designed in
such a way that the cost of connecting the cities should be minimum and number of roads
required to connect all the cities should be minimum (if there are N cities then only N-1 roads
need to be constructed). He asks you for help. Now, you have to help your friend by designing an
algorithm which will find minimum cost required to connect these cities. (use Prim's algorithm)
Input Format:
The first line of input takes number of vertices in the graph.
Input will be the graph in the form of adjacency matrix or adjacency list.
Output Format:
Output will be minimum spanning weight
Input: Output:
7 Minimum Spanning Weight: 39
0075000
0085000
7809700
5 0 9 0 15 6 0
0 5 7 15 0 8 9
0 0 0 6 8 0 11
0 0 0 0 9 11 0
8.12 Output
8.21 Objective
Implement the previous problem using Kruskal's algorithm.
Input Format:
The first line of input takes number of vertices in the graph.
Input will be the graph in the form of adjacency matrix or adjacency list.
Output Format:
Output will be minimum spanning weight
8.21 Output
8.31 Objective
Assume that same road construction project is given to another person. The amount he will earn from
this project is directly proportional to the budget of the project. This person is greedy, so he decided
to maximize the budget by constructing those roads who have highest construction cost. Design an
algorithm and implement it using a program to find the maximum budget required for the project.
Input Format:
The first line of input takes number of vertices in the graph.
Input will be the graph in the form of adjacency matrix or adjacency list.
Output Format:
Out will be maximum spanning weight.
8.32 Output
Week 9
9.11 Objective
Given a graph, Design an algorithm and implement it using a program to implement Floyd-
Warshall all pair shortest path algorithm.
Input Format:
The first line of input takes number of vertices in the graph.
Input will be the graph in the form of adjacency matrix or adjacency list. If a direct edge is not
present between any pair of vertex (u,v), then this entry is shown as AdjM[u,v] = INF.
Output Format:
Output will be shortest distance matrix in the form of V X V matrix, where each entry (u,v)
represents shortest distance between vertex u and vertex v.
9.12 Output
9.21 Objective
Given a knapsack of maximum capacity w. N items are provided, each having its own value and
weight. You have to Design an algorithm and implement it using a program to find the list of the
selected items such that the final selected content has weight w and has maximum value. You can
take fractions of items,i.e. the items can be broken into smaller pieces so that you have to carry
only a fraction xi of item i, where 0 ≤xi≤ 1.
Input Format:
First input line will take number of items N which are provided.
Second input line will contain N space-separated array containing weights of all N items.
Third input line will contain N space-separated array containing values of all N items.
Last line of the input will take the maximum capacity w of knapsack.
Output Format:
First output line will give maximum value that can be achieved.
Next Line of output will give list of items selected along with their fraction of amount which has
been taken.
9.22 Output
9.31 Objective
Given an array of elements. Assume arr[i] represents the size of file i. Write an algorithm and a
program to merge all these files into single file with minimum computation. For given two files
A and B with sizes m and n, computation cost of merging them is O(m+n). (Hint: use greedy
approach)
Input Format:
First line will take the size n of the array.
Second line will take array s an input.
Output Format:
Output will be the minimum computation cost required to merge all the elements of the array.
Solved example: Consider arr[5] = { 10, 5, 100, 50, 20, 15}. As per the brute force approach,
first of all merge first two files (having 10 and 5 file size).
Cost of merging will be = 10+5=15.
List will become {15, 100, 50, 20, 15}.
Similarly, again merging first two files ( i.e. having 15 and 100 file size).
Cost of merging will be = 15+100=115.
List will become {115, 50, 20, 15}.
For the subsequent steps the list becomes, (165, 20, 15}, {185, 15} and {200}.
Therefore total cost of merging = 15+115+165+185+200 = 680.
But this is not minimum computation cost. To find minimum cost, consider the order arr[5] = {5,
10, 15, 20, 50, 100}. By applying the same approach, the total cost of merging =
15+30+50+100+200 = 395.
9.32 Output
Week 10
10.11 Objective
Given a list of activities with their starting time and finishing time. Your goal is to select
maximum number of activities that can be performed by a single person such that selected
activities must be non-conflicting. Any activity is said to be non-conflicting if starting time of an
activity is greater than or equal to the finishing time of the other activity. Assume that a person
can only work on a single activity at a time.
Input Format:
First line of input will take number of activities N.
Second line will take N space-separated values defining starting time for all the N activities.
Third line of input will take N space-separated values defining finishing time for all the N
activities.
Output Format:
Output will be the number of non-conflicting activities and the list of selected activities.
10.12 Output
10.21 Objective
Given a long list of tasks. Each task takes specific time to accomplish it and each task has a
deadline associated with it. You have to design an algorithm and implement it using a program to
find maximum number of tasks that can be completed without crossing their deadlines and also
find list of selected tasks.
Input Format:
First line will give total number of tasks n.
Second line of input will give n space-separated elements of array representing time taken by
each task.
Third line of input will give n space-separated elements of array representing deadline associated
with each task.
Output Format:
Output will be the total number of maximum tasks that can be completed.
10.22 Output
10.31 Objective
Given an unsorted array of elements, design an algorithm and implement it using a program to
find whether majority element exists or not. Also find median of the array. A majority element is
an element that appears more than n/2 times, where n is the size of array.
Input Format:
First line of input will give size n of array.
Second line of input will take n space-separated elements of array.
Output Format:
First line of output will be 'yes' if majority element exists, otherwise print 'no'.
Second line of output will print median of the array.
10.32 Output
Week 11
11.11 Objective
Given a sequence of matrices, write an algorithm to find most efficient way to multiply these
matrices together. To find the optimal solution, you need to find the order in which these
matrices should be multiplied.
Input Format:
First line of input will take number of matrices n that you need to multiply.
For each line i in n, take two inputs which will represent dimensions aXb of matrix i.
Output Format:
Output will be the minimum number of operations that are required to multiply the list of
matrices.
Solved Example: Consider a sequence of three matrices A of size 10X30, B of size 30X5, C of
size 5X60. Then,
(AB)C = (10*30*5) + (10*5*60) = 4500 operations
A(BC) = (30*5*60) + (10*30*60) = 27000 operations.
Hence the ouput of the program must be 4500
11.12 Output
11.21 Objective
Given a set of available types of coins. Let suppose you have infinite supply of each type of coin.
For a given value N, you have to Design an algorithm and implement it using a program to find
number of ways in which these coins can be added to make sum value equals to N.
Input Format:
First line of input will take number of coins that are available.
Second line of input will take the value of each coin.
Third line of input will take the value N for which you need to find sum.
Output Format:
Output will be the number of ways.
Solved Example: Let coin value set is C = {2,3,6,5} and the value N = 10. There are five
solutions: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Hence the output is 5.
11.22 Output
11.31 Objective
Given a set of elements, you have to partition the set into two subsets such that the sum of
elements in both subsets is same. Design an algorithm and implement it using a program to solve
this problem.
Input Format:
First line of input will take number of elements n present in the set.
Second line of input will take n space-separated elements of the set.
Output Format:
Output will be 'yes' if two such subsets found otherwise print 'no'.
Solved Example: Let set is S = {1, 5, 4, 11, 5, 14, 10}. Sum of the elements =
1+5+4+11+5+14+10 = 50. Now dividing the set into two halves such that sum of elements of
both the subsets = (50/2) = 25. Therefore, subsets are {1, 5, 5, 14} and {4, 11, 10}.
11.32 Output
Week 12
12.11 Objective
Given two sequences, Design an algorithm and implement it using a program to find the length
of longest subsequence present in both of them. A subsequence is a sequence that appears in the
same relative order, but not necessarily contiguous.
Input Format:
First input line will take character sequence 1.
Second input line will take character sequence 2.
Output Format:
Output will be the longest common subsequence along with its length.
Solved Example: Consider two input strings “AGGTAB” and “GXTXAYB. Then the length of
longest common subsequence is 4 i.e. for subsequence “GTAB”.
12.12Output
12.21 Objective
Given a knapsack of maximum capacity w. N items are provided, each having its own value and
weight. Design an algorithm and implement it using a program to find the list of the selected
items such that the final selected content has weight <= w and has maximum value. Here, you
cannot break an item i.e. either pick the complete item or don't pick it. (0-1 property).
Input Format:
First line of input will provide number of items n.
Second line of input will take n space-separated integers describing weights for all items.
Third line of input will take n space-separated integers describing value for each item.
Last line of input will give the knapsack capacity.
Output Format:
Output will be maximum value that can be achieved and list of items selected along with their
weight and value.
12.22 Output
12.31 Objective
Given a string of characters, design an algorithm and implement it using a program to print all
possible permutations of the string in lexicographic order.
Input Format:
String of characters is provided as an input.
Output Format:
Output will be the list of all possible permutations in lexicographic order.
12.32 Output
Week 13
13.11 Objective
Given an array of characters, you have to find distinct characters from this array. Design an
algorithm and implement it using a program to solve this problem using hashing. (Time
Complexity = O(n))
Input Format:
First line of input will give the size n of the character array.
Second line of input will give n space-separated elements to character array.
Output Format: Output will be the list of characters present in the array in alphabetical order
and frequency of each character in the array.
13.12 Output
13.21 Objective
Given an array of integers of size n, design an algorithm and write a program to check whether
this array contains duplicate within a small window of size k < n.
Input Format:
First input line contains number of test cases T.
For each test case T, there will be three input lines.
First line contains size n of array.
Second input line contains n space-separated array elements.
Third input line contains value k.
Output Format:
Output will have T number of lines.
For each test case, output will be “Duplicate present in window k” if the duplicate element is
found in the array, otherwise “Duplicate not present in window k”.
13.22 Output
13.31 Objective
Given an array of nonnegative integers, Design an algorithm and implement it using a program
to find two pairs (a,b) and (c,d) such that a*b = c*d, where a, b, c and d are distinct elements of
array.
Input Format:
First line of input will give size of array n.
Second line of input will give n space-separated array elements.
Output Format:
First line of output will give pair (a,b)
Second line of output will give pair (c,d).
13.32 Output
Week 14
14.11 Objective
Given a number n, write an algorithm and a program to find nth ugly number. Ugly numbers are
those numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12,
15, 16, 18, 20, 24,. .... is sequence of ugly numbers.
Input:
First line of input will give number of test cases T.
For each test case T, enter a number n.
Output:
There will be T output lines.
For each test case T, Output will be nth ugly number.
14.12 Output
14.21 Objective
Given a directed graph, write an algorithm and a program to find mother vertex in a graph.
A mother vertex is a vertex v such that there exists a path from v to all other vertices of the
graph.
Input:
Graph in the form of adjacency matrix or adjacency list is provided as an input.
Output:
Output will be the mother vertex number.
14.22 Output