CS 112 Introduction to Programming Array Reference Semantics; 2D Arrays Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:

Slides:



Advertisements
Similar presentations
CS 112 Introduction to Programming
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
1 Various Methods of Populating Arrays Randomly generated integers.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
Arrays Chapter 6 Chapter 6.
8. Arrays 8.1 Using Arrays 8.2 Reference versus Value Again 8.3 Passing Array Arguments and Returning Arrays 8.4 An Example: Simulating Rolls of the Dice.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Class design. int month; int year class Month Defining Classes A class contains data declarations (state) and method declarations (behaviors) Data declarations.
Search - CIS 1068 Program Design and Abstraction
1 Reference semantics. 2 A swap method? Does the following swap method work? Why or why not? public static void main(String[] args) { int a = 7; int b.
CS 112 Introduction to Programming Reference Semantics; 2D Arrays; Array as State Yang (Richard) Yang Computer Science Department Yale University 308A.
Search - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 19/23/2015.
Week 2 - Monday.  What did we talk about last time?  Exceptions  Threads  OOP  Interfaces.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008.
1.4 Arrays "... By the way, we rank 10 th among the industrialized world in broadband technology and its availability. That’s not good enough for America.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Multidimensional Arrays. 2 Two Dimensional Arrays Two dimensional arrays. n Table of grades for each student on various exams. n Table of data for each.
CS 112 Introduction to Programming Arrays; Loop Patterns (break) Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
CompSci 100E 3.1 Random Walks “A drunk man wil l find his way home, but a drunk bird may get lost forever”  – Shizuo Kakutani Suppose you proceed randomly.
CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
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.
1 Array basics. Data Structures Sometimes, we have data that have some natural structure to them  A few examples: Texts are sequences of characters Images.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading:
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
By Mr. Muhammad Pervez Akhtar
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Simple algorithms on an array - compute sum and min.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CompSci 100E 4.1 Google’s PageRank web site xxx web site yyyy web site a b c d e f g web site pdq pdq.. web site yyyy web site a b c d e f g web site xxx.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Ljiljana Rajačić. Page Rank Web as a directed graph  Nodes: Web pages  Edges: Hyperlinks 2 / 25 Ljiljana Rajačić.
CS 112 Introduction to Programming Loop Examples; Variable Scoping; Nested Loops; Yang (Richard) Yang Computer Science Department Yale University 208A.
CS 112 Introduction to Programming Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Chapter 9 Introduction to Arrays Fundamentals of Java.
CS 112 Introduction to Programming Summary of Method Definition and Invocation; printf/format Formatting Yang (Richard) Yang Computer Science Department.
Midterm 2 Review Notes on the CS 5 midterm Take-home exam due by 5:00 pm Sunday evening (11/14) Hand in your solutions under the door of my office, Olin.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
CSCI 162 – Introduction to Programming II William Killian
Building Java Programs
Lecture 13: More Arrays Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Dr. Kyung Eun Park Summer 2017
Building Java Programs
Building Java Programs Chapter 7
Multidimensional Arrays
Building Java Programs
Multidimensional Arrays
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from
Building Java Programs
CS2011 Introduction to Programming I Methods (II)
Why did the programmer quit his job?
Building Java Programs
Building Java Programs
Chapter 8 Multidimensional Arrays
Building Java Programs
Presentation transcript:

CS 112 Introduction to Programming Array Reference Semantics; 2D Arrays Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:

Admin  Exam 1 m Media: 42; Max: 50+5; Min: 19 m See Schedule page for solution and grading rubric  PS6 m Allows pair programming m See pset page on a link on how pair programming works.  Puzzle day starts this Friday m Runs from Friday 1 pm to Saturday 6 pm (24+5 hours) m Students can form teams: 3 members/team m Office hours and preference on location 2

Recap: Value Semantics vs Reference Semantics  Primitive data types use value semantics: variable stores value  Non primitive data types (e.g., arrays and objects) use reference semantics: variable stores reference address  When variable a is assigned to variable b : b = a it is always that the content of a is copied to b => if a is a reference, then b becomes an alias of a 3 x5 int x = 5; int[] a = {1, 2, 3}; Address of an array a 1 2 3

4 Special Case: Value/Reference Semantics and Parameter Passing  Each time a method is called, the actual argument in the invocation is copied into the corresponding formal argument => if a parameter is a reference type, then the actual argument and the formal argument now refer to the same object

5 Example static void doubleArray (int[] iqp) public static void main(String[] args) { int[] iq = {120, 160, 95}; doubleArray(iq); System.out.println(Arrays.toString(iq)); } { for (int i = 0; i < iqp.length; i++) iqp[i] *= 2; }

6 Example: Illustration static void doubleArray (int[] iqp) public static void main(String[] args) { int[] iq = {120, 160, 95}; doubleArray(iq); System.out.println(Arrays.toString(iq)); } iq { for (int i = 0; i < iqp.length; i++) iqp[i] *= 2; } iqp i

Exercise  Write a method swapTwo that accepts an array of integers and two indexes and swaps the elements at those indexes. int[] a1 = {12, 34, 56}; swapTwo(a1, 1, 2); System.out.println(Arrays.toString(a1)); // [12, 56, 34] // Swaps the values at the given two indexes. public static void swapTwo(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; }

Exercise  Write a method swapAll that accepts two same-size arrays of integers as parameters and swaps their entire contents. int[] a1 = {10, 11, 12}; int[] a2 = {20, 21, 22}; swapAll(a1, a2); System.out.println(Arrays.toString(a1)); // [20, 21, 22] System.out.println(Arrays.toString(a2)); // [10, 11, 12] // Does this method swap the entire contents of // a1 with those of a2? public static void swapAll1(int[] a1, int[] a2) { int[] temp = a1; a1 = a2; a2 = temp; }

9 Why it does not work? Init. public static void main(String[] args) { int[] a1 = {10, 11, 12}; int[] a2 = {20, 21, 22}; } a a2

10 Why it does not work? Invoke public static void main(String[] args) { int[] a1 = {10, 11, 12}; int[] a2 = {20, 21, 22}; swapAll (a1, a2); } a a2

11 Why it does not work? Invoke public static void main(String[] args) { int[] a1 = {10, 11, 12}; int[] a2 = {20, 21, 22}; swapAll (a1, a2); } a a2 static void swapAll(int[] a1, int[] a2) { }{ } a1 a2

12 Why it does not work? Swap (temp = a1) public static void main(String[] args) { int[] a1 = {10, 11, 12}; int[] a2 = {20, 21, 22}; swapAll (a1, a2); } a a2 static void swapAll(int[] a1, int[] a2) { int[] temp = a1; } a1 a2 temp

13 Why it does not work? Swap (a1 = a2) public static void main(String[] args) { int[] a1 = {10, 11, 12}; int[] a2 = {20, 21, 22}; swapAll (a1, a2); } a a2 static void swapAll(int[] a1, int[] a2) { int[] temp = a1; a1 = a2; } a1 a2 temp

14 Why it does not work? Swap (a2 = temp) public static void main(String[] args) { int[] a1 = {10, 11, 12}; int[] a2 = {20, 21, 22}; swapAll (a1, a2); } a a2 static void swapAll(int[] a1, int[] a2) { int[] temp = a1; a1 = a2; a2 = temp; } a1 a2 temp

15 Why it does not work? After swapAll public static void main(String[] args) { int[] a1 = {10, 11, 12}; int[] a2 = {20, 21, 22}; swapAll (a1, a2); } a a2

Solution // Swaps the entire contents of a1 with those of a2. public static void swapAll(int[] a1, int[] a2) { for (int i = 0; i < a1.length; i++) { int temp = a1[i]; a1[i] = a2[i]; a2[i] = temp; }

Outline  Admin and recap  Array reference semantics  2D arrays

18 Understanding Two Dimensional Array: An Array of Arrays ref to array 0 ref to array 1 ref to array 2table[ 2 ] table[ 1 ] table[ 0 ] int[][] table = new int[3][4]; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) table[i][j] = i + j; table

Outline  Admin and recap  Array reference semantics  2D arrays m Regular 2D arrays

Using 2-Dimensional Arrays  In most cases, you deal with regular 2D arrays, and hence no need to worry about the internal storage structure of 2-D arrays final int NROWS = 3; final int NCOLUMNS = 4; int[][] table = new int[NROWS][NCOLUMNS]; for (int i = 0; i < NROWS; i++) for (int j = 0; j < NCOLUMNS; j++) table[i][j] = i + j;

Example: Simulate Self-Avoiding Walk  Model. m N-by-N lattice. m Start in the middle. m Each step randomly moves to a neighboring intersection, if a previously moved intersections, no move. m Two possible outcomes: dead end and escape.  Applications. Polymers ( statistical mechanics, etc. 21

Key Design Points  How to represent the state of the visited status of the N-by-N lattice?  How to detect if (x,y) is a terminating state: m dead end (trap): m escape 22 boolean[][] visited = new boolean[N][N]; visited[x+1][y] && visited[x-1][y] && visited[x][y-1] && visited[x][y+1] x == 0 || x == N-1 || y == 0 || y == N-1

Self-Avoiding Random Walk 23 how to implement? // read in lattice size N as command-line argument. // read in number of trials T as command-line argument. // repeat T times: // initialize (x, y) to center of N-by-N grid. // repeat as long as (x, y) is not escape or trap // mark (x, y) as visited. // take a random step, updating (x, y). // if not escape increase #deadEnds // print fraction of dead ends.

24 % java SelfAvoidingWalks % dead ends % java SelfAvoidingWalks % dead ends % java SelfAvoidingWalks % dead ends … % java SelfAvoidingWalks % dead ends

Outline  Admin and recap  Array reference semantics  2D arrays m Regular 2D arrays m Irregular 2D arrays

26 Irregular Two-Dimensional Array ref to array 0 ref to array 1 ref to array 2table[ 2 ] table[ 1 ] table[ 0 ] table 1 ref to array 3 table[ 3 ] int[][] table = { {1, 2, 3, 4}, {5, 6, 7}, {8, 9}, {0} };

27 Irregular Two-Dimensional Array ref to array 0 ref to array 1 ref to array 2 table[ 2 ] table[ 1 ] table[ 0 ] table ref to array 3 table[ 3 ] int[][] table = { {1, 2, 3, 4}, {5, 6, 7}, {8, 9}, {0} }; int[][] table = new int[4][]; int[] temp0 = {1, 2, 3, 4}; table[0] = temp0; int[] temp1 = {5, 6, 7}; table[1] = temp1; int[] temp2 = {8, 9}; table[2] = temp2; int[] temp3 = {0}; table[3] = temp3;

28 Access Irregular 2D Arrays public class Test2DArray { public static void main(String[] args) { int[][] days = { {1, 2, 3, 4}, {5, 6, 7}, {8, 9}, {0} }; for (int i = 0; i < days.length; i++) { for (int j = 0; j < days[i].length; j++) System.out.print( days[i][j] ); System.out.println (); } } }

PageRank [Sergey Brin and Larry Page, 1998]  Problem: many Web pages may contain the searched key word (e.g., Yale), how to rank the pages when displaying search results?  Basic PageRank™ idea m The rule 10% of the time surfer types a random page 90% of the time surfer clicks (follows) a random link on a given page m PageRank ranks pages according to frequencies (we call the pageranks) surfer visits the pages 29

Interpretation: Random Walk among Web Pages 30 p1 p2 0.10/ /2 0.90

Round-Based Visit Distribution  Assume current round page rank (frequency) of page p i is PR c (p i )  Next round page rank m Each page gets 1/N of the 0.1 of total visits m Each page distributes its current-round frequency equally among its outgoing hyperlinks x p1p1 p2p2 pnpn … =100*.9/ =200*.9/

PageRank 32

Computing PageRank: Obtain Web Graph  Crawl the Web to obtain Web pages  Parse pages to obtain links among pages 33

Storing Web Graph  Number pages 0 to N – 1  Approach 1: a regular array storing connectivity matrix m conn[i][j] = 1 if page i links to page j; 0 otherwise m Q: How big is the array? 34

Storing Web Graph  Number pages 0 to N – 1  Approach 2: adjacent list m edges[i]: stores the pages that page i links to 35

Storing Web Graph: Adjacent List page 0 has 1 outgoing link to page 1 page 1 has four 5 outgoing links to pages 2, 2, 3, 3, 4 Outgoing adjacency List: -Each line for one page -Stores the links contained in this page #pages; allow 1-pass read

37 PageRank: Load Web Graph Scanner input = new Scanner(new File("tiny-web.txt")); // First read N, the number of pages int N = Integer.parseInt(input.nextLine()); // An irregular 2D array to keep track of outgoing links int[][] outgoingLinks = new int[N][]; // read in graph one line at a time for (int i = 0; i < N; i++) { String line = input.nextLine(); // read outgoing links of i String[] links = line.split(" "); outgoingLinks[i] = new int[links.length]; for (int j = 0; j < links.length; j++) { outgoingLinks[i][j] = Integer.parseInt(links[j]); } }

Approach 1: Simulating Random Walk among Web Pages 38 p1 p2 0.10/ /2 0.90

Approach 2: (Large-Popultion) Round-Based Visit Distribution  Assume current round page rank (frequency, #visitors) of page p i is PR c (p i )  Next round page rank m Each page gets 1/N of the 0.1 of total visitors m Each page distributes its 90% of its current-round frequency (visitors) equally among its outgoing hyperlinks x p1p1 p2p2 pnpn … =100*.9/ =200*.9/

Computing PageRank  Initialize arbitrary page ranks  Iterative algorithm to simulate visit redistribution m Assume current round page rank of page p i is PR c (p i ) m Update next round x p1p1 p2p2 pnpn …

41 PageRank: Compute Rank double[] pr = new double[N]; pr[0] = 1; // initialize to assume start at web page 0 // or Arrays.fill(pr, 1.0 / N); for (int t = 0; t < 20; t++) { double[] newpr = new double[N]; // init newpr Arrays.fill(newpr, 0.1 / N); // loop over the node to redistribute the frequencies for (int i = 0; i < N; i++) { // redistribute node i for (int j = 0; j < outgoingLinks[i].length; j++) { int to = outgoingLinks[i][j]; newpr[to] += 0.9 * pr[i] / outgoingLinks[i].length; } } pr = newpr; // swap newpr to be pr System.out.printf("pr[%2d] = %s\n", t, Arrays.toString(pr) ); }

Comment  In real life, since the number of pages is too large for a single machine to compute, Google uses its MapReduce framework to do the updates using multiple machines  We will touch upon Hadoop (open source implementation of MapReduce) and Spark later in the course 42

Computing PageRank x p1p1 p2p2 pnpn …