Algorithms Flashcards

(22 cards)

1
Q

what is an algorithm?

A

a set of clearly defined , step by step instructions used to solve a problem, with a clear start and end point, providing a valid output from valid inputs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what kinds of problem are solved by algorithms (generally)?

A
  • routing problems - data packets across internet, quickest salesman route, etc.
  • timetabling aircraft crews so they don’t work overtime
  • searching info from the internet on a database
  • encrypting communication
  • sorting lots of data
  • writing a compiler program to translate a high level language into machine code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what makes a good algorithm?

A
  • correct outputs for all valid inputs
  • must terminate at some point
  • each step is a clearly defined task
  • well defined inputs + outputs
  • problem should be solvable
  • designed so others can understand + modify
  • precise + efficient
  • able to handle invalid inputs
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is time complexity?

A

how much time is required to solve a particular problem using an algorithm - refers to the number of operations or steps to complete + independent of hardware performance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is Big O notation?

A
  • shows the effectiveness of an algorithm by expressing time/space complexity of an algorithm
  • shows an upper limit for the amount of time taken relative to the number of data elements inputted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is scalability?

A

how much the algorithm slows down when input size into the algorithm increases

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is constant time complexity?

A
  • O(1)
  • algorithm always takes the same number of steps, regardless of input size
  • eg. accessing an item in an array using an index
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is linear time complexity?

A
  • O(n)
  • the number of steps increases proportionally with input size n
  • eg. traversing an array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is polynomial time complexity?

A
  • O(n^2)
  • performance is directly proportional to the square of the size of the data set
  • eg. nested FOR loops
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is exponential time complexity?

A
  • O(2^n)
  • running time will double with every additional item in the dataset
  • run time increases exponentially and quickly becomes very large
  • usually when you have to try every possible combination
  • eg. recursive algorithms
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is logarithmic time complexity?

A
  • O(log n)
  • time taken depends on half the input size - in proportion to the log of the input
  • number of steps grows slowly even when input size grows rapidly
  • eg. binary search
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is a permutation?

A

the number of ways that n items can be arranged

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what are the 2 types of permutation?

A
  • repetition allowed - eg. combination lock with 4 digits 0-9
  • no repetition allowed - eg. 4 differently coloured ball in a bag that you draw out one at a time
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is the time complexities of the 2 types of permuation?

A
  • repetition allowed: O(n^r), where n=number of available elements, r= length of each permutation
  • no repetition allowed: O(n!) - very very inefficient for large values of n
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

rank the different time complexities in order of most to least efficient

A
  1. O(1)
  2. O(log n)
  3. O(n)
  4. O(n^2)
  5. O(2^n)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how do you calculate the Big O of an algorithm?

A
  • count the number of assignment statements in the algorithm
  • only the dominant term is significant - any other constant coefficient is ignored
  • eg. O(3n^2 + 5n + 2) -> O(n^2)
17
Q

what is a binary tree?

A

a rooted tree with a maximum of 2 child nodes

18
Q

what are the 3 parts of a node?

A
  • data (primitive/complex)
  • left pointer to a child
  • right pointer to a child
19
Q

what are the 3 types of traversals?

A
  • pre-order - visit the root, then traverse the left subtree, then the right subtree
  • in-order - traverse left subtree, then visit the root, the traverse the right sub tree
  • post-order - traverse left subtree, then traverse the right subtree, then visit the root
20
Q

what is a balanced binary tree?

A

where nodes are distributed in such a way that the height is kept to a minimum to enable more efficient searching

21
Q

what is an unbalanced binary tree?

A

where the heights of the left and right subtrees of one or more nodes differs significantly - more linear structure -> inefficient operations