2.3 - Algorithms (Unfinished) Flashcards
What is an Algorithm?
A set of instructions that complete a task when executed
Stack PUSH Algorithm
PROCEDURE AddToStack (item):
IF top == max THEN
stackFull = True
ELSE
top = top + 1
stack[top] = item
ENDIF
ENDPROCEDURE
Stack POP Algorithm
PROCEDURE DeleteFromStack (item):
IF top == min THEN
stackEmpty = True
ELSE
stack[top] = item
top = top - 1
ENDIF
ENDPROCEDURE
Queue PUSH Algorithm
PROCEDURE AddToQueue (item):
IF ((front - rear) + 1) == max THEN
queueFull = True
ELSE
rear = rear - 1
queue[rear] = item
ENDIF
ENDPROCEDURE
Queue POP Algorithm
PROCEDURE DeleteFromQueue (item):
IF front == min THEN
queueEmpty = True
ELSE
queue[front] = item
front = front + 1
ENDIF
ENDPROCEDURE
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
Depth-First Graph Traversal
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
Breadth-First Graph Traversal
Algorithm to Output a Linked List
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
Depth-First Graph Traversal
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
Binary Search Algorithm
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
What is the purpose of Big O notation?
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
What are the different types of Big O Notation, in ascending order of length? (Both Names)
Constant O(1)
Logarithmic O(LogN)
Linear O(N)
Polynomial O(n^2)
Exponential O(2^n)