Algorithms Flashcards
What two pieces of information allow you to analyse
an algorithm?
● Time Complexity
● Space Complexity
What is meant by the time complexity of an
algorithm?
The amount of time required to solve a particular problem
How do you measure of the time complexity?
Big-O notation
What does the Big-O notation show?
The effectiveness of an algorithm
What is the Big-O notation good for?
It allows you to predict the amount of time taken to solve an algorithm given the number of items stored
What does a linear time complexity mean?
The amount of time taken to complete an algorithm is independent from the number of elements inputted.
What does a constant time complexity mean?
The amount of time taken to complete an algorithm is independent to the number of inputted elements
What does a polynomial time complexity mean?
The amount of time taken to complete an algorithm is proportional to the number of items inputted to the power of n
What does an exponential time complexity mean?
The amount of time taken to complete an algorithm is proportional to 2 to the power of the number of items inputted.
What does a logarithmic time complexity mean?
The time taken to complete an algorithm will increase at a smaller rate as the number of elements inputted.
What is a logarithm?
How many times a certain number (base) is multiplied together to reach another number.
What is space complexity?
The space complexity is the amount of storage space an algorithm takes up
What is an algorithm?
An algorithm is a series of steps that complete a task
How do you reduce the space
complexity?
Try to complete all of the operations on the same data set
How do you reduce the time complexity of an
algorithm?
You reduce the amount of embedded for loops, and then reduce the amount of items you complete the operations on i.e. divide and conquer
best case time complexity of Quicksort
O(n log(n))
log-linear complexity
worst case time complexity of Quicksort
O(n^2)
Quadratic complexity
best case time complexity of Mergesort
O(n log(n))
log-linear complexity
worst case time complexity of Mergesort
O(n log(n))
log-linear complexity
best case time complexity of Bubble Sort
O(n)
Linear Complexity
worst case time complexity of Bubble Sort
O(n^2)
Quadratic complexity
best case time complexity of Insertion Sort
O(n)
Linear Complexity
worst case time complexity of Insertion Sort
O(n^2)
Quadratic complexity
best case time complexity of Linear Search
0(1)
Constant Time Complexity
Worst case time complexity of Linear Search
O(n)
Linear Complexity
Best case time complexity of Binary Search
0(1)
Constant Time Complexity
Worst case time complexity of Binary Search
O(log n)
logmarithmic complexity
Are stacks FIFO or FILO?
FILO
Which function adds an item to a stack?
Push
Which function removes an element from a stack?
Pop
What is the significance of the back pointer in the
array representation of a queue?
Holds the location of the next available space in the queue
Which function returns the item at the front of a
queue without removing it?
Peek
What is the purpose of the front pointer in the array
representation of a queue?
Points to the space containing the first item in the queue
What value is the top pointer initialised to in the array
representation of a stack?
-1
Give pseudocode for the two functions which add new elements to stacks and queues
Stacks
push(element):
top += 1
A[top] = element
Queues
enqueue(element):
A[back] = element
back += 1
Which function returns the item at the top of a stack
without removing it?
Peek
Which of our sorting algorithms is the most efficient?
Merge sort
What is Dijkstra’s algorithm?
A shortest path algorithm used to find the shortest distance between two nodes in a network.
How is Dijkstra’s algorithm implemented?
You use a priority queue which has the shortest lengths at the front of the queue.
What is the A* algorithm?
The A* algorithm is a general path-finding algorithm which has two cost functions: actual cost and an approximate cost.
How does the A* algorithm differ from
Dijkstra’s algorithm?
A* algorithm has two cost functions: the actual cost and the approximate cost
Why are heuristics used in the A* algorithm?
To calculate an approximate cost from node x to the final node. This aims to make the shortest path finding process more efficient.
What data structure can be used to implement
Dijkstra’s algorithm?
Priority queue
State a disadvantage of using the A* algorithm
The speed of the algorithm is constrained by the accuracy of the heuristic
How does the Breath-first algorithm work?
Works by visiting all the nodes on a row, before moving to the next row.
In what data structure is a breadth-first search algorithm used?
A queue (FIFO)
In this tree diagram: https://gyazo.com/349ec19d07959bf5ae8d1b2bc42fd5ed. Describe how this tree would be searched using a breath-first search algorithm.
- A
- B,C
- D,E,F,G
- H,I
How does the Depth-first algorithm work?
works by visiting all the nodes on the same verticle, before horitizontal.
In what data structure is a Depth-first algorithm used?
Stacks (LIFO)
In this tree diagram: https://gyazo.com/01975111eff5d629c81b74fec90f7c58. Describe how this tree would be searched using a depth-first search algorithm.
- A
2.B - C,D,E
- F
5.G,H,I
How does bubble sort work?
Compares pairs of data and then goes through list again and compares pairs again.
Use bubble sort on:
2, 8, 5, 3, 9, 4, 1
2, 8, 5,3 ,9, 4, 1
2>8
8>5 = swap
2,5,8,3,9,4,1
8>3 = swap
2,5,3,8,9,4,1
8<9
9>4 = swap
2,5,3,8,4,9,1
9>1
2,5,3,8,4,1,9
KEEPS GOING
how is Merge Sort done?
Recursively, using divide and conquer which is when you break a problem into a smaller problem in order to solve it.
How does merge sort work?
Breaks down elements into individuals then doubles them up while comparing and changing until list is sorted
Use Merge Sort on:
2, 8, 5, 3, 9, 4, 1, 7
Divide the array until elements are individual elements.
2 8 3 5 4 9 1 7
2,8 3,5 4,9 1,7
2,3,5,8 1,4,7,9
1,2,3,4,5,7,8,9
How to remove a node from a linked list
- Traverse through list to find data.
- Find the previous and next node of the data.
- Switch the previous nodes next pointer to datas next pointer
- And switch datas next pointers, previous pointer to datas previous pointer
- Set data to null