280 likes | 399 Views
This recitation covers foundational concepts in matrix encoding and multiplication, along with advanced data structures like trees and columnar transposition. We explore matrix representation in row-by-row and column-by-column formats with practical examples in Python. The session also delves into tree structures represented as lists, illustrating how to build and access tree nodes efficiently. Understanding these concepts is vital for optimizing data manipulation and storage in computational tasks.
E N D
CS 177 Week 9 Recitation Slides Matrix Encoding Matrix Multiplication Trees as Lists Columnar Transposition
Matrix Encoding: Row by Row A • A = [1, 1, 0, 3, 2, 8, 5, -3, 5] • rowLength = 3 • A[rowLength*y +x]
Matrix Encoding: Column by Column A • A = [1, 3, 5, 1, 2, -3, 0, 8, 5] • columnLength = 3 • A[columnLength*y +x]
Matrix Multiplication C A B C A B = *
Matrix Multiplication C A B
Matrix Multiplication C A B
Matrix Multiplication C A B
Matrix Multiplication C A B
Matrix Multiplication C A B
Matrix Multiplication C A B
Matrix Multiplication C A B
Matrix Multiplication C A B
Matrix Multiplication C A B
Matrix Multiplication A = [[1, 1, 1] ,[2,2,2] ,[3,3,3]] B = [[1, 2, 3], [4,5,6] ,[7,8, 9]] C = [3*[0] for i in range(3)] for i in range(3): for j in range(3): for k in range(3): C[i][j] = C[i][j] + ( A[i][k] * B[k][j])
Trees as Lists Root Root L2 Root L2 L0 L1 L3 L4 Leaf1 Leaf2 Leaf3 L0 L1 L3 L4 L5 L5 L6 Lists are powerful structures Lists can be used to build trees
Simple trees Tree1 Tree2 Tree3 L0 L1 L2 L0 L1 L0 L5 L3 L4 L1 L2 >>> Tree1 = ['L0', 'L1'] >>>Tree2 = ['L0', 'L1', 'L2'] >>>Tree3 = ['L0', 'L1', 'L2','L3', 'L4', 'L5']
Tree 0 1 0 1 0 1 L0 L1 L2 L3 >>> Tree = [['L0', 'L1'], ['L2','L3']] What is tree[1][0]? What is tree[0][1]?
Question Tree A B C D E F Build the list representing the following tree, (and call it tree)
Answer Tree A B C D E F >>>Tree = [[‘A’,’B’],[’C’,’D’],[’E’,’F’]]
Question Tree B D E F X Y Z Build the following tree:
Answer 0 Tree 1 2 0 1 0 1 0 1 B 0 1 D 0 E F X Y Z Build the following tree >>> Tree = [[['X','Y'],'B'],[['Z'],'D'] ,['E','F'] ]
Question 0 Tree 1 2 0 1 0 1 0 1 B 0 1 D 0 E F X Y Z How to access: X, Z, and E?
Answer 0 Tree 1 2 0 1 0 1 0 1 B 0 1 D 0 E F X Y Z >>>Tree[0][0][0] ‘X’ >>>Tree[1][0][0] ‘Z’ >>>Tree[2][0] ‘E’
Columnar Transposition S = [[‘P’, ‘R’, ‘O’], [‘G’, ‘R’, ‘A’], [‘M’, ‘~’, ‘~’]] How to get “PROGRAM”? How to get “MGP~RP~AO”?
D = ""for i in range(0,3): for j in range(0,3): D = D + S[i][j]print(D) D = ""for i in range(0,3): for j in range(0,3): D = D + S[3-1-j][i]print(D)