Computer algorithms
This presentation is the property of its rightful owner.
Sponsored Links
1 / 32

Computer Algorithms PowerPoint PPT Presentation


  • 107 Views
  • Uploaded on
  • Presentation posted in: General

Computer Algorithms. Submitted by: Rishi Jethwa Suvarna Angal. Contents. Red-Black Trees Basics Properties Rotations Insertions Union Find Algorithms Linked List Representation Union By Rank Path compression. Red-Black Trees.

Download Presentation

Computer Algorithms

An Image/Link below is provided (as is) to download presentation

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 - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Computer algorithms

Computer Algorithms

Submitted by:

Rishi Jethwa

Suvarna Angal


Contents

Contents

  • Red-Black Trees

    • Basics

    • Properties

    • Rotations

    • Insertions

  • Union Find Algorithms

    • Linked List Representation

    • Union By Rank

    • Path compression


Red black trees

Red-Black Trees

  • 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.


Red black trees1

Red-Black Trees

  • 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.


Red black trees2

Red-Black Trees

  • 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).


Red black trees3

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.

Red-Black Trees


Red black trees4

Red-Black Trees

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


Red black trees5

Red-Black Trees

Insertion in red black trees.

11

2

14

Original tree

15

1

7

5

8

Number 4 added

4


Red black trees6

Red-Black Trees

  • 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.


Red black trees7

Red-Black Trees

Diagram depicting rule for insertion mentioned in the previous slide.


Red black trees8

Red-Black Trees

Insertion example

11

2

14

15

1

7

5

8

4

Violation of rule( after 4 added to the tree)


Red black trees9

Red-Black Trees

Insertion example

11

2

14

15

1

7

5

8

4

Case 1


Red black trees10

Red-Black Trees

Insertion example

11

7

14

15

2

8

1

5

4

Case 2


Red black trees11

Red-Black Trees

Insertion example

7

7

11

2

14

1

5

8

15

4

Case 3


Union operation

Union: Merge 2 sets and create a new set

3

4

Union Operation

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


Find operation

Find operation

  • 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.


Linked list representation

Linked List Representation

  • 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.


Linked list representation1

Linked List Representation

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


Drawbacks

Drawbacks :

  • 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.


Develop new data structure

Develop new data structure

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


Combination of the 2 sets

Combination of the 2 sets

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


Union by rank algorithm

Union by Rank Algorithm

  • 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.


Longest path length unions

Longest path length unions:

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.


Worst case

Worst 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


Path compression

Path Compression

  • For union by rank,

Best Case

1

Worst Case

Log n


Algorithm for path compression

Algorithm for Path Compression

  • 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.


Path compression1

Path compression

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


Amortized analysis

Amortized Analysis

  • Time for n-1 unions and n finds: O(nlog*n)

  • Log* n is a slow growing function.


Comparisons of functions

Comparisons of functions

F(n)=n

Log n

Log*n

4

(n)


Inverse ackermann s function

Inverse Ackermann’s function

  • α(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.


Calculations

Calculations

  • 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


Nature of function

Nature of function

  • 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)).


  • Login