Algorithms Flashcards

A Level Comp Sci algorithms

You may prefer our related Brainscape-certified flashcards:
1
Q

Breadth-First Traversal

A

Graph traversal algorithm that uses a queue to store visited nodes.

Starts at the first node then visits all its neighbours, then visits their neighbours until all the nodes have been visited

A vertex is added to the queue when visited and removed when the algorithm moves on to its neighbours

Used to find the shortest path for an unweighted graph

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

Depth-First Traversal

A

Graph traversal algorithm that uses a stack to store visited nodes

Starts at the first node, then follows an edge until a dead end is reached, then backtracks up to a vertex and reapplies the algorithm until all nodes are visited

Used in pathfinding algorithms, eg through a maze

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

Depth-First Traversal Types

A

Pre-order = Visiting the root node, then the left subtree, then the right subtree
- Used to copy a tree
- Output is before recursive calls

In-order = Left subtree, then the root node, then the right subtree
- Output a binary search trees contents
- Output is in between recursive calls

Post-order = Left subtree, then the right subtree, then the root node
- Convert infix expression to postfix notation
- Output is after recursive calls

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

Linear Search - O(n)

A

Search algorithm that starts at the beginning of the data structure and compares each data item one by one until the item is found or until the algorithm reaches the end of the data structure

Time complexity = O(n)

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

Binary Search -
O(log2 n)

A

Search algorithm that repeatedly divides the search area (the data structure) into 2 until the data item is found

CAN ONLY BE DONE ON SORTED DATA

2 pointers are used to indicate the upper and lower bounds of the search area

The key of the required record is compared to the key of the record in the middle of the search area - if the needed key is larger than the middle key, the top half of the search area is checked (Bottom = Mid + 1)
If the needed key is smaller than the middle key, the bottom half is searched (Top = Mid - 1)

Time complexity = O(log2 n)

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

Binary Sort - O(n^2)

A

Algorithm that compares neighbouring elements to each other, starting from the start of the list

If the elements are in the wrong order they are swapped around.

The next 2 elements are compared until the end of the list is reached - the process is then repeated at the start of the list until the list is sorted

Time complexity = O(n^2)

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

Merge Sort - O(n log n)

A

Algorithm that uses a divide and conquer approach to sorting a list.

It divides the unsorted lists into halves and keeps going until each sublist contains 1 element, then the algorithm merges each sublist to produce new sorted sublists until only one list is left - the sorted list

Time complexity = O(n log n)

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

Binary Tree Searching

A

Starting from the root node, the required item is compared to the node

  • If they are equal the item has been found
  • If the required item is less than the node, the search moves to the left sub-tree
  • If the required item is greater than the node, the search moves to the right sub-tree

The search continues until the item is found or until a terminator is reached - the item doesnt exist in the tree

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

Dijkstra’s Algorithm

A

An algorithm designed to find the shortest path between a start node and every other node in a WEIGHTED graph

Used in route planning and network optimisation

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

Reverse Polish Notation

A

Expression notation where the operator is placed after the operands it acts on
eg. 3+4 in RPN is 3 4 +
Infix = operator is in between the operands

Benefits include:
- Unambiguous and simpler for a machine to evaluate
- Not limited to a certain number of operations

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