def freivalds(N, A, B, C): Z = N * [0] # vector fila de ceros for I in range(N): Z[I] = Random(0, 1) # construimos vector aleatorio Y = multiply(N, B, Z) X1 = multiply(N, A, Y) X2 = multiply(N, C, Z) return X1 == X2
def partition_randomized(values,sortBy, p, r): pivot_index = Random(p, r) pivot1 = sortBy[pivot_index] pivot2 = values[pivot_index] sortBy[pivot_index] = sortBy[r] values[pivot_index] = values[r] values[r] = pivot2 sortBy[r] = pivot1 return partition_with_pivot(values, sortBy, p, r, pivot1)
def freivalds(n, A, B, C): def multiply(n, A, Z): # Crea el vector a retornar R = n * [0] # Recorre los elementos del vector R y las filas de la matriz A for i in range(n): # Recorre los elementos del vector Z y los elementos de la fila i de A for j in range(n): R[i] = R[i] + (A[i][j] * Z[j]) return R # Genera un vector Z lleno de ceros y unos Z = n * [n] for i in range(n): Z[i] = Random(0, 1) # Multiplica B x Z, luego A x (B x Z) y C x Z # Obteniendo 2 vectores x1 y x2 de largo n Y = multiply(n, B, Z) x1 = multiply(n, A, Y) x2 = multiply(n, C, Z) # Chequea si A x (B x Z) = C x Z return x1 == x2
def partition_randomized(A, p, r): pivot_index = Random(p, r) pivot = A[pivot_index] A[pivot_index], A[r] = A[r], A[pivot_index] return partition_with_pivot(A, p, r, pivot)
B = mergesort(A) print B R = False for i in range(len(B) - 1): start = i + 1 end = len(B) - 1 while start < end: mid = (start + end) / 2 if B[mid] + B[i] == x: R = True break elif B[mid] + B[i] < x: start = mid + 1 elif B[mid] + B[i] > x: end = mid - 1 if B[start] + B[i] == x: R = True return R A = [Random(0, 2) for i in range(100)] x = 71 #print A print mergesort(A) #print problema_3_8(A,x)