Closed yzhang-gh closed 9 years ago
It seems you're right. Do you have any ideas how can it be implemented in code?
I had looked for solutions for "backwash". A straight-forward method is to declare another WeightedQuickUnionUF wquf2 which don't connect to the virtual bottom site. Use wquf(1) in function percolates(), use wquf2 in function isFull(). Someone provided another elegant approach but I can't understand... This is the screenshot
By the way, thank you for your codes. I'm learning Algorithms on Coursera recently. I often have a look at your codes after writing my own version. It's really helpful.
I guess I'll leave solution the way it is. Don't have enough time to change it. You're welcome. Algorithms II will be here soon too :)
I think it's not important too. It's so trivial a problem.
Implementing the solution provided does seem like it would be clever, but I think it's better to instantiate two UF's: the percolation client should not need to know details of how UF is implemented in order to determine whether a cell is full. (Some implementations of UF might not have a findRoot method.)
@yzhang-gh @firezdog @alexilyenko Hi guys! I thought you might be interested. I got another solution for backwash problem and all is needed is only single n*n+1 size array (I don't use OOTB union path structures). I use quick search + path compression algorithm. Though it's not as fast as weighted QU + path compression, it's logN, so it's fine. The main idea is to keep the greatest id as root and always set it as root on union operation. There's only 1 top abstract element. To check whether the grid percolates, we need to get top abstract element's root and check whether it's in the last row. Element with value -1 represents closed site. Please see the implementation here:
@yzhang-gh @firezdog @alexilyenko Hi guys! I thought you might be interested. I got another solution for backwash problem and all is needed is only single n*n+1 size array (I don't use OOTB union path structures). I use quick search + path compression algorithm. Though it's not as fast as weighted QU + path compression, it's logN, so it's fine. The main idea is to keep the greatest id as root and always set it as root on union operation. There's only 1 top abstract element. To check whether the grid percolates, we need to get top abstract element's root and check whether it's in the last row. Element with value -1 represents closed site. Please see the implementation here:
This should not happen, he asked you to use Weighted Quick Union in specific. Also how did you pass the test? i see you use iterations O(N)
, If you want a solution you can jump to my Algo repo but i do not think so because it has been a year now 😀️.
It seems that you didn't solve the "backwash" problem. I run the PercolationVisualizer with input20.txt, it's the result
But maybe the correct one should be