0% found this document useful (0 votes)
6 views

Lec4, Algorithm Analysis & Design, Randamized Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lec4, Algorithm Analysis & Design, Randamized Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 95

1

Randomized Algorithms
DR/ Hossam Hawash
2 Randomized Algorithms

 Probability and randomness often serve as tools for algorithm design and
analysis, by making part of the algorithm behave randomly.
 We call an algorithm randomized if its behavior is determined not only by its
input but also by values produced by a random-number generator.

Input Algorithm Output

Random
Variable
3 Randomized Algorithms

 When analyzing the running time of a randomized algorithm, we take the


expectation of the running time over the distribution of values returned by
the random number generator.
 We distinguish these algorithms from those in which the input is random by
referring to the running time of a randomized algorithm as an expected
running time.
4 Randomized Algorithms

 Randomized algorithms are classified into two main types:


 Las Vegas Algorithms
 Las Vegas algorithms are randomized algorithms that always produce the correct
output.
 However, the running time of Las Vegas algorithms is allowed to vary due to the use of
randomness.
 The algorithm may terminate quickly with a high probability, but there is no guarantee
on the worst-case running time.
 Monte Carlo Algorithms
 Monte Carlo algorithms are randomized algorithms that have a fixed running time.
 The output of these algorithms may be incorrect with a certain probability.
 Despite the potential for incorrect results, Monte Carlo algorithms are often useful in
situations where approximate solutions are acceptable.
5 QuickSort Algorithm (Example)

 Input

0 1 2 3 4 5

6 5 9 12 3 4
6 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 9 12 3 4
pivot= ?

Partition Initialization...
7 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 9 12 3 4
pivot=6

Partition Initialization...
8 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 9 12 3 4
pivot=6
left

Partition Initialization...
9 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 9 12 3 4
pivot=6
left right

Partition Initialization...
10 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 9 12 3 4
pivot=6
left right

right moves to the left until


value that should be to left
of pivot...
11 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 9 12 3 4
pivot=6
left right
12 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 9 12 3 4
pivot=6
left right

left moves to the right until


value that should be to right
of pivot...
13 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 9 12 3 4
pivot=6
left right
14 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 9 12 3 4
pivot=6
left right
15 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 9 12 3 4
pivot=6
left right

swap arr[left] and arr[right]


16 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 4 12 3 9
pivot=6
left right

repeat right/left scan


UNTIL left & right cross
17 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 4 12 3 9
pivot=6
left right

right moves to the left until


value that should be to left
of pivot...
18 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 4 12 3 9
pivot=6
left right
19 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 4 12 3 9
pivot=6
left right

left moves to the right until


value that should be to right
of pivot...
20 QuickSort Algoritm

0 1 2 3 4 5

partition(arr,0,5) 6 5 4 12 3 9
pivot=6
left right
21 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 4 12 3 9
pivot=6
left right

swap arr[left] and arr[right]


22 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 4 3 12 9
pivot=6
left right

swap arr[left] and arr[right]


23 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 4 3 12 9
pivot=6
left right

repeat right/left scan


UNTIL left & right cross
24 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 4 3 12 9
pivot=6
left right

right moves to the left until


value that should be to left
of pivot...
25 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 4 3 12 9
pivot=6
left

right
26 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 4 3 12 9
pivot=6
left

right

right & left CROSS!!!


27 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 6 5 4 3 12 9
pivot=6
left

right

right & left CROSS!!!


1 - Swap pivot and arr[right]
28 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 3 5 4 6 12 9
pivot=6
left

right

right & left CROSS!!!


1 - Swap pivot and arr[right]
29 QuickSort Algorithm (Example)

0 1 2 3 4 5

partition(arr,0,5) 3 5 4 6 12 9
pivot=6
left

right

right & left CROSS!!!


1 - Swap pivot and arr[right]
2 - Return new location of pivot to caller

return 3
30 QuickSort Algorithm (Example)

0 1 2 3 4 5
QuickSort(arr,0,5)
3 5 4 6 12 9

pivot
position

Recursive calls to QuickSort()


using partitioned array...
31 QuickSort Algorithm (Example)

0 1 2 3 4 5
QuickSort(arr,0,5)
3 5 4 6 12 9
QuickSort(arr,0,3) QuickSort(arr,4,5)

0 1 2 3 4 5

3 5 4 6 12 9
32 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)
partition(arr,0,3)

0 1 2 3 4 5

3 5 4 6 12 9
33 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)
partition(arr,0,3)

0 1 2 3 4 5

3 5 4 6 12 9

Partition Initialization...
34 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)
partition(arr,0,3)

0 1 2 3 4 5

3 5 4 6 12 9

left

Partition Initialization...
35 QuickSort Algoritm

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)
partition(arr,0,3)

0 1 2 3 4 5

3 5 4 6 12 9

left right

Partition Initialization...
36 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)
partition(arr,0,3)

0 1 2 3 4 5

3 5 4 6 12 9

left right

right moves to the left until


value that should be to left
of pivot...
37 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)
partition(arr,0,3)

0 1 2 3 4 5

3 5 4 6 12 9

left right
38 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)
partition(arr,0,3)

0 1 2 3 4 5

3 5 4 6 12 9

left right
39 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)
partition(arr,0,3)

0 1 2 3 4 5

3 5 4 6 12 9

left

right
40 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)
partition(arr,0,3)

0 1 2 3 4 5

3 5 4 6 12 9

left

right

right & left CROSS!!!


41 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)
partition(arr,0,3)

0 1 2 3 4 5

3 5 4 6 12 9

left

right

right & left CROSS!!!


1 - Swap pivot and arr[right]
42 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)
partition(arr,0,3)

0 1 2 3 4 5

3 5 4 6 12 9
right & left CROSS!!!
left
1 - Swap pivot and arr[right]
right 2 - Return new location of pivot to caller

return 0
43 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

0 1 2 3 4 5

3 5 4 6 12 9

Recursive calls to QuickSort()


using partitioned array...
44 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5

0 1 2 3
12 9
3 5 4 6
45 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5

0 1 2 3
12 9
3 5 4 6

Base case triggered...


halting recursion.
46 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5

0 1 2 3
12 9
3 5 4 6

Base Case: Return


47 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5
partition(arr,1,3)
0 1 2 3
12 9
3 5 4 6

Partition Initialization...
48 QuickSort Algorithm (Example)
QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5
partition(arr,1,3)
0 1 2 3
12 9
3 5 4 6

Partition Initialization...
49 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5
partition(arr,1,3)
0 1 2 3
12 9
3 5 4 6

left

Partition Initialization...
50 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5
partition(arr,1,3)
0 1 2 3
12 9
3 5 4 6

left right

right moves to the left until


value that should be to left
51 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5
partition(arr,1,3)
0 1 2 3
12 9
3 5 4 6

left right
52 QuickSort Algorithm (Example)
QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5
partition(arr,1,3)
0 1 2 3
12 9
3 5 4 6

left right

left moves to the right until


value that should be to right
of pivot...
53 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5
partition(arr,1,3)
0 1 2 3
12 9
3 5 4 6

right

left
54 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5
partition(arr,1,3)
0 1 2 3
12 9
3 5 4 6

right

left

right & left CROSS!


55 QuickSort Algorithm (Example)
QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5
partition(arr,1,3)
0 1 2 3
12 9
3 5 4 6

right

left

right & left CROSS!


1- swap pivot and arr[right]
56 QuickSort Algorithm (Example)
QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5
partition(arr,1,3)
0 1 2 3
12 9
3 4 5 6

right

left

right & left CROSS!


1- swap pivot and arr[right]
57 QuickSort Algorithm (Example)
QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5
partition(arr,1,3)
0 1 2 3
12 9
3 4 5 6

right

left

right & left CROSS!


1- swap pivot and arr[right] return 2
2 – return new position of pivot
58 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5

0
12 9
3 QuickSort(arr,1,2) QuickSort(arr,3,3)

1 2 3

4 5 6
59 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5

0
12 9
3 QuickSort(arr,1,2) QuickSort(arr,3,3)
partition(arr,1,2)
1 2 3

4 5 6
60 QuickSort Algorithm (Example)

QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5

0
12 9
3 QuickSort(arr,1,2) QuickSort(arr,3,3)

1 2 3

4 5 6
61 QuickSort Algorithm (Example)
QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5

0
12 9
3 QuickSort(arr,1,2) QuickSort(arr,3,3)

QuickSort(arr,1,1) QuickSort(arr,2,2)

1 2

4 5
62 QuickSort Algorithm (Example)
QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)
partition(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5

0
12 9
3 QuickSort(arr,1,2) QuickSort(arr,3,3)

QuickSort(arr,1,1) QuickSort(arr,2,2)

1 2

4 5
63 QuickSort Algorithm (Example)
QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)
partition(arr,4,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) 4 5

0
9 12
3 QuickSort(arr,1,2) QuickSort(arr,3,3)

QuickSort(arr,1,1) QuickSort(arr,2,2)

1 2

4 5
64 QuickSort Algorithm (Example)
QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,6,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) QuickSort(arr,4,5)

0 4 5

3 QuickSort(arr,1,2) QuickSort(arr,3,3)
9 12

QuickSort(arr,1,1) QuickSort(arr,2,2)

1 2

4 5
65 QuickSort Algorithm (Example)
QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,6,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) QuickSort(arr,4,5)


partition(arr,4,5)
0 4 5

3 QuickSort(arr,1,2) QuickSort(arr,3,3)
9 12

QuickSort(arr,1,1) QuickSort(arr,2,2)

1 2

4 5
66 QuickSort Algorithm (Example)
QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,6,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) QuickSort(arr,4,5)

0 4 5

3 QuickSort(arr,1,2) QuickSort(arr,3,3)
9 12

QuickSort(arr,1,1) QuickSort(arr,2,2)

1 2

4 5
67 QuickSort Algorithm (Example)
QuickSort(arr,0,5)

QuickSort(arr,0,3) QuickSort(arr,4,5)

QuickSort(arr,6,5)

QuickSort(arr,0,0) QuickSort(arr,1,3) QuickSort(arr,4,5)

3 QuickSort(arr,1,2) QuickSort(arr,3,3)

3 QuickSort(arr,4,4)
4
6
9
QuickSort(arr,1,1) QuickSort(arr,2,2)
QuickSort(arr,5,5)
1 2 5
4 5 12
68 QuickSort Algorithm (Output)

 Sorted Array

0 1 2 3 4 5

3 4 5 6 9 12
69 QuickSort Algorithm (Worst-case time)

𝑇 𝑛
= 𝑐𝑛 + 𝑐 𝑛 − 1 + 𝑐 𝑛 − 2 + … . . + 2𝑐
= 𝑐 𝑛 + 𝑛 − 1 + 𝑛 − 2 + …..+ 2
𝑛
= 𝑐 𝑛+1 − 1 = 𝑂(𝑛2 )
2
70 QuickSort Algorithm (Best-case time)

𝑇 𝑛 = 𝑐𝑛 ∗ log 𝑛 = 𝑂(𝑛𝑙𝑜𝑔 𝑛)
71 QuickSort Algorithm (Average-case
time)

• The left child of each node


represents a subproblem size
1/4 as large.
• The right child represents a
subproblem size 3/4 as large.

𝑇 𝑛 = 𝑛 log 4/3 𝑛 = 𝑂(𝑛 log 𝑛)


72 Randomized QuickSort Algorithm

 If the pivot is chosen poorly, Quicksort can degrade to quadratic time


complexity in the worst case.
 Randomized Quicksort addresses this issue by randomly selecting the pivot
element during each partitioning step.
 This randomization helps to distribute the data more evenly, reducing the
likelihood of encountering worst-case scenarios and improving the
expected average-case performance.
73 Randomized QuickSort Algorithm

 Suppose that your worst enemy has given you an array to sort with
quicksort, knowing that you always choose the rightmost element in each
subarray as the pivot, and has arranged the array so that you always get
the worst-case split. How can you foil your enemy?
 you could randomly choose an element in the subarray, and use that
element as the pivot.
 The partition function assumes that the pivot is in the rightmost position of
the subarray.
 No problem—just swap the element that you chose as the pivot with the
rightmost element, and then partition as before.
74 Expected average-case performance
of Randomized Quicksort
 Random Pivot Selection: The probability of choosing any particular element as the pivot is
1/𝑛 (assuming a uniformly random selection).

 Expected Time to Partition:


• The expected time to partition an array of size 𝑛 is the sum of the probabilities of all
possible pivot choices multiplied by the corresponding partitioning cost.
• The cost of partitioning at position 𝑖 is 𝑂(𝑛), as it involves comparing each element with
the chosen pivot.
1
• Therefore, the expected partitioning time is 𝑂(𝑛) × ∑𝑛𝑖=1 = 𝑂(𝑛).
𝑛
75 Expected average-case performance
of Randomized Quicksort
 Expected Time for Recursive Calls:
 The expected time for recursive calls depends on the sizes of the resulting subarrays.
 Since the partitioning is assumed to be balanced on average, the sizes of the resulting subarrays are
approximately 𝑛/2 on average.
𝑇(𝑛) = 𝑂(𝑛) + ∑subarray sizes 𝑚 (probability of subarray sizes 𝑚 × (𝑇(𝑚) + 𝑇(𝑛 − 𝑚)) )

 Approximating the subarray sizes as 𝑛/2 on average, we get:

1
𝑇(𝑛) = 𝑂(𝑛) + ෍ × (𝑇(𝑚) + 𝑇(𝑛 − 𝑚 − 1))
2
𝑚

 Now, we have a simplified expected time complexity recurrence relation that captures the
essence of Randomized Quicksort.

 Solving this recurrence relation typically leads to an expected time complexity of 𝑂 𝑛log 𝑛
when considering average-case performance.
76 Randomized Binary Search Algorithm

 Instead of always choosing the middle element as the mid-point during


each iteration, the randomized version selects a random index within the
current search space.
 It is a Las Vegas randomized algorithm as it always finds the correct result.

1
𝑇(𝑛) = 𝑂(1) + ෍ × (𝑇(𝑚) + 𝑇(𝑛 − 𝑚))
2
𝑚

 expected time complexity of 𝑂(log 𝑛)


77 Fisher-Yates Algorithm

 The Fisher-Yates Shuffle algorithm shuffles a finite sequence of elements by


generating a random permutation.
 The goal of the shuffle is to ensure that every permutation is equally likely.
78 Fisher-Yates Algorithm
79 Fisher-Yates Algorithm
80 Fisher-Yates Algorithm
81 Fisher-Yates Algorithm
82 Fisher-Yates Algorithm
83 Fisher-Yates Algorithm
84 Fisher-Yates Algorithm
85 Knuth Shuffle

 Start from the last element of the array.


 Generate a random index between 0 and the current index (inclusive).
 Swap the element at the current index with the element at the randomly
chosen index.
 Repeat this process for the remaining elements, moving backward through
the array
 time complexity of 𝑂(𝑛)
86 Knuth Shuffle

 Step 1:

0 1 2 3 4 5

6 5 9 12 3 4
Rand(0,4)
87 Knuth Shuffle

 Step 1:

0 1 2 3 4 5

6 5 9 12 3 4

0 1 2 3 4 5

6 4 9 12 3 5
88 Knuth Shuffle

 Step 2:

0 1 2 3 4 5

6 4 9 12 3 5
Rand(0,3)
89 Knuth Shuffle

 Repeat till you reach index 1.

0 1 2 3 4 5

6 4 9 12 3 5
Rand(0,3)
90 Freivald’s Algorithm

 Freivalds' Algorithm is a randomized algorithm used in computer science


and linear algebra.
 Developed by Rūsinis Freivalds, a Latvian computer scientist, in 1977.
 Utilizes randomization to achieve efficiency.
 Provides a probabilistic guarantee of correctness.
 Since the output is not always correct, it is a Monte Carlo randomized
algorithm.
 Widely used in algorithmic testing and verification of matrix multiplication.
91 Freivald’s Algorithm

 Input : 𝐴 = , B = [], C = []
 Output : Is that C = A x B? True or False
 Algorithmic Steps:
 Generate a random vector 𝑥 of appropriate dimensions
 Vector 𝑥 is consisting of 0/1 only..
 Compute 𝑌 = 𝐴 ⋅ (𝐵 ⋅ 𝑥) − 𝐶 ⋅ 𝑥.
 Return true if 𝑌? = 0, 0, … , 0 𝑇
, otherwise, return false.
92 Freivald’s Algorithm: An Example

1 3 2 0 5 4
 Input : 𝐴 = ,B = ,C =
5 1 1 2 11 8
 Generate a random vector 𝑥 of appropriate size. Let's say 𝑥 = [1,0]𝑇 .
1 3 2 0 1 1 3 2 5
 𝐴× 𝐵×𝑥 = × × = × =
5 1 1 2 0 5 1 1 11
5 4 1 5
 𝐶×𝑥 = × =
11 8 0 11
5 5 0
 Now, subtract the two results:Y = 𝐴 × (𝐵 × 𝑥) − 𝐶 × 𝑥 = − = .
11 11 0
93 Probabilistic Analysis of Freivalds' Algorithm

 Generating a random vector x of size n takes 𝑂(𝑛) time.


 The randomness ensures that the verification is not biased toward certain
matrix properties.
 Matrix multiplication 𝐴 × (𝐵 × 𝑥) both take 𝑂(𝑛2 ) time.
 If the matrices are not equal, the algorithm will detect this with high
probability.
 If the matrices are equal, the algorithm will return a false positive with a
certain probability.
 To increase confidence, the algorithm is often applied iteratively 𝑘 times,
where 𝑘 is a user-defined parameter.
 T(n)= 𝑂(𝑘 ∙ 𝑛2 )+ 𝑂(𝑛)
94
Questions?
95 Refences

 Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2022). Introduction to
algorithms. MIT press.
 Motwani, Rajeev, and Prabhakar Raghavan. Randomized algorithms.
Cambridge university press, 1995.
 Donald, E. K. (1999). The art of computer programming. Sorting and
searching, 3(426-458), 4.
 Freivalds, R. (1979, September). Fast probabilistic algorithms. In International
Symposium on Mathematical Foundations of Computer Science (pp. 57-
69). Berlin, Heidelberg: Springer Berlin Heidelberg.

You might also like