4.3 Fundamentals of algorithms

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/10

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

11 Terms

1
New cards

Explain the basic principle of a depth-first search and give two uses

- each branch of the graph is fully explored (left to right) before backtracking to fully explore the next branch

- uses: maze navigation, detecting cycles in graphs

<p>- each branch of the graph is fully explored (left to right) before backtracking to fully explore the next branch</p><p>- uses: maze navigation, detecting cycles in graphs</p>
2
New cards

Explain how a stack is used to implement a depth-first search

- add the first node (e.g. the root of a rooted tree) to the result and push onto the stack (using A from diagram)

- observe the nodes that A has a direct connection to (B, C, D) and push the highest alphabetical node onto the stack

- repeat this process for B until the branch is completely explored at I

- when the end of a branch is reached (e.g. at I, F, G, H) then POP THIS node off the stack (backtrack to the previous node) and explore other undiscovered adjacent nodes

- process continues until the stack is empty (NOT when the last node has been discovered, as that whole branch will need to be popped off the stack, so we always backtrack back to the first node

- return the result (probably a list)

<p>- add the first node (e.g. the root of a rooted tree) to the result and push onto the stack (using A from diagram)</p><p>- observe the nodes that A has a direct connection to (B, C, D) and push the highest alphabetical node onto the stack</p><p>- repeat this process for B until the branch is completely explored at I</p><p>- when the end of a branch is reached (e.g. at I, F, G, H) then POP THIS node off the stack (backtrack to the previous node) and explore other undiscovered adjacent nodes</p><p>- process continues until the stack is empty (NOT when the last node has been discovered, as that whole branch will need to be popped off the stack, so we always backtrack back to the first node</p><p>- return the result (probably a list)</p>
3
New cards

Explain the basic principle of breadth-first search and give two uses

- each node in the graph is fully explored before moving on to the next node

(- a node is considered fully explored when all of its neighbours have been completely explored)

- uses: shortest path for an unweighted graph, level order traversal to generate hierarchal charts

<p>- each node in the graph is fully explored before moving on to the next node</p><p>(- a node is considered fully explored when all of its neighbours have been completely explored)</p><p>- uses: shortest path for an unweighted graph, level order traversal to generate hierarchal charts</p>
4
New cards

Explain how a queue is used to implement a breadth-first search

- add to starting node (e.g. the root of a tree) TO THE RESULT (here, A) and enqueue ADJACENT nodes in alphabetic order (B, C, D)

- whenever a node is enqueued add it to the result (order of visited)

- the start node is inherently always completely explored after the first step

- dequeue the node at the front of the queue (B) and add its undiscovered nodes to the queue (E, F)

-> example queue state here: C, D, E, F

- repeat this process (if a node has no undiscovered nodes i.e. on the bottom level it is immediately completely explored and process repeats)

<p>- add to starting node (e.g. the root of a tree) TO THE RESULT (here, A) and enqueue ADJACENT nodes in alphabetic order (B, C, D)</p><p>- whenever a node is enqueued add it to the result (order of visited)</p><p>- the start node is inherently always completely explored after the first step</p><p>- dequeue the node at the front of the queue (B) and add its undiscovered nodes to the queue (E, F)</p><p>-&gt; example queue state here: C, D, E, F</p><p>- repeat this process (if a node has no undiscovered nodes i.e. on the bottom level it is immediately completely explored and process repeats)</p>
5
New cards

Pre order traversal and use

- LEFT of the line around the nodes

(root, left, right)

-use: copying a tree, task scheduling

<p>- LEFT of the line around the nodes</p><p>(root, left, right)</p><p>-use: copying a tree, task scheduling</p>
6
New cards

Post order traversal

- RIGHT of the line around the nodes

(left, right, root)

-use: producing postfix (RPN) from an expression tree, emptyting a tree with no backtracking

<p>- RIGHT of the line around the nodes</p><p>(left, right, root)</p><p>-use: producing postfix (RPN) from an expression tree, emptyting a tree with no backtracking</p>
7
New cards

In order traversal

- when the line hits the BOTTOM of each node

- uses: binary search tree (ouputs contents in ascending order) -> can only be used on binary trees

8
New cards

Remember when converting infix to RPN

- conversion via post-order traversal of expression tree

- the order of the numbers must stay the same

9
New cards

Explain how a stack can be used to evaluate RPN

- initialise an empty stack

- read the RPN left to right sequentially:

-> if an operand is encountered then push it onto the stack

-> if an opcode (e.g. +) is read then pop 2 elements from the top of the stack, perform the operation with the element popped second first in sequence, and push the result back onto the stack

- repeat until stack contains only one element - the evaluated RPN

10
New cards

Advantages of using RPN over infix

- no need for parentheses as order of operations is implicitly expressed

-> removes ambiguity

- simple algorithm (easy) for computer to evaluate using a stack due to no backtracking

11
New cards

Where is RPN used in practice?

- in interpreters based on stack functions, e.g. Postscript, bytecode

- in spreadsheets to evaluate user inputs without needing to handle order of operation parsing