码迷,mamicode.com
首页 > 编程语言 > 详细

《算法》第三章部分程序 part 6

时间:2018-10-16 01:56:23      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:for   cep   indexing   math   print   0.11   lin   length   pre   

? 书中第三章部分程序,加上自己补充的代码,包含双向索引表、文建索引、稀疏向量类型

● 双向索引表

 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 d)
 12     {
 13         this.d = d;
 14         this.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("\n<put> i < 0 || i >= d.\n");
 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("\n<get> i < 0 || i >= d.\n");
 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 (this.d != that.d)
 45             throw new IllegalArgumentException("\n<dot> dimension disagree.\n");
 46         double sum = 0.0;
 47         if (this.st.size() <= that.st.size())       // 遍历元素较少的向量,去元素较多的向量中查找
 48         {
 49             for (int i : this.st.keys())
 50             {
 51                 if (that.st.contains(i))
 52                     sum += this.get(i) * that.get(i);
 53             }
 54         }
 55         else
 56         {
 57             for (int i : that.st.keys())
 58             {
 59                 if (this.st.contains(i))
 60                     sum += this.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] * this.get(i);
 71         return sum;
 72     }
 73 
 74     public double magnitude()
 75     {
 76         return Math.sqrt(this.dot(this));
 77     }
 78 
 79     public class01 scale(double alpha)
 80     {
 81         class01 c = new class01(d);
 82         for (int i : this.st.keys())
 83             c.put(i, alpha * this.get(i));
 84         return c;
 85     }
 86 
 87     public class01 plus(class01 that)
 88     {
 89         if (this.d != that.d)
 90             throw new IllegalArgumentException("\n<plus> dimension disagree.\n");
 91         class01 c = new class01(d);                 // 新建一个向量存放结果
 92         for (int i : this.st.keys())
 93             c.put(i, this.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 }

 

《算法》第三章部分程序 part 6

标签:for   cep   indexing   math   print   0.11   lin   length   pre   

原文地址:https://www.cnblogs.com/cuancuancuanhao/p/9795441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!