Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Representation Part 3

Similar presentations


Presentation on theme: "Data Representation Part 3"— Presentation transcript:

1 Data Representation Part 3
Chapter 3 Data Representation Part 3

2 Applications Bin Sort Former sorting algorithms take O(n2) time
Bin sort takes O(n) time In bin sort the nodes are placed into bins, each bin containing nodes with the same key. Then bins are combined to create the sorted list

3 An example - Student records

4 Algorithm Move down the input chain moving nodes to the appropriate bin Collect and concatenate chains from the bins into a single sorted chain

5 Node class operator overloading

6 Type converter

7 Combine operator overloading and type converter

8 Bin Sort Each deletion and insertion take Θ(1) time
First for loop Θ(n) Second for loop Θ(n+range) Overall Θ(n+range)

9 Bin Sort as a Member of the Class Chain

10 Bin Sort as a Member of the Class Chain

11 Evaluation The 1st and 3rd for loops take Θ(range) time
The 2nd takes Θ(n) time Overall: Θ(n+range)

12 Generalization Make it available for sorting on different key fields in different occasions Add a function that returns the key value to BinSort as a parameter: void Chain<T>::BinSort(int range, int(*value)(T& x))

13 Sorting on different fields

14 Sorting on different fields (continue)

15 Radix Sort An extension of bin sort for sorting integers in the range [0..nc-1] If using BinSort with range=nc, the complexity will be Θ(n+range)= Θ(nc) In radix sort, the numbers are decomposed into digits using some radix r and then sort by digits, e.g., 928 has the radix 10 decomposition 9, 2, and 8 Time complexity of radix sort is Θ(n)

16 Example 1

17 Example 2 Problem: sort 1000 integers in the range [0..106-1]
Steps used in different solutions

18 Evaluation Decomposition:
Radix 10 decomposition (from least significant to most significant): x%10, (x%100)/10, (x%1000)/100, … Radix r decomposition: x%r, (x%r2)/r, (x%r3)/r2, … Use radix r=n to decompose n integers in the range [0..nc-1], then d = c, The time needed is Θ(cn) = Θ(n)

19 Equivalence Classes Equivalence relation:
Given a set U={1,2,…,n} and a set R={(i1,j1),(i2,j2),…,(ir,jr)}, R is equivalence relation iff (a,a) Є R for all a (reflexivity) (a,b) Є R iff (b,a) Є R (symmetry) (a,b) Є R and (b,c) Є R imply that (a,c) Є R (transitivity)

20 Equivalence class a and b are equivalent iff (a,b) Є R
Equivalence class: maximal set of equivalent elements e.g., n=14, R={(1,11),(7,11),(2,12),(12,8),(11,12),(3,13),(4,13),(13,14),(14,9),(5,14),(6,10)}, equivalence classes: {1,2,7,8,11,12}, {3,4,5,9,13,14}, {6,10}

21 Union-Find problem Offline equivalence class problem: given n and R, determine equivalence classes Online equivalence class problem: begin with n elements, each in a separate class, process a sequence of Combine(a,b) and Find(e) -- Whenever given a new relation (a,b), determine whether a and b are already in the same class, if not, perform a Union on the two classes that contain a and b Also called as union-find problem

22 Scheduling with Deadlines
The problem: single machine that is to perform n tasks, each requires one unit of time, e.g., Task Release time Deadline Algorithm Sort the tasks into nonincreasing order of release time For each task determine the free slot nearest to, but not after, its deadline. If the free slot is before the task’s release time, fail. Otherwise, assign the task to this slot

23 Implementation For any slot a (i<=a<=d), near(a) is the largest i such that i<=a and slot i is free. If no such i exists, define near(a)=near(0)=0. Two slots a and b are in the same equivalence class iff near(a)=near(b) At the beginning, near(a)=a When slot a is assigned a task, near changes for all slots b with near(b)=a. For these slots the new value of near is near(a-1)

24 Implementation Therefore, when slot a is assigned a task, we need the union on the equiv. Classes of a and (a-1)

25 From Wires to Nets An electronic circuit consists of components, pins, and wires Two pins a and b are electrically equivalent iff they are connected directly by a wire or indirectly by wires A net is a maximal set of electrically equivalent pins

26 An example Pins: 1, 2, …, 14 Wires: {(1,11), (7,11), (2,12), (12,8), (11,12), (3,13), (4,13), (13,14), (14,9), (5,14), (6,10)} Nets: {1,2,7,8,11,12}, {3,4,5,9,13,14} and {6,10}

27 Net finding problem Offline net finding problem - modeled by the offline equivalence problem Online net finding problem - online equivalence problem add a wire to connect pins a and b - Combine(a,b) find the net that contains a - Find(a)

28 Array based solution Initilization - Θ(n)

29 Union and Find Union - Θ(n) Find - Θ(1)
u unions and f finds - Θ(n+u*n+f) = Θ(u*n+f)

30 Second solution Use a chain (simulated pointers) to represent an equivalence class Introduce class EquivNode with private data members E, size, and link E - pointer to the 1st node in the chain size - the number of nodes on the chain, defined only in the 1st node on the chain link - next node on the chain Initialize, Union, and Find are friends of EquivNode node[1:n] represent the n elements

31 Initialize - Θ(n) Union - O(n)

32 Find - Θ(1)

33 Complexity of u unions and f finds
Lemma 3.1 If we start with n classes that have one element each and perform u union, then No class has more than u+1 elements At least n-2u singleton classes remain u < n Complexity of initialization, u unions and f finds is O(n+ulogu+f)

34 Convex Hull Polygon - closed planar figure with three or more straight lines A polygon is convex iff all line segments that join two points on or in the polygon include no point that is outside the polygon

35 Convex Hull Convex hull of a set S of points is the smallest convex polygon that contains all these points extreme points - corners of the convex hull

36 Point ordering

37 Algorithm Step 1 - Θ(n) Step 2 - O(n2) Step 3 - O(n)

38 The end of Chapter 3 Part 3


Download ppt "Data Representation Part 3"

Similar presentations


Ads by Google