Date post: | 04-Jun-2018 |
Category: |
Documents |
Upload: | frankjamison |
View: | 221 times |
Download: | 0 times |
of 17
8/13/2019 07b-DepthAndBreathSearch(AdditionalNotes)
1/17
Page 1
CHAPTER 4
Searching
8/13/2019 07b-DepthAndBreathSearch(AdditionalNotes)
2/17
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.
The array L is divided at index
into 3 parts
8/13/2019 07b-DepthAndBreathSearch(AdditionalNotes)
3/17
Page 3
Binary Search Algorithm
This algorithm searches for the value keyin the nondecreasing array L[i], ..., L[j]. If keyis found, the algorithm returns an index ksuch that L[k] equalskey. If keyis not found, the algorithm returns -1, which is assumed not tobe a valid index.
Input Parameters: L,i,j,keyOutput Parameters: None
bsearch(L,i,j,key) {while (i
8/13/2019 07b-DepthAndBreathSearch(AdditionalNotes)
4/17
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.
When n = 2k or 2k + 1, the size of the larger half isalways
Thus we have the recurrence
By the main recurrence theorem, the solution is:
8/13/2019 07b-DepthAndBreathSearch(AdditionalNotes)
5/17
Page 5
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:
8/13/2019 07b-DepthAndBreathSearch(AdditionalNotes)
6/17
Page 6
Depth First Search
Problem: How do we visit every vertex of a graph exactlyonce?
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.
8/13/2019 07b-DepthAndBreathSearch(AdditionalNotes)
7/17Page 7
Example: Depth First SearchConsider the graph:
The graph can be represented byadjacency lists as:
8/13/2019 07b-DepthAndBreathSearch(AdditionalNotes)
8/17Page 8
Example: Depth First Search
We start a depth first search at 1 and visit unvisitedadjacent vertices in the order they appear in the adjacentlists. We can keep going as: 1 3265
There are no unvisited vertices adjacent to 5, so webacktrack to the most recently visited vertex 6 that hasunvisited adjacent vertices: 784
There are no unvisited vertices adjacent to 4, so webacktrack to the most recently visited vertex 8 that hasunvisited adjacent vertices: 9 and then from 9 to 8 andfrom 8 to 12
There are no unvisited vertices adjacent to 12, so webacktrack to the most recently visited vertex 11 that hasunvisited adjacent vertices to 10 and then backtrack to11 which goes to 13
8/13/2019 07b-DepthAndBreathSearch(AdditionalNotes)
9/17Page 9
Example: Depth First Search
The resulting search is: 1 3 2 6 5 7 8 4 9
12 11 10 13
8/13/2019 07b-DepthAndBreathSearch(AdditionalNotes)
10/17Page 10
Depth-First Search Algorithm
This algorithm executes a depth-first search beginning at vertex startin a graph with vertices 1, ... , nandoutputs the vertices in the order inwhich they are visited. The graph isrepresented using adjacency lists;
adj[i] is a reference to the first nodein a linked list of nodes representingthe vertices adjacent to vertex i. Eachnode has members ver, the vertexadjacent to i, and next, the nextnode in the linked list or null, for the
last node in the linked list. To trackvisited vertices, the algorithm usesan array visit; visit[i] is set to true ifvertex ihas been visited or to false ifvertex ihas not been visited.
Input Parameters: adj,start
Output Parameters: Nonedfs(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
}
}
8/13/2019 07b-DepthAndBreathSearch(AdditionalNotes)
11/17Page 11
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 searchalgorithm is always when the graph isconnected.
8/13/2019 07b-DepthAndBreathSearch(AdditionalNotes)
12/17Page 12
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.
8/13/2019 07b-DepthAndBreathSearch(AdditionalNotes)
13/17Page 13
Example: Breath First Search Consider the graph:
Start a breadth first search from
vertex 1.
Visit all the unvisited verticesadjacent 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.
8/13/2019 07b-DepthAndBreathSearch(AdditionalNotes)
14/17
Page 14
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.
8/13/2019 07b-DepthAndBreathSearch(AdditionalNotes)
15/17
Page 15
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.
8/13/2019 07b-DepthAndBreathSearch(AdditionalNotes)
16/17
Page 16
Breath-First Search Algorithm
8/13/2019 07b-DepthAndBreathSearch(AdditionalNotes)
17/17
Page 17
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
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
Therefore the algorithm always runs in time
when the graph is connected.
)(V
)(E
)( VE