Presentation is loading. Please wait.

Presentation is loading. Please wait.

On Search Search is one of the most common CS problems. There are lots of different algorithms for searching through a set of alternatives. They can be.

Similar presentations


Presentation on theme: "On Search Search is one of the most common CS problems. There are lots of different algorithms for searching through a set of alternatives. They can be."— Presentation transcript:

1 On Search Search is one of the most common CS problems. There are lots of different algorithms for searching through a set of alternatives. They can be compared in terms of –Time/number of steps needed to find a solution –Space needed by the algorithm –Optimality

2 Searching Large Structures For most interesting problems (game trees, Prolog rules, route finding, circuit layout, etc) you can’t keep the entire data structure in memory. Instead, we select a set of nodes to examine and keep in memory. For each node in memory, we check to see if there is other nodes (successors) that we can visit from this node. A node with no successors is called a goal node or a terminal node.

3 An Example father(Father,Son) :- parent(Father,Son),male(Father) parent(marge,bart). parent(homer,bart). male(homer). father(X,Y)

4 An Example father(Father,Son) :- parent(Father,Son),male(Father) parent(marge,bart). parent(homer,bart). male(homer). father(X,Y) parent(X,Y)

5 An Example father(Father,Son) :- parent(Father,Son),male(Father) parent(marge,bart). parent(homer,bart). male(homer). father(X,Y) parent(X,Y) parent(marge,bart) {X=marge Y=bart}

6 An Example father(Father,Son) :- parent(Father,Son),male(Father) parent(marge,bart). parent(homer,bart). male(homer). father(X,Y) parent(X,Y) parent(marge,bart) {X=marge Y=bart} male(marge) Fails!

7 An Example father(Father,Son) :- parent(Father,Son),male(Father) parent(marge,bart). parent(homer,bart). male(homer). father(X,Y) parent(X,Y) parent(X,bart) {Y=bart} male(marge) Fails! We need to backtrack And undo the binding Of X to marge.

8 An Example father(Father,Son) :- parent(Father,Son),male(Father) parent(marge,bart). parent(homer,bart). male(homer). father(X,Y) parent(X,Y) parent(homer,bart) {X=homer, Y=bart} male(homer) Success!

9 Depth-First Search Rule: –Expand a node. –Choose the “leftmost” child. –If it’s a solution, stop. –Else, expand it. If it has children, follow its leftmost child. –Else, back up to the parent and follow the next leftmost node. Pro: Uses a linear amount of memory. Con: Not guaranteed to find a solution.

10 Breadth-first Search Rule: –Expand a node –Check if any of the children are solutions. –If not, expand each of these nodes and visit those children in order. –Traverse each level of the structure in order. Pro: Will find a solution of one exists. Con: Requires exponential space.

11 Search and Priority Queues Any search strategy can be implemented with a priority queue. Algorithm: –Dequeue first node, check to see if it’s a solution. –Expand node. –Enqueue children. –If we use a normal queue, we get breadth-first search. –If we use a stack, we get depth-first search. –We can get a hybrid search by assigning priorities to nodes.


Download ppt "On Search Search is one of the most common CS problems. There are lots of different algorithms for searching through a set of alternatives. They can be."

Similar presentations


Ads by Google