2.3 - Algorithms (Unfinished) Flashcards

1
Q

What is an Algorithm?

A

A set of instructions that complete a task when executed

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

Stack PUSH Algorithm

A

PROCEDURE AddToStack (item):
IF top == max THEN
stackFull = True
ELSE
top = top + 1
stack[top] = item
ENDIF
ENDPROCEDURE

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

Stack POP Algorithm

A

PROCEDURE DeleteFromStack (item):
IF top == min THEN
stackEmpty = True
ELSE
stack[top] = item
top = top - 1
ENDIF
ENDPROCEDURE

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

Queue PUSH Algorithm

A

PROCEDURE AddToQueue (item):
IF ((front - rear) + 1) == max THEN
queueFull = True
ELSE
rear = rear - 1
queue[rear] = item
ENDIF
ENDPROCEDURE

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

Queue POP Algorithm

A

PROCEDURE DeleteFromQueue (item):
IF front == min THEN
queueEmpty = True
ELSE
queue[front] = item
front = front + 1
ENDIF
ENDPROCEDURE

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

What is this code for?

FUNCTION ______(graph, node, visited):
markAllVertices (notVisited)
createStack()
start = currentNode
markAsVisited(start)
pushIntoStack(start)
WHILE StackIsEmpty() == false
popFromStack(currentNode)
WHILE allNodesVisited() == false
markAsVisited(currentNode)
//following sub-routine pushes all nodes connected to
//currentNode AND that are unvisited
pushUnvisitedAdjacents()
ENDWHILE
ENDWHILE
ENDFUNCTION

A

Depth-First Graph Traversal

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

What is this code for?

FUNCTION bfs(graph, node):
markAllVertices (notVisited)
createQueue()
start = currentNode
markAsVisited(start)
pushIntoQueue(start)
WHILE QueueIsEmpty() == false
popFromQueue(currentNode)
WHILE allNodesVisited() == false
markAsVisited(currentNode)
//following sub-routine pushes all nodes connected to
//currentNode AND that are unvisited
pushUnvisitedAdjacents()
ENDWHILE
ENDWHILE
ENDFUNCTION

A

Breadth-First Graph Traversal

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

Algorithm to Output a Linked List

A

FUNCTION OutputLinkedListInOrder ():
Pointer = start value
REPEAT
Go to node(Pointer value)
OUTPUT data at node
Pointer = value of next item Pointer at node
UNTIL Pointer = 0
ENDFUNCTION

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

Depth-First Graph Traversal

A

FUNCTION ______(graph, node, visited):
markAllVertices (notVisited)
createStack()
start = currentNode
markAsVisited(start)
pushIntoStack(start)
WHILE StackIsEmpty() == false
popFromStack(currentNode)
WHILE allNodesVisited() == false
markAsVisited(currentNode)
//following sub-routine pushes all nodes connected to
//currentNode AND that are unvisited
pushUnvisitedAdjacents()
ENDWHILE
ENDWHILE
ENDFUNCTION

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

Binary Search Algorithm

A

FUNCTION BinarySearch (A[0..N-1], value, low, high)

 if high < low THEN
      return error_message
 endif

 mid = (low + high) / 2

 if A[mid] > value THEN
      return BinarySearch(A, value, low, mid-1)
 elseif A[mid] < value THEN
       return BinarySearch(A, value, mid +1, high)

 else
      return mid
 endif

endfunction

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

What is the purpose of Big O notation?

A

To:

Evaluate the complexity of the algorithm

Show how the time / memory / resources increase as the data size increases

Evaluate worst case scenario for the algorithm

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

What are the different types of Big O Notation, in ascending order of length? (Both Names)

A

Constant O(1)
Logarithmic O(LogN)
Linear O(N)
Polynomial O(n^2)
Exponential O(2^n)

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