Algorithms presentation on Path Matrix, Bell Number and Sorting
-
Upload
rishabh-mehan -
Category
Education
-
view
246 -
download
1
description
Transcript of Algorithms presentation on Path Matrix, Bell Number and Sorting
![Page 1: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/1.jpg)
Group 5
Algorithms Presentation
![Page 2: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/2.jpg)
Agenda Items and Presenters
Bell NumbersAll Pairs Shortest PathShell Sort and Radix Sort Psuedocode
![Page 3: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/3.jpg)
Bell Numbers
In how many ways, counting ties, can 8 horses cross a finish line?
A Use Case
![Page 4: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/4.jpg)
Bell NumbersLet’s consider just four horses for now and develop a recurrence relation, a systematic way of counting the possible outcomes.
In mathematics, a recurrence relation is an equation that recursively defines a sequence, one or more initial terms are given: each further term of the sequence is defined as a function of the preceding terms.
Four horses may finish in one, two, three or four “blocks”. Define “blocks”…
A block in this case is a pattern of possible ways the horses can finish.
If we have fours horses as follows…
Horse Racing Example
![Page 5: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/5.jpg)
Bell NumbersAlfred
![Page 6: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/6.jpg)
Bell NumbersBoxtrot
![Page 7: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/7.jpg)
Bell NumbersYisele
![Page 8: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/8.jpg)
Bell NumbersXray
![Page 9: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/9.jpg)
Bell NumbersThree block example:
(Alpha) (Boxtrot) (Yisele, Xray) have finished separately.Yisele and Xray have tied.
More Background Info
![Page 10: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/10.jpg)
Bell Numbers
Three blocks – (Alpha), (Beta), (Yisele, Xray) may be arranged in 3! = 6 ways.
The arrangements for a block of three ways in which the horses can finish.
Note: This does not take into account places (first, second, third, fourth).
(Alpha) (Boxtrot) (Yisele, Xray), (Alpha) (Yisele) (Boxtrot, Xray),(Alpha) (Xray) (Boxtro, Yisele), (Boxtrot) (Yisele) (Alpha, Xray), (Boxtrot) (Xray) (Alpha, Yisele), (Yisele) (Xray) (Alpha, Beta)
Blocks
![Page 11: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/11.jpg)
Bell Numbers Blocks, PartitionsArrangements and Outcomes
The number of ways a set of n elements can be partitioned into m nonempty subsets S(n,m) is the Bell number. In this case 4 horses (elements) can be partitioned in 15 different ways. In this case we added it up manually but we can determine a formula and also a graphical way to calculate as follows….
![Page 12: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/12.jpg)
Bell Numbers Calculate Graphically
The numbers can be constructed by using the Bell Triangle. Start with a row with the number one. Afterward each row begins with the last number of the previous row and continues to the right adding each number to the number above it to get the next number in the row..
![Page 13: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/13.jpg)
Bell Numbers The Formula
S(n+1) = S(n,m-1) + m * S(n,m)
n = elementsm = blocks
B(5) = S(5,1) + S(5,2) + S(5,3) + S(5,4) + S(5,5)
![Page 14: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/14.jpg)
Answer to the ProblemH8 =
Where S(n,k) denotes the # ways n horses can cross in k blocks. H8 = 1×1! + 127×2! + 966×3! + 1701×4! +
1050×5! + 266×6! + 28×7! + 1×8!
= 545835.
H8 denotes the number of ways 8 horses can cross the finish line.
![Page 15: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/15.jpg)
Bell Numbers Psuedocode
Technical Information:Technical Information:
Language:Language:Objects:Objects:
How do you represent a partitioning of a set of n elements?
(This example is somewhat more challenging that the previous ones. Feel free to skim it, and go on.) The n’th Bell number Bn is the number of ways of partitioning n (distinct) objects. One way of computing the Bell numbers is by using the following double recursive algorithm. This algorithm computes numbers with two arguments: B(i,j). The n’th Bell number, Bn is computed as
B(n,n). For example to find B3 compute B(3,3).1
B(1,1) = 1.
B(n,1) = B(n-1,n-1) for n > 1.
B(i,j) = B(i-1,j-1) + B(i,j-1) for n > 1 and 1 < j # i.
![Page 16: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/16.jpg)
All Pairs Shortest PathGiven a weighted graph G(V,E,w), the all-pairs shortest paths problem is to find the shortest paths between all pairs of vertices vi, vj V. ∈
A number of algorithms are known for solving this problem.
![Page 17: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/17.jpg)
All Pairs Shortest PathConsider the multiplication of the weighted adjacency matrix with itself - except, in this case, we replace the multiplication operation in matrix multiplication by addition, and the addition operation by minimization. Notice that the product of weighted adjacency matrix with itself returns a matrix that contains shortest paths of length 2 between any pair of nodes. It follows from this argument that An contains all shortest paths.
Transitive Closure : of R is the smallest transitive relation containing R.
![Page 18: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/18.jpg)
All Pairs Shortest Path0 0 1 0
0 1 0 1
0 0 0 1
0 1 0 0
0 0 1 0
0 1 0 1
0 0 0 1
0 1 0 0
R P1
R[i,j] = {1
0Pk [i,j] = {
1
0
If there is a path of exactly K from i-> j
![Page 19: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/19.jpg)
All Pairs Shortest Path0 0 0 1
0 1 0 1
0 1 0 1
0 1 0 1
0 1 0 0
0 1 0 1
0 1 0 1
0 1 0 1
0 1 0 1
0 1 0 1
0 1 0 1
0 1 0 1
0 1 1 1
0 1 0 1
0 1 0 1
0 1 0 1
P2 P3
P4 P = P1 + P2+P3+P4
![Page 20: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/20.jpg)
All Pairs Shortest PathAn is computed by doubling powers - i.e., as A, A2, A4, A8, and so on. We need log n matrix multiplications, each taking time O(n3). The serial complexity of this procedure is O(n3log n). This algorithm is not optimal, since the best known algorithms have complexity O(n3).
![Page 21: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/21.jpg)
All Pairs Shortest PathParallel Formulation
Each of the log n matrix multiplications can be performed in parallel. We can use n3/log n processors to compute each matrix-matrix product in time log n. The entire process takes O(log2n) time.
![Page 22: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/22.jpg)
All Pairs Shortest PathParallel Formulation
def adj2(i,j): if adj(i,j) == 1: return 1 else: for k in range(0,n): # where n is the number of vertices in G if adj(i,k) == 1 and adj(k,j) == 1: return 1 return 0
![Page 23: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/23.jpg)
80 93 60 6812 8542 30 10
Initial Segmenting Gap = 4
10 30 60 6812 8542 93 80
Shell Sort – Example 1
![Page 24: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/24.jpg)
10 30 60 6812 8542 93 80
Resegmenting Gap = 2
10 12 42 6830 9360 85 80
Shell Sort – Example 2
![Page 25: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/25.jpg)
10 12 30 8042 8560 68 93
10 12 42 6830 9360 85 80
Resegmenting Gap = 1
Shell Sort – Example 3
![Page 26: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/26.jpg)
Shell Sort Psuedocode
Technical Information:Technical Information:
Language:Language:Programming Constructs:Programming Constructs:Efficiency:Efficiency:
# Sort an array a[0...n-1].gaps = [701, 301, 132, 57, 23, 10, 4, 1]
foreach (gap in gaps){ # Do an insertion sort for each gap size. for (i = gap; i < n; i += 1) { temp = a[i] for (j = i; j >= gap and a[j - gap] > temp; j -= gap) { a[j] = a[j - gap] } a[j] = temp }
}
![Page 27: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/27.jpg)
Radix Sort
For simplicity, say you want to use the decimal radix (=10) for sorting. Then you would start by separating the numbers by units and then putting them together again; next you would separate the numbers by tens and then put them together again; then by hundreds and so on until all the numbers are sorted. Each time you loop, just read the list from left to right. You can also imagine you are separating the numbers into buckets. Here is an illustration using
![Page 28: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/28.jpg)
Radix Sort
5, 213, 55, 21, 2334, 31, 20, 430
Separate by units:
zeros: 20, 430
ones: 21, 31
twos:
threes: 213
fours: 2334
fives: 5, 55
Back together: 20, 430, 21, 31, 213, 2334, 5, 55
To put them back together, first read the zeroes bucket, then the ones bucket, then so on, until you read the nines bucket.
![Page 29: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/29.jpg)
Radix Sort
Separate by tens:
zeros: 05
ones: 213
twos: 20, 21
threes: 430, 2334,
fours:
fives: 55
Back together: 5, 213, 20, 21, 430, 2334, 55
![Page 30: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/30.jpg)
Radix Sort
Separate by hundreds:
zeros: 005, 020, 021, 055
ones:
twos: 213
threes: 2334
fours: 430
fives:
Back together: 5, 20, 21, 55, 213, 2334, 430
![Page 31: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/31.jpg)
Radix Sort
Separate by thousands:
zeros: 0005, 0020, 0021, 0055,0213, 0430
ones:
twos: 2334
threes:
fours:
fives:
Back together: 5, 20, 21, 55, 213, 430, 2334
![Page 32: Algorithms presentation on Path Matrix, Bell Number and Sorting](https://reader036.fdocuments.us/reader036/viewer/2022062319/5575e351d8b42af74e8b4795/html5/thumbnails/32.jpg)
Thank You