Analysis of Prim's, Dijkstra's, Kruskal's, Floyd's Algorithms, Bin Packing, Bubble Sort, a, Lecture notes of Linear Programming

An overview of various algorithms, including Prim's, Simplex, Bin packing, Dijkstra's, Kruskal's, Floyd's, and sorting algorithms like Bubble Sort and Quick Sort. Learn about their significance, how they work, and their applications.

Typology: Lecture notes

2021/2022

Uploaded on 09/12/2022

linden
linden 🇬🇧

4.4

(8)

217 documents

1 / 56

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Decision 1:: Algorithms
Andrew.Blacket[email protected]g
Last modified: 11th July 2020
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38

Partial preview of the text

Download Analysis of Prim's, Dijkstra's, Kruskal's, Floyd's Algorithms, Bin Packing, Bubble Sort, a and more Lecture notes Linear Programming in PDF only on Docsity!

Decision 1:: Algorithms

[email protected]

Last modified: 11th^ July 2020

1 :: Algorithms Sorting and bin packing. 4 :: Route inspection Find the shortest route which travels along all roads 2 :: Graphs and networks What is a graph and how they represent things. 5 :: The Travelling Salesman Find the shortest route which visits all places. 3 :: Algorithms on graphs What algorithms do I need to be able to apply? 6 :: Linear Programming How to find an optimal solution graphically 7 :: The simplex algorithm How to find an optimal solution algebraically. 8 :: Critical path analysis How to plan a project.

Decision 1 Overview

Algorithms can be written in code, flowcharts, or just a list of instructions. Python code now = datetime.now() mm = str(now.month) dd = str(now.day) yyyy = str(now.year) hour = str(now.hour) mi = str(now.minute) ss = str(now.second) print mm + "/" + dd + "/" + yyyy + " " + hour + ":" + mi + ":" + ss What is this programme doing? How to eat cake

  1. Buy cake
  2. Eat cake

1.1 Using and understanding algorithms

The ‘peasant’ algorithm on two numbers is as follows: Make a table with two columns, A and B.

  1. Write A and B
  2. Let A = half A (ignoring remainders), Let B = double B.
  3. Write A and B.
  4. Repeat step 2 & 3 until A = 1.
  5. Delete all the rows where A is even
  6. Find the sum of the non deleted Bs, let this equal p.
  7. Write p = …. What does the peasant algorithm do? Further reading… http://mathforum.org/dr.math/faq/faq.peasant.html

1.1 Examples of algorithms

Step n A B C Write down 1 - 2 1 1 1 1, 1 3 - 4 2 2 5 - 6 2 1 2 3 - 4 3 3 5 - 6 3 2 3 3 - 4 5 5 5 - 6 4 3 5 3 - 4 8 11 5 5 5 8 7 - stop Implement this algorithm:

  1. Let n = 1, A = 1, B = 1
  2. Write down A and B
  3. Let C = A + B
  4. Write down C
  5. Let n = n + 1, A = B, B = C
  6. If n < 5 go to step 3
  7. If n = 5 stop It is best to only go onto the next row when you have to, and leaving cells empty rather than repeating values but the MS says… “ condone repeated values in all columns or a single value in each row” so just do it however you like, so long as it is clear. Reveal

1.1 Example 2 – using a trace table

Step n A B C Write down

Reveal A B C D 2 3 0 0 2 3 4 6 6 Print 6

A B C D

?

Test Your Understanding

There are three shapes of boxes which are used in the examination: The boxes in a flowchart are linked by arrows. As with an algorithm written in words, you need to follow each step in order. Start/End Instruction Decision

1.2 Flow Charts

Start Stop Let n = 0 Let n = n + 1 Let E = 2 n Print E Is n > 9? No Yes Implement this algorithm using a trace table. n E Decision 0 1 2 No 2 4 No 3 6 No 4 8 No 5 10 No 6 12 No 7 14 No 8 16 No 9 18 No 10 20 Yes n E Decision

Example 4

Input various values for A=16 and B=12, use a trace table to show your results and work out what the Euclidian Algorithm is doing. step 𝑨 𝑩 𝑩 = 𝟎 𝑨 > 𝑩 1 - 3 16 12 no Yes 6 4 2 - 5 8 no no 2 - 5 4 no no 2 - 5 0 no no 2 yes Print 4 ? step 𝑨 𝑩 𝑩 = 𝟎 𝑨 > 𝑩 It is finding the HCF of A and B?

Test your understanding – The Euclidian algorithm

The diagram describes an algorithm in the form of a flow chart, where a is a positive integer. List P, which is referred to in the flow chart, comprises the prime numbers 2, 3, 5, 7, 11, 13, 17, (b) Explain the significance of the output list. (c) Write down the final value of c for any initial value of a. Reveal ?

Test Your Understanding

?

Pearson Decision 1, Page 9, Q2, Q

Exercise 1B

Click to Animate We look at each pair of values in turn, and swap them if they’re in the incorrect order: This is known as ‘one pass’ of the algorithm. Clearly we’re not done, but we definitely know one value will be in the correct place. Which? The last value (112), because it’s guaranteed to be moved right on each comparison.

Click to Animate After the second pass, we know that the 67 is now in the right place. We keep doing passes, ignoring one extra item at the end each time. We can stop if there are no swaps on a pass. ? We can then do a ‘ second pass ’, and can exclude the last value, given we know it’s now in the correct position: ?

Bubble Sort

! Bubble Sort

  1. On each pass, look at each pair of terms left-to-right, swapping if in the wrong order.
  2. Keep doing further passes, ignoring one extra value at the end each time.
  3. We are done if there are no swaps on a pass. Exam Tip : Bubble Sort should not be confused with a ‘ Bubble Snort ’, which should be avoided at all costs. Pros Cons
  • Simple to implement.
  • Efficient way to check if list is in order , as if list of size 𝑛, only 𝑛 − 1 comparisons needed
  • Extremely inefficient. We need 𝑛 − 1 + 𝑛 − 2 + ⋯ + 3 + 2 + 1 = 𝑛 𝑛− 1 2 comparisons. Since this is a quadratic expression, we say bubble sort takes is of quadratic order Fun Fact : The algorithm is called ‘bubble sort’ because the greatest value ‘bubbles up’ to the end of the list on each pass. ??

Analysis of Bubble Sort