Data Structures More List Methods Our first encoding Matrix.

Slides:



Advertisements
Similar presentations
Recursion Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
Advertisements

1 Union-find. 2 Maintain a collection of disjoint sets under the following two operations S 3 = Union(S 1,S 2 ) Find(x) : returns the set containing x.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Announcements Midterm next week! No class next Friday Review this Friday.
Review Binary –Each digit place is a power of 2 –Any two state phenomenon can encode a binary number –The number of bits (digits) required directly relates.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
CS 206 Introduction to Computer Science II 12 / 10 / 2008 Instructor: Michael Eckmann.
Python Control of Flow.
Java Unit 9: Arrays Declaring and Processing Arrays.
Introduction to Data Structures. Data Structures A data structure is a scheme for organizing data in the memory of a computer. Some of the more commonly.
Python quick start guide
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Computing Science 1P Lecture 13: Friday 26 th January Simon Gay Department of Computing Science University of Glasgow 2006/07.
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Topics: Sequence Sequences Index into a sequence [] notation Slicing and other operations.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
I Power Int 2 Computing Software Development High Level Language Constructs.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
Announcements Additional office hours tomorrow: 3-6pm in HAAS 142 Midterm Bring a #2 pencil (test given via scantron sheet) Photo ID No Labs or Recitation.
Introduction to programming in the Java programming language.
Built-in Data Structures in Python An Introduction.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Huffman Encodings Section 9.4. Data Compression: Array Representation Σ denotes an alphabet used for all strings Each element in Σ is called a character.
Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07.
CPSC 252 Hashing Page 1 Hashing We have already seen that we can search for a key item in an array using either linear or binary search. It would be better.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Example: Expressions Python Programming, 2/e 1 [+, [*, 3, 5], [*, 2, [-, 6, 1]]]
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
Python Programing: An Introduction to Computer Science
Multidimensional Arrays Computer and Programming.
Matrix Multiplication The Introduction. Look at the matrix sizes.
CS 177 Week 9 Recitation Slides 1 Matrix Encoding Matrix Multiplication Trees as Lists Columnar Transposition 3/1/2016.
Fundamental Data Structures and Algorithms Ananda Guna March 18, 2003 Dynamic Programming Part 1.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
Announcements No Labs / Recitation this week On Friday we will talk about Project 3 Release late afternoon / evening tomorrow Cryptography.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
1 CS Review, iClicker -Questions Week 15. ANY QUESTIONS? 2.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
1 CS Review, iClicker -Questions Week 15. Announcements 2.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
INTRODUCTION TO DATA STRUCTURES 1. DATA STRUCTURES A data structure is a scheme for organizing data in the memory of a computer. Some of the more commonly.
Data Structures So far, we have seen native data structures: Simple values: int, float, Boolean Sequences: Range, string, list Tuple, dictionary (chapter.
Topic: Binary Encoding – Part 2
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Advanced Algorithms Analysis and Design
Data Encoding Characters.
ITEC 2620M Introduction to Data Structures
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
Advanced Algorithms Analysis and Design
Data Structures and Algorithms
Digital Encodings.
Trees Addenda.
Dynamic Programming II DP over Intervals
Huffman Coding Greedy Algorithm
Presentation transcript:

Data Structures More List Methods Our first encoding Matrix

CQ:Are these programs equivalent? b = [‘h’,’e’,’l’,’l’,’o’] b.insert(len(b), “w”) print(b) b = [‘h’,’e’,’l’,’l’,’o’] b.append(“w”) print(b) 21 A: yes B: no

Advanced List Operations L = [0, 1, 2, 0] L.reverse() print(L) will print: [0, 2, 1, 0] L.remove(0) print(L) will print: [2, 1, 0] L.remove(0) print(L) will print: [2, 1] print (L.index(2)) will print 0

Why are Lists useful? They provide a mechanism for creating a collection of items def doubleList(b): i = 0 while i < len(b): b[i] = 2 * b[i] i = i +1 return (b) print(doubleList([1,2,3]))

Using Lists to Create Our Own Encodings Python provides a number of encodings for us: Binary, ASCII, Unicode, RGB, Pictures etc. We know the basic building blocks, but we are still missing something … We need to be able to create our own encodings What if I want to write a program that operates on proteins?

Under the hood: what is a matrix? A matrix is not “pre defined” in python We “construct” a way to encode a matrix through the use of lists We will see how to do so using a single list (not ideal) We will see how to do so using a list of lists Two different ways Row by Row Column by Column ()

Lists Suppose we wanted to extract the value 3 The first set of [] get the array in position 1 of y. The second [] is selecting the element in position 2 of that array. This is equiv. to: y = [“ABCD”, [1,2,3], ”CD”, ”D”] y[1][2] z = y[1] z[2]

Lets make it more concrete! Lets revisit encoding a matrix Lets try something simple: A = [1, -1, 0, 2] B = [1, 0, 0, 0.5, 3, 4, -1, -3, 6]

Does this work? We lose a bit of information in this encoding Which numbers correspond to which row We can explicitly keep track of rows through a row length variable B = [1, 0, 0, 0.5, 3, 4, -1, -3, 6] rowLength = 3 B[rowLength*y +x]

Lets convince ourselves x = 0 y = 0 B[3*0 + 0] B = [1, 0, 0, 0.5, 3, 4, -1, -3, 6] rowLength = 3 B[rowLength*y +x] x = 1 y = 1 B[3*1 + 1] x = 2 y = 1 B[3*1 + 2]

Can we encode it another way? We can encode column by column, but we lose some information again Which numbers correspond to which column We can explicitly keep track of columns through a column length variable B = [1, 0.5, -1, 0, 3, -3, 0, 4, 6] columnLength = 3 B[columnLength*x + y]

Lets convince ourselves x = 0 y = 0 B[3*0 + 0] x = 1 y = 1 B[3*1 + 1] x = 2 y = 1 B[3*2 + 1] B = [1, 0.5, -1, 0, 3, -3, 0, 4, 6] columnLength = 3 B[columnLength*x + y]

Lists of Lists Recall that when we had a string in our list B = [“ABCD”, 0, 1, 3] We could utilize the bracket syntax multiple times print B[0][1] would print B Lists can store other Lists B = [[0, 1, 3], 4, 5, 6]

Another way to encode a Matrix Lets take a look at our example matrix What about this? B= [[1, 0, 0], [0.5, 3, 4], [-1, -3, 6]]

Why is this important? We can now write code that more closely resembles mathematical notation i.e., we can use x and y to index into our matrix B = [[1, 0, 0], [0.5, 3, 4], [-1, -3, 6]] for x in range(3): for y in range(3): print (B[x][y])

…but first some more notation We can use the “*” to create a multi element sequence 6 * [0] results in a sequence of 6 0’s [0, 0, 0, 0, 0, 0] 3 * [0, 0] results in a sequence of 6 0’s [0, 0, 0, 0, 0, 0] 10 * [0, 1, 2] results in what?

What is going on under the hood? Lets leverage some algebraic properties 3 * [0, 0] is another way to write [0, 0] + [0, 0] + [0, 0] We know that “+” concatenates two sequences together

What about lists of lists? We have another syntax for creating lists [ X for i in range(y)] This creates a list with y elements of X Example: [ 0 for i in range(6)] ≡ [0]*6 [0, 0,0,0,0,0] Example: [[0, 0] for i in range(3)] ≡ [[0,0]]*3 [[0, 0], [0, 0], [0, 0]] What does this does: [2*[0] for i in range(3)]?

Lets put it all together m1 = [ [1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0] ] m2 = [ [2, 4, 6, 0], [1, 3, 5, 0], [0, -1, -2, 0] ] m3= [ 4*[0] for i in range(3) ] for x in range(3): for y in range(4): m3[x][y]= m1[x][y]+m2[x][y]

Data structures We have constructed our first data structure! As the name implies, we have given structure to the data The data corresponds to the elements in the matrix The structure is a list of lists The structure allows us to utilize math-like notation

Clicker Question: did we encode the same matrix in both programs? [[1, 2], [3, 4]][[1, 3], [2, 4]] 21 A: yes B: no C: maybe

Lets explore why it depends [[1, 3], [2, 4]] 2 [[1, 2], [3, 4]] 1 (()) Both lists of lists can either be a row by row or a column by column encoding

Let us encode something else using lists!

We call this structure a tree Root

How might we encode such a structure? What structures do we know of in python? Strings, Ranges, Lists We know that lists allow us to encode complex structures via sub lists We used sub list to encode either a row or a column in the matrix We used an ‘outer’ list of rows or columns as a matrix

Can we use the same strategy? How might we encode a simple tree? Root Leaf1Leaf2Leaf3 Tree = [‘Leaf1’, ‘Leaf2’, ‘Leaf3’]

Trees can be more complex Root Leaf1Leaf2 Leaf3Leaf4Leaf5 Tree = [‘Leaf1’, ‘Leaf2’, [‘Leaf3’, ‘Leaf4’, ‘Leaf5’]]

Trees can be more complex Root Leaf1 Leaf2 Leaf3Leaf4Leaf5 Tree = [[‘Leaf0’,‘Leaf1’], ‘Leaf2’, [‘Leaf3’, ‘Leaf4’, ‘Leaf5’]] Leaf0

Trees can be more complex Root Leaf1 Leaf2 Leaf3Leaf4 Leaf6 Tree = [[‘Leaf0’,‘Leaf1’], ‘Leaf2’, [‘Leaf3’, ‘Leaf4’, [‘Leaf5’, ‘Leaf6’]]] Leaf0 Leaf5

What is the intuition Each sub list encodes the ‘branches’ of the tree We can think of each sub list as a ‘sub tree’ We can use indexes (the bracket notation []) to select out elements or ‘sub trees’

How can we select out the leaves? Root Leaf1 Tree[0] Leaf2 Tree[1] Leaf3 Tree[2] Tree = [‘Leaf1’, ‘Leaf2’, ‘Leaf3’]

Indexes provide us a way to “traverse” the tree Root Leaf1 Leaf2 Leaf3Leaf4 Leaf6 Tree = [[‘Leaf0’,‘Leaf1’], ‘Leaf2’, [‘Leaf3’, ‘Leaf4’, [‘Leaf5’, ‘Leaf6’]]] Leaf0 Leaf

Indexes provide us a way to “traverse” the tree Root Leaf1 Leaf2 Leaf3Leaf4 Leaf6 Tree = [[‘Leaf0’,‘Leaf1’], ‘Leaf2’, [‘Leaf3’, ‘Leaf4’, [‘Leaf5’, ‘Leaf6’]]] Tree[2] Leaf0 Leaf

Indexes provide us a way to “traverse” the tree Root Leaf1 Leaf2 Leaf3Leaf4 Leaf6 Tree = [[‘Leaf0’,‘Leaf1’], ‘Leaf2’, [‘Leaf3’, ‘Leaf4’, [‘Leaf5’, ‘Leaf6’]]] Tree[2][2] Leaf0 Leaf

Indexes provide us a way to “traverse” the tree Root Leaf1 Leaf2 Leaf3Leaf4 Leaf6 Tree = [[‘Leaf0’,‘Leaf1’], ‘Leaf2’, [‘Leaf3’, ‘Leaf4’, [‘Leaf5’, ‘Leaf6’]]] Tree[2][2][1] Leaf0 Leaf

CQ: How do we select ‘Leaf4’ from the Tree? Tree = [[‘Leaf0’,‘Leaf1’], ‘Leaf2’, [‘Leaf3’, ‘Leaf4’, [‘Leaf5’, ‘Leaf6’]]] A: Tree[2][1] B: Tree[3][2] C: Tree[1][2]

Operations on Trees Trees, since they are encoded via lists support the same operations lists support We can “+” two trees We can embedded two trees within a list This creates a larger tree with each of the smaller trees as sub trees Example: Tree1 = [‘Leaf1’, ‘Leaf2’] Tree2 = [‘Leaf3’, ‘Leaf4’] Tree = [Tree1, Tree2]

“+” two trees Leaf1 Leaf2 Leaf3Leaf4Leaf0 Leaf6Leaf5 Tree1 = [[‘Leaf0’, ‘Leaf1’]] Tree2 = [‘Leaf2’] Tree3 = [[‘Leaf3’, ‘Leaf4’, [‘Leaf5’, ‘Leaf6’]]]

“+” two trees Leaf1 Leaf2 Leaf3Leaf4Leaf0 Leaf6Leaf5 Tree1 = [[‘Leaf0’, ‘Leaf1’]] Tree2 = [‘Leaf2’] Tree4 = Tree1+Tree2 [[‘Leaf0’, ‘Leaf1’], ‘Leaf2’]

“+” two trees Leaf1 Leaf2 Leaf3Leaf4Leaf0 Leaf6Leaf5 Tree4 = [[‘Leaf0’, ‘Leaf1’], ‘Leaf2’] Tree3 = [[‘Leaf3’, ‘Leaf4’, [‘Leaf5’, ‘Leaf6’]]] Tree = Tree4+Tree3

Why are trees important? They are a fundamental structure in computer science They enable us to search very quickly We will revisit trees later in the course What have we covered so far: Given a tree diagram we can write a list of lists Given a complex list we can select elements