Algorithms Flashcards

1
Q

What type of data structure does a depth-first traversal use?

A

1) Stack
2) When a node is visited place it on the stack
3) Once every node connected to that node has been visited, pop that node from the stack
4) In theory, should reach the bottom of the graph before coming back up to the start (LIFO)

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

What type of data structure does a breadth-first traversal use?

A

1)Queue
2)Place the first node onto the queue and every node connected to that onto the queue
3)That node has then been fully discovered, so dequeue it
4) Repeat this process until the whole thing is traversed

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

What is a Pre-Order Traversal used for?

A

Copying trees - can be performed on any tree

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

How do you perform a pre-order traversal?

A

Place a marker on the left side of each leaf
(Root, Left, Right)

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

What is an In-Order Traversal used for?

A

To search the contents of a binary tree and output them in the correct order
(Only used on binary trees)

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

How do you perform an In-Order Traversal?

A

Place a marker on the bottom of each leaf

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

What is a Post-Order Traversal used for?

A

Useful for converting Infix to Reverse Polish Notation
(Can be used on any tree)

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

How do you perform a Post Order traversal?

A

Place a marker on the right of each leaf

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
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
10
Q

What is a linear search?

A

A search to find an item in an unordered list. You search each item until you find the one you are searching for

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

What are the Pros/Cons of Linear Search?

A

Pros -
Easy to program

Cons -
Very inefficient so rarely used in the real world

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

What is the time complexity of a linear search?

A

O(N)

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

What is a binary search?

A

A search to find an item in an ordered list. Works by looking at the midpoint of a list and determining if the target is higher or lower

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

What is the time complexity of a binary search?

A

O(logN)

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

What is a bubble sort?

A

An algorithm that sorts a list of data by swapping the position of adjacent items to order them

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

What is the time complexity of a bubble sort?

A

O(N^2) - Very Inefficient

17
Q

What is a merge sort?

A

An algorithm that sorts arrays by splitting them into smaller lists and reforming them - Divide and Conquer Method

18
Q

What is the time complexity of a merge sort?

A

O(nlogn)

19
Q

What is Dijkstra’s shortest path algorithm?

A

An algorithm used to find the shortest path from a starting node to each other node in a network

20
Q

What is meant by a recursive subroutine?

A

A subroutine which calls itself

21
Q

What is Reverse Polish Notation?

A

A postfix way of writing expressions, eliminating any brackets and order of execution.

22
Q

What are the uses of RPN?

A

They are used on a stack, meaning it is used in any interpreters based upon a stack

23
Q

Describe how a stack is used to evaluate an RPN expression

A

1) Algorithm moves along the expression, pushing each operand onto the stack
2) When an operator is reached, two items are popped off the stack and applied to the operator
3) The result is then stored on the stack
4) This repeats until the end of the expression, in which the answer should be calculated - pop the answer off the stack

24
Q

Explain why Reverse Polish Notation is used over infix

A

Brackets aren’t required, so its easier to write the program
Operators appear in the order required by computation, makes it easier for the computer to evaluate

25
Q

Why does a bubble sort have the time complexity O(N^2)

A

Because in each pass through there are n items, and there will be at most n passes through the list

26
Q

Why does a linear search have the time complexity O(N)

A

As the list gets bigger, the time taken to search the list increases at the same rate
It looks at each item in the list, so if there are n items in the list - at worst there are n comparisons

27
Q

What is the usage of Dijkstra’s shortest path algorithm?

A

1) Routers deploy the algorithm when trying to route packets by using the shortest path
2) Also used in satelite systems that display the fastest route to a location

28
Q

Pseudocode for Linear Search

A

Do Until Found == True or Count == Array[Count]
If Target == Array[Count]
Found = True
Else
Count = Count +1
END IF

If Found = True
OUTPUT Target

ELSE
  OUTPUT Target Not Found
29
Q

Pseudocode for Binary Search

A

Do until Found = TRUE or TopPointer < BottomPointer
If Array(MidPoint) = Target
Found = True
If Array(MidPoint) > Target
THEN TopPointer = MidPoint - 1

If Array(MidPoint) < Target
BottomPointer = MidPoint + 1