Searching - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Searching

Description:

CHAPTER 4 Searching Algorithm 4.1.1 Binary Search Algorithm 4.2.2 Depth-First Search Algorithm 4.3.2 Breadth-First Search Algorithm 4.3.4 Finding Shortest Path ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 23
Provided by: MScha
Learn more at: http://condor.depaul.edu
Category:
Tags: queen | searching

less

Transcript and Presenter's Notes

Title: Searching


1
CHAPTER 4
  • Searching

2
Algorithm 4.1.1 Binary Search
This algorithm searches for the value key in the
nondecreasing array Li, ... , Lj. If key is
found, the algorithm returns an index k such that
Lk 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 Lk) // found
return k if (key lt Lk) // search first
part j k - 1 else // search second
part i k 1 return -1 // not
found
3
Algorithm 4.2.2 Depth-First Search
This algorithm executes a depth-first 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 adji 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 visiti is set to
true if vertex i has been visited or to false if
vertex i has not been visited.
4
Input Parameters adj,start Output Parameters
None dfs(adj,start) n adj.last for i 1
to n visiti false dfs_recurs(adj,start)
dfs_recurs(adj,start) println(start)
visitstart true trav adjstart
while (trav ! null) v trav.ver if
(!visitv) dfs_recurs(adj,v) trav
trav.next
5
Algorithm 4.3.2 Breadth-First Search
This algorithm executes a breadth-first 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 adji 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, a reference to 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
visiti is set to true if vertex i has been
visited or to false if vertex i has not been
visited. The algorithm uses an initially empty
queue q to store pending current vertices. The
expression q.enqueue(val) adds val to q. The
expression q.front() returns the value at
the front of q but does not remove it. The
expression q.dequeue() removes the item at
the front of q. The expression
q.empty() returns true if q is empty or false if
q is not empty.
6
Input Parameters adj,start Output Parameters
None bfs(adj,start) n adj.last for i 1
to n visiti false visitstart true
println(start) q.enqueue(start) // q is an
initially empty queue while (!q.empty())
current q.front() q.dequeue() trav
adjcurrent while (trav ! null)
v trav.ver if (!visitv)
visitv true println(v)
q.enqueue(v) trav
trav.next
7
Algorithm 4.3.4 Finding Shortest Path Lengths
Using Breadth-First Search
This algorithm finds the length of a shortest
path from the start vertex start to every other
vertex in a graph with vertices 1, ... , n. The
graph is represented using adjacency lists
adji 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, a reference
to the next node in the linked list or null, for
the last node in the linked list. In the array
length, lengthi is set to the length of a
shortest path from start to vertex i if this
length has been computed or to 8 if the length
has not been computed. If there is no path from
start to i, when the algorithm terminates,
lengthi is 8.
8
Input Parameters adj,start Output Parameters
length shortest_paths(adj,start,length) n
adj.last for i 1 to n lengthi 8
lengthstart 0 q.enqueue(start) // q is an
initially empty queue while (!q.empty())
current q.front() q.dequeue() trav
adjcurrent while (trav ! null)
v trav.ver if (lengthv 8)
lengthv 1 lengthcurrent
q.enqueue(v) trav
trav.next
9
Algorithm 4.4.1 Topological Sort
This algorithm computes a topological sort of a
directed acyclic graph with vertices 1, ... , n.
The vertices in the topological sort are stored
in the array ts. The graph is represented using
adjacency lists adji 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
visiti is set to true if vertex i has been
visited or to false if vertex i has not been
visited.
10
Input Parameters adj Output Parameters
ts top_sort(adj,ts) n adj.last // k is
the index in ts where the next vertex is to
be // stored in topological sort. k is assumed
to be global. k n for i 1 to n
visiti false for i 1 to n if
(!visitv) top_sort_recurs(adj,i,ts) top
_sort_recurs(adj,start,ts) visitstart
true trav adjstart while (trav ! null)
v trav.ver if (!visitv)
top_sort_recurs(adj,v,ts) trav
trav.next tsk start k k - 1
11
Algorithm n-Queens, Initial Version
The n-queens problem is to place n queens on an n
n board so that no two queens are in the same
row, column, or diagonal. Using backtracking,
this algorithm outputs all solutions to this
problem. We place queens successively in the
columns beginning in the left column and working
from top to bottom. When it is impossible to
place a queen in a column, we return to the
previous column and move its queen down.
12
n_queens(n) rn_queens(1,n) rn_queens(k,n)
for rowk 1 to n if (position_ok(k,n))
if (k n) for i 1 to n
print(rowi ) println()
else rn_queens(k
1,n) position_ok(k,n) for i 1 to k - 1
// abs is absolute value if (rowk
rowi abs(rowk - rowi) k - i)
return false return true
13
Algorithm 4.5.2 Solving the n-Queens Problem
Using Backtracking
The n-queens problem is to place n queens on an n
n board so that no two queens are in the same
row, column, or diagonal. Using backtracking,
this algorithm outputs all solutions to this
problem. We place queens successively in the
columns beginning in the left column and working
from top to bottom. When it is impossible to
place a queen in a column, we return to the
previous column and move its queen down.
14
The value of rowk is the row where the queen in
column k is placed. The algorithm begins when
n_queens calls rn_queens(1,n). When
rn_queens(k, n) is called, queens
have been properly placed in columns 1 through k
- 1, and rn_queens(k,n) tries to place a queen in
column k. If it is successful and k equals n, it
prints a solution. If it is successful and k does
not equal n, it calls
rn_queens(k 1,n). If it is not successful, it
backtracks by returning to its caller
rn_queens(k - 1 ,n). The value of
row_usedr is true if a queen occupies row r and
false otherwise. The value of ddiag_usedd is
true if a queen occupies ddiag diagonal d and
false otherwise. According to the numbering
system used, the queen in column k, row r, is in
ddiag diagonal n - k r. The value of
udiag_usedd is true if a queen occupies udiag
diagonal d and false otherwise. According to the
numbering system used, the queen in column k, row
r, is in udiag k r - 1. The function
position_ok(k,n) assumes that queens have been
placed in columns 1 through k - 1. It returns
true if the queen in column k does not conflict
with the queens in columns 1 through k- 1 or
false if it does conflict.
15
Input Parameter n Output Parameters
None n_queens(n) for i 1 to n row_usedi
false for i 1 to 2 n - 1 ddiag_usedi
udiag_usedi false rn_queens(1,n) ...
16
... // When rn_queens(k,n) is called, queens have
been // properly placed in columns 1 through k -
1. rn_queens(k,n) for rowk 1 to n if
(position_ok(k,n)) row_usedrowk
true ddiag_usedn - k rowk
true udiag_usedk rowk - 1 true
if (k n) // Output a solution. Stop if
only one // solution is desired.
for i 1 to n print(rowi
) println() else
rn_queens(k 1,n) row_usedrowk
false ddiag_usedn - k rowk
false udiag_usedk rowk - 1 false ...
17
... // position_ok(k,n) returns true if the
queen in column k // does not conflict with the
queens in columns 1 // through k - 1 or false if
it does conflict. position_ok(k,n) return
!(row_usedrowk ddiag_usedn - k
rowk udiag_usedk rowk - 1 )
18
Form of Backtracking Algorithm
Suppose that we solve a problem using
backtracking as in Algorithm 4.5.2 in which the
solution is of the form x1, ... ,
xn. Suppose also that the values of xi are in
the set S (e.g., in Algorithm 4.5.2, S 1, . .
. , n). We require a function bound(k) with
the following property. Whenever
x1, ... , xk - 1 is a partial solution
and xk has been assigned a value, then bound(k)
has to return true if x1, ... , xk is a
partial solution and false otherwise. The key to
writing a useful back- tracking algorithm is to
write an efficient bound function that
eliminates many potential nodes from the search
tree.
19
The general form of a backtracking algorithm is
backtrack(n) rbacktrack(1,n) rbacktrack(k
,n) for each xk ? S if (bound(k))
if (k n) // Output a solution. Stop if
only for i 1 to n // one solution is
desired. print(xi )
println() else rbacktrack(k
1, n)
20
Algorithm 4.5.4 Searching for a Hamiltonian Cycle
This algorithm inputs a graph with vertices 1,
... , n. The graph is represented as an adjacency
matrix adj adjij is true if (i,j) is an edge
or false if it is not an edge. If the graph has a
Hamiltonian cycle, the algorithm computes one
such cycle (x1, ... , xn,
x1). If the graph has no Hamiltonian cycle, the
algorithm returns false and the contents of the
array x are not specified. In the array used,
usedi is true if i has been selected as one of
the vertices in a potential Hamiltonian cycle or
false if i has not been selected. The function
path_ok(adj,k,x) assumes that (x1, ... , xk -
1) is a path from x1 to xk - 1 and that the
vertices x1, ... , xk - 1 are distinct. It
then checks whether xk is different from each
of x1, ... , xk - 1 and whether (xk - 1,
xk) is an edge. If k n, path_ok also checks
whether (xn, x1) is an edge.
21
Input Parameter adj Output Parameter
x hamilton(adj,x) n adj.last x1
1 used1 true for i 2 to n usedi
false rhamilton(adj,2,x) rhamilton(adj,k,x)
n adj.last for xk 2 to n if
(path_ok(adj,k,x)) usedxk true
if (k n rhamilton(adj,k 1,x))
return true usedxk false return
false ...
22
... path_ok(adj,k,x) n adj.last if
(usedxk) return false if (k lt n)
return adjxk - 1xk else return
adjxn - 1xn adjx1xn
Write a Comment
User Comments (0)
About PowerShow.com