Professional Documents
Culture Documents
http://algs4.cs.princeton.edu
3.5 S YMBOL T ABLE A PPLICATIONS
‣ sets
‣ dictionary clients
‣ indexing clients
Algorithms
‣ sparse vectors
http://algs4.cs.princeton.edu
Set API
Q. How to implement?
3
Exception filter
% more list.txt
list of exceptional words
was it the of
4
Exception filter applications
5
Exception filter: Java implementation
In in = new In(args[0]);
while (!in.isEmpty()) read in whitelist
set.add(in.readString());
while (!StdIn.isEmpty())
{
String word = StdIn.readString();
print words not in list
if (set.contains(word))
StdOut.println(word);
}
}
}
6
Exception filter: Java implementation
In in = new In(args[0]);
while (!in.isEmpty()) read in whitelist
set.add(in.readString());
while (!StdIn.isEmpty())
{
String word = StdIn.readString();
print words not in list
if (!set.contains(word))
StdOut.println(word);
}
}
}
7
3.5 S YMBOL T ABLE A PPLICATIONS
‣ sets
‣ dictionary clients
‣ indexing clients
Algorithms
‣ sparse vectors
http://algs4.cs.princeton.edu
3.5 S YMBOL T ABLE A PPLICATIONS
‣ sets
‣ dictionary clients
‣ indexing clients
Algorithms
‣ sparse vectors
http://algs4.cs.princeton.edu
Dictionary lookup
Command-line arguments.
% more ip.csv
・A comma-separated value (CSV) file. www.princeton.edu,128.112.128.15
www.cs.princeton.edu,128.112.136.35
・Key field. www.math.princeton.edu,128.112.18.11
・Value field.
www.cs.harvard.edu,140.247.50.127
www.harvard.edu,128.103.60.24
www.yale.edu,130.132.51.8
www.econ.yale.edu,128.36.236.74
www.cs.yale.edu,128.36.229.30
Ex 1. DNS lookup. espn.com,199.181.135.201
URL is key IP is value yahoo.com,66.94.234.13
msn.com,207.68.172.246
google.com,64.233.167.99
% java LookupCSV ip.csv 0 1 baidu.com,202.108.22.33
adobe.com yahoo.co.jp,202.93.91.141
sina.com.cn,202.108.33.32
192.150.18.60
ebay.com,66.135.192.87
www.princeton.edu adobe.com,192.150.18.60
128.112.128.15 163.com,220.181.29.154
ebay.edu IP is key URL is value
passport.net,65.54.179.226
tom.com,61.135.158.237
Not found
nate.com,203.226.253.11
cnn.com,64.236.16.20
% java LookupCSV ip.csv 1 0 daum.net,211.115.77.211
128.112.128.15 blogger.com,66.102.15.100
www.princeton.edu fastclick.com,205.180.86.4
wikipedia.org,66.230.200.100
999.999.999.99
rakuten.co.jp,202.72.51.22
Not found ...
10
Dictionary lookup
11
Dictionary lookup
・Value field.
10,Douglas,Malia Morioka,P01,malia
12,Haddock,Sara Lynn,P01,shaddock
12,Hantman,Nicole Samantha,P01,nhantman
11,Hesterberg,Adam Classen,P01,ahesterb
Ex 3. Class list. 13,Hwang,Roland Lee,P01,rhwang
13,Hyde,Gregory Thomas,P01,ghyde
first name 13,Kim,Hyunmoon,P01,hktwo
login is key is value 12,Korac,Damjan,P01,dkorac
11,MacDonald,Graham David,P01,gmacdona
10,Michal,Brian Thomas,P01,bmichal
% java LookupCSV classlist.csv 4 1 12,Nam,Seung Hyeon,P01,seungnam
eberl 11,Nastasescu,Maria Monica,P01,mnastase
Ethan section 11,Pan,Di,P01,dpan
nwebb login is key is value 12,Partridge,Brenton Alan,P01,bpartrid
Natalie 13,Rilee,Alexander,P01,arilee
13,Roopakalu,Ajay,P01,aroopaka
% java LookupCSV classlist.csv 4 3 11,Sheng,Ben C,P01,bsheng
dpan 12,Webb,Natalie Sue,P01,nwebb
P01 ⋮
12
Dictionary lookup: Java implementation
while (!StdIn.isEmpty())
{
process lookups
String s = StdIn.readString(); with standard I/O
if (!st.contains(s)) StdOut.println("Not found");
else StdOut.println(st.get(s));
}
}
}
13
3.5 S YMBOL T ABLE A PPLICATIONS
‣ sets
‣ dictionary clients
‣ indexing clients
Algorithms
‣ sparse vectors
http://algs4.cs.princeton.edu
3.5 S YMBOL T ABLE A PPLICATIONS
‣ sets
‣ dictionary clients
‣ indexing clients
Algorithms
‣ sparse vectors
http://algs4.cs.princeton.edu
File indexing
16
File indexing
Goal. Given a list of files specified, create an index so that you can
efficiently find all files containing a given query string.
% ls *.txt % ls *.java
aesop.txt magna.txt moby.txt BlackList.java Concordance.java
sawyer.txt tale.txt DeDup.java FileIndex.java ST.java
SET.java WhiteList.java
% java FileIndex *.txt
% java FileIndex *.java
freedom
magna.txt moby.txt tale.txt import
FileIndex.java SET.java ST.java
whale
moby.txt Comparator
null
lamb
sawyer.txt aesop.txt
Solution. Key = query string; value = set of files containing that string.
17
File indexing
import java.io.File;
public class FileIndex
{
public static void main(String[] args)
{
ST<String, SET<File>> st = new ST<String, SET<File>>(); symbol table
while (!StdIn.isEmpty())
{
String query = StdIn.readString(); process queries
StdOut.println(st.get(query));
}
}
}
18
Book index
19
Concordance
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
20
Concordance
while (!StdIn.isEmpty())
{ process queries
String query = StdIn.readString(); and print
SET<Integer> set = st.get(query); concordances
for (int k : set)
// print words[k-4] to words[k+4]
}
}
}
21
3.5 S YMBOL T ABLE A PPLICATIONS
‣ sets
‣ dictionary clients
‣ indexing clients
Algorithms
‣ sparse vectors
http://algs4.cs.princeton.edu
3.5 S YMBOL T ABLE A PPLICATIONS
‣ sets
‣ dictionary clients
‣ indexing clients
Algorithms
‣ sparse vectors
http://algs4.cs.princeton.edu
Matrix-vector multiplication (standard implementation)
Matrix-vector multiplication
...
double[][] a = new double[N][N];
double[] x = new double[N];
double[] b = new double[N];
...
// initialize a[][] and x[]
... 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;
}
24
Sparse matrix-vector multiplication
A * x = b
25
Vector representations
st
26
Sparse vector data type
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
0.0
0.0 0.0
0.0 .36
.36 .36
.36 .18
.18
0 0 0 0
1 1 0 0 1 1 2 2 3 3 4 4 1 1
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
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
0 0 1 1 2 2 3 3 4 4
.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 28
Sparse matrix-vector multiplication
Matrix-vector multiplication
..
SparseVector[] a = new SparseVector[N];
double[] x = new double[N];
double[] b = new double[N];
...
// Initialize a[] and x[]
...
for (int i = 0; i < N; i++) linear running time
b[i] = a[i].dot(x); for sparse matrix
29
3.5 S YMBOL T ABLE A PPLICATIONS
‣ sets
‣ dictionary clients
‣ indexing clients
Algorithms
‣ sparse vectors
http://algs4.cs.princeton.edu
Algorithms R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu