You are on page 1of 17

CHAPTER 4 Searching

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

Binary Search Algorithm


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

Page 3

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.


When n = 2k or 2k + 1, the size of the larger half is always Thus we have the recurrence By the main recurrence theorem, the solution is:

Page 4

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.

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

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. Depth first search can be described recursively as follows. Visit any vertex v of the graph. For each unvisited vertex w adjacent to v, visit w and do a depth first search from w.

Page 6

Example: Depth First Search


Consider the graph:
The graph can be represented by adjacency lists as:

Page 7

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. We can keep going as: 1 3 2 6 5 There are no unvisited vertices adjacent to 5, 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, 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

Example: Depth First Search


The resulting search is: 1 3 2 6 5 7 8 4 9 12 11 10 13

Page 9

Depth-First Search Algorithm


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

Page 10

Analysis of the Depth First Search (dfs) Algorithm

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

Breadth First Search


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

Page 12

Example: Breath First Search


Consider the graph: Start a breadth first search from vertex 1. Visit all the unvisited vertices adjacent to 1: 3. Visit all the unvisited vertices adjacent to 3: 2, 7, 4 (assume that this is the adjacency order).

Visit all the unvisited vertices adjacent to 2: 6.


Page 13

Example: Breath First Search


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

Page 14

Example: Breath First Search


The search can be represented as a diagram. The order of visiting is from top to bottom, and within a level, from left to right.

Page 15

Breath-First Search Algorithm

Page 16

Analysis of the Breadth First Search Algorithm


Let the graph have E edges and V vertices. The initialization of the visit[ ] array runs in time (V )

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

You might also like