Esc101 Lecture 1 Aprl, 2008 Generatng Permutaton In ths class we wll look at a problem to wrte a program that takes as nput 1,2,...,N and prnts out all possble permutatons of the numbers 1,2,...,N. For e.g. gven 1 2 3 t wll prnt all ( 1 2 3 ) ( 1 3 2 ) ( 2 1 3 ) ( 2 3 1 ) ( 3 1 2 ) ( 3 2 1 ) How do we break ths problem up nto smaller problems? One way to do t s the followng. Fgure 1: The problem of generatng all permutatons of 1,2,3 has been reduced to the problems of generatng all permutatons of 1,2, 1,3, and 2,3.
In general, all permutatons of N elements can be expressed n terms of all permutatons of N - 1 elements. Havng a set of N elements, we take one of them and append t to all the permutatons of the remanng N - 1 elements. Repeatng t for every element of N gves us all permutatons of N elements. Unsurprsngly, t fts well nto the known formula of the amount of permutatons of N elements: N! = N(N-1)! Consder we are gven all the elements n an array of sze N. The ndexng of array arts from 0.. 1 2 3 4 5 6 7 8 Array For a gven nance consder we have reached at poston, (.e we have fxed the poston of elements at ndex up to -1 ),n the array, where 0 < N. For all, such that < < N, we wll pck element from ndex and swap t wth element at ndex then generate all the possble permutaton of elements from ndex +1 to N. Before swappng 1 2 3 4 5 6 7 8 After swappng the element at poston wth the element at poston, the array looks lke followng. = +1 1 2 3 5 4 6 7 8 = + 2 1 2 3 6 4 5 7 8 = + 3 1 2 3 7 4 5 6 8 = + 4 1 2 3 8 4 5 6 7 Fgure 2: For N = 8 =3
Once we have terated through all the values of, from +1 up to N. Note that we have altered the poston of elements. We need to put the elements back to ts place as they were before ths swappng. If we look at the above fgure 2 we would notce that the elements are rght shfted by 1 poston, artng from, whereas the la element s now at poston. So n order to get the elements back to ther place where they where before the swap, we need to left shft the data artng from +1 up to (N -1), the element n wll be moved to (N- 1) th poston. After Swappng 1 2 3 8 4 5 6 7 Applyng Left Rotaton 1 2 3 8 4 5 6 7 Resultant 1 2 3 4 5 6 7 8 Fgure 3: Left Rotaton We ll need to fgure out the base cases. We know that 1! = 1 thus when reaches the end of array we are done.e > N-1, then what left s to prnt the elements. The code that mplements the above dea s below. class Permutaton atc nt numpermutaton=0; atc vod Permute_tems(nt a[],nt ) nt,t;
/* Base case */ f( >a.length-1) Prnt_Permutaton(a); return; /* Generate the permutaon of +1 to N */ Permute_tems(a,+1); /* Generatng Permutaon after swappng of element at poston wth element at poston */ for(=+1;<a.length;++) /* Swappng element at poston wth element at poston */ t=a[]; a[]=a[]; a[]=t; /* Generatng the permutaton +1 to N*/ Permute_tems(a,+1); /* Applyng Left rotaton to put the elements back to ts correct place */ t=a[+1]; // Temporarly copyng the element at poston. /* Left rotatng */ for(=+1;<a.length;++) a[-1]=a[]; a[a.length-1]=t; //copyng the element at poston to (N-1) th poston /* Prntng the content of the array whch represents a permutaton */ publc atc vod Prnt_Permutaton(nt a[]) nt ; Syem.out.prnt( ( );
for(=0;<a.length;++) Syem.out.prnt( +a[] ); Syem.out.prnt( ) ); /* Incrementg the total count of permutaton */ numpermutaton +=1; publc atc vod man(strng args[]) nt a[]=new nt[args.length]; nt ; try for(=0;<args.length;++) a[]=integer.parseint(args[]); catch (Excepton e) Syem.err.prntln( Error n nput + e); Syem.ext(-1); Permute_tems(a,0); Syem.out.prntln( Total no of Permutaton +numpermutaton); In the above code we have used try -catch block because the method parint(strng s) of class Integer throws an nance of NumberFormatExcepton whch needs to be handle. For e.g. nt a=interger.parseint("07a"); It wll throw NumberFormatExcepton at runtme. Hence such thngs need to be handled. When the exceptons are caught, the code n the correspondng catch block s executed.
Here when we encounter such an excepton we need to op the executon of the program for that we use the method ext(nt rv) of class Syem. The parameter -1 s to ndcate that the program termnated due to exceptons. Tme Complexty The recurrence relaton of the permutaton s as follows T()= () * T(-1)+k 1 ; T(0)= k 2; Thus, the tme complexty would be O(n!). Data Structure A data ructure s a way of representng data n a computer so that t can be used effcently. For e.g. Stack, Queue, Lnked L. Lnked L Consder a problem of readng an array of elements from user. What we usually do s to ask how many elements user wants to enter and then create an array of that much sze. Then read the elements one by one and ore them n the created array. The dsadvantage of such desgn s that we need to specfy the no. of elements before specfyng the elements. The other way could be to assume some large sze of array and create and array of that sze. Now ju read the elements one by one and ore them n the array. There may be stuatons where the users may enter elements more than the array could handle. The other stuaton could be that the sze of the array declared s too large, and mo of the tme the user s utlzng only mnor fractonal part of the whole array. Hence, even ths scheme s also not effcent. The effcent way of handlng such scenaro s wth the use of Lnked L. Lnked l s a knd of data ructure used for dynamc sze of nput. In lnk l conss of chan of Nodes. Each node conss of two felds; one s called the data feld and other the reference feld. There may be more than one data and reference feld n a Node.
The reference feld contans the reference of the next element n the L. Hence the elements are lnked through the reference feld, hence the name Lnk L. Each Lnk L has specal reference called head to access the ntal element n the l and the reference of the la element s ground (null). head 5 7 Node 8 head 5 7 8 Fgure 4: Inserton of a Node n the Lnked L We wll carry on wth the detals of Lnked L n next lecture.