Assignment PDF
Assignment PDF
Question 1 In a group of 60 people, 27 like cold drinks and 42 like hot drinks and each person likes at least one of the two
drinks. How many like both coffee and tea?
Question 2 Each student in a class of 40 plays at least one indoor game chess, carrom and scrabble. 18 play chess, 20 play
scrabble and 27 play carrom. 7 play chess and scrabble, 12 play scrabble and carrom and 4 play chess, carrom and
scrabble. Find the number of students who play (i) chess and carrom. (ii) chess, carrom but not scrabble
Question 3 What is Dijkstra’s algorithm? Give a simple example to elaborate how this algorithm works. Also, give any real
time application of Dijkstra’s algorithm.
[Practice this algorithm in the same way as other algorithms covered in class. No analysis is required for this
Algorithm]
Question 4 What is Binomial Theorem? Is it possible to find the coefficient of x7 in (1 + x)11?
Question 5 Compute C(9,6). [use formula of C(n,k)]
Question 6 A computer programming team has 14 members.
(a) How many ways can a group of seven be chosen to work on a project?
(b) Suppose eight team members are women and six are men. How many groups of seven can be chosen that
contain four women and three men
Question 7
List the first four terms of the following sequence, beginning with n = 0.
Question 8 Use the Extended Euclidean algorithm (GCD as linear combination) to find GCD (330, 156).
Question 9 What is the least common multiple of 233572 and 2433?
Question 10 State whether the pairs (17, 22), (10, 21) and (10, 24) are relatively prime.
Question 11 Determine whether 17 is congruent to 5 modulo 6 and whether 24 and 14 are congruent modulo 6.
Question 12 An urn contains four red and five blue balls. What is the probability that a ball chosen from the urn is blue?
Question 13 Use the Affine cipher to encrypt your name. Mention the value of k and a, you used while encryption.
Question 14 Decrypt the message using Ceaser Cipher, where shift is 21 “Adivggt, Ydnxmzoz Hvoczhvodxvg Nompxopmzn dn Jqzm!”
Question 15 Which graphs shown below have an Euler path or Euler circuit?
1 Page
Department of Computer Science, University of Engineering and Technology Lahore, KSK Campus
Question 16 Does the following graph have a Hamiltonian Circuit?
Question 18 Use Djikstras Algorithm to find out the shortest path from node 1.
Question 20 Draw the binary search tree that is created if the following numbers are inserted in the tree in the given order:
12 15 3 35 21 42 14
Also, write down the nodes in the order after pre, post and in-order walk.
Question 21 Sort the sequence [6 1 3 4 9 8 7] using Merge sort. Analyze the worst case of the given sorting algorithms. Using
linear search, find out the maximum element of the sorted array. How much time did it take to find that element?
Question 22 Find the following sum 1/(22 –1) +1/(42 –1) + 1/(62 –1) + …. +1/(202 –1).
Question 23 What is one to one, onto and into function? Give an example for each.
Question 24 Write the following series using summation notation, beginning with n = 1: 2 – 4 + 6 – 8 + 10
Question 25 Sorting:
To sort means to arrange a sequence of elements, like [a0, a1, a2, . . . , an] in ascending or descending order. In
practice sorting is used to arrange records based on a key – the key is a field in the record.
An algorithm that maps the given input/output pair is called a sorting algorithm
Input: An array A that contains n orderable elements A[0,1,2,…,n-1]
Output: A sorted permutation of A, named B, such that B [0] ≤ B [1] ≤ B [2] … ≤B [n-1] or vice versa depending on
the condition (ascending or descending).
There are two classes of Comparison Based Sorting Algorithms:
O(n2) O(nlogn )
Bubble Sort Merge Sort
Insertion Sort
The other type of sorting algorithms i.e. Distributive Sorting Algorithms include Radix Sort, Counting Sort and
Bucket Sort.
Comparing different Sorting Algorithms:
Bubble Sort: Under best-case conditions (the list is already sorted), the bubble sort can approach a constant
O (n) level of complexity. General-case is an abysmal O (n2)
While the insertion, selection, and shell sorts also have O (n 2) complexities, they are significantly more
efficient than bubble sort.
Insertion Sort: Like bubble sort, the insertion sort has a complexity of O (n 2). Although it has the same
complexity, the insertion sort is a little over twice as efficient as the bubble sort.
Merge Sort: The merge sort is slightly faster than the heap sort for larger sets, but it requires twice the
memory of the heap sort because of the second array.
Time Complexity
Algorithm
Best Case Average Case Worst Case
Insertion Sort O(n) O(n2) O(n2)
Bubble Sort O(n2) O(n2) O(n2)
Merge Sort O(nlogn) O(nlogn) O(nlogn)
Big O Notation Revised:
We can group functions into classes by focusing on the fastest-growing term in the expression for the number of
operations that they perform.
e.g., an algorithm that performs n2/2 – n/2 operations is a O(n2)-time or quadratic-time algorithm
• Common classes of algorithms:
Department of Computer Science, University of Engineering and Technology Lahore, KSK Campus
You have to fill in the table with execution time of the Sorting Algorithms. Results will vary for different input size
and different machines [any two different systems/laptops].
To do:
In the provided code, write a routine that generates a text file of unique random numbers and take it as an
input.
• For this you can use a built in function random () or rand ()
• Initially the number in text file must be 1000 and keep increasing +1000 numbers for next five runs, later
start doubling the input size for next five runs. Lastly, test it for 250000 and 500000 numbers.[see if it
crashes]
• You don’t need to do anything except for incrementing the number of iterations in for/while loop that
generates random numbers.
1000 2000 3000 4000 5000 10000 20000 40000 80000 160000 250000 500000
For output, a corresponding output file must be created to see if the code (logic) worked properly. i.e. the
file with numbers sorted accordingly [ascending/descending].
To measure the time taken by the function/routine we can use clock () function which is available time.h /
ctime.h. *see the code snippet for reference
#include <stdio.h>
#include <time.h>
void funcTest() { //Implementation }
int main() {
clock_t t;
t = clock();
funcTest(); //function call
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
printf("funcTest() took %f seconds to execute \n", time_taken);
return 0; }
Input Size [N] 1000 2000 3000 4000 5000 10000 20000 40000 80000 160000 250000 500000
Execution
Time X1ms X1ms X2ms X3ms X4ms X5ms X6ms X7ms X8ms -- -- --
[Machine A]
Execution
Time X1ms X1ms X2ms X3ms X4ms X5ms X6ms -- -- -- -- --
[Machine B]
*Code for Insertion Sort, Bubble Sort and Merge Sort would be provided
X
Drakhshan Bokhat
Lecturer CSD UET KSK