Skip to content

PKUFlyingPig/Princeton-Algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 

Repository files navigation

Princeton Algorithm

This repository contains my solution for the coursera course Algorithm I & II

Useful Link

Coursera course website : part1

Coursera course website : part2

Project 1: percolation

Project 2 : Queues

Project 3 : colinear

Project 4 : 8puzzle

Project 5 : kd-trees

Project 6 : WordNet

Project 7 : Seam-Carving

Project 8 : Baseball Elimenation

Project 9 : Boggle

Project 10 : Burrows-Wheeler

Tips for projects

  • project 1 : percolation

    Pay attention to the definition of the full site. If you add two virtual sites for optimization, you should may encounter the bug below :

    for a 3x3 grid, do the following instructions in turn:
    open(1, 3), open(2, 3), open(3, 3), open(3, 1)
    then the grid should look like this: (# for blocked, * for open)
    # # *
    # # *
    * # * 
    Obviously the site (3, 1) should not be full, because there is
    not a path from it to one top open site. But if you add two
    virtual sites, then the site (3, 1) will be connected with the
    bottom virtual site which is connected with the top virtual
    site through the straight line path on the right.
    

    To solve this, my solution is use two union-find, one with two virtual sites, and another with just one top virtual site.

  • Project 2 : Queues

    • Deque (linked-list based): to avoid handling the special case for adding into an empty deque, I add a guard node for every deque, this simplifies the code gracefully.
    • RandomizedDeque (resizing array based) : to achieve const amortized time for deque, you can first randomly pick one item then switch it with the last element in the array. Don't confuse between size (the number of elements in the deque) and capacity (the number of elements the deque can contains).
  • Project 3 : collinear points

    • to avoid duplicated segment, I choose the strategy below : After sorting the points in terms of one specific point x, the first element must be the x itself. To add a new segment into your solution, you only need to ensure that x is the smallest point.
  • Project 5 : kd trees

    • Nearest() : if the two subtrees both need to be checked, first check the subtree which the query point lies in, and remember that after you check one subtree, you may now prune the other subtree.

Wanna Learn More ?

Check out this repository which contains all my self-learning materials : )

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages