Fundamentals of Algorithms (C3) Flashcards
(8 cards)
Describe Depth-first searches
- They use a stack, and are used for navigating mazes.
- Start on a node, go the next adjacent node based on alphabetic order.
- Add each node you visit to a stack.
- Once there are no adjacent nodes, pop the top value from the stack, and visit the new top value.
- Repeat until you reach the end.
Describe Breadth-first searches
- These use queues, where each node is visited and added to the queue.
- The next node is taken from the end (so the first one in), as opposed from the top like a stack.
- So essentially same as depth first, but you look at the BOTTOM of the stack (a queue).
Compare depth first and breadth first searches
- Breadth first uses queues, depth first uses queues.
- Breadth first better nodes are closer to starting node, depth first better when solutions further away from source.
- Breadth first worse for decision-making trees.
Describe pre, in, and post order traversal of a tree.
- Pre (node-left-right): Visit root, left subtree, then right subtree.
- In (left-node-right): Traverse left subtree, visit root, traverse right subtree. (add from bottom up)
- In (left-right-node): Traverse left subtree, traverse right subtree, visit node. (add from bottom up)
Uses of RPN
- Expressions are simpler for computers to evaluate using a stack.
- Eliminates the need for brackets in sub-expressions.
How to make bubble sort more efficient?
- Add variable set to true if a swap is made each pass, and change the outer loop so it only runs if the flag variable is set to true.
- After the inner loop, subtract 1 from N
What does * mean in regex?
Zero or more
What does ? mean in regex?
Zero or one