Esc101 Lecture 1 st April, 2008 Generating Permutation

Similar documents
Today s Outline. Sorting: The Big Picture. Why Sort? Selection Sort: Idea. Insertion Sort: Idea. Sorting Chapter 7 in Weiss.

Sequential search. Building Java Programs Chapter 13. Sequential search. Sequential search

CSE 326: Data Structures Quicksort Comparison Sorting Bound

CSE 326: Data Structures Quicksort Comparison Sorting Bound

Sorting: The Big Picture. The steps of QuickSort. QuickSort Example. QuickSort Example. QuickSort Example. Recursive Quicksort

Sorting Review. Sorting. Comparison Sorting. CSE 680 Prof. Roger Crawfis. Assumptions

For instance, ; the five basic number-sets are increasingly more n A B & B A A = B (1)

The Greedy Method. Outline and Reading. Change Money Problem. Greedy Algorithms. Applications of the Greedy Strategy. The Greedy Method Technique

Notes on Organizing Java Code: Packages, Visibility, and Scope

High level vs Low Level. What is a Computer Program? What does gcc do for you? Program = Instructions + Data. Basic Computer Organization

Priority queues and heaps Professors Clark F. Olson and Carol Zander

CS1100 Introduction to Programming

Sorting. Sorting. Why Sort? Consistent Ordering

CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar

Insertion Sort. Divide and Conquer Sorting. Divide and Conquer. Mergesort. Mergesort Example. Auxiliary Array

Brave New World Pseudocode Reference

Programming in Fortran 90 : 2017/2018

Introduction to Programming. Lecture 13: Container data structures. Container data structures. Topics for this lecture. A basic issue with containers

The Codesign Challenge

An Optimal Algorithm for Prufer Codes *

Complex Numbers. Now we also saw that if a and b were both positive then ab = a b. For a second let s forget that restriction and do the following.

Pass by Reference vs. Pass by Value

News. Recap: While Loop Example. Reading. Recap: Do Loop Example. Recap: For Loop Example

Sorting and Algorithm Analysis

Module Management Tool in Software Development Organizations

R s s f. m y s. SPH3UW Unit 7.3 Spherical Concave Mirrors Page 1 of 12. Notes

CSCI 104 Sorting Algorithms. Mark Redekopp David Kempe

Course Introduction. Algorithm 8/31/2017. COSC 320 Advanced Data Structures and Algorithms. COSC 320 Advanced Data Structures and Algorithms

Analysis of Continuous Beams in General

Private Information Retrieval (PIR)

Harvard University CS 101 Fall 2005, Shimon Schocken. Assembler. Elements of Computing Systems 1 Assembler (Ch. 6)

Quicksort. Part 1: Understanding Quicksort

A Binarization Algorithm specialized on Document Images and Photos

Design and Analysis of Algorithms

Problem Definitions and Evaluation Criteria for Computational Expensive Optimization

Compiler Design. Spring Register Allocation. Sample Exercises and Solutions. Prof. Pedro C. Diniz

ELEC 377 Operating Systems. Week 6 Class 3

Mathematics 256 a course in differential equations for engineering students

Computer models of motion: Iterative calculations

CMPS 10 Introduction to Computer Science Lecture Notes

Assembler. Shimon Schocken. Spring Elements of Computing Systems 1 Assembler (Ch. 6) Compiler. abstract interface.

Assembler. Building a Modern Computer From First Principles.

CS240: Programming in C. Lecture 12: Polymorphic Sorting

Nachos Project 3. Speaker: Sheng-Wei Cheng 2010/12/16

Lecture 5: Multilayer Perceptrons

CE 221 Data Structures and Algorithms

ON SOME ENTERTAINING APPLICATIONS OF THE CONCEPT OF SET IN COMPUTER SCIENCE COURSE

Imperialist Competitive Algorithm with Variable Parameters to Determine the Global Minimum of Functions with Several Arguments

3D vector computer graphics

On Some Entertaining Applications of the Concept of Set in Computer Science Course

Intro. Iterators. 1. Access

Memory Modeling in ESL-RTL Equivalence Checking

Report on On-line Graph Coloring

Pose, Posture, Formation and Contortion in Kinematic Systems

Assignment # 2. Farrukh Jabeen Algorithms 510 Assignment #2 Due Date: June 15, 2009.

Sorting. Sorted Original. index. index

Loop Transformations, Dependences, and Parallelization

Today Using Fourier-Motzkin elimination for code generation Using Fourier-Motzkin elimination for determining schedule constraints

Kent State University CS 4/ Design and Analysis of Algorithms. Dept. of Math & Computer Science LECT-16. Dynamic Programming

PHYSICS-ENHANCED L-SYSTEMS

Optimization Methods: Integer Programming Integer Linear Programming 1. Module 7 Lecture Notes 1. Integer Linear Programming

9. BASIC programming: Control and Repetition

Outline. CIS 110: Introduction to Computer Programming. Review: Interactive Sum. More Cumulative Algorithms. Interactive Sum Trace (2)

Array transposition in CUDA shared memory

More on Sorting: Quick Sort and Heap Sort

Ramsey numbers of cubes versus cliques

Lecture - Data Encryption Standard 4

Subspace clustering. Clustering. Fundamental to all clustering techniques is the choice of distance measure between data points;

GSLM Operations Research II Fall 13/14

Circuit Analysis I (ENGR 2405) Chapter 3 Method of Analysis Nodal(KCL) and Mesh(KVL)

Feature-Based Matrix Factorization

Load Balancing for Hex-Cell Interconnection Network

Hierarchical clustering for gene expression data analysis

A Fast Content-Based Multimedia Retrieval Technique Using Compressed Data

Loop Transformations for Parallelism & Locality. Review. Scalar Expansion. Scalar Expansion: Motivation

Verification by testing

6.854 Advanced Algorithms Petar Maymounkov Problem Set 11 (November 23, 2005) With: Benjamin Rossman, Oren Weimann, and Pouya Kheradpour

Virtual Memory. Background. No. 10. Virtual Memory: concept. Logical Memory Space (review) Demand Paging(1) Virtual Memory

THE low-density parity-check (LDPC) code is getting

Exercises (Part 4) Introduction to R UCLA/CCPR. John Fox, February 2005

An Approach in Coloring Semi-Regular Tilings on the Hyperbolic Plane

Problem Set 3 Solutions

Run-Time Operator State Spilling for Memory Intensive Long-Running Queries

A Taste of Java and Object-Oriented Programming

TPL-Aware Displacement-driven Detailed Placement Refinement with Coloring Constraints

Fast Computation of Shortest Path for Visiting Segments in the Plane

A SYSTOLIC APPROACH TO LOOP PARTITIONING AND MAPPING INTO FIXED SIZE DISTRIBUTED MEMORY ARCHITECTURES

HCMX: AN EFFICIENT HYBRID CLUSTERING APPROACH FOR MULTI-VERSION XML DOCUMENTS

Accounting for the Use of Different Length Scale Factors in x, y and z Directions

Parallel Inverse Halftoning by Look-Up Table (LUT) Partitioning

The stream cipher MICKEY-128 (version 1) Algorithm specification issue 1.0

Performance Evaluation of Information Retrieval Systems

Parallel matrix-vector multiplication

SAO: A Stream Index for Answering Linear Optimization Queries

Efficient Distributed File System (EDFS)

PYTHON IMPLEMENTATION OF VISUAL SECRET SHARING SCHEMES

The example below contains two doors and no floor level obstacles. Your panel calculator should now look something like this: 2,400


2x x l. Module 3: Element Properties Lecture 4: Lagrange and Serendipity Elements

Setup and Use. For events not using AuctionMaestro Pro. Version /7/2013

Transcription:

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.