Programming
Practicum
Day 3: Problem Solving with Graphs
Aaron Tan
NUS School of Computing
Contents
Review of Day 2 problems
Graphs
[Programming Practicum, December 2009]
2
Day 2 Ex 1: Max. Subseq. Sum (1/3)
Given list
-2
7
1
-9
3
-1
9
-1
Answer = 11
[Programming Practicum, December 2009]
3
Day 2 Ex 1: Max. Subseq. Sum (2/3)
An algorithm
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;
}
What is the time complexity?
[Programming Practicum, December 2009]
4
Day 2 Ex 1: Max. Subseq. Sum (3/3)
Another algorithm
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;
}
What is the time complexity?
[Programming Practicum, December 2009]
5
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.
[Programming Practicum, December 2009]
6
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]
7
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]
8
Graphs: Example
A road network, where nodes represent cities
and edges represent costs (distance, or time).
A
5
1
3
B
3
F
[Programming Practicum, December 2009]
1
1
4
C
E
5
2
D
9
Graphs: Representation
One simple representation is the 2-dimensional
array, known as adjacency matrix.
A
5
1
3
F
4
C
C
1
1
2
C
D
E
F
1
3
5
1
3
5
1
D
5
E
E
B
A
B
3
B
A
D
F
2
4
(There are other graph representations that
can give rise to faster algorithms. We
introduce adjacency matrix for its simplicity.)
[Programming Practicum, December 2009]
10
Graphs: Exploration
How do you determine the in-degree and outdegree of each node by using the adjacency
matrix representation of the directed graph?
A
5
1
3
F
4
C
C
1
1
5
[Programming Practicum, December 2009]
2
D
C
D
E
F
1
3
5
1
3
5
1
D
E
E
B
A
B
3
B
A
F
2
4
11
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.
2 3 0 1
0 2 1 0
3
6
6
0
1 2 1 3
0 0 1 0
11 12
8
1
2
4
4
1
10 18
10
2
0 2 1 0
3 1 2 2
[Programming Practicum, December 2009]
2 4 2 1
3 2 1 0
=
12
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]
13
THE END
14
© Copyright 2026 Paperzz