35SearchingApplications 2x2
35SearchingApplications 2x2
http://algs4.cs.princeton.edu http://algs4.cs.princeton.edu
Mathematical set. A collection of distinct keys. ・Read in a list of words from one file.
・Print out all words from standard input that are { in, not in } the list.
public class SET<Key extends Comparable<Key>>
Iterator<Key> iterator() iterator through keys in the set % java BlackList list.txt < tinyTale.txt
best times worst times
age wisdom age foolishness
epoch belief epoch incredulity
season light season darkness
spring hope winter despair
Q. How to implement?
3 4
Exception filter applications Exception filter: Java implementation
・Read in a list of words from one file. ・Read in a list of words from one file.
・Print out all words from standard input that are { in, not in } the list. ・Print out all words from standard input that are in the list.
5 6
7
Dictionary lookup Dictionary lookup
9 10
http://algs4.cs.princeton.edu
14
Goal. Given a list of files, create an index so that you can efficiently find all import java.io.File;
public class FileIndex
files containing a given query string. {
public static void main(String[] args)
{
ST<String, SET<File>> st = new ST<String, SET<File>>(); symbol table
% ls *.txt % ls *.java
aesop.txt magna.txt moby.txt BlackList.java Concordance.java for (String filename : args) { list of file names
File file = new File(filename); from command line
sawyer.txt tale.txt DeDup.java FileIndex.java ST.java
In in = new In(file);
SET.java WhiteList.java
while (!in.isEmpty())
% java FileIndex *.txt
{ for each word in file,
% java FileIndex *.java
String key = in.readString(); add file to
freedom if (!st.contains(key)) corresponding set
magna.txt moby.txt tale.txt import st.put(word, new SET<File>());
FileIndex.java SET.java ST.java SET<File> set = st.get(key);
whale set.add(file);
moby.txt Comparator }
null }
lamb
sawyer.txt aesop.txt while (!StdIn.isEmpty())
{
String query = StdIn.readString(); process queries
StdOut.println(st.get(query));
}
Solution. Key = query string; value = set of files containing that string. }
}
15 16
Book index Concordance
Goal. Index for an e-book. Goal. Preprocess a text corpus to support concordance queries:
given a word, find all occurrences with their immediate contexts.
majesty
their turnkeys and the *majesty* of the law fired
me treason against the *majesty* of the people in
of his most gracious *majesty* king george the third
princeton
no matches
Solution. Key = query string; value = set of indices containing that string.
17 18
Concordance
Matrix-vector multiplication
...
double[][] a = new double[N][N];
double[] x = new double[N];
double[] b = new double[N];
...
// initialize a[][] and x[]
A * x = b
... nested loops
for (int i = 0; i < N; i++) (N2 running time)
{
sum = 0.0;
for (int j = 0; j < N; j++)
sum += a[i][j]*x[j];
b[i] = sum;
}
21 22
2D array (standard) matrix representation: Each row of matrix is an array. a[][] x[] b[]
・Space proportional to N . 2
0 0 .36 .36 .18 .04 .297
0 0 0 .90 0 .36 = .333
Sparse matrix representation: Each row of matrix is a sparse vector. .90 0 0 0 0 .37 .045
.47 0 .47 0 0 .19 .1927
・Efficient access to elements.
・Space proportional to number of nonzeros (plus N).
array of double[]objects
Matrix-vector multiplication
array of double[]objects array of SparseVector
array objects
of SparseVector objects
0 0 1 1 2 2 3 3 4 4 st st
1 1 .90
.90
0.0
0.0 .90
.90 0.0
0.0 0.0
0.0 0.0
0.0
keykey value
value
..
0 0 1 1 2 2 3 3 4 4 st st
a a a a .36 3 3 .36
2 2 .36 .36 4 4 .18
.18 SparseVector[] a = new SparseVector[N];
0.0
0.0 0.0
0.0 .36
.36 .36
.36 .18
.18
0 0 0 0 double[] x = new double[N];
1 1 0 0 1 1 2 2 3 3 4 4 1 1 double[] b = new double[N];
st st
2 2 0.0
0.0 0.0
0.0 0.0
0.0 .90
.90 0.0
0.0 2 2 3 3 .90
.90 independent
independent
symbol-table
symbol-table ...
3 3 3 3 objects
objects
0 0 1 1 2 2 3 3 4 4 // Initialize a[] and x[]
4 4 4 4 st st
.90
.90 0.0
0.0 0.0
0.0 0.0
0.0 0.0
0.0 0 0 .90
.90
...
for (int i = 0; i < N; i++) linear running time
0 0 1 1 2 2 3 3 4 4
b[i] = a[i].dot(x); for sparse matrix
.45
.45 0.0
0.0 .45
.45 0.0
0.0 0.0
0.0 st st
0 0 .45
.45 2 2 .45
.45
a[4][2]
a[4][2]
Sparse matrix
Sparse representations
matrix representations 25 26