12.07.2015 Views

Heap sort and complexity, Disjoint Union & Find, Complexity ...

Heap sort and complexity, Disjoint Union & Find, Complexity ...

Heap sort and complexity, Disjoint Union & Find, Complexity ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 2: Elementary Data Structuresprocedure HEAPSORT(A:arraytype; n:integer);var i:integer; item: itemtype;beginHEAPIFY(A,n)for i := n downto 2 dobeginitem := A[i];A[i] := A[1];A[1] := item;ADJUST(A,1,i-1);end;end.


Chapter 2: Elementary Data Structures<strong>Heap</strong>-<strong>sort</strong> algorithm• Build a heap from the array• Remove elements from the heap one by one<strong>and</strong> insert them back into the array


Chapter 2: Elementary Data Structures0123456781008090702050106030012345678


Chapter 2: Elementary Data Structures0123456781008090702050106030012345678


Chapter 2: Elementary Data Structures0012345678809070205010603012345678100


Chapter 2: Elementary Data Structures0123456789080507020301060012345678100


Chapter 2: Elementary Data Structures0123456789080507020301060012345678100


Chapter 2: Elementary Data Structures0012345678805070203010601234567890100


Chapter 2: Elementary Data Structures0123456788070506020301001234567890100


Chapter 2: Elementary Data Structures0123456788070506020301001234567890100


Chapter 2: Elementary Data Structures0012345678705060203010123456788090100


Chapter 2: Elementary Data Structures0123456787060501020300123456788090100


Chapter 2: Elementary Data Structures0123456787060501020300123456788090100


Chapter 2: Elementary Data Structures0012345678605010203012345678708090100


Chapter 2: Elementary Data Structures0123456786030501020012345678708090100


Chapter 2: Elementary Data Structures0123456786030501020012345678708090100


Chapter 2: Elementary Data Structures0012345678305010201234567860708090100


Chapter 2: Elementary Data Structures0123456785030201001234567860708090100


Chapter 2: Elementary Data Structures0123456785030201001234567860708090100


Chapter 2: Elementary Data Structures0012345678302010123456785060708090100


Chapter 2: Elementary Data Structures0123456783010200123456785060708090100


Chapter 2: Elementary Data Structures0123456783010200123456785060708090100


Chapter 2: Elementary Data Structures0123456781020012 303504605706807908100


Chapter 2: Elementary Data Structures0123456782010012 303504605706807908100


Chapter 2: Elementary Data Structures0123456782010012 303504605706807908100


Chapter 2: Elementary Data Structures0012345678101234567820305060708090100


Chapter 2: Elementary Data Structures0123456781001234567820305060708090100


Chapter 2: Elementary Data Structures0123456781001234567820305060708090100


Chapter 2: Elementary Data Structures0123456780123456781020305060708090100Final Sorted List


Chapter 2: Elementary Data StructuresThough the call of HEAPIFY requires only O(n) operations,ADJUST possible requires O(log n) operations for eachinvocation.Thus the worse case time is O(n log n).


Chapter 2: Elementary Data StructuresSets <strong>and</strong> disjoint unionsProblem: Suppose we have a finite universe of n elements U,out of which sets will be created.Representation: SET(1:n) such that SET(i) = 1 if i th elementof U is present, otherwise 0.This array is called the characteristic vector for the set.


Chapter 2: Elementary Data StructuresAdvantages: It can be easily determined whether or notparticular element i is present. <strong>Union</strong> <strong>and</strong> Intersection can bedone using “logical <strong>and</strong>” <strong>and</strong> “logical or”.Disadvantages: This representation is inefficient when thevalue of n is large (say larger than the number of bits in theword) <strong>and</strong> the size of the set is smaller compare to U. The timewill be proportional to n rather than the number of elements inthe set.


Chapter 2: Elementary Data StructuresAlternate representation: Represent each set by its element.If there is any ordering relationship between them then theoperation such as union <strong>and</strong> intersection can be carried out intime proportional to the length of the sets.We represent sets as trees.We assume that they are pair wise disjoint <strong>and</strong> perform theseoperations.


Chapter 2: Elementary Data Structures<strong>Disjoint</strong> <strong>Union</strong>:S Ui<strong>Find</strong>(i): find a set containing element i.S = {all elements x such that x is in S i orjS j }.Challenge is to device data representation for disjoint sets sothat these operation can be performed efficiently.One possible representation of S 1 , S 2 , S 3 can be given by17 8 925 1034 6


Chapter 2: Elementary Data Structures<strong>Union</strong>: Make one of the trees a subtree of the other127 8 9 251105 107 8 9In order to find <strong>Union</strong> of two sets all one has to do is, set theparent field of the root to the other root. We identify the setsby the index of roots.


Chapter 2: Elementary Data StructuresThe operation F(i) will find the root of the tree containingelement i. U(i,j) require two trees with roots i, j to be joined.procedure U(i,j);var i,j : integer;beginparent(i) := j;end.


procedure F(i);var i,j : integer;beginj:=i;While (parent(j) > 0) doj := parent(j)return(j)end.Chapter 2: Elementary Data Structuresperformance of U <strong>and</strong> F is not good, for ex. S i = {i}, 1 ≤ i ≤ n,then there is forest of n nodes with parent(i) = 0, 1 ≤ i ≤ n.


Chapter 2: Elementary Data StructuresIf we perform these sequence of <strong>Union</strong> <strong>and</strong> <strong>Find</strong>-U(1,2), F(1), U(2,3), F(1), U(3,4), F(1),........., U(n-1,n).results in the degenerate tree.nn-11


Chapter 2: Elementary Data StructuresSince the time taken for union is constant, n-1 calls to U canbe processed in O(n) time. Time required to process F at leveli is O(i), n-2 calls of find takes O(n 2 ) time.Weighting rule: “if the number of nodes in tree i is less thanthe number of nodes in tree j, then make j the parent of i,else make i, the parent of j”.using the rule on the data set given before, <strong>and</strong> using the samesequence we have-


Chapter 2: Elementary Data Structures1 2 n 23 n 24nInitially1UNION(1,2)1 3UNION(FIND(1),3)25n2UNION(FIND(n-1),n)UNION(FIND(3),4)1 3 41 3 4 nIn order to implement the weighting rule, we need to knowhow many nodes are there in a tree. We maintain a COUNTfield in the root of every tree.


Chapter 2: Elementary Data StructuresIf i is the root of the tree then COUNT(i) = number of nodesin that tree. COUNT can be maintained in the PARENT fieldas a negative number.


Chapter 2: Elementary Data Structuresprocedure UNION(i,j);//PARENT(i) = -COUNT(i), PARENT(j) = -COUNT(j), //vari,j,x: integer;beginx := PARENT(i) + PARENT(j);if (PARENT(i) > PARENT(j)) thenPARENT(i) :=j;PARENT(j) :=x;elsePARENT(j) :=i;PARENT(i) :=x;end.


Chapter 2: Elementary Data StructuresTime required by UNION is still bounded by constant. Themaximum time required by FIND is given by the lemma-Lemma: Let T be a tree with n nodes created as a result ofalgorithm UNION. No node in the tree has a level greater thanlog n +1⎣ ⎦ .


Chapter 2: Elementary Data StructuresProof: Theorem is true for n = 1, assume that it is true for alltrees with i nodes, i ≤ n −1. We show that it is true for i = n.Consider the last operation performed, UNION(k,j). Let m benumber of nodes in tree j <strong>and</strong> n-m are number of nodes in k.We may assume 1 ≤ m ≤ n / 2. The maximum level of any nodein T iseither is same as that in k oris one more than that in j


Chapter 2: Elementary Data StructuresIf first is the case then maximum levelT≤≤⎣log( n − m)⎦ + 1⎣log n⎦ + 1later is the case than it is≤⎣log m⎦ + 1+1≤≤⎢ nlog⎥+ 1⎢⎣ 2⎥ ⎦⎣log n⎦+ 1


Chapter 2: Elementary Data StructuresAction of UNION(1,2), UNION(3,4), UNION(5,6),UNION(7,8), UNION(1,3), UNION(5,7), UNION(1,5)1 2 3 4 5 6 7 8 INITIALLY12345678UNION(1,2)UNION(3,4)UNION(5,6)UNION(7,8)


Chapter 2: Elementary Data Structures152 36 7UNION(1,3)UNION(5,7)481235UNION(1,5)4678


Chapter 2: Elementary Data StructuresAs a result of the lemma, the maximum time to process a findis O(log n) if there are n elements in the tree.Sequence of nunions <strong>and</strong> m finds is bounded by O(m log n). Furtherimprovement is possible if we make use of collapsing rule.Collapsing rule: If j is the node on the path from i to its rootthen set PARENT(j) := ROOT(i).


procedure FIND(i);var j:integer;beginj:= i;while( PARENT(j) > 0) doj := PARENT(j);k := i;while (k j) dot := PARENT(k);PARENT(k) := j;k := t;return(j);end.Chapter 2: Elementary Data Structures


Chapter 2: Elementary Data Structuresprocessing FIND(8), FIND(8), FIND(8), FIND(8)FIND(8), FIND(8), FIND(8), FIND(8)using old F(8) we need 24 moves <strong>and</strong> with FIND we need 13moves.


Maze Application• Build a r<strong>and</strong>om maze by erasing edges.Ref: Ben Lerner Slides51


• Pick Start <strong>and</strong> EndMaze ApplicationStartEndRef: Ben Lerner Slides52


Maze Application• Repeatedly pick r<strong>and</strong>om edges to delete.StartEndRef: Ben Lerner Slides53


Maze Generator• None of the boundary is deleted• R<strong>and</strong>omly remove walls until the Start <strong>and</strong> Endcells are in the same set.• Removing a wall is the same as doing a unionoperation.• Do not remove a r<strong>and</strong>omly chosen wall if thecells it separates are already in the same set.• There are no cycles – no cell can reach itselfby a path unless it retraces some part of thepath.Ref: Ben Lerner Slides54


A CycleStartEndRef: Ben Lerner Slides55


A Good SolutionStartEndRef: Ben Lerner Slides56


A Hidden TreeStartEndRef: Ben Lerner Slides57


Number the CellsWe have disjoint sets S ={ {1}, {2}, {3}, {4},… {36} } each cell is unto itself.We have all possible edges E ={ (1,2), (1,7), (2,8), (2,3), … } 60 edges total.Start1 2 3 4 5 67 8 9 10 11 1213 14 15 16 17 1819 20 21 22 23 2425 26 27 28 29 3031 32 33 34 35 36EndRef: Ben Lerner Slides58


Basic Algorithm• S = set of sets of connected cells• E = set of edges• Maze = set of maze edges initially emptyWhile there is more than one set in Spick a r<strong>and</strong>om edge (x,y) <strong>and</strong> remove from Eu := <strong>Find</strong>(x);v := <strong>Find</strong>(y);if u ≠ v then<strong>Union</strong>(u,v)elseadd (x,y) to MazeAll remaining members of E together with Maze form the mazeRef: Ben Lerner Slides59


Example StepPick (8,14)Start 1 2 3 4 5 67 8 9 10 11 1213 14 15 16 17 1819 20 21 22 23 2425 26 27 28 29 3031 32 33 34 35 36Ref: Ben Lerner SlidesEndS{1,2,7,8,9,13,19}{3}{4}{5}{6}{10}{11,17}{12}{14,20,26,27}{15,16,21}..{22,23,24,29,30,3233,34,35,36}60


ExampleS{1,2,7,8,9,13,19}{3}{4}{5}{6}{10}{11,17}{12}{14,20,26,27}{15,16,21}..{22,23,24,29,39,3233,34,35,36}Ref: Ben Lerner Slides<strong>Find</strong>(8) = 7<strong>Find</strong>(14) = 20<strong>Union</strong>(7,20)S{1,2,7,8,9,13,19,14,20 26,27}{3}{4}{5}{6}{10}{11,17}{12}{15,16,21}..{22,23,24,29,39,3233,34,35,36}61


ExamplePick (19,20)Start 1 2 3 4 5 67 8 9 10 11 1213 14 15 16 17 1819 20 21 22 23 2425 26 27 28 29 3031 32 33 34 35 36Ref: Ben Lerner SlidesEndS{1,2,7,8,9,13,1914,20,26,27}{3}{4}{5}{6}{10}{11,17}{12}{15,16,21}..{22,23,24,29,39,3233,34,35,36}62


Example at the EndStart1 2 3 4 5 67 8 9 10 11 1213 14 15 16 17 1819 20 21 22 23 2425 26 27 28 29 3031 32 33 34 35 36EndS{1,2,3,4,5,6,7,… 36}EMazeRef: Ben Lerner Slides63


Chapter 2: Elementary Data StructuresEnd of Chapter 2

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!