Week1-3Quick Union
2015-09-09 22:32
309 查看
Data Structure
Integer array id[] of size NRoot of i isid[id[id[…id[i]…]]](keep going until it doesn’t change)
Operations
Find( p, q ): check if p and q have the same rootUnion( p, q ): to merge components containing p and q, set id of p’s root to the id of q’s root
Implementation
public class QuickUnionUF { private int[] id; public QuickUnionUF( int N ) { id = new int ; for( int i = 0; i< N; i++ ) { id[i] = i; } } private int root( int i ) { // chase parent pointer until reach root while( i != id[i] ) { i = id[i]; } return i; } public boolean connected( int p, int q ) { return root( p ) == root( q ); } public void union( int p, int q ) { int i = root( p ), j = root( q ); id[i] = j; } }
Cost Model
Algorightm | Initialize | Union | Find |
---|---|---|---|
quick-find | O(N) | O(N) | O(1) |
quick-union | O(N) | O(N) | <
Defects
相关文章推荐
- iOS开发:标签视图控制器(UITabBarController)
- git 出现 The requested URL returned error: 403
- 使UIBUTTON失效
- 关于Android开发中布局文件中各种值的设置存放的个人观点(按照布局文件来进行存放value值)
- 按钮精灵用代码动态控制时需要添加UIButton.normalsprite
- UI设计编程:基础视图、程序启动流程
- EFI,UEFI和操作系统
- AdaultBird--我的黑马程序员之路!Chapter7---File类,IO流里的其他工具类,GUI
- COCI2014silueta
- java parseInt和valueof的区别
- hdu 5014__Number Sequence
- ACE_Message_Queue
- UIPageControl
- UILabel
- UVA 11324 The Largest Clique (拆点+KM)
- iOS—实现UI imageview的底层
- Expression Tree Build
- 09-09 Service(续)、UI主线程、AsyncTask(UI主线程)
- Android Service——在子线程中更新UI
- iOS小结(五) 结合 Instrument 分析并解决memory issues