Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
9.10. Breadth First Search Analysis
Before we continue with other graph algorithms let us analyze the run
time performance of the breadth first search algorithm. The first thing
to observe is that the while loop is executed,
at most, one time for each vertex in the graph . You can
see that this is true because a vertex must be white before it can be
examined and added to the queue. This gives us for the
while loop. The for loop, which is nested inside the while is executed at most once for each edge in the graph,
. The reason is that every vertex is dequeued at most once
and we examine an edge from node to node only
when node is dequeued. This gives us for the
for loop. combining the two loops gives us .
Of course doing the breadth first search is only part of the task.
Following the links from the starting node to the goal node is the other
part of the task. The worst case for this would be if the graph was a
single long chain. In this case traversing through all of the vertices
would be . The normal case is going to be some fraction of
but we would still write .
Finally, at least for this problem, there is the time required to build
the initial graph. We leave the analysis of the buildGraph
function
as an exercise for you.
You have attempted
1 of
1 activities on this page