├── CSE221LabAssignment01_Summer2023 ├── Question.pdf ├── input1a.txt ├── input1b.txt ├── input2.txt ├── input3.txt ├── input4.txt ├── output1a.txt ├── output1b.txt ├── output2.txt ├── output3.txt ├── output4.txt ├── task1.py ├── task2.py ├── task3.py └── task4.py ├── CSE221LabAssignment02_Summer2023 ├── Question.pdf ├── explanation.pdf ├── input1.txt ├── input2.txt ├── input3.txt ├── input4.txt ├── output1.txt ├── output2.txt ├── output3.txt ├── output4.txt ├── task1.py ├── task2.py ├── task3.py └── task4.py ├── CSE221LabAssignment03_Summer2023 ├── Question.pdf ├── explanation.pdf ├── input1.txt ├── input2.txt ├── input3.txt ├── input4.txt ├── output1.txt ├── output2.txt ├── output3.txt ├── output4.txt ├── task1.py ├── task2.py ├── task3.py └── task4.py ├── CSE221LabAssignment04_Summer2023 ├── Question.pdf ├── explanation.pdf ├── input1a_2.txt ├── input1b_2.txt ├── input2_2.txt ├── input3_3.txt ├── input4_2.txt ├── input5_2.txt ├── input6_3.txt ├── output1a_2.txt ├── output1b_2.txt ├── output2_2.txt ├── output3_3.txt ├── output4_2.txt ├── output5_2.txt ├── output6_3.txt ├── task1A.py ├── task1B.py ├── task2.py ├── task3.py ├── task4.py ├── task5.py └── task6.py ├── CSE221LabAssignment05_Summer2023 ├── Question.pdf ├── explanation.pdf ├── input1_1.txt ├── input2_3.txt ├── input3_1.txt ├── output1_1.txt ├── output2_3.txt ├── output3_1.txt ├── task1A.py ├── task1B.py ├── task2.py └── task3.py ├── CSE221LabAssignment06_Summer2023 ├── Question.pdf ├── explanation.pdf ├── input1_1.txt ├── input1_2.txt ├── input2_1.txt ├── input2_2.txt ├── input2_3.txt ├── input3_1.txt ├── input3_2.txt ├── output1_1.txt ├── output1_2.txt ├── output2_1.txt ├── output2_2.txt ├── output2_3.txt ├── output3_1.txt ├── output3_2.txt ├── task1.py ├── task2.py └── task3.py ├── CSE221LabAssignment07_Summer2023 ├── Question.pdf ├── explanation.pdf ├── input1_1.txt ├── input1_2.txt ├── input1_3.txt ├── input2_1.txt ├── input2_2.txt ├── input2_3.txt ├── input2_4.txt ├── input3_1.txt ├── input3_2.txt ├── output1_1.txt ├── output1_2.txt ├── output1_3.txt ├── output2_1.txt ├── output2_2.txt ├── output2_3.txt ├── output2_4.txt ├── output3_1.txt ├── output3_2.txt ├── task1.py ├── task2.py └── task3.py └── CSE221LabAssignment08_Summer2023 ├── Question.pdf ├── explanation.pdf ├── input1_1.txt ├── input1_2.txt ├── input2_1.txt ├── input2_2.txt ├── input2_3.txt ├── input2_4.txt ├── input3_1.txt ├── input3_2.txt ├── output1_1.txt ├── output1_2.txt ├── output2_1.txt ├── output2_2.txt ├── output2_3.txt ├── output2_4.txt ├── output3_1.txt ├── output3_2.txt ├── task1.py ├── task2.py └── task3.py /CSE221LabAssignment01_Summer2023/Question.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MasiatHasin/CSE221-Algorithms-Lab-Summer2023/66192dc4e21b01b08615bd20ef7b80f455b645b3/CSE221LabAssignment01_Summer2023/Question.pdf -------------------------------------------------------------------------------- /CSE221LabAssignment01_Summer2023/input1a.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 10 3 | 19 4 | 7 5 | 3 6 | 100 -------------------------------------------------------------------------------- /CSE221LabAssignment01_Summer2023/input1b.txt: -------------------------------------------------------------------------------- 1 | 15 2 | calculate 67 + 41 3 | calculate 85 / 5 4 | calculate 13 - 56 5 | calculate 99 - 95 6 | calculate 3 / 10 7 | calculate 12 * 19 8 | calculate 14 - 6 9 | calculate 3 * 88 10 | calculate 45 * 68 11 | calculate 81 - 0 12 | calculate 77 + 40 13 | calculate 8 * 84 14 | calculate 73 - 22 15 | calculate 85 - 86 16 | calculate 28 * 58 -------------------------------------------------------------------------------- /CSE221LabAssignment01_Summer2023/input2.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 3 2 1 4 5 -------------------------------------------------------------------------------- /CSE221LabAssignment01_Summer2023/input3.txt: -------------------------------------------------------------------------------- 1 | 7 2 | 7 4 9 3 2 5 1 3 | 40 50 50 20 10 10 10 -------------------------------------------------------------------------------- /CSE221LabAssignment01_Summer2023/input4.txt: -------------------------------------------------------------------------------- 1 | 13 2 | ABCD will departure for Mymensingh at 00:30 3 | DhumketuExpress will departure for Chittagong at 02:30 4 | SubornoExpress will departure for Chittagong at 14:30 5 | ABC will departure for Dhaka at 17:30 6 | ShonarBangla will departure for Dhaka at 12:30 7 | SubornoExpress will departure for Rajshahi at 14:30 8 | ABCD will departure for Chittagong at 01:00 9 | SubornoExpress will departure for Dhaka at 11:30 10 | ABC will departure for Barisal at 03:00 11 | PadmaExpress will departure for Chittagong at 20:30 12 | ABC will departure for Khulna at 03:00 13 | ABCE will departure for Sylhet at 23:05 14 | PadmaExpress will departure for Dhaka at 19:30 -------------------------------------------------------------------------------- /CSE221LabAssignment01_Summer2023/output1a.txt: -------------------------------------------------------------------------------- 1 | 10 is an Even number. 2 | 19 is an Odd number. 3 | 7 is an Odd number. 4 | 3 is an Odd number. 5 | 100 is an Even number. -------------------------------------------------------------------------------- /CSE221LabAssignment01_Summer2023/output1b.txt: -------------------------------------------------------------------------------- 1 | The result of calculate 67 + 41 is 108 2 | The result of calculate 85 / 5 is 17.0 3 | The result of calculate 13 - 56 is -43 4 | The result of calculate 99 - 95 is 4 5 | The result of calculate 3 / 10 is 0.3 6 | The result of calculate 12 * 19 is 228 7 | The result of calculate 14 - 6 is 8 8 | The result of calculate 3 * 88 is 264 9 | The result of calculate 45 * 68 is 3060 10 | The result of calculate 81 - 0 is 81 11 | The result of calculate 77 + 40 is 117 12 | The result of calculate 8 * 84 is 672 13 | The result of calculate 73 - 22 is 51 14 | The result of calculate 85 - 86 is -1 15 | The result of calculate 28 * 58 is 1624 -------------------------------------------------------------------------------- /CSE221LabAssignment01_Summer2023/output2.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 4 5 -------------------------------------------------------------------------------- /CSE221LabAssignment01_Summer2023/output3.txt: -------------------------------------------------------------------------------- 1 | ID: 4 Mark: 50 2 | ID: 9 Mark: 50 3 | ID: 7 Mark: 40 4 | ID: 3 Mark: 20 5 | ID: 1 Mark: 10 6 | ID: 2 Mark: 10 7 | ID: 5 Mark: 10 -------------------------------------------------------------------------------- /CSE221LabAssignment01_Summer2023/output4.txt: -------------------------------------------------------------------------------- 1 | ABC will departure for Dhaka at 17:30 2 | ABC will departure for Barisal at 03:00 3 | ABC will departure for Khulna at 03:00 4 | ABCD will departure for Chittagong at 01:00 5 | ABCD will departure for Mymensingh at 00:30 6 | ABCE will departure for Sylhet at 23:05 7 | DhumketuExpress will departure for Chittagong at 02:30 8 | PadmaExpress will departure for Chittagong at 20:30 9 | PadmaExpress will departure for Dhaka at 19:30 10 | ShonarBangla will departure for Dhaka at 12:30 11 | SubornoExpress will departure for Chittagong at 14:30 12 | SubornoExpress will departure for Rajshahi at 14:30 13 | SubornoExpress will departure for Dhaka at 11:30 -------------------------------------------------------------------------------- /CSE221LabAssignment01_Summer2023/task1.py: -------------------------------------------------------------------------------- 1 | #Task 1 2 | def task_a(): 3 | f1 = open("input1a.txt", "r") 4 | f2 = open("output1a.txt", "w") 5 | f1 = f1.read().split("\n") 6 | T = int(f1[0]) 7 | res = '' 8 | for i in range(1, T+1, 1): 9 | N= f1[i] 10 | if (int(N) % 2==0): 11 | res = 'Even' 12 | else: 13 | res = 'Odd' 14 | if i arr[j+1]: 9 | arr[j], arr[j+1] = arr[j+1], arr[j] 10 | sortHappened = True 11 | # If a swap happens, it means that the array was not sorted. 12 | # So we set sortHappened to True. 13 | if sortHappened==False: 14 | break 15 | # sortHappened is False if no values needed swapping. This is only possible 16 | # if the loop has been completely sorted. So, We break out of the loop. 17 | return arr 18 | 19 | f1 = open("input2.txt","r") 20 | f2 = open("output2.txt","w") 21 | array = f1.read().split("\n")[1] 22 | array = array.split(" ") 23 | array = list(map(int, array)) 24 | sortedArray = bubbleSort(array) 25 | sortedArray = ' '.join(map(str, sortedArray)) 26 | f2.write(sortedArray) 27 | f2.close() -------------------------------------------------------------------------------- /CSE221LabAssignment01_Summer2023/task3.py: -------------------------------------------------------------------------------- 1 | #Task 3 2 | def insertionSortPairs(pairs): 3 | pairs = list(pairs.items()) 4 | for i in range(1, len(pairs)): 5 | id, mark = pairs[i] 6 | j = i - 1 7 | prevMark, prevId = pairs[j][1], pairs[j][0] 8 | while j>=0 and (int(prevMark) < int(mark) or (int(prevMark) == int(mark) and int(prevId) > int(id))): 9 | pairs[j+1] = pairs[j] 10 | j -= 1 11 | prevMark, prevId = pairs[j][1], pairs[j][0] 12 | pairs[j+1] = (id, mark) 13 | return pairs 14 | 15 | f1 = open("input3.txt","r") 16 | f2 = open("output3.txt","w") 17 | 18 | f1 = f1.read().split("\n") 19 | length = int(f1[0]) 20 | id = f1[1].split(" ") 21 | mark = f1[2].split(" ") 22 | 23 | pairs = {} 24 | 25 | for i in range(length): 26 | pairs[id[i]] = mark[i] 27 | 28 | sortedPairs = insertionSortPairs(pairs) 29 | 30 | for i in range(length): 31 | pair = sortedPairs[i] 32 | if i=0 and (prevTrain > train or (prevTrain == train and prevHour < hour)): 10 | info[j+1] = info[j] 11 | j -= 1 12 | prevTrain, prevDest, prevTime = info[j][0], info[j][1], info[j][2] 13 | prevHour = int(prevTime.split(":")[0]) 14 | info[j+1] = (train, dest, time) 15 | return info 16 | 17 | f1 = open("input4.txt","r") 18 | f2 = open("output4.txt","w") 19 | 20 | f1 = f1.read().split("\n") 21 | length = int(f1[0]) 22 | 23 | info = [] 24 | 25 | for i in range(1,length+1,1): 26 | line = f1[i].split(" ") 27 | train=line[0] 28 | dest=line[4] 29 | time=line[6] 30 | info.append([train,dest,time]) 31 | 32 | sortedInfo = insertionSortSchedule(info) 33 | 34 | for i in range(length): 35 | info = sortedInfo[i] 36 | if i right[j]: 21 | merged.append(left[i]) 22 | count += len(right) - j 23 | i += 1 24 | else: 25 | merged.append(right[j]) 26 | j += 1 27 | 28 | merged.extend(left[i:]) 29 | merged.extend(right[j:]) 30 | 31 | return merged, count 32 | 33 | f1 = open("input1.txt", "r") 34 | f2 = open("output1.txt", "w") 35 | inp = f1.read().split("\n") 36 | f1.close() 37 | arr = inp[1].split(" ") 38 | arr = list(map(int, arr)) 39 | sorted_array, count = countPair(arr) 40 | f2.write(str(count)) 41 | f2.close() -------------------------------------------------------------------------------- /CSE221LabAssignment03_Summer2023/task2.py: -------------------------------------------------------------------------------- 1 | def findMax(arr): 2 | if len(arr) <= 1: 3 | return arr, int(arr[0]) 4 | 5 | mid = len(arr) // 2 6 | left, max_value = findMax(arr[:mid]) 7 | right, max_value = findMax(arr[mid:]) 8 | 9 | findMaxMaind, max_value = findMaxMain(left, right, max_value) 10 | return findMaxMaind, max_value 11 | 12 | 13 | def findMaxMain(left, right, max_value): 14 | i = 0 15 | j = 0 16 | 17 | while i < len(left) and j < len(right): 18 | exp = left[i] + right[j]*right[j] 19 | max_value = max(max_value, exp) 20 | if left[i] <= right[j]: 21 | i += 1 22 | else: 23 | j += 1 24 | 25 | return left+right, max_value 26 | 27 | f1 = open("input2.txt", "r") 28 | f2 = open("output2.txt", "w") 29 | inp = f1.read().split("\n") 30 | f1.close() 31 | arr = inp[1].split(" ") 32 | arr = list(map(int, arr)) 33 | sorted, max = findMax(arr) 34 | f2.write(str(max)) 35 | f2.close() -------------------------------------------------------------------------------- /CSE221LabAssignment03_Summer2023/task3.py: -------------------------------------------------------------------------------- 1 | def quicksort(a,p,r): 2 | if pk-1: 20 | r = pivotIdx-1 21 | else: 22 | p = pivotIdx+1 23 | return None 24 | 25 | f1 = open("input4.txt", "r") 26 | f2 = open("output4.txt", "w") 27 | inp = f1.read().split("\n") 28 | f1.close() 29 | na = (inp[0].split(" "))[0] 30 | nq = (inp[0].split(" "))[2] 31 | arr = inp[1].split(" ") 32 | arr = list(map(int, arr)) 33 | 34 | for i in range(3, len(inp), 1): 35 | result = findKSmallest(arr, int(inp[i])) 36 | if i0: 8 | currentPath = pathList[0] 9 | lastNode = currentPath[-1] 10 | pathList.pop(0) 11 | nextNodes = G[lastNode] 12 | 13 | if end in nextNodes: 14 | currentPath.append(end) 15 | return currentPath 16 | 17 | for node in nextNodes: 18 | if not node in visited: 19 | newPath = list(currentPath) 20 | newPath.append(node) 21 | pathList.append(newPath) 22 | visited.append(node) 23 | return [] 24 | 25 | f1 = open("input5_2.txt", "r") 26 | f2 = open("output5_2.txt", "w") 27 | lines = f1.read().split("\n") 28 | N, M, E = map(int, lines[0].split()) 29 | 30 | G = {i:[] for i in range(N+1)} 31 | for i in range(1, len(lines), 1): 32 | info = lines[i].split(" ") 33 | node1 = int(info[0]) 34 | node2 = int(info[1]) 35 | arr = G[node1] 36 | arr.append(node2) 37 | arr = G[node2] 38 | arr.append(node1) 39 | 40 | path = shortestPath(G, 1, E) 41 | 42 | f2.write("TIME: "+str(len(path)-1)+"\n") 43 | f2.write("Shortest Path: "+" ".join(list(map(str, path)))) 44 | f2.close() -------------------------------------------------------------------------------- /CSE221LabAssignment04_Summer2023/task6.py: -------------------------------------------------------------------------------- 1 | def DFS_FloodFill(r, c, rows, cols, G): 2 | if r < 0 or r >= rows or c < 0 or c >= cols or G[r][c] == '#': 3 | return 0 4 | 5 | count = 0 6 | if G[r][c] == 'D': 7 | count += 1 8 | 9 | G[r][c] = '#' 10 | 11 | count += DFS_FloodFill(r + 1, c, rows, cols, G) 12 | count += DFS_FloodFill(r - 1, c, rows, cols, G) 13 | count += DFS_FloodFill(r, c + 1, rows, cols, G) 14 | count += DFS_FloodFill(r, c - 1, rows, cols, G) 15 | 16 | return count 17 | 18 | def countdiamonds(rows, cols, G): 19 | maxDiamonds = 0 20 | for r in range(rows): 21 | for c in range(cols): 22 | if G[r][c] == '.': 23 | diamond = DFS_FloodFill(r, c, rows, cols, G) 24 | maxDiamonds = max(maxDiamonds, diamond) 25 | return maxDiamonds 26 | 27 | 28 | f1 = open("input6_3.txt", "r") 29 | f2 = open("output6_3.txt", "w") 30 | lines = f1.read().split("\n") 31 | R, H = map(int, lines[0].split()) 32 | 33 | G = [list(line) for line in lines[1:]] 34 | 35 | result = countdiamonds(R, H, G) 36 | 37 | f2.write(str(result)) 38 | f2.close() -------------------------------------------------------------------------------- /CSE221LabAssignment05_Summer2023/Question.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MasiatHasin/CSE221-Algorithms-Lab-Summer2023/66192dc4e21b01b08615bd20ef7b80f455b645b3/CSE221LabAssignment05_Summer2023/Question.pdf -------------------------------------------------------------------------------- /CSE221LabAssignment05_Summer2023/explanation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MasiatHasin/CSE221-Algorithms-Lab-Summer2023/66192dc4e21b01b08615bd20ef7b80f455b645b3/CSE221LabAssignment05_Summer2023/explanation.pdf -------------------------------------------------------------------------------- /CSE221LabAssignment05_Summer2023/input1_1.txt: -------------------------------------------------------------------------------- 1 | 5 3 2 | 3 1 3 | 1 2 4 | 4 5 -------------------------------------------------------------------------------- /CSE221LabAssignment05_Summer2023/input2_3.txt: -------------------------------------------------------------------------------- 1 | 8 10 2 | 1 2 3 | 1 4 4 | 2 4 5 | 2 5 6 | 2 3 7 | 4 6 8 | 4 5 9 | 6 5 10 | 5 3 11 | 7 8 -------------------------------------------------------------------------------- /CSE221LabAssignment05_Summer2023/input3_1.txt: -------------------------------------------------------------------------------- 1 | 5 5 2 | 1 2 3 | 2 3 4 | 2 4 5 | 3 1 6 | 4 5 -------------------------------------------------------------------------------- /CSE221LabAssignment05_Summer2023/output1_1.txt: -------------------------------------------------------------------------------- 1 | 4 5 3 1 2 -------------------------------------------------------------------------------- /CSE221LabAssignment05_Summer2023/output2_3.txt: -------------------------------------------------------------------------------- 1 | 1 2 4 6 5 3 7 8 -------------------------------------------------------------------------------- /CSE221LabAssignment05_Summer2023/output3_1.txt: -------------------------------------------------------------------------------- 1 | 1 3 2 2 | 4 3 | 5 -------------------------------------------------------------------------------- /CSE221LabAssignment05_Summer2023/task1A.py: -------------------------------------------------------------------------------- 1 | def TopoSortBFS(G, indeg): 2 | count = 0 3 | q = [] 4 | f = [] 5 | for u in G: 6 | if indeg[u] == 0: 7 | q.append(u) 8 | while q: 9 | v = q.pop() 10 | count+=1 11 | f.append(v) 12 | for u in G[v]: 13 | indeg[u] = indeg[u]-1 14 | if indeg[u] == 0: 15 | q.append(u) 16 | if count!= len(G): 17 | return ["IMPOSSIBLE"] 18 | else: 19 | return f 20 | 21 | f1 = open("input1_1.txt", "r") 22 | f2 = open("output1_1.txt", "w") 23 | 24 | inp = f1.read().split("\n") 25 | f1.close() 26 | line1 = inp[0].split(" ") 27 | nodes = int(line1[0]) 28 | edges = int(line1[1]) 29 | 30 | indeg = [0]*(nodes+1) 31 | dic = {i:[] for i in range(1,nodes+1)} 32 | for i in range(1, len(inp), 1): 33 | info = inp[i].split(" ") 34 | node1 = int(info[0]) 35 | node2 = int(info[1]) 36 | arr = dic[node1] 37 | arr.append(node2) 38 | indeg[node2] = indeg[node2]+1 39 | 40 | result = TopoSortBFS(dic,indeg) 41 | f2.write(" ".join(list(map(str, result)))) 42 | f2.close() -------------------------------------------------------------------------------- /CSE221LabAssignment05_Summer2023/task1B.py: -------------------------------------------------------------------------------- 1 | def DFS(G, u, time, visited, s_time, e_time,stack): 2 | s_time[u] = time 3 | time += 1 4 | visited[u] = True 5 | stack[u] = True 6 | cycle_detected = False 7 | 8 | for v in G[u]: 9 | if not visited[v]: 10 | time, e_time, cycle_detected = DFS(G, v, time, visited, s_time, e_time, stack) 11 | if cycle_detected: 12 | break 13 | elif stack[v]: 14 | cycle_detected = True 15 | break 16 | 17 | e_time[u] = time 18 | time += 1 19 | stack[u] = False 20 | return time, e_time, cycle_detected 21 | 22 | def isCycle(G): 23 | num_vertices = len(G) 24 | visited = [False] * num_vertices 25 | s_time = [False] * num_vertices 26 | e_time = {i:[] for i in range(num_vertices)} 27 | stack = [False] * num_vertices 28 | time = 1 29 | cycle_detected = False 30 | for u in G: 31 | if not visited[u]: 32 | time, e_time, cycle_detected = DFS(G, u, time, visited, s_time, e_time, stack) 33 | if cycle_detected: 34 | break 35 | 36 | return e_time, cycle_detected 37 | 38 | f1 = open("input1_1.txt", "r") 39 | f2 = open("output1_1.txt", "w") 40 | 41 | inp = f1.read().split("\n") 42 | f1.close() 43 | line1 = inp[0].split(" ") 44 | nodes = int(line1[0]) 45 | edges = int(line1[1]) 46 | 47 | dic = {i:[] for i in range(nodes+1)} 48 | for i in range(1, len(inp), 1): 49 | info = inp[i].split(" ") 50 | node1 = int(info[0]) 51 | node2 = int(info[1]) 52 | arr = dic[node1] 53 | arr.append(node2) 54 | 55 | end_times, cycle = isCycle(dic) 56 | if not cycle: 57 | result = [key for key, value in sorted(end_times.items(), key=lambda item: item[1], reverse=True)] 58 | result.remove(0) 59 | f2.write(" ".join(list(map(str, result)))) 60 | else: 61 | f2.write("IMPOSSIBLE") 62 | f2.close() -------------------------------------------------------------------------------- /CSE221LabAssignment05_Summer2023/task2.py: -------------------------------------------------------------------------------- 1 | def TopoSortBFS(G, indeg): 2 | count = 0 3 | q = [] 4 | f = [] 5 | for u in G: 6 | if indeg[u] == 0: 7 | q.append(u) 8 | while q: 9 | v = min(q) 10 | q.remove(v) 11 | count+=1 12 | f.append(v) 13 | for u in G[v]: 14 | indeg[u] = indeg[u]-1 15 | if indeg[u] == 0: 16 | q.append(u) 17 | if count!= len(G): 18 | return ["IMPOSSIBLE"] 19 | else: 20 | return f 21 | 22 | f1 = open("input2_3.txt", "r") 23 | f2 = open("output2_3.txt", "w") 24 | 25 | inp = f1.read().split("\n") 26 | f1.close() 27 | line1 = inp[0].split(" ") 28 | nodes = int(line1[0]) 29 | edges = int(line1[1]) 30 | 31 | indeg = [0]*(nodes+1) 32 | dic = {i:[] for i in range(1,nodes+1)} 33 | for i in range(1, len(inp), 1): 34 | info = inp[i].split(" ") 35 | node1 = int(info[0]) 36 | node2 = int(info[1]) 37 | arr = dic[node1] 38 | arr.append(node2) 39 | indeg[node2] = indeg[node2]+1 40 | 41 | result = TopoSortBFS(dic,indeg) 42 | f2.write(" ".join(list(map(str, result)))) 43 | f2.close() -------------------------------------------------------------------------------- /CSE221LabAssignment05_Summer2023/task3.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | 3 | def DFS(G, v, visited, stack): 4 | visited[v] = True 5 | for i in G[v]: 6 | if not visited[i]: 7 | DFS(G, i, visited, stack) 8 | stack.append(v) 9 | 10 | def transpose(G): 11 | transposed_G = {i:[] for i in range(len(G))} 12 | for i in G: 13 | for j in G[i]: 14 | transposed_G[j].append(i) 15 | return transposed_G 16 | 17 | def SCC_DFS(G, v, visited, scc): 18 | visited[v] = True 19 | scc.append(v) 20 | for i in G[v]: 21 | if not visited[i]: 22 | SCC_DFS(G, i, visited, scc) 23 | 24 | def SCC(G): 25 | stack = [] 26 | visited = [False] * len(G) 27 | for i in range(len(G)): 28 | if not visited[i]: 29 | DFS(G, i, visited, stack) 30 | 31 | transposed = transpose(G) 32 | visited = [False] * len(G) 33 | scc_components = [] 34 | 35 | while stack: 36 | v = stack.pop() 37 | if v!= 0: 38 | if not visited[v]: 39 | scc = [] 40 | SCC_DFS(transposed, v, visited, scc) 41 | scc_components.append(scc) 42 | 43 | return scc_components 44 | f1 = open("input3_1.txt", "r") 45 | f2 = open("output3_1.txt", "w") 46 | inp = f1.read().split("\n") 47 | f1.close() 48 | line1 = inp[0].split(" ") 49 | nodes = int(line1[0]) 50 | edges = int(line1[1]) 51 | 52 | dic = {i:[] for i in range(nodes+1)} 53 | for i in range(1, len(inp), 1): 54 | info = inp[i].split(" ") 55 | node1 = int(info[0]) 56 | node2 = int(info[1]) 57 | arr = dic[node1] 58 | arr.append(node2) 59 | 60 | scc = SCC(dic) 61 | for i in range(len(scc)): 62 | f2.write(" ".join(list(map(str, scc[i])))) 63 | if i=0: 28 | p.append(curr) 29 | 30 | for i in range(len(p)): 31 | elem = p[i] 32 | f2.write(str(elem[0])+" "+str(elem[1])) 33 | if i0: 31 | d = int(curr[0]) - int(total[j][-1][1]) 32 | else: 33 | d = -9999 34 | if d>=0: 35 | pos.append([j,d]) 36 | if len(pos)>0: 37 | pos = sorted(pos, key=lambda x: x[1]) 38 | idx = pos[0][0] 39 | total[idx].append(curr) 40 | count+=1 41 | else: 42 | if [] in total: 43 | idx = total.index([]) 44 | total[idx]=[curr] 45 | count+=1 46 | pos = [] 47 | f2.write(str(count)) -------------------------------------------------------------------------------- /CSE221LabAssignment07_Summer2023/task3.py: -------------------------------------------------------------------------------- 1 | def getParent(parent, n): 2 | par = parent[n] 3 | if par==n: 4 | return par 5 | else: 6 | return getParent(parent, par) 7 | 8 | f1 = open("input3_1.txt", "r") 9 | f2 = open("output3_1.txt", "w") 10 | 11 | inp = f1.read().split("\n") 12 | f1.close() 13 | 14 | config = inp[0].split(" ") 15 | n = int(config[0]) 16 | k = int(config[1]) 17 | 18 | parent = [i for i in range(n + 1)] 19 | friends = [1]*(n+1) 20 | 21 | for i in range(1, len(inp),1): 22 | data = inp[i].split(" ") 23 | node1 = int(data[0]) 24 | node2 = int(data[1]) 25 | 26 | parent1 = getParent(parent, node1) 27 | parent2 = getParent(parent, node2) 28 | 29 | if (parent1!=parent2): 30 | parent[node2] = parent1 31 | parent[parent2] = parent1 32 | friends[parent1]= friends[parent1]+friends[parent2] 33 | f2.write(str(friends[parent1])) 34 | if i