Skip to content

Coursera, ComputerScience, Princeton, AlgorithmsPart1, Week1, DynamicConnectivity using TDD with TravisCI and SonarCloud

Notifications You must be signed in to change notification settings

dominicfarr/UnionFindAlgorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Coursera - Computer Science from Princeton.

Algorithms, Part I, Week 1

Quality Gate Status

Quick Find

Cost model for Quick Find is too expensive.

Algorithm Initialise Union Find
Quick Find N N 1

It takes N2 array accesses to process a sequence of N union commands on N objects. That is, Quadratic time, which does not scale.

Quick Union

Cost model for Quick Union is too expensive.

Algorithm Initialise Union Find
Quick Find N N N

If Trees get tall, find is too expensive (could be N array accesses).

Weighted Quick Union

Avoid large trees determine. Balance trees by linking root of smaller tree to root of larger tree.

A new data structure is required to hold the size of each tree. Initial size is 1, as each tree is the root of itself and of size 1

Running time for the following operations:

Find: Takes time proportional to depth of p and q.

Union: takes constant time, given roots.

Proposition for Find complexity

Time to find is proportional to the depth of any node x, and is at most log2 N.

Depth Proposition

source: Algorithms, Part I by Princeton University

Question 1: When does depth increase?

Depth of node X increases by 1 when tree T1 containing x is merged into another tree T2.

   1      4       union(5,3)      1
  / \     |            =>        /|\
 2   3    5(x,D = 1)            2 3 4 // merge roots 4 and 1
                                     \
                                      5(X, D=2)

Before union 1 hop to find 5, from the root of 4. After union, 2 hops to find 5 from root of 1

The proposition states: The size of the tree containing x at least doubles since | T2 | ≥ | T1 |

Question 2: Why can trees double in size.

Because the maximum tree size being merged is equal to the size of the tree receiving...which means at it can double.

Question 3: Why can it only double log2 times

Total size is N. What is Log2 N? That is, how many times can we double to reach N

Example N = 16 We can only double 4 times before we get to 16. That is. Log216 = 4

1 + 1 = 2  
2 + 2 = 4
4 + 4 = 8
8 + 8 = 16

Therefore the complexity for find operation is proportional to depth, which is Log2 N

About

Coursera, ComputerScience, Princeton, AlgorithmsPart1, Week1, DynamicConnectivity using TDD with TravisCI and SonarCloud

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages