Download presentation
Presentation is loading. Please wait.
Published byWillis Booth Modified over 9 years ago
1
CS 177 Week 9 Recitation Slides 1 Matrix Encoding Matrix Multiplication Trees as Lists Columnar Transposition 3/1/2016
2
Announcements 3/1/2016 2
3
Matrix Encoding: Row by Row 3/1/2016 3 A 110 328 5-35 0,00,10,2 1,01,11,2 2,02,1 A = [1, 1, 0, 3, 2, 8, 5, -3, 5] rowLength = 3 A[rowLength*y +x]
4
Matrix Encoding: Column by Column 3/1/2016 4 A 110 328 5-35 0,00,10,2 1,01,11,2 2,02,1 A = [1, 3, 5, 1, 2, -3, 0, 8, 5] columnLength = 3 A[columnLength*y +x]
5
Matrix Multiplication 3/1/2016 5 0,00,10,2 1,01,11,2 2,02,1 0,00,10,2 1,01,11,2 2,02,1 0,00,10,2 1,01,11,2 2,02,1 = * CA B CAB
6
Matrix Multiplication 3/1/2016 6 111 222 333 123 456 789 12 CA B C 0,0 = A 0,0 * B 0,0 + A 0,1 * B 1,0 + A 0,2 * B 2,0 12111417
7
Matrix Multiplication 3/1/2016 7 111 222 333 123 456 789 1215 CA B C 0,1 = A 0,0 * B 0,1 + A 0,1 * B 1,1 + A 0,2 * B 2,1 15121518
8
Matrix Multiplication 3/1/2016 8 111 222 333 123 456 789 121518 CA B C 0,2 = A 0,0 * B 0,2 + A 0,1 * B 1,2 + A 0,2 * B 2,2 18131619
9
Matrix Multiplication 3/1/2016 9 111 222 333 123 456 789 121518 24 CA B C 1,0 = A 1,0 * B 0,0 + A 1,1 * B 1,0 + A 1,2 * B 2,0 24212427
10
Matrix Multiplication 3/1/2016 10 111 222 333 123 456 789 121518 2430 CA B C 1,1 = A 1,0 * B 0,1 + A 1,1 * B 1,1 + A 1,2 * B 2,1 30222528
11
Matrix Multiplication 3/1/2016 11 111 222 333 123 456 789 121518 243036 CA B C 1,2 = A 1,0 * B 0,2 + A 1,1 * B 1,2 + A 1,2 * B 2,2 36232629
12
Matrix Multiplication 3/1/2016 12 111 222 333 123 456 789 1518 243036 CA B C 2,0 = A 2,0 * B 0,0 + A 2,1 * B 1,0 + A 2,2 * B 2,0 36313437
13
Matrix Multiplication 3/1/2016 13 111 222 333 123 456 789 121518 243036 45 CA B C 2,1 = A 2,0 * B 0,1 + A 2,1 * B 1,1 + A 2,2 * B 2,1 45323538
14
Matrix Multiplication 3/1/2016 14 111 222 333 123 456 789 121518 243036 4554 CA B C 2,2 = A 2,0 * B 0,2 + A 2,1 * B 1,2 + A 2,2 * B 2,2 54333639
15
3/1/2016 15 1,0= *0,0+1,1*1,0+1,2*2,0 2,2=2,0*0,2+2,1*1,2+2,2* 2,1=2,0*0,1+2,1*1,1+2,2*2,1 2,0= *0,0+2,1*1,0+2,2*2,0 1,1=1,0*0,1+1,1* +1,2*2,1 1,2=1,0*0,2+1,1*1,2+ *2,2 0,1=0,0*0,1+ *1,1+0,2*2,1 0,0= * +0,1*1,0+0,2*2,0 0,2=0,0*0,2+0,1*1,2+0,2*2,2 Indexes in Matrix Multiplication
16
Matrix Multiplication 3/1/2016 16 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])
17
Trees as Lists Lists are powerful structures Lists can be used to build trees 3/1/2016 17 Root Leaf1Leaf2Leaf3 Root L1 L2 L3L4L5L0 L1 L2 L3L4 L6 L0 L5 Root
18
Simple trees >>> Tree1 = ['L0', 'L1'] >>>Tree2 = ['L0', 'L1', 'L2'] >>>Tree3 = ['L0', 'L1', 'L2','L3', 'L4', 'L5'] 3/1/2016 18 L1 Tree1 L0 L1 L2 L0 Tree2 L0 Tree3 L1 L2 L3L4 L5
19
>>> Tree = [['L0', 'L1'], ['L2','L3']] What is tree[1][0]? What is tree[0][1]? 3/1/2016 19 1 Tree L1L0 0 1 0 L2L3 0 1
20
Question Build the list representing the following tree, (and call it tree) 3/1/2016 20 Tree BA EF C D
21
Answer >>>Tree = [[‘A’,’B’],[’C’,’D’],[’E’,’F’]] 3/1/2016 21 Tree BA EF C D
22
Question Build the following tree: 3/1/2016 22 Tree B EF D XYZ
23
Answer Build the following tree >>> Tree = [[['X','Y'],'B'],[['Z'],'D'],['E','F'] ] 3/1/2016 23 Tree B EF D XY Z 0 12 0 0 1 1 0 0 0 1 1
24
Question How to access: X, Z, and E? 3/1/2016 24 Tree B EF D XY Z 0 12 0 0 1 1 0 0 0 1 1
25
Answer >>>Tree[0][0][0] ‘X’ >>>Tree[1][0][0] ‘Z’ >>>Tree[2][0] ‘E’ 3/1/2016 25 Tree B EF D XY Z 0 12 0 0 1 1 0 0 0 1 1
26
Columnar Transposition S = [[‘P’, ‘R’, ‘O’], [‘G’, ‘R’, ‘A’], [‘M’, ‘~’, ‘~’]] How to get “PROGRAM”? How to get “MGP~RP~AO”? 26 PRO GRA M~~
27
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) 27
28
ANY QUESTIONS? 3/1/2016 28
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.