Graphs Flashcards
examples of brute sorting algorithms for graphs
depth first search
breadth first search
examples of decrease and conquer algorithms for graphs
topological sort
which graphing algorithm can deal with cycles
topological sort
what is a path
sequence of vertices connected by edges
what is a cyccle
path whose first and last vertices are the same
what is an adjacency list
an array with each element containing a vertex
vertex has a linked list of all the nodes that it has a link to
does depth first search work for noth directed and undirected graphs
yes
what data structure is used when implementing depth first seatch
stack
how does depth first search algorithm work
follow edges as far as possible
if we encounter a node that has already been visited, retrace steps
time complexity of dfs
O(V+E)
vertices for the seen for loop
edges for the adjacency for loop
space complexity of dfs
O(V)
seen boolean array
what is dfs useful for
seeing if there is a path that exists between two nodes
what data structure is used for breadth first search
queue
what is bfs used for
to find shortest path between two nodes
time complexity of bf s
O(V+E)
how does bfs work
start at a node
visit all its neighbours
visit all their neighbours and so on
in which order foes BFS examine vertices
in increasing distance from the source vertex
what does it mean if a graph is directed
the edges flow in one direction
examples of directed graphs irl
games - legal moves
web pages - hyperlinks
streets - one way
how do adjacency lists work for directed graphs
edges are listed in the directions they exist
time complexity of topological sort
O(V+E)
when is topological sorting useful
when some events have to occur before others can eg class prequisties
what data structure has a topological order by nature
tree
how does topological sort algorithm work
pick an unvisited node
do dfs exploring only unvisited nodes
add topological ordering back in reverse order