-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdivide_and_conquer.py
More file actions
53 lines (41 loc) · 2.08 KB
/
divide_and_conquer.py
File metadata and controls
53 lines (41 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""20161349 JaehoKim
Intro to Algorithms divide and conquer"""
#Import Libray. I only used numpy for numpy array. None of the functions implemented in numpy has been used during matrix calculations
import numpy as np
import time
def read_input(input):
""" Both inputs are stored in the input.txt file """
array = np.loadtxt(input,dtype='i',delimiter=' ')
#Seperate array
array_first,array_second = np.split(array,2,axis=0)
return array_first, array_second
def save_ouput(output):
output_array = np.savetxt("output.txt",output,delimiter=' ')
def divide_and_conquer(array_first,array_second):
n = len(array_first)
if n == 1:
return array_first * array_second
else:
a11 = array_first[:int(len(array_first)/2),:int(len(array_first)/2)]
a12 = array_first[:int(len(array_first)/2),int(len(array_first)/2):]
a21 = array_first[int(len(array_first)/2):,:int(len(array_first)/2)]
a22 = array_first[int(len(array_first)/2):,int(len(array_first)/2):]
b11 = array_second[:int(len(array_second)/2),:int(len(array_second)/2)]
b12 = array_second[:int(len(array_second)/2),int(len(array_second)/2):]
b21 = array_second[int(len(array_second)/2):,:int(len(array_second)/2)]
b22 = array_second[int(len(array_second)/2):,int(len(array_second)/2):]
c11 = divide_and_conquer(a11,b11) + divide_and_conquer(a12,b21)
c12 = divide_and_conquer(a11,b12) + divide_and_conquer(a12,b22)
c21 = divide_and_conquer(a21,b11) + divide_and_conquer(a22,b21)
c22 = divide_and_conquer(a21,b12) + divide_and_conquer(a22,b22)
result = np.zeros((n,n))
result[:int(len(result)/2),:int(len(result)/2)] = c11
result[:int(len(result)/2),int(len(result)/2):] = c12
result[int(len(result)/2):,:int(len(result)/2)] = c21
result[int(len(result)/2):,int(len(result)/2):] = c22
#print("Divide and Conquer")
return result
if __name__ == "__main__":
array_first,array_second = read_input('input.txt')
output = divide_and_conquer(array_first,array_second)
save_ouput(output)