▶ 书中第三章部分程序,加上自己补充的代码,包含双向索引表、文建索引、稀疏向量类型
● 双向索引表
1 package package01; 2 3 import edu.princeton.cs.algs4.ST; 4 import edu.princeton.cs.algs4.Queue; 5 import edu.princeton.cs.algs4.In; 6 import edu.princeton.cs.algs4.StdIn; 7 import edu.princeton.cs.algs4.StdOut; 8 9 public class class01 10 { 11 private class01() {} 12 13 public static void main(String[] args) 14 { 15 String filename = args[0]; 16 String separator = args[1]; 17 In in = new In(filename); 18 19 ST<String, Queue<String>> st = new ST<String, Queue<String>>(); 20 ST<String, Queue<String>> ts = new ST<String, Queue<String>>(); // 反向索引 21 22 for(;in.hasNextLine();) 23 { 24 String line = in.readLine(); 25 String[] fields = line.split(separator); 26 String key = fields[0]; 27 for (int i = 1; i < fields.length; i++) // 一个 key 对应多个值,分别存放,以后可以根据值反向查找键 28 { 29 String val = fields[i]; 30 if (!st.contains(key)) 31 st.put(key, new Queue<String>()); 32 if (!ts.contains(val)) 33 ts.put(val, new Queue<String>()); 34 st.get(key).enqueue(val); 35 ts.get(val).enqueue(key); 36 } 37 } 38 StdOut.println("Done indexing"); 39 for(;!StdIn.isEmpty();) // 交互式查找,可以正向也可以反向 40 { 41 String query = StdIn.readLine(); 42 if (st.contains(query)) 43 { 44 for (String vals : st.get(query)) 45 StdOut.println(" " + vals); 46 } 47 if (ts.contains(query)) 48 { 49 for (String keys : ts.get(query)) 50 StdOut.println(" " + keys); 51 } 52 } 53 } 54 }
● 文件索引
1 package package01; 2 3 import java.io.File; 4 import edu.princeton.cs.algs4.SET; 5 import edu.princeton.cs.algs4.ST; 6 import edu.princeton.cs.algs4.In; 7 import edu.princeton.cs.algs4.StdIn; 8 import edu.princeton.cs.algs4.StdOut; 9 10 public class class01 11 { 12 private class01() {} 13 14 public static void main(String[] args) 15 { 16 ST<String, SET<File>> st = new ST<String, SET<File>>(); 17 StdOut.println("Indexing files"); 18 for (String filename : args) 19 { 20 StdOut.println(" " + filename); 21 File file = new File(filename); 22 for(In in = new In(file);!in.isEmpty();) 23 { 24 String word = in.readString(); 25 if (!st.contains(word)) 26 st.put(word, new SET<File>()); 27 SET<File> set = st.get(word); 28 set.add(file); 29 } 30 } 31 for(;StdIn.isEmpty();) // 交互式查找包含特定单词的文件 32 { 33 String query = StdIn.readString(); 34 if (st.contains(query)) 35 { 36 SET<File> set = st.get(query); 37 for (File file : set) 38 StdOut.println(" " + file.getName()); 39 } 40 } 41 } 42 }
● 稀疏向量类型
1 package package01; 2 3 import edu.princeton.cs.algs4.ST; 4 import edu.princeton.cs.algs4.StdOut; 5 6 public class class01 7 { 8 private int d; // 向量维数 9 private ST<Integer, Double> st; // 向量 index - value 对 10 11 public class01(int dim) 12 { 13 d = dim; 14 st = new ST<Integer, Double>(); 15 } 16 17 public void put(int i, double value) 18 { 19 if (i < 0 || i >= d) 20 throw new IllegalArgumentException(" <put> i < 0 || i >= d. "); 21 if (value == 0.0) 22 st.delete(i); 23 else 24 st.put(i, value); 25 } 26 27 public double get(int i) 28 { 29 if (i < 0 || i >= d) 30 throw new IllegalArgumentException(" <get> i < 0 || i >= d. "); 31 return (st.contains(i)) ? st.get(i) : 0.0; 32 } 33 34 public int nnz() 35 { 36 return st.size(); 37 } 38 39 @Deprecated 40 public int dimension() { return d; } 41 42 public double dot(class01 that) 43 { 44 if (d != that.d) 45 throw new IllegalArgumentException(" <dot> dimension disagree. "); 46 double sum = 0.0; 47 if (st.size() <= that.st.size()) // 遍历元素较少的向量,去元素较多的向量中查找 48 { 49 for (int i : st.keys()) 50 { 51 if (that.st.contains(i)) 52 sum += get(i) * that.get(i); 53 } 54 } 55 else 56 { 57 for (int i : that.st.keys()) 58 { 59 if (st.contains(i)) 60 sum += get(i) * that.get(i); 61 } 62 } 63 return sum; 64 } 65 66 public double dot(double[] that) 67 { 68 double sum = 0.0; 69 for (int i : st.keys()) 70 sum += that[i] * get(i); 71 return sum; 72 } 73 74 public double magnitude() 75 { 76 return Math.sqrt(dot(this)); 77 } 78 79 public class01 scale(double alpha) 80 { 81 class01 c = new class01(d); 82 for (int i : st.keys()) 83 c.put(i, alpha * get(i)); 84 return c; 85 } 86 87 public class01 plus(class01 that) 88 { 89 if (d != that.d) 90 throw new IllegalArgumentException(" <plus> dimension disagree. "); 91 class01 c = new class01(d); // 新建一个向量存放结果 92 for (int i : st.keys()) 93 c.put(i, get(i)); 94 for (int i : that.st.keys()) 95 c.put(i, that.get(i) + c.get(i)); 96 return c; 97 } 98 99 public String toString() // toString 接口 100 { 101 StringBuilder s = new StringBuilder(); 102 for (int i : st.keys()) 103 s.append("(" + i + ", " + st.get(i) + ") "); 104 return s.toString(); 105 } 106 107 public static void main(String[] args) 108 { 109 class01 a = new class01(10); 110 class01 b = new class01(10); 111 a.put(3, 0.50); 112 a.put(9, 0.75); 113 a.put(6, 0.11); 114 a.put(6, 0.00); 115 b.put(3, 0.60); 116 b.put(4, 0.90); 117 StdOut.println("a = " + a); 118 StdOut.println("b = " + b); 119 StdOut.println("a dot b = " + a.dot(b)); 120 StdOut.println("a + b = " + a.plus(b)); 121 } 122 }