楼主: Lisrelchen
2891 4

Algorithms(using Java) 4th Edition [推广有奖]

  • 0关注
  • 62粉丝

VIP

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
49957 个
通用积分
79.5487
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币


Algorithms (4th Edition)


Robert Sedgewick (Author), Kevin Wayne (Author)


This fourth edition of Robert Sedgewick and Kevin Wayne’s Algorithms is the leading textbook on algorithms today and is widely used in colleges and universities worldwide. This book surveys the most important computer algorithms currently in use and provides a full treatment of data structures and algorithms for sorting, searching, graph processing, and string processing -- including fifty algorithms every programmer should know. In this edition, new Java implementations are written in an accessible modular programming style, where all of the code is exposed to the reader and ready to use.

The algorithms in this book represent a body of knowledge developed over the last 50 years that has become indispensable, not just for professional programmers and computer science students but for any student with interests in science, mathematics, and engineering, not to mention students who use computation in the liberal arts.

The companion web site, algs4.cs.princeton.edu contains

An online synopsisFull Java implementationsTest dataExercises and answersDynamic visualizationsLecture slidesProgramming assignments with checklistsLinks to related material

The MOOC related to this book is accessible via the "Online Course" link at algs4.cs.princeton.edu. The course offers more than 100 video lecture segments that are integrated with the text, extensive online assessments, and the large-scale discussion forums that have proven so valuable. Offered each fall and spring, this course regularly attracts tens of thousands of registrants.

Robert Sedgewick and Kevin Wayne are developing a modern approach to disseminating knowledge that fully embraces technology, enabling people all around the world to discover new ways of learning and teaching. By integrating their textbook, online content, and MOOC, all at the state of the art, they have built a unique resource that greatly expands the breadth and depth of the educational experience.


Product Details
  • Hardcover: 992 pages
  • Publisher: Addison-Wesley Professional; 4 edition (March 9 2011)
  • Language: English
  • ISBN-10: 032157351X
  • ISBN-13: 978-0321573513
  • Product Dimensions: 19 x 3.8 x 23.4 cm
http://algs4.cs.princeton.edu/code/






二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Algorithms Algorithm Edition editio dition currently important including searching computer

本帖被以下文库推荐

沙发
Lisrelchen 发表于 2014-11-5 11:14:18 |只看作者 |坛友微信交流群
  1. PolynomialRegression.java


  2. Below is the syntax highlighted version of PolynomialRegression.java from § Algorithms.   Here is the Javadoc.


  3. /*************************************************************************
  4. *  Compilation:  javac -cp .:jama.jar PolynomialRegression.java
  5. *  Execution:    java  -cp .:jama.jar PolynomialRegression
  6. *  Dependencies: jama.jar StdOut.java
  7. *
  8. *  % java -cp .:jama.jar PolynomialRegression
  9. *  0.01 N^3 + -1.64 N^2 + 168.92 N + -2113.73 (R^2 = 0.997)
  10. *
  11. *************************************************************************/

  12. import Jama.Matrix;
  13. import Jama.QRDecomposition;

  14. /**
  15. *  The <tt>PolynomialRegression</tt> class performs a polynomial regression
  16. *  on an set of <em>N</em> data points (<em>y<sub>i</sub></em>, <em>x<sub>i</sub></em>).
  17. *  That is, it fits a polynomial
  18. *  <em>y</em> = β<sub>0</sub> +  β<sub>1</sub> <em>x</em> +
  19. *  β<sub>2</sub> <em>x</em><sup>2</sup> + ... +
  20. *  β<sub><em>d</em></sub> <em>x</em><sup><em>d</em></sup>
  21. *  (where <em>y</em> is the response variable, <em>x</em> is the predictor variable,
  22. *  and the β<sub><em>i</em></sub> are the regression coefficients)
  23. *  that minimizes the sum of squared residuals of the multiple regression model.
  24. *  It also computes associated the coefficient of determination <em>R</em><sup>2</sup>.
  25. *  <p>
  26. *  This implementation performs a QR-decomposition of the underlying
  27. *  Vandermonde matrix, so it is not the fastest or most numerically
  28. *  stable way to perform the polynomial regression.
  29. *
  30. *  @author Robert Sedgewick
  31. *  @author Kevin Wayne
  32. */
  33. public class PolynomialRegression {
  34.     private final int N;         // number of observations
  35.     private final int degree;    // degree of the polynomial regression
  36.     private final Matrix beta;   // the polynomial regression coefficients
  37.     private double SSE;          // sum of squares due to error
  38.     private double SST;          // total sum of squares

  39.   /**
  40.      * Performs a polynomial reggression on the data points <tt>(y[i], x[i])</tt>.
  41.      * @param x the values of the predictor variable
  42.      * @param y the corresponding values of the response variable
  43.      * @param degree the degree of the polynomial to fit
  44.      * @throws java.lang.IllegalArgumentException if the lengths of the two arrays are not equal
  45.      */
  46.     public PolynomialRegression(double[] x, double[] y, int degree) {
  47.         this.degree = degree;
  48.         N = x.length;

  49.         // build Vandermonde matrix
  50.         double[][] vandermonde = new double[N][degree+1];
  51.         for (int i = 0; i < N; i++) {
  52.             for (int j = 0; j <= degree; j++) {
  53.                 vandermonde[i][j] = Math.pow(x[i], j);
  54.             }
  55.         }
  56.         Matrix X = new Matrix(vandermonde);

  57.         // create matrix from vector
  58.         Matrix Y = new Matrix(y, N);

  59.         // find least squares solution
  60.         QRDecomposition qr = new QRDecomposition(X);
  61.         beta = qr.solve(Y);


  62.         // mean of y[] values
  63.         double sum = 0.0;
  64.         for (int i = 0; i < N; i++)
  65.             sum += y[i];
  66.         double mean = sum / N;

  67.         // total variation to be accounted for
  68.         for (int i = 0; i < N; i++) {
  69.             double dev = y[i] - mean;
  70.             SST += dev*dev;
  71.         }

  72.         // variation not accounted for
  73.         Matrix residuals = X.times(beta).minus(Y);
  74.         SSE = residuals.norm2() * residuals.norm2();
  75.     }

  76.    /**
  77.      * Returns the <tt>j</tt>th regression coefficient
  78.      * @return the <tt>j</tt>th regression coefficient
  79.      */
  80.     public double beta(int j) {
  81.         return beta.get(j, 0);
  82.     }

  83.    /**
  84.      * Returns the degree of the polynomial to fit
  85.      * @return the degree of the polynomial to fit
  86.      */
  87.     public int degree() {
  88.         return degree;
  89.     }

  90.    /**
  91.      * Returns the coefficient of determination <em>R</em><sup>2</sup>.
  92.      * @return the coefficient of determination <em>R</em><sup>2</sup>, which is a real number between 0 and 1
  93.      */
  94.     public double R2() {
  95.         if (SST == 0.0) return 1.0;   // constant function
  96.         return 1.0 - SSE/SST;
  97.     }

  98.    /**
  99.      * Returns the expected response <tt>y</tt> given the value of the predictor
  100.      *    variable <tt>x</tt>.
  101.      * @param x the value of the predictor variable
  102.      * @return the expected response <tt>y</tt> given the value of the predictor
  103.      *    variable <tt>x</tt>
  104.      */
  105.     public double predict(double x) {
  106.         // horner's method
  107.         double y = 0.0;
  108.         for (int j = degree; j >= 0; j--)
  109.             y = beta(j) + (x * y);
  110.         return y;
  111.     }

  112.    /**
  113.      * Returns a string representation of the polynomial regression model.
  114.      * @return a string representation of the polynomial regression model,
  115.      *   including the best-fit polynomial and the coefficient of determination <em>R</em><sup>2</sup>
  116.      */
  117.     public String toString() {
  118.         String s = "";
  119.         int j = degree;

  120.         // ignoring leading zero coefficients
  121.         while (j >= 0 && Math.abs(beta(j)) < 1E-5)
  122.             j--;

  123.         // create remaining terms
  124.         for (j = j; j >= 0; j--) {
  125.             if      (j == 0) s += String.format("%.2f ", beta(j));
  126.             else if (j == 1) s += String.format("%.2f N + ", beta(j));
  127.             else             s += String.format("%.2f N^%d + ", beta(j), j);
  128.         }
  129.         return s + "  (R^2 = " + String.format("%.3f", R2()) + ")";
  130.     }

  131.     public static void main(String[] args) {
  132.         double[] x = { 10, 20, 40, 80, 160, 200 };
  133.         double[] y = { 100, 350, 1500, 6700, 20160, 40000 };
  134.         PolynomialRegression regression = new PolynomialRegression(x, y, 3);
  135.         StdOut.println(regression);
  136.     }
  137. }



  138. Copyright © 2002–2010, Robert Sedgewick and Kevin Wayne.
  139. Last updated: Sat Oct 5 06:29:24 EDT 2013.
复制代码


使用道具

藤椅
Lisrelchen 发表于 2014-11-5 11:15:21 |只看作者 |坛友微信交流群
  1. Bag.java


  2. Below is the syntax highlighted version of Bag.java from § Algorithms.   Here is the Javadoc.


  3. /*************************************************************************
  4. *  Compilation:  javac Bag.java
  5. *  Execution:    java Bag < input.txt
  6. *
  7. *  A generic bag or multiset, implemented using a singly-linked list.
  8. *
  9. *  % more tobe.txt
  10. *  to be or not to - be - - that - - - is
  11. *
  12. *  % java Bag < tobe.txt
  13. *  size of bag = 14
  14. *  is
  15. *  -
  16. *  -
  17. *  -
  18. *  that
  19. *  -
  20. *  -
  21. *  be
  22. *  -
  23. *  to
  24. *  not
  25. *  or
  26. *  be
  27. *  to
  28. *
  29. *************************************************************************/

  30. import java.util.Iterator;
  31. import java.util.NoSuchElementException;

  32. /**
  33. *  The <tt>Bag</tt> class represents a bag (or multiset) of
  34. *  generic items. It supports insertion and iterating over the
  35. *  items in arbitrary order.
  36. *  <p>
  37. *  This implementation uses a singly-linked list with a static nested class Node.
  38. *  See {@link LinkedBag} for the version from the
  39. *  textbook that uses a non-static nested class.
  40. *  The <em>add</em>, <em>isEmpty</em>, and <em>size</em> operations
  41. *  take constant time. Iteration takes time proportional to the number of items.
  42. *  <p>
  43. *  For additional documentation, see <a href="http://algs4.cs.princeton.edu/13stacks">Section 1.3</a> of
  44. *  <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
  45. *
  46. *  @author Robert Sedgewick
  47. *  @author Kevin Wayne
  48. */
  49. public class Bag<Item> implements Iterable<Item> {
  50.     private int N;               // number of elements in bag
  51.     private Node<Item> first;    // beginning of bag

  52.     // helper linked list class
  53.     private static class Node<Item> {
  54.         private Item item;
  55.         private Node<Item> next;
  56.     }

  57.     /**
  58.      * Initializes an empty bag.
  59.      */
  60.     public Bag() {
  61.         first = null;
  62.         N = 0;
  63.     }

  64.     /**
  65.      * Is this bag empty?
  66.      * @return true if this bag is empty; false otherwise
  67.      */
  68.     public boolean isEmpty() {
  69.         return first == null;
  70.     }

  71.     /**
  72.      * Returns the number of items in this bag.
  73.      * @return the number of items in this bag
  74.      */
  75.     public int size() {
  76.         return N;
  77.     }

  78.     /**
  79.      * Adds the item to this bag.
  80.      * @param item the item to add to this bag
  81.      */
  82.     public void add(Item item) {
  83.         Node<Item> oldfirst = first;
  84.         first = new Node<Item>();
  85.         first.item = item;
  86.         first.next = oldfirst;
  87.         N++;
  88.     }


  89.     /**
  90.      * Returns an iterator that iterates over the items in the bag in arbitrary order.
  91.      * @return an iterator that iterates over the items in the bag in arbitrary order
  92.      */
  93.     public Iterator<Item> iterator()  {
  94.         return new ListIterator<Item>(first);  
  95.     }

  96.     // an iterator, doesn't implement remove() since it's optional
  97.     private class ListIterator<Item> implements Iterator<Item> {
  98.         private Node<Item> current;

  99.         public ListIterator(Node<Item> first) {
  100.             current = first;
  101.         }

  102.         public boolean hasNext()  { return current != null;                     }
  103.         public void remove()      { throw new UnsupportedOperationException();  }

  104.         public Item next() {
  105.             if (!hasNext()) throw new NoSuchElementException();
  106.             Item item = current.item;
  107.             current = current.next;
  108.             return item;
  109.         }
  110.     }

  111.     /**
  112.      * Unit tests the <tt>Bag</tt> data type.
  113.      */
  114.     public static void main(String[] args) {
  115.         Bag<String> bag = new Bag<String>();
  116.         while (!StdIn.isEmpty()) {
  117.             String item = StdIn.readString();
  118.             bag.add(item);
  119.         }

  120.         StdOut.println("size of bag = " + bag.size());
  121.         for (String s : bag) {
  122.             StdOut.println(s);
  123.         }
  124.     }


  125. }


  126. Copyright © 2002–2010, Robert Sedgewick and Kevin Wayne.
  127. Last updated: Tue Mar 25 04:52:35 EDT 2014.
复制代码


使用道具

板凳
Lisrelchen 发表于 2014-11-5 11:21:48 |只看作者 |坛友微信交流群
  1. BTree.java


  2. Below is the syntax highlighted version of BTree.java from §6.2 B-trees.


  3. /*************************************************************************
  4. *  Compilation:  javac BTree.java
  5. *  Execution:    java BTree
  6. *
  7. *  B-tree.
  8. *
  9. *  Limitations
  10. *  -----------
  11. *   -  Assumes M is even and M >= 4
  12. *   -  should b be an array of children or list (it would help with
  13. *      casting to make it a list)
  14. *
  15. *************************************************************************/


  16. public class BTree<Key extends Comparable<Key>, Value>  {
  17.     private static final int M = 4;    // max children per B-tree node = M-1

  18.     private Node root;             // root of the B-tree
  19.     private int HT;                // height of the B-tree
  20.     private int N;                 // number of key-value pairs in the B-tree

  21.     // helper B-tree node data type
  22.     private static final class Node {
  23.         private int m;                             // number of children
  24.         private Entry[] children = new Entry[M];   // the array of children
  25.         private Node(int k) { m = k; }             // create a node with k children
  26.     }

  27.     // internal nodes: only use key and next
  28.     // external nodes: only use key and value
  29.     private static class Entry {
  30.         private Comparable key;
  31.         private Object value;
  32.         private Node next;     // helper field to iterate over array entries
  33.         public Entry(Comparable key, Object value, Node next) {
  34.             this.key   = key;
  35.             this.value = value;
  36.             this.next  = next;
  37.         }
  38.     }

  39.     // constructor
  40.     public BTree() { root = new Node(0); }

  41.     // return number of key-value pairs in the B-tree
  42.     public int size() { return N; }

  43.     // return height of B-tree
  44.     public int height() { return HT; }


  45.     // search for given key, return associated value; return null if no such key
  46.     public Value get(Key key) { return search(root, key, HT); }
  47.     private Value search(Node x, Key key, int ht) {
  48.         Entry[] children = x.children;

  49.         // external node
  50.         if (ht == 0) {
  51.             for (int j = 0; j < x.m; j++) {
  52.                 if (eq(key, children[j].key)) return (Value) children[j].value;
  53.             }
  54.         }

  55.         // internal node
  56.         else {
  57.             for (int j = 0; j < x.m; j++) {
  58.                 if (j+1 == x.m || less(key, children[j+1].key))
  59.                     return search(children[j].next, key, ht-1);
  60.             }
  61.         }
  62.         return null;
  63.     }


  64.     // insert key-value pair
  65.     // add code to check for duplicate keys
  66.     public void put(Key key, Value value) {
  67.         Node u = insert(root, key, value, HT);
  68.         N++;
  69.         if (u == null) return;

  70.         // need to split root
  71.         Node t = new Node(2);
  72.         t.children[0] = new Entry(root.children[0].key, null, root);
  73.         t.children[1] = new Entry(u.children[0].key, null, u);
  74.         root = t;
  75.         HT++;
  76.     }


  77.     private Node insert(Node h, Key key, Value value, int ht) {
  78.         int j;
  79.         Entry t = new Entry(key, value, null);

  80.         // external node
  81.         if (ht == 0) {
  82.             for (j = 0; j < h.m; j++) {
  83.                 if (less(key, h.children[j].key)) break;
  84.             }
  85.         }

  86.         // internal node
  87.         else {
  88.             for (j = 0; j < h.m; j++) {
  89.                 if ((j+1 == h.m) || less(key, h.children[j+1].key)) {
  90.                     Node u = insert(h.children[j++].next, key, value, ht-1);
  91.                     if (u == null) return null;
  92.                     t.key = u.children[0].key;
  93.                     t.next = u;
  94.                     break;
  95.                 }
  96.             }
  97.         }

  98.         for (int i = h.m; i > j; i--) h.children[i] = h.children[i-1];
  99.         h.children[j] = t;
  100.         h.m++;
  101.         if (h.m < M) return null;
  102.         else         return split(h);
  103.     }

  104.     // split node in half
  105.     private Node split(Node h) {
  106.         Node t = new Node(M/2);
  107.         h.m = M/2;
  108.         for (int j = 0; j < M/2; j++)
  109.             t.children[j] = h.children[M/2+j];
  110.         return t;   
  111.     }

  112.     // for debugging
  113.     public String toString() {
  114.         return toString(root, HT, "") + "\n";
  115.     }
  116.     private String toString(Node h, int ht, String indent) {
  117.         String s = "";
  118.         Entry[] children = h.children;

  119.         if (ht == 0) {
  120.             for (int j = 0; j < h.m; j++) {
  121.                 s += indent + children[j].key + " " + children[j].value + "\n";
  122.             }
  123.         }
  124.         else {
  125.             for (int j = 0; j < h.m; j++) {
  126.                 if (j > 0) s += indent + "(" + children[j].key + ")\n";
  127.                 s += toString(children[j].next, ht-1, indent + "     ");
  128.             }
  129.         }
  130.         return s;
  131.     }


  132.     // comparison functions - make Comparable instead of Key to avoid casts
  133.     private boolean less(Comparable k1, Comparable k2) {
  134.         return k1.compareTo(k2) < 0;
  135.     }

  136.     private boolean eq(Comparable k1, Comparable k2) {
  137.         return k1.compareTo(k2) == 0;
  138.     }


  139.    /*************************************************************************
  140.     *  test client
  141.     *************************************************************************/
  142.     public static void main(String[] args) {
  143.         BTree<String, String> st = new BTree<String, String>();

  144. //      st.put("www.cs.princeton.edu", "128.112.136.12");
  145.         st.put("www.cs.princeton.edu", "128.112.136.11");
  146.         st.put("www.princeton.edu",    "128.112.128.15");
  147.         st.put("www.yale.edu",         "130.132.143.21");
  148.         st.put("www.simpsons.com",     "209.052.165.60");
  149.         st.put("www.apple.com",        "17.112.152.32");
  150.         st.put("www.amazon.com",       "207.171.182.16");
  151.         st.put("www.ebay.com",         "66.135.192.87");
  152.         st.put("www.cnn.com",          "64.236.16.20");
  153.         st.put("www.google.com",       "216.239.41.99");
  154.         st.put("www.nytimes.com",      "199.239.136.200");
  155.         st.put("www.microsoft.com",    "207.126.99.140");
  156.         st.put("www.dell.com",         "143.166.224.230");
  157.         st.put("www.slashdot.org",     "66.35.250.151");
  158.         st.put("www.espn.com",         "199.181.135.201");
  159.         st.put("www.weather.com",      "63.111.66.11");
  160.         st.put("www.yahoo.com",        "216.109.118.65");


  161.         StdOut.println("cs.princeton.edu:  " + st.get("www.cs.princeton.edu"));
  162.         StdOut.println("hardvardsucks.com: " + st.get("www.harvardsucks.com"));
  163.         StdOut.println("simpsons.com:      " + st.get("www.simpsons.com"));
  164.         StdOut.println("apple.com:         " + st.get("www.apple.com"));
  165.         StdOut.println("ebay.com:          " + st.get("www.ebay.com"));
  166.         StdOut.println("dell.com:          " + st.get("www.dell.com"));
  167.         StdOut.println();

  168.         StdOut.println("size:    " + st.size());
  169.         StdOut.println("height:  " + st.height());
  170.         StdOut.println(st);
  171.         StdOut.println();
  172.     }

  173. }


  174. Copyright © 2002–2010, Robert Sedgewick and Kevin Wayne.
  175. Last updated: Mon Jul 25 08:34:07 EDT 2011.
复制代码


使用道具

报纸
unseenshore 发表于 2016-5-7 07:09:22 |只看作者 |坛友微信交流群
请问楼主忘记贴附件了吗?还是我阅读权限不够,看不到附件。

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-28 08:00