Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Practicum Day 3: Problem Solving with Graphs Aaron Tan NUS School of Computing.

Similar presentations


Presentation on theme: "Programming Practicum Day 3: Problem Solving with Graphs Aaron Tan NUS School of Computing."— Presentation transcript:

1 Programming Practicum Day 3: Problem Solving with Graphs Aaron Tan NUS School of Computing

2 2 Contents Review of Day 2 problems Graphs [Programming Practicum, December 2009]

3 3 Day 2 Ex 1: Max. Subseq. Sum (1/3) Given list 3 [Programming Practicum, December 2009] -271-939 Answer = 11

4 4 Day 2 Ex 1: Max. Subseq. Sum (2/3) An algorithm 4 [Programming Practicum, December 2009] What is the time complexity? public static int maxSubseqSum(int[] arr) { int sum, maxSum; maxSum = 0; for (int i=0; i<arr.length; i++) { sum = 0; for (int j=i; j<arr.length; j++) { sum += arr[j]; if (sum > maxSum) maxSum = sum; } return maxSum; }

5 5 Day 2 Ex 1: Max. Subseq. Sum (3/3) Another algorithm 5 [Programming Practicum, December 2009] What is the time complexity? public static int maxSubseqSum(int[] arr) { int sum, maxSum; sum = maxSum = 0; for (int i=0; i<arr.length; i++) { sum += arr[i]; if (sum < 0) sum = 0; else if (sum > maxSum) maxSum = sum; } return maxSum; }

6 6 Day 2 Ex 3: Finding k th Smallest Element Belongs to the classic selection problem Many algorithms available We adapt the heapsort:  Create min-heap instead of max-heap  After heapify, instead of swapping the top elements n-1 times (n = array size), we need only to swap it k-1 times and sift-down.  Heapify: O(n)  Swapping k-1 times and sift-down: O(k lg n). If k < n/(lg n), then this is O(n) as well. 6 [Programming Practicum, December 2009]

7 7 Graphs A graph consists of a set of nodes (vertices) connected by edges. Undirected graphs: edges are undirected. Directed graphs: directed edges from a node to another. Edges may be weighted, that is, each of them contains a value (weight). Graph is a very important data structure that supports many applications (Shortest-path, minimum spanning tree, etc.) [Programming Practicum, December 2009]

8 8 Graphs: Degrees Directed graphs:  In-degree of a node: Number of edges pointing towards that node  Out-degree of a node: Number of edges pointing away from that node. Undirected graphs:  Degree of a node: Number of edges connected to that node. [Programming Practicum, December 2009]

9 9 Graphs: Example A road network, where nodes represent cities and edges represent costs (distance, or time). [Programming Practicum, December 2009] A C D B EF 5 51 2 3 1 3 1 4

10 10 Graphs: Representation One simple representation is the 2-dimensional array, known as adjacency matrix. [Programming Practicum, December 2009] A C D B EF 5 51 2 3 1 3 1 4 ABCDEF A5 B113 C351 D E2 F4 (There are other graph representations that can give rise to faster algorithms. We introduce adjacency matrix for its simplicity.)

11 11 Graphs: Exploration How do you determine the in-degree and out- degree of each node by using the adjacency matrix representation of the directed graph? [Programming Practicum, December 2009] A C D B EF 5 51 2 3 1 3 1 4 ABCDEF A5 B113 C351 D E2 F4

12 12 Matrix Multiplication (1/2) Multiplication on square matrices (2-dimensional arrays)  To compute C = A  B, where A, B, C are matrices c i,j = (a i,0  b 0,j ) + (a i,1  b 1,j ) +... + (a i,n-1  b n-1,j ) c i,j is sum of terms produced by multiplying the elements of A’s row i with B’s column j. [Programming Practicum, December 2009] 2301 1213 0210 3122 0210 0010 2421 3210 3660 111281 2441 1018102  =

13 13 Matrix Multiplication (2/2) In CS1101, you were given Matrices.java and told to complete the matrixProduct() method.  Download Matrices.java from the Practicum website. [Programming Practicum, December 2009]

14 14 THE END


Download ppt "Programming Practicum Day 3: Problem Solving with Graphs Aaron Tan NUS School of Computing."

Similar presentations


Ads by Google