You are on page 1of 17

CHAPTER 4 Searching

Page 1

• The array L is divided at index into 3 parts Page 2 .Binary Search • Let array L be sorted such that L[i] · · · L[j] • The value key can be found quickly with binary search.

Binary Search Algorithm This algorithm searches for the value key in the nondecreasing array L[i].i..j.1 else // search second part i = k + 1 } return -1 // not found } The binary search algorithm is naturally recursive. ..j. If key is found. the algorithm returns -1. which is assumed not to be a valid index. L[j].key Output Parameters: None bsearch(L.key) { while (i <= j) { k = (i + j)/2 if (key == L[k]) // found return k if (key < L[k]) // search first part j = k . If key is not found. but can be easily implemented with iteration: Page 3 . the algorithm returns an index k such that L[k] equals key. Input Parameters: L. .i.

the solution is: Page 4 .Analysis of Binary Search • Let T(n) be the worst case number of times the while loop is executed when the array has n elements. the size of the larger half is always • Thus we have the recurrence • By the main recurrence theorem. • When n = 2k or 2k + 1.

i = 1. • Given L[1] < L[2] < · · · < L[n].A Lower Bound for the Searching Problem • Any comparison-based algorithm that searches a sorted array having n elements requires time in the worst case. equal to. the result of a search must discriminate one of the 2n + 1 possibilities for the value key: key = L[i]. · · · . i = 0. or greater than the element. · · · . • Thus the search process can be modeled as a ternary tree. n. or L[i] < key < L[i + 1]. • 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. n with the convention that: Page 5 .

For each unvisited vertex w adjacent to v. • Visit any vertex v of the graph. visit w and do a depth first search from w. • Depth first search can be described recursively as follows. Page 6 .Depth First Search • Problem: How do we visit every vertex of a graph exactly once? • One way to visit every vertex of a graph exactly once is Depth First Search.

Example: Depth First Search • Consider the graph: The graph can be represented by adjacency lists as: Page 7 .

so we backtrack to the most recently visited vertex 6 that has unvisited adjacent vertices: 7  8  4 • There are no unvisited vertices adjacent to 4.Example: Depth First Search • We start a depth first search at 1 and visit unvisited adjacent vertices in the order they appear in the adjacent lists. so we backtrack to the most recently visited vertex 8 that has unvisited adjacent vertices: 9 and then from 9 to 8 and from 8 to 12 • There are no unvisited vertices adjacent to 12. so we backtrack to the most recently visited vertex 11 that has unvisited adjacent vertices to 10 and then backtrack to 11 which goes to 13 Page 8 . We can keep going as: 1  3  2  6  5 • There are no unvisited vertices adjacent to 5.

Example: Depth First Search • The resulting search is: 1 3 2 6 5 7 8 4 9 12 11 10 13 Page 9 .

The graph is represented using adjacency lists.last for i = 1 to n visit[i] = false dfs_recurs(adj. Page 10 .start) { println(start) visit[start] = true trav = adj[start] while (trav != null) { v = trav.v) trav = trav. the vertex adjacent to i. the algorithm uses an array visit. adj[i] is a reference to the first node in a linked list of nodes representing the vertices adjacent to vertex i. visit[i] is set to true if vertex i has been visited or to false if vertex i has not been visited.start) { n = adj. for the last node in the linked list.start Output Parameters: None dfs(adj. To track visited vertices. Each node has members ver.next } } This algorithm executes a depthfirst search beginning at vertex start in a graph with vertices 1..start) } dfs_recurs(adj. and next. . . the next node in the linked list or null..Depth-First Search Algorithm Input Parameters: adj. n and outputs the vertices in the order in which they are visited.ver if (!visit[v]) dfs_recurs(adj.

v) is called once to set visit[v] true. the cost of the depth first search algorithm is always when the graph is connected. Page 11 . • dfs_recurs(adj. • There are 2E vertices in all the adjacency lists. • The initialization of the visit[] array runs in time • For each vertex v. dfs_recurs(adj. v) traverse each vertex in the adjacency list of v.Analysis of the Depth First Search (dfs) Algorithm • Suppose the graph has E edges and V vertices. • Thus the total time due to all dfs recurs() is • That is.

visit all the vertices adjacent to v. Page 12 . Repeat the process by visiting all the unvisited vertices adjacent to the least recently visited vertex. • After visiting a vertex v.Breadth First Search • Another way to visit every vertex of a graph exactly once is breadth first search.

• Visit all the unvisited vertices adjacent to 3: 2. Page 13 . 4 (assume that this is the adjacency order). 7. • Visit all the unvisited vertices adjacent to 2: 6.Example: Breath First Search • Consider the graph: • Start a breadth first search from vertex 1. • Visit all the unvisited vertices adjacent to 1: 3.

8. 9. 3. 12. 4. • Visit all the unvisited vertices adjacent to 8: 9.Example: Breath First Search • Visit all the unvisited vertices adjacent to 7: 11. 9. • There are no unvisited vertices adjacent to 4. 2. • Visit all the unvisited vertices adjacent to 11: 13. 6. 12. 10. 10. • Visit all the unvisited vertices adjacent to 6: 5. 10. 13. 13. • Thus the vertices are visited in the order of: 1. • There are no unvisited vertices adjacent to 5. 5. 8 (assume that this is the adjacency order). 12 (assume that this is the adjacency order). 7. 11. Page 14 .

The order of visiting is from top to bottom. and within a level. from left to right.Example: Breath First Search • The search can be represented as a diagram. Page 15 .

Breath-First Search Algorithm Page 16 .

• The initialization of the visit[ ] array runs in time (V ) • Each vertex v is enqueued after visit[v] is set to true.Analysis of the Breadth First Search Algorithm • Let the graph have E edges and V vertices. • Each vertex is later dequeued and its adjacent list is traversed. 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 . • As there are a total of 2E vertices in all the lists.