# randomized algorithms

Post on 03-Jan-2016

35 views

Embed Size (px)

DESCRIPTION

Pasi Fränti. Randomized Algorithms. 1.10.2014. Treasure island. Treasure worth 20.000 awaits. ?. Map for sale: 3000. 5000. 5000. 5000. ?. DAA expedition. To buy or not to buy. Buy the map:. 20000 – 5000 – 3000 = 12.000. Take a change:. 20000 – 5000 = 15.000. - PowerPoint PPT PresentationTRANSCRIPT

Randomized Algorithms9.10.2015

Treasure islandTreasure worth 20.000 awaits5000DAA expedition50005000??Map for sale: 3000

To buy or not to buyBuy the map:Take a change:20000 5000 3000 = 12.00020000 5000 = 15.00020000 5000 5000 = 10.000

To buy or not to buyBuy the map:Take a change:20000 5000 3000 = 12.00020000 5000 = 15.00020000 5000 5000 = 10.000Expected result:0.5 15000 + 0.5 10000 = 12.500

Three type of randomizationLas VegasOutput is always correct resultResult is not always foundProbability of success pMonte CarloResult is always foundResult can be inaccurate (or even false!)Probability of success pSherwoodBalancing the worst case behavior

Las Vegas

Dining philosophersWho eats?

Las Vegas Input:Bit-vector A[1,n]Output:Index of any 1-bit from A

LasVegas(A, n) indexREPEATk Random(1, n);UNTIL A[k]=1;RETURN k

6

8-Queens puzzleINPUT: Eight chess queens and an 88 chessboardOUTPUT: Setup where no queens attack each other

8-Queens brute forceBrute force Try all positions Mark illegal squares Backtrack if dead-end 114 setups in totalRandom Select positions randomly If dead-end, start overRandomized Select k rows randomly Rest rows by Brute Force864Where next?

Pseudo code8-Queens(k)FOR i=1 TO k DO// k Queens randomly r Random[1,8];IF Board[i,r]=TAKEN THEN RETURN Fail;ELSE ConquerSquare(i,r);FOR i=k+1 TO 8 DO // Rest by Brute Forcer1; foundNO;WHILE (r8) AND (NOT found) DOIF Board[i,r] NOT TAKEN THEN ConquerSquare(i,r); foundYES;IF NOT found THEN RETURN Fail;

ConquerSquare(i,j)Board[i,j] QUEEN;FOR z=i+1 TO 8 DOBoard[z,j] TAKEN;Board[z,j-(z-i)] TAKEN;Board[z,j+(z-i)] TAKEN;

Probability of successs = processing time in case of successe = processing time in case of failurep = probability of successq = 1-p = probability of failureExample:s=e=1, p=1/6t=1+5/11=6

Experiments with varying kFastestexpectedtime

Random SwapClustering

Swap-based clustering

Clustering by Random SwapRandomSwap(X) C, PC SelectRandomRepresentatives(X);P OptimalPartition(X, C);REPEAT T times(Cnew, j) RandomSwap(X, C);Pnew LocalRepartition(X, Cnew, P, j);Cnew, Pnew Kmeans(X, Cnew, Pnew);IF f(Cnew, Pnew) < f(C, P) THEN(C, P) Cnew, Pnew;RETURN (C, P);P.Frnti and J.Kivijrvi, "Randomised local search algorithm for the clustering problem", Pattern Analysis and Applications, 3 (4), 358-369, 2000.Select random neighborAccept only if it improves

1. Random swap:

2. Re-partition vectors from old cluster:

3. Create new cluster:

Clustering by Random Swap

Choices for swapO(M) clusters to be removedO(M) clusters where to addO(M2) different choices in total=

Select a proper centroid for removal:M clusters in total: premoval=1/M.Select a proper new location:N choices: padd=1/NM of them significantly different: padd=1/MIn total:M2 significantly different swaps.Probability of each is pswap=1/M2Open question: how many of these are goodTheorem: are good for add and removal.

Probability for successful Swap

Probability of not finding good swap:Estimated number of iterations:Clustering by Random SwapIterated T times

Upper limit:Lower limit similarly; resulting in:Bounds for the iterations

Number of iterations needed (T):t = O(N)Total time:Time complexity of single step (t):Total time complexity

Probability of success (p)depending on T

Time-distortion performance

Monte Carlo

Input:Bit-vector A[1,n], max iterations zOutput:An index of any 1-bit in A.MonteCarlo(A, n, z) True/Falsei 0;REPEATk Random(1, n);i i+1;UNTIL (A[k]=1) OR (i>z);RETURN kMonte Carlo

Monte CarloPotential problems to be considered: Detecting prime numbers Calculating integral of a functionTo appear in future

Sherwood

Used in QuicksortData is already sortedWorst can happens always if sorted dataN-3O(N2)Selection of pivot elementNave: first or last item

Selection of pivot elementRandom item75%O(NlogN)Worst can still happensBut with probability (1/n)n1125%75%25%25%

Simulated dynamic linked listSorted arraySearch efficient: O(logN)Insert and Delete slow: O(N)Dynamically linked listInsert and Delete fast: O(1)Search inefficient: O(N)

Simulated dynamic linked listExample115247521HeadLinked list:Head=4Simulated by array:

i1234567Value241515217Next2561703

- SEARCH (A, x)i := A.HEAD;max := A[i].VALUE;FOR k:=1 TO N DOj:=RANDOM(1, N);y:=A[j].VALUE;IF (max
Analysis of the searchDivide into N segmentsEach segment has N/N = N elementsLinear search within one segment.Expected time complexity = N + N = O(N)

Experiment with studentsData (N=100) consists of numbers from 1..100:Select N breaking points:

Searching for77

Empty space for notes

Recommended