Computer Algorithms - PowerPoint PPT Presentation

About This Presentation
Title:

Computer Algorithms

Description:

Red-Black Trees ... From the analysis we can see that Ak is a very fast growing function. a(n) is inverse of Ak. Inverse is very slow growing. Run time: O(n a(n) ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 33
Provided by: rbj7
Learn more at: https://ranger.uta.edu
Category:

less

Transcript and Presenter's Notes

Title: Computer Algorithms


1
Computer Algorithms
  • Submitted by
  • Rishi Jethwa
  • Suvarna Angal

2
Contents
  • Red-Black Trees
  • Basics
  • Properties
  • Rotations
  • Insertions
  • Union Find Algorithms
  • Linked List Representation
  • Union By Rank
  • Path compression

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

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

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

6
Red-Black Trees
  • 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.

7
Red-Black Trees
Left rotation
c
b
c
a
e
b
Right rotation
a
d
d
e
The above diagram depicts left and right rotations
Here in right diagram a lt b lt d lt c lt e
8
Red-Black Trees
Insertion in red black trees.
11
2
14
Original tree
15
1
7
5
8
Number 4 added
4
9
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.

10
Red-Black Trees
Diagram depicting rule for insertion mentioned in
the previous slide.
11
Red-Black Trees
Insertion example
11
2
14
15
1
7
5
8
4
Violation of rule( after 4 added to the tree)
12
Red-Black Trees
Insertion example
11
2
14
15
1
7
5
8
4
Case 1
13
Red-Black Trees
Insertion example
11
7
14
15
2
8
1
5
4
Case 2
14
Red-Black Trees
Insertion example
7
7
11
2
14
1
5
8
15
4
Case 3
15
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 Kruskals algorithm
16
Find operation
  • Every set has a name
  • Thus Find(number) returns name of the set.
  • Perfect application in Kruskals algorithm when
    there is a new edge added. Discard the already
    accounted for edge.

17
Linked List Representation
  • Represent each set using a linked list
  • First object in each linked list serves as the
    sets name
  • Each list maintains pointers head, to the
    representative, and tail, to the last object in
    the list.

18
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
19
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.

20
Develop new data structure
2
3
6
Add the pointer pointing from new sets head to
the old old one.
5
7
1
Union of 1 and 4
4
21
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
22
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.

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

24
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
25
Path Compression
  • For union by rank,

Best Case
1
Worst Case
Log n
26
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.

27
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
28
Amortized Analysis
  • Time for n-1 unions and n finds O(nlogn)
  • Log n is a slow growing function.

n Log n Logn
22 2 1
222 4 2
2222 222 3
22222 2222 4
29
Comparisons of functions
F(n)n
Log n
Logn
4
?(n)
30
Inverse Ackermanns function
  • a(n) quick growing function
  • For kgt 0, jgt1
  • Ak(j) j 1 if k 0
  • Aj1k-1(j) if kgt1
  • This is a recursive function.

31
Calculations
  • A3(2) A2(A2(A2(2)))
  • When k1, A1(j) 2j1
  • For k 2, A2(j) A1(A1(. A1(j)))
  • A2(j) 2j1(j1) -1
  • Using the above analysis
  • A3(1) 2047
  • A4(1) 220471(20471) - 1

32
Nature of function
  • From the analysis we can see that Ak is a very
    fast growing function.
  • a(n) is inverse of Ak
  • Inverse is very slow growing.
  • Run time O(n a(n)).
Write a Comment
User Comments (0)
About PowerShow.com