2.3 Algorithms Flashcards
What is the Big O Notation?
A measure of the complexity of an algorithm.
State all the Big O Notations from worst to best in terms of efficiency.
O(n!) - Factorial
O(2^n) – Exponential
O(n^2) – Polynomial
O(n) – Linear
O(log n ) – Logarithmic
O(1) – Constant
Which Big O Notation has the smallest complexity?
O(1) – Constant
Which Big O Notation has the largest complexity?
O(n!) - Factorial
What is the definition of Dijkstra’s Shortest Path?
An algorithm for finding the shortest path between nodes in a graph.
What are some key facts of Dijkstra’s Shortest Path? ( 3 points)
- Originally depth first (Finds quickest route to one node
- Then it finds the distances to all nodes
- It is iterative
What are the steps of Dijkstra’s Algorithm? (6 points !)
- Set distance to first node to 0. Set all others to infinity.
- Record the distance from first node to all connected nodes. Mark first node as visited.
- Repeat this until done:
- Pick next unvisited node with smallest distance.
- Record the distance from this node to all connected nodes.
- Mark as visited, record the last node we visited.
What is the definition of the A* Algorithm?
A graph traversal algorithm that finds the shortest path between two nodes using heuristics
A* vs Dijkstra’s, Advantages and Disadvantages comparing them:
A*
+ Only finds route to chosen mode.
+ More efficient if good heuristic chosen.
- Heuristic must be calculated.
Dijkstra’s
+ Finds distance to all nodes.
- Slower/ less efficient.
+ Guaranteed to find the best solution.
What is a Linear search and what is its complexity?
A searching algorithm that checks all items in order.
Time Complexity: O(n)
Space complexity is constant O(1)
Why should you use Linear search?
List doesn’t need to be sorted.
Can be very quick (if you’re lucky)
Doesn’t require additional memory.
How does a linear search work? ( 3 points)
Iterate through all items.
When sough item found, record index.
Return index.
What is a Binary Search and what is its complexity?
A divide and conquer algorithm that splits data until it finds the sought item.
Complexity: O(log n)
What are some key facts of the Binary Search?
List must be sorted.
Scales very well with large data sets
Doesn’t require extra memory
Runs on a loop ( not recursion)
What is the Quick Sort Algorithm and what is its complexity in both the normal and worst case?
A recursive, divide and conquer algorithm that sorts a list
Time Complexity: O(n log n)
Time Worse Case: O(n^2)
Space is O(log n)