(P1) Fundamentals of Algorithms Flashcards

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

What is graph traversal?

A

The process of visiting each vertex in a graph.

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

What are the two graph traversals?

A

Depth-first and breadth-first.

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

Describe depth-first traversal.

A

A branch is fully explored before backtracking.
Uses a stack and is used navigating mazes.

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

Describe breadth-first traversal.

A

A node is fully explored before venturing on the next node.
Uses a queue.
Used for determining the shortest path on an unweighted graph.

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

What is an algorithm?

A

A set of instructions which completes a task in a finite time and always terminates.

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

What is a tree?

A

A connected acyclic graph.

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

Describe tree traversal.

A

Process of visiting each node in a tree.
Unique to trees and must start at the root.

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

Where should the dot be drawn for a pre-order traversal?

A

Left.

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

Where should the dot be drawn for a in-order traversal?

A

Bottom.

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

Where should the dot be drawn for a post-order traversal?

A

Right.

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

What can pre-order traversal be used for?

A

Copying trees.

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

What can an in-order traversal be used for?

A

Used in binary trees to outputs the contents of a binary tree in ascending order.

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

What can post-order traversals be used for?

A

Infix to RPN conversions.
Producing a postfix expression from an expression tree.
Emptying a tree.

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

How are Infix to RPN and binary trees related?

A

Infix to RPN involve the making and traversing of a binary tree.

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

What is an opcode?

A

An operator.

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

What is an operand?

A

Data which is applied to the operator.

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

When creating an expression tree, what should always be the parent node and the child nodes?

A

Parents should be the opcode.
Children should be the operands.

18
Q

What is produced from performing an in-order traversal on an expression tree?

A

Infix expression.

19
Q

What is produced from performing an post-order traversal on an expression tree?

A

Postfix expression.

20
Q

How do you work out a postfix expression?

A

Perform the calculation of the opcode and only the two preceding operands and put back into expression. Repeat.

21
Q

What notation do humans use?

A

Infix notation.

22
Q

What does RPN stand for?

A

Reverse Polish Notation.

23
Q

What is RPN?

A

A postfix way of writing expressions.
Eliminates need for brackets and any confusion of the order of execution.
Opcode is written after the operands.

24
Q

What data structure can be used to evaluate postfix equations?

A

Stacks.

25
Q

Why is RPN ideal for interpreters?

A

RPN can be executed on a stack, making it ideal for interpreters which are based on a stack such as Bytecode or PostScript.

26
Q

What is the pseudocode for a RPN calculation?

A

Stack1 <— Stack
RPN <— Array
Op2 <— Single
Op1 <— Single
Result <— Single
For i = 0 to RPN.count - 1
If RPN (i) = operand
Stack1.push (RPN(i))
ElseIf RPN (i) = opcode
Op2 = Stack1.pop
Op1 = Stack1.pop
Result = Perform(RPN(i), Op1, Op2)
End If
End For
Print Stack1.peek

27
Q

What are the three different searching algorithms?

A

Linear search, binary search and binary tree search.

28
Q

Describe a linear search.

A

Conducted on an unordered list.
Hugh time complexity.
Has one loop.
Time complexity of O(n).

29
Q

What is pseudocode for a Linear search?

A

LinearSearch(Target, ArrayofValues)
Boolean Found
Integer Count
Found <— FALSE
Count <— 0
Do Until Found == TRYE or Count == ArrayofValues Count
If Target == ArrayofValues(Count)
Found <— True
Else
Count <— Count + 1
End If
Loop
If Found = True
Output Target found at Count
Else
Output Target not found
End if

30
Q

Describe a binary search.

A

Used on any ordered list.
Works by looking at the midpoint of a list and determining if the target is higher or lower.
Time complexity is O(log(n))

31
Q

What sorting algorithms can be used to order a list?

A

Merge or bubble sort.

32
Q

What is the pseudocode for a binary search?

A

BinarySearch(Target, ArrayorValues)
Integer TopPointer
Integer BottomPointer
Integer Midpoint
Boolean Found
Found <— FALSE
BottomPointer <— 0
TopPointer <— ArrayorValues Count - 1

Do Until Found = True or TopPointer < BottomPointer
Midpoint = int mid TopPointer, BottomPointer
If ArrayofValues(Midpoint) = Target
Found = TRUE
ElseIf ArrayofValues(Midpoint) > Target
TopPointer = Midpointer - 1
ElseIf ArrayofValues(Midpoint) < Target
BottomPointer = Midpoint + 1
End If
Loop
If Found = TRUE
Output Target found at Midpoint
Else
Output Target not found
End if

33
Q

What method can a binary search be completed through?

A

Recursion.

34
Q

What is the time complexity for a binary tree search?

A

O(log(n))

35
Q

What is Big O notation?

A

A way of classifying algorithms based on time and space complexity.

36
Q

Describe bubble sort.

A

The idea of swapping the position of adjacent items to order them.
Time complexity of O(n^2)
Very inefficient.

37
Q

What is pseudocode for a bubble sort?

A

Integer Max
String Temp
Boolean Swapped
Integer Passes
Max <— List.Count - 1
Swapped <— TRUE
Passes <— 0

Do Until Swapped == FALSE or Max == 0
Swapped <— FALSE
Max <— Max - 1
Passes <— Passes + 1
For a = 0 to Max
If List(a) > List(a + 1)
Temp <— List(a)
List(a) <— List(a + 1)
List(a + 1) <— Temp
Swapped <— TRUE
End If
Next
Loop

OUTPUT Passes
OUTPUT List

38
Q

Describe merge sort.

A

Orders arrays by splitting them into smaller lists and them reforming them
Divide and conquer
Quicker than bubble sort
Time complexity of O(nlog(n))

39
Q

What is an optimisation algorithm?

A

Finds the best possible solution to the problem posed.

40
Q

Give an example of an optimisation algorithm.

A

Dijkstra’s algorithm.

41
Q

What is the purpose of Dijksta’s algorithm?

A

Finds the shortest path between two nodes in a graph.