Programming Practicum Day 3: Problem Solving with Graphs Aaron Tan NUS School of Computing.
-
Upload
elmer-ramsey -
Category
Documents
-
view
213 -
download
0
Transcript of Programming Practicum Day 3: Problem Solving with Graphs Aaron Tan NUS School of Computing.
Programming Practicum
Day 3: Problem Solving with GraphsAaron Tan
NUS School of Computing
2
Contents Review of Day 2 problems Graphs
[Programming Practicum, December 2009]
3
Day 2 Ex 1: Max. Subseq. Sum (1/3) Given list
3[Programming Practicum, December 2009]
-2 7 1 -9 3 -1 9 -1
Answer = 11
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
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
Day 2 Ex 3: Finding kth 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
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
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
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
31
4
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
31
4
A B C D E F
A 5
B 1 1 3
C 3 5 1
D
E 2
F 4
(There are other graph representations that can give rise to faster algorithms. We introduce adjacency matrix for its simplicity.)
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
31
4
A B C D E F
A 5
B 1 1 3
C 3 5 1
D
E 2
F 4
12
Matrix Multiplication (1/2) Multiplication on square matrices (2-dimensional
arrays) To compute C = A B, where A, B, C are matrices
ci,j = (ai,0 b0,j ) + (ai,1 b1,j ) + . . . + (ai,n-1 bn-1,j ) ci,j is sum of terms produced by multiplying the elements of A’s row i with B’s column j.
[Programming Practicum, December 2009]
2 3 0 1
1 2 1 3
0 2 1 0
3 1 2 2
0 2 1 0
0 0 1 0
2 4 2 1
3 2 1 0
3 6 6 0
11
12
8 1
2 4 4 1
10
18
10 2
=
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
THE END