ALGORITHMS Flashcards
Pros and cons of a bubble sort
- Easy set of instructions
- Can check for errors on the way
- List containing n elements requires
n-1 comparisons (max) - Could take a long time
- Inefficient - quadratic order
1/2[n(n-1)]
*smallest/largest number always at the end after 1st pass
EDIT
Efficient for CHECKING IF LIST IS IN ORDER - list of size n requires max n-1 comparisons
Inefficient in that carrying out a full sort requires 1/2(n)(n-1), unless list is partially/fully in order
Describe how to carry out a bubble sort
(If starting at lefthand side) in first pass compare first value with second value and swap if the second is larger. Then compare the second and third value and swap if the second is larger. Repeat until the end of the list - first pass
Repeat process until list no longer changes after a pass (no swaps in a pass)
Describe how to carry out a quicksort
- Select a pivot (usually midpoint of
list) - Write down pivot
- Write down objects greater than and
less than the pivot, keeping them in
a sublist - Choose new pivot
- Apply steps to each new sublist
- Algorithm finishes when all objects
selected as pivots
Assumptions made when using resource histograms
A worker can only be assigned to one activity at a time,
A worker can start a new activity immediately after finishing an old one
One worker per activity
What is levelling in CPA?
Adjusting the start/finish of an activity to accommodate restraints on resources
Assumptions for scheduling diagrams
One worker per activity
Always use first available worker
If we can choose more than one activity to assign to a worker, choose the one with the lower late finish time
How do you calculate the lower bound for the number of workers required to complete a project in its minimum time
Sum off all activities’ durations/ minimum finish time
Differences between Kruskal’s and Prim’s
n Prim’s algorithm, the starting point can be any node, whereas Kruskal’s algorithm starts
from the arc of least weight. In Prim’s algorithm, each new node and arc is added to the
existing tree as it builds, whereas in applying Kruskal’s algorithm, the arcs are selected
according to their weight and may not be connected until the end.
Calculating order of bubble sort
1st pass requires (n-1) comparisons, 2nd requires (n-2) … until only 1 comparison. so is the sum of integers up to n-1 = 1/2(n-1)((n-1)+1)
= 1/2(n-1)(n)
Explain why the first fit bin packing algorithm has quadratic order
To pack the k
the item requires at most (k-1) comparisons, if every item placed so far is in a separate bin. Hence the total number of comparisons for n items is ∑ (k-1) to n
Difference between dikstra and floyds algorithm
Floyd’s gives you shortest distance between any pair of vertices
Order of prims algorithm on a graph order nxn step lvl
Crossing out first row means there are n-1 elements to compare, which requires (n-1) -1 comparisons. Adding second column, and crossing out second row means there are (n-2) elements in each row, however 2 columns, so in total there are ((n-2)x2 -1) comparisons, repeat this up to ((n-(n-1) x (n-1) -1). To compare values after picking the rth value, you will need r(n-r)-1 comparisons. As (n-1) vertices are picked, u get the sum of (n-r)(r) -1 up to n-1 which equals (1/6)(n^3-7n+6) so order is cubic
(Draw out matrix it helps to visualise)
Number of arcs in a mst w n vertices
n-1
Number of arcs in Kn
NC2 arcs
Number of iterations required for floyds on a matrix w n vertices
n iterations all the same type