Professional Documents
Culture Documents
Page 1
Binary Search
Let array L be sorted such that L[i] L[j] The value key can be found quickly with binary search. The array L is divided at index
into 3 parts
Page 2
Page 3
Page 4
Comparison-based means the only operation to obtain information is to compare a key to an element and find out if the key is less than, equal to, or greater than the element. Thus the search process can be modeled as a ternary tree. Given L[1] < L[2] < < L[n], the result of a search must discriminate one of the 2n + 1 possibilities for the value key: key = L[i], i = 1, , n; or L[i] < key < L[i + 1], i = 0, , n
with the convention that:
Page 5
Page 6
Page 7
Page 9
Page 10
Suppose the graph has E edges and V vertices. The initialization of the visit[] array runs in time For each vertex v, dfs_recurs(adj, v) is called once to set visit[v] true. dfs_recurs(adj, v) traverse each vertex in the adjacency list of v. There are 2E vertices in all the adjacency lists. Thus the total time due to all dfs recurs() is That is, the cost of the depth first search algorithm is always when the graph is connected.
Page 11
Page 12
Page 14
Page 15
Page 16
Each vertex v is enqueued after visit[v] is set to true. Each vertex is later dequeued and its adjacent list is traversed. As there are a total of 2E vertices in all the lists, Thus the cost of the while loops is ( E ) Therefore the algorithm always runs in time ( E V ) when the graph is connected.
Page 17