- 116 Views
- Uploaded on
- Presentation posted in: General

Computer Algorithms

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Computer Algorithms

Submitted by:

Rishi Jethwa

Suvarna Angal

- Red-Black Trees
- Basics
- Properties
- Rotations
- Insertions

- Union Find Algorithms
- Linked List Representation
- Union By Rank
- Path compression

- RB tress is a binary tree with one extra bit of storage per node; its color, which can be either RED or BLACK.
- Its data structure for binary search tree with only difference that the trees are approximately balanced.

- A binary tree is a red-black tree if it satisfies following rules for red-black tree.
- Every node is either red or black.
- The root is always black.
- Leaf nodes are black.
- If a node is red, then both its children are black.
- The number of black nodes on every path are same.

- Properties of red-black trees
- Suppose number of black nodes are 10, then the minimum height can be 10 and maximum height of the tree can be at most 19. Hence the maximum can be at most 1 less than twice of its minimum height.
- Maximum path length is O(log n).
- Lookup for searches are good, O(log n).
- Insertion and deletion are not an overhead exactly, complexity is O(log n).

Rotation of red-black trees.

A structural change to the red-black trees.

Insertion and deletion modify the tree, the result may violate the properties of red-black trees. To restore this properties rotations are done.

We can have either of left rotation or right rotation.

Left rotation

c

b

a

c

b

e

Right rotation

a

d

d

e

The above diagram depicts left and right rotations

Here in right diagram a < b < d < c < e

Insertion in red black trees.

11

2

14

Original tree

15

1

7

5

8

Number 4 added

4

- The idea to insertion is that we traverse the tree to see where it fits, assume it fits at end , so the idea is to traverse up again.
- Coloring rule while insertion.
- Look at the father node, if it is red and the uncle node is red too and if the grandfather node is black , then make father and uncle as black and grandfather as red.

Diagram depicting rule for insertion mentioned in the previous slide.

Insertion example

11

2

14

15

1

7

5

8

4

Violation of rule( after 4 added to the tree)

Insertion example

11

2

14

15

1

7

5

8

4

Case 1

Insertion example

11

7

14

15

2

8

1

5

4

Case 2

Insertion example

7

7

11

2

14

1

5

8

15

4

Case 3

Union: Merge 2 sets and create a new set

3

4

1

2

Initially each number is a set by itself.

From n singleton sets gradually merge to form a set.

After n-1 union operations we get a single set of n numbers.

Union operation is used for merging sets in Kruskal’s algorithm

- Every set has a name
- Thus Find(number) returns name of the set.
- Perfect application in Kruskal’s algorithm when there is a new edge added. Discard the already accounted for edge.

- Represent each set using a linked list
- First object in each linked list serves as the set’s name
- Each list maintains pointers head, to the representative, and tail, to the last object in the list.

Extra pointers pointing to the head

2

3

6

When uniting these 2 sets, pointers for nodes 5 and 7 will have to be made pointing to 2.

5

7

- By using this representation, Find will take constant time O(1).
- But Union takes linear time as after union all the pointers have to be redirected to the head.

2

3

6

Add the pointer pointing from new set’s head to the old old one.

5

7

1

Union of 1 and 4

4

2

3

6

While combining, we can have a pointer from 1 to 2 or from 2 to1.

But we choose the one from 1 to 2.

This gives us a balanced structure. The highest hop remains 2.

5

7

1

4

- The root of the tree with fewer nodes is made to point to the root of the tree with more nodes.
- For each node, a rank is maintained that is an upper bound on the height of the node.
- In union by rank, the root with smaller rank is made to point to the root with larger rank during a UNION operation.

When we always take singleton sets and keep merging the sets we get a star structure.

Time taken: n finds and n-1 unions.

This is best case.

- Merge sets of equal path lengths.

1

Here, the path length becomes 3.

For n-1 unions and n finds:

Union: O(n)

Finds: path lengths can get bigger so, O(n log n).

Total sum: O(n log n).

2

3

4

5

6

7

8

- For union by rank,

Best Case

1

Worst Case

Log n

- 1st walk: Find the name of the set . Take a walk until we reach the root.
- 2nd walk: Retrace the path and join all the elements along the path to the root using another pointer.
- This enables future finds to take shorter paths.

After Find: Each node points directly to the root

Before Find: Each node has pointer to its parent

root

root

3

1

2

3

2

1

- Time for n-1 unions and n finds: O(nlog*n)
- Log* n is a slow growing function.

F(n)=n

Log n

Log*n

4

(n)

- α(n) – quick growing function
- For k>= 0, j>=1
- Ak(j) = j + 1 if k = 0
= Aj+1k-1(j) if k>=1

- This is a recursive function.

- A3(2) = A2(A2(A2(2)))
- When k=1, A1(j) = 2j+1
- For k =2, A2(j) = A1(A1(…. A1(j)))
- A2(j) = 2j+1(j+1) -1
- Using the above analysis:
- A3(1) = 2047
- A4(1) = 22047+1(2047+1) - 1

- From the analysis we can see that Ak is a very fast growing function.
- α(n) is inverse of Ak
- Inverse is very slow growing.
- Run time: O(n α(n)).