You are on page 1of 9

CIS 2203

Faculty:
Dr. Zoheir Ezziane
202110

Student name Student ID

Part 1 (CLO1): Logic and proof


 Express English statements in logic using propositions and logical connectives
Example 4:

Let P, Q and R be the propositions


P: You played football.
Q: You did warm up.
R: You got an injury.
Express the following statement in logic using the propositions; p, q and r and logical
connectives (¬ ∧ ∨ → ↔)
a) You played football without getting an injury. P ∧ ¬R
b) If you play football without warming up, you get an injury. (P ∧ ¬Q) →R
c) You get injured if and only if you don’t warm up. R ↔ ¬Q
d) You might get an injury if you played football or if you did not warm up.R → ( P ∨
¬Q)

Part 2 (CLO2): Sorting Algorithms


 Explore the process of one of the following Sorting functions
o Random Quick Sort

1.1 Write the Algorithm that describes the process (use the correct keywords covered in
Week7).
1) Randomly choose element as pivot
2) Swap pivot with first element
3) Check if the next element < pivot
4) If false, check the next element
5) If true
6) Swap index 2 with element index 1
7) Check if the next element <pivot
8) If false, check the next element
9) then randomly choose another element as pivot
10) Swap pivot index 3 with first element index 2
11) Check if element < pivot
12) If false, Check next element
13) Now we have two partition
14) Repeat again for partitions

1.2 Write the algorithm as a code in Python and test your code using any input.

from random import randint


def quicksort(alist, start, end):
if start < end:
pIndex = partition(alist, start, end)
quicksort(alist, start, pIndex-1)
quicksort(alist, pIndex+1, end)

return alist

def partition(alist, start, end):


pivot = randint(start, end)
temp = alist[end]
alist[end] = alist[pivot]
alist[pivot] = temp
pIndex = start

for i in range(start, end):


if alist[i] <= alist[end]:
temp = alist[i]
alist[i] = alist[pIndex]
alist[pIndex] = temp
pIndex += 1
temp1 = alist[end]
alist[end] = alist[pIndex]
alist[pIndex] = temp1

return pIndex

if __name__ == '__main__':
list = [2, 5, 6, 8, 3, 7, 5, 6, 20, 100, 85, -20]
print(quicksort(list, 0, len(list)-1))

the output of the code


Part 3 (CLO3): Complexity of
algorithm

 Explore the process of one of the following Algorithms


 Shell Sort algorithm is a sorting algorithm.

1.1 Calculate the total number of steps required by the algorithm, and express it as a
function of n (f(n) ).
1.2 Determine the Time complexity of the algorithm using Big-O notation.
2. #import math to use the sqrt function
3. import math
# A function to print all prime factors of # a given number n
def primeFactors(n):
4. # Print the number of two's that divide n
5. while n % 2 == 0: print (2) n=n/2
6. # n must be odd at this point
# so a skip of 2 ( i = i + 2) can be used, notice i is incremented by 2
for i in range(3,int(math.sqrt(n))+1,2):
7. # while i divides n , print i ad divide n
8. while n % i== 0:
9. print (i)
n=n/i

# Condition if n is a prime # number greater than 2


if n > 2:

print (n)
#call the function
primeFactors(315)

Answer :-

The first while loop can run at most m times so that 2rn >= n. Therefor at
worst log2 n times.

For loop runs √n times.

Inner while loop run log2 n times.


∴ n(log2n + √n log3n)
and

O(√n log n)
Part 4 (CLO4): Structures
1. Use Graph coloring to solve one of the following problems
o Class scheduling.
HCT needs to schedule the following classes so that no classes can be at the same time
if they are being taught by the same teacher or they have common students.
 CIS3003
 CIS1103
 CIS1203
 CIS1303
 CIS1403
 CIS3303
 CIS2403
 CIS2303
 CIS3403
You need to consider the following:
 All 1000 level classes’ students have students in common.
 All 2000 level classes’ students have students in common.
 All 3000 level classes’ students have students in common.
 CIS3003 and CIS 1403 are taught by Dr. Rashed.
 CIS1103 and CIS2403 are taught by Dr. Salem.
 CIS 1303 and CIS3403 are taught by Dr. Mohammad.
Use your knowledge in Graph coloring to solve this problem, create the schedule and
identify how many time slots will be needed to schedule the classes.
2. Construct a directed graph using an Adjacency matrix (use a matrix from the Appendix –
Part 4.2 as assigned by your teacher, each group must get a different matrix).

Adjacency Matrix 4
1, 0, 0, 0, 1, 0, 1, 0, 0,
0, 1, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 1, 0,
0, 1, 1, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 1, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 1, 0, 1,
0, 0, 0, 0, 0, 0, 1, 0, 0,
1, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 1, 0, 0, 1, 0,
3. Analyze the following weighed graph, and find the shortest path between two vertices
(each group will be assigned a different vertices by their teacher).

Vertices X and J

Shortest path = XW AGIJ


Distance = 994
4. Use Kruskal’s algorithm to construct the Minimum Spanning Tree (MST) for a graph (use
a graph from the Appendix – Part 4.4 as assigned by your teacher, each group must get
a different graph).

(9,5 1 🗸 (1,7 5 🗸
) )
(9,8 2 🗸 (2,4 5 🗸
) )
(0,3 2 🗸 (6,1 6 X
) )
(5,6 4 🗸 (0,7 6 X
) )
(6,9 5 x (8,4 6 🗸
) )
(6,8 5 X (8,0 7
) )
(6,0 5 🗸 (4,3 8
) )
n-1 = 10-
(7,3 5 🗸 (2,3 9 1 =9
) )
total
cost = 35

You might also like