Breadth First Search Flashcards
Graph
Models connections. Composed of nodes and edges
Shortest-path-problem
Solved by breadth first search algorithm
Questions breadth first search can answer
Is there a path from Node A to Node B
What is the shortest path between two nodes
Breadth-first-search algorithm
Uses a queue to search first degree connection, then 2nd degree connections then 3rd degree etc.
Keep a list of nodes you have already checked to avoid an infinite loop.
Add connections of nodes to end of queue
Queue
Data structure like a line. The first in line is the first to leave
Enqueue
Dequeue
Enqueue
Like push()
Add to end of queue
Dequeue
Remove from start of queue
Like pop()
Queue vs stack
Stacks are LIFO - stack of books
Queues are FIFO - line of people
Implementation of graph
A hash of nodes to their 1st degree connections
Directed vs undirected graphs
Directed graphs are 1 way
Breadth first search runtime
O(Vertices+ edges)
Topological sort
A graph where if node A depends on node B, it will appear later in the list
Tree
A type of graph where edges don’t point back to nodes