Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 CS 112 Introduction to Programming Array Reference Semantics; 2D Arrays Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

2 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

3 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 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 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 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 120 160 95 240 320 190

7 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; }

8 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 9 Why it does not work? Init. public static void main(String[] args) { int[] a1 = {10, 11, 12}; int[] a2 = {20, 21, 22}; } a1 120 160 95 10 11 12 120 160 95 20 21 22 a2

10 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); } a1 120 160 95 10 11 12 120 160 95 20 21 22 a2

11 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); } a1 120 160 95 10 11 12 120 160 95 20 21 22 a2 static void swapAll(int[] a1, int[] a2) { }{ } a1 a2

12 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); } a1 120 160 95 10 11 12 120 160 95 20 21 22 a2 static void swapAll(int[] a1, int[] a2) { int[] temp = a1; } a1 a2 temp

13 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); } a1 120 160 95 10 11 12 120 160 95 20 21 22 a2 static void swapAll(int[] a1, int[] a2) { int[] temp = a1; a1 = a2; } a1 a2 temp

14 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); } a1 120 160 95 10 11 12 120 160 95 20 21 22 a2 static void swapAll(int[] a1, int[] a2) { int[] temp = a1; a1 = a2; a2 = temp; } a1 a2 temp

15 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); } a1 120 160 95 10 11 12 120 160 95 20 21 22 a2

16 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; }

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

18 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

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

20 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;

21 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 (http://en.wikipedia.org/wiki/Polymer), statistical mechanics, etc. 21

22 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

23 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 24 % java SelfAvoidingWalks 4 100000 0% dead ends % java SelfAvoidingWalks 8 100000 1% dead ends % java SelfAvoidingWalks 16 100000 20% dead ends … % java SelfAvoidingWalks 64 100000 94% dead ends

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

26 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} }; 2 3 4 5 6 7 8 9 0

27 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} }; 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 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 (); } } }

29 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 10-90 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

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

31 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 45=100*.9/2 45 60=200*.9/3200 60

32 PageRank 32

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

34 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

35 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

36 Storing Web Graph: Adjacent List 5 1 2 2 3 3 4 3 0 0 2 36 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 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]); } }

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

39 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 45=100*.9/2 45 60=200*.9/3200 60

40 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 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) ); }

42 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

43 Computing PageRank x p1p1 p2p2 pnpn …


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

Similar presentations


Ads by Google