Worded explanations on algorithms Flashcards
1
Q
BFS
explain
A
- The BFS algorithm uses a queue ADT such that it stores the unvisited nodes in the queue ADT
- After each node has been visited, it marked the node as visited and enqueues all adjacent unvisited nodes in the queue
2
Q
Dijkstra’s
explain
A
- Dijkstra’s algorithm starts by initializing the distance of all nodes as infinity and the starting node as 0
- it iteratively picks the unvisited node with the lowest distance whilst updating the distances
- After visiting a node, it marks that node as visited then repeats until all nodes are visited or the target node is found
3
Q
Bellman-Ford
explain
A
- The bellman ford algorithm loops through all edges |V|-1 times in any order whilst updating the distances of each node in each iteration such that it is trying to minimize the distance
- Then it loops through all edges one more time, if a change has been made there exists a negative cycle, which then updates all effected edges as -infinity
4
Q
Floyd-Warshall’s transitive closure algorithm
explain
A
- Floyd-Warshall’s algorithm** initializing an adjacency matrix**
- Then through a triple nested for loop, it updates i through j whilst considering k, an intermediate node
- It repeats until it tries all the combinations of i,j and k
5
Q
Prim’s
explain
A
- Prim’s algorithm starts by considering all edges from a node while marking the starting node as visited
- Prim’s will then pick the edge with the lowest edge that leads to an unvisited node
- After it goes to an unvisited node, it marks it as visited then repeats until all nodes are visited
6
Q
Merge Sort
explain
A
- Mergesort divides the array into each sub array where len(sub array) is 1
- It then merges each subarray together whilst sorting each subarray
- It recursively does this until the array is back together
7
Q
Quick Sort
explain
A
- Quick sort has a pivot that splits the array into two parts, one for elements smaller than or equal to the pivot and one for the other.
- It then splits those sub array in a similar manor until reaching single size sub array then merges those back together.
8
Q
Binary search
explain
A
- The binary search algorithm divides the array in half and keeps the half that includes the target number
- It repeats until the target value is found
9
Q
Greedy graph coloring
explain
A
- The algorithm starts by colouring a node the lowest colour
- For each uncoloured neighbouring node, it colours the node the lowest colour such that the node adjacent to it is not the same colour
- It repeats this until all nodes are coloured and returns how many colours are used
10
Q
Change making brute force
explain
A
- The algorithm could first generate all the combinations as an array of the list of coins as an array
- Then by looping through sub arrays in the array, it gets the sub array with the smallest amount of coins in the array
- It then will return the lengh of that particular sub array
11
Q
Change making greedy
explain
A
- The algorithm could first sort the array in ascending order
- continusly adds the highest value until it exceeds the max number of coins
- Then it goes to the element below, repeating until it can does not exceed the max number of coins
- If it reaches the last element, the algorithm stops and returns how many coins were used
12
Q
Knapsack brute force
A
- The algorithm could first generate all the combinations of each item
- Then, it can remove all the solutions that are exceeding the weight
- Through this, it can pick the items with the highest value
13
Q
Knapsack heuristic
A
- The knapsack can generate a ratio between the value and the weight
- Then, it will continually add the highest ratio until it does not exceed the weight
14
Q
TSP Brute force
A
- The TSP brute force can get every combination all the possible solutions
- It then picks the solution with the lowest distance
15
Q
TSP heuristic (nearest neighbor)
A
- An algorithm can always pick the smallest unvisited node starting from the starting node
- Repeats until all nodes are visited, then connect the final node to the target node