Posted by: ajreiner | October 12, 2010

Lecture 18: 10/11/10

(Guest post by Alex Reiner)

Yesterday’s class was focused mostly on run time analysis of the Breadth First Search algorithm. As a prep, Atri asked two questions of the class.

First, he asked how we should represent the input G = (V,E). The answer is to  use an adjacency list. Second, he asked how to represent each layer in the BFS algorithm. For this, we will use a linked list, but an array will work also.

Next he showed us the BFS algorithm as follows.

First, some preliminaries. L[i] is the ith layer in the search. CC[u] is true if u has already been explored.We will start at vertex s.

CC[s] = True & CC[u] = False   ∀ u ≠ s
L[i] = {s}
While(L[i] != 0)                                                 <= n iterations
L[i+1] = NULL
For every u ε L[i]                                           <= n iterations
For every (u, v) ε E                                      <= n iterations
If CC[v] = False then
Add v to L[i+1]
CC[v] = True

Atri then proposed to us that this algorithm runs in O(m+n) time.
To do this he first proved it was O(n^3), then O(n^2), and finally O(m+n).

To prove O(n^3), he stated that the will loop can run at most n times, as well as both of the internal for loops. Therefore, it will b n*n*n, or n^3.

To prove O(n^2), he made an observation that for every vertex u ε V, u ε L[i] for at most one i. This can be used to say that over ALL iteration of the while loop, the outer for loop will run <= n times. This cuts the run time down to n*n, or n^2.

Finally, using an observation from an earlier class that 2m = \Sigma_{u\epsilon V}n_u, we can conclude that the inner for loop runs in O(m) time. Combine this with the O(n) time of the outer for loop, we can conclude that the BFS algorithm runs in O(m+n) time.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: