CS1020 Lab 1 Discussion.

Slides:



Advertisements
Similar presentations
 Given a matrix of size N x N and some operations of rotation or reflection, determine the final state of the matrix.  The operations can be: ◦ Rotate.
Advertisements

Unit 1: Factorising There are 4 factorising skills that you need to master at National 5 level. Pick the one you want to work on. Common Factors Difference.
Introduction to Computing Science and Programming I
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
CMPS1371 Introduction to Computing for Engineers SORTING.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Backtracking What is backtracking?
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
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.
CSC401 – Analysis of Algorithms Lecture Notes 12 Dynamic Programming
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
© 2004 Goodrich, Tamassia Dynamic Programming1. © 2004 Goodrich, Tamassia Dynamic Programming2 Matrix Chain-Products (not in book) Dynamic Programming.
Analysis of Algorithm.
MOHAMMAD IMRAN DEPARTMENT OF APPLIED SCIENCES JAHANGIRABAD EDUCATIONAL GROUP OF INSTITUTES.
1 Lab Session-VI CS121 Fall 2000 l HW#2 Assigned l Multiple Choice Selectors l The While Loop l Switch-Case-Break Exercise l The Counter Controlled Loops.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall.
CS1020E Sitin 1 Discussion -- Counting Palindromes.
Matrices Jordi Cortadella Department of Computer Science.
Chapter 8 Matrices and Determinants Copyright © 2014, 2010, 2007 Pearson Education, Inc Matrix Operations and Their Applications.
A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.
1 CS 132 Spring 2008 Chapter 6 Recursion Read p Skip example 6-3 (Fibonacci), 6-4 (Hanoi) Read example 6-5 (p. 387)
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Data Structure Introduction.
Matrices Matrices A matrix (say MAY-trix) is a rectan- gular array of objects (usually numbers). An m  n (“m by n”) matrix has exactly m horizontal.
2009/9 1 Matrices(§3.8)  A matrix is a rectangular array of objects (usually numbers).  An m  n (“m by n”) matrix has exactly m horizontal rows, and.
Symmetry Two points, P and P ₁, are symmetric with respect to line l when they are the same distance from l, measured along a perpendicular line to l.
Graphs – Part II CS 367 – Introduction to Data Structures.
ITI 1120 Lab #9 Slides by: Diana Inkpen and Alan Williams.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Merge Sort: Taught By Example CO1406: Algorithms and Data Structures Module Lecturer: Dr. Nearchos Paspallis Week 10 Lab - Practice with Merge sort and.
Transformations To move a figure in the coordinate system to another location or image, by a rule.
Introduction to Algorithms Jiafen Liu Sept
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
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:
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
1 UNIT-I BRUTE FORCE ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 3:
CS 171: Intro to AI Discussion Week 2 Jan 15 th 2016.
Reflections A reflection is a FLIP over a line.. Also a reflection has: The same DISTANCE from a central line. The same SIZE as the original image.
Transformations for GCSE Maths Enlargement Translation Reflection Rotation.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Radical Functions and Equations ( 무리함수 ) Radical sign index The term is called a radical. The symbol is called a radical sign(sometimes just called radical.
Arrays. 1D Array Representation In C++ 1-dimensional array x = [a, b, c, d] map into contiguous memory locations Memory abcd start location(x[i]) = start.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
MS-EXCEL PART 3. Use data validation in Excel to make sure that users enter certain values into a cell. Data Validation Example In this example, we restrict.
Lecture 5 of Computer Science II
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Model Multiplication of Decimals
CS2011 Introduction to Programming I Loop Statements (II)
CS Software Studio Assignment 1
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Lets Play with arrays Singh Tripty
Introduction to Computer Science
Presentation transcript:

CS1020 Lab 1 Discussion

Problem 1: Transformation Given a matrix of size N x N and some operations of rotation or reflection, determine the final state of the matrix. The operations can be: Rotate by X degree, X = 90, 180, or 270 Reflect across the x-axis. Reflect across the y-axis.

Rotate by 90 degree 0 1 2 0 1 2 0 1 2 0 1 2 3 0 1 0 7 4 1 1 4 5 6 1 2 1 8 5 2 2 7 8 9 2 3 2 9 6 3 We process all the numbers in the original matrix one by one, using a for loop. j1 j2 j2 i1 i2 i2

Do you see any pattern? Let (j1, i1) be the coordinate of a number. #1 is at (0, 0), after rotation of 90 degree clockwise, it will be at index (2, 0) #2 is at (1, 0), after rotation of 90 degree clockwise, it will be at index (2, 1) #3 is at (2, 0), after rotation of 90 degree clockwise, it will be at index (2, 2) Do you see any pattern?

Do you see any pattern? Let (j1, i1) be the coordinate of a number. #1 is at (0, 0), after rotation of 90 degree clockwise, it will be at index (2, 0) #2 is at (1, 0), after rotation of 90 degree clockwise, it will be at index (2, 1) #3 is at (2, 0), after rotation of 90 degree clockwise, it will be at index (2, 2) Do you see any pattern?

Number Before Rotation After Rotation 1 2 3 4 5 6 7 8 9

Number Before Rotation After Rotation 1 2 3 4 5 6 7 8 9

𝑥′ 𝑦′ = cos 𝜃 sin 𝜃 − sin 𝜃 cos 𝜃 𝑥 𝑦 𝑥′ 𝑦′ = 0 1 −1 0 𝑥 𝑦 𝑥′ 𝑦′ = 0 1 −1 0 𝑥 𝑦 𝑥′ 𝑦′ = 𝑦 −𝑥 Number Before Rotation After Rotation 1 2 3 4 5 6 7 8 9 Why Is It So? Interested? Take MA1101R 

- Rotation 180 (clockwise)= 2 x Rotation 90 (clockwise).

degree = input/90 void rotate(int degree) { int[][] temp = new int[size][size]; while (degree > 0) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { temp[j][size - 1 - i] = matrix[i][j]; } degree--; //decrement the “degree” //don’t forget to replace the content of matrix with temp

There is also a pattern for reflection Reflect by X-axis 0 1 2 0 1 2 0 1 2 0 1 2 0 7 4 1 0 9 6 3 1 8 5 2 1 8 5 2 2 9 6 3 2 7 4 1 Reflect by Y-axis j1 j2 i1 i2

void reflectX() { int[][] temp = new int[size][size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { temp[i][j] = matrix[size - 1 - i][j]; }

Problem 2 - Land CS1020 Sit-in Lab #1 (AY2011/2012 SEM2)

Given an NxN square with trees occupying some cells. Find the length of the largest square with no trees inside. (Find the number of squares having length S and exactly k trees inside) 14 14

Why not the other way (i.e. from 1x1 to NxN)? Solution Read & store input: Tree data: Create array NxN to store all the position of trees Exhaustive search! For each possible size (NxN to 1x1) For each square of that size in the grid (e.g. 2x2 square, 3x3 square) Check if the square has no trees in it Why not the other way (i.e. from 1x1 to NxN)? 15 15

16 16

Implementation 17 17

R x C matrix with each cell containing a lower-case letter Problem 3 – Counting Palindromes Given: Find the number of palindromes that are constructed horizontally, vertically, or diagonally. R x C matrix with each cell containing a lower-case letter a b s d f q w e r R C 18 18

What is a palindrome? A string is called a palindrome if it is read the same backward or forward. algorithms refer radar fly level science

What is a palindrome? A string is called a palindrome if it is read the same backward or forward. algorithms refer radar fly level science

Solution Read & store input: Brute-Force Solution: 2-dimensional array of size R*C to represent each cell in its respective row and column. Use a separate isPalindrome(String) function to decide if a given string is a palindrome. Then, iterate through all rows, columns, and diagonals by executing this function on all possible strings in the matrix. Accumulate the total in a variable. 21 21

a b s d f q w e r a b s d f q w e r a b s d f q w e r a b s d f q w e r

a b s d f q w e r a b s d f q w e r a b s d f q w e r a b s d f q w e r

a b s d f q w e r a b s d f q w e r a b s d f q w e r a b s d f q w e r

Just loop through each rows and construct the string Solution Generating the rows and columns is easy: Just loop through each rows and construct the string a b s d f q w e r for each row i, column j: i = 0 j = 3 i = 0 j = 2 i = 0 j = 1 i = 0 j = 0 appendedString = abba appendedString = abb appendedString = ab appendedString = a 25

Constructing Diagonals Start from the leftmost point, and try to extend to the lower right if that cell is still inside the matrix. String constructDiagonal(int x, int y) { String current = ""; while (isInside(x, y)) { current += matr[x][y]; x += 1; //advance y += 1; } return current;

Constructing Diagonals Start from the leftmost point, and try to extend to the lower right if that cell is still inside the matrix. String constructDiagonal(int x, int y) { String current = ""; while (isInside(x, y)) { current += matr[x][y]; x += 1; //advance y += 1; } return current;

Constructing Diagonals b s d f q w e r while isInside(x,y) x = 2 y = 2 x = 1 y = 1 x = 0 y = 0 x = 3 y = 3 appendedString = aae appendedString = a appendedString = aa

Do not count the single letter palindromes multiple times Warning!!! Do not count the single letter palindromes multiple times

The isPalindrome(String) function If String length is even, start from position (n/2) and (n/2 + 1). Check if they are of the same value. Repeat until the whole string is checked. If length is odd, disregard the middle letter and iterate from floor(n/2 -1) and ceil(n/2 + 1). V1 N O O N N O O N M A K A N M A K A N Idea: if the string “aaa” is a palindrome, is “XaaaX” a palindrome as well if X is equal to X? V2

Well, for the presentation that is… The End… Well, for the presentation that is… You still need to implement the solutions 

Good Luck! :)