3. search (part 1) - university of...

35
Computer Science and Software Engineering University of Wisconsin - Platteville Computer Science and Software Engineering University of Wisconsin - Platteville 3. Search (Part 1) CS 3030 Lecture Notes Yan Shi UW-Platteville Read: Textbook Chapter 3.7-3.9,3.12, 4.

Upload: duongminh

Post on 23-May-2018

217 views

Category:

Documents


1 download

TRANSCRIPT

Computer Science and Software Engineering

University of Wisconsin - Platteville

Computer Science and Software Engineering

University of Wisconsin - Platteville

3. Search (Part 1)

CS 3030 Lecture Notes

Yan Shi

UW-Platteville

Read: Textbook Chapter 3.7-3.9,3.12, 4.

Problem Solving as Search

Problem: — Initial state to start

— A description of all possible actions at any state

— Transition model: what each action does

— The goal states

— A path cost function that assign a cost to each path.

Problem solution: an action sequence that leads from the initial state to a goal state.

State Space

The river crossing puzzle

Initial state: PFCG||

Actions: Person can take one item to cross the river

Transition model: fox eats chicken, chicken eats grain if left alone

Goal state: ||PFCG

Path cost function: equal efforts

State space is usually represented as a graph.

Search tree is more often used for problem solving.— All possible paths through a graph.

Can you construct the state space and search tree for the river crossing puzzle?

Search in Search Tree

Start from the initial state (or the goal state)

Expand a state by applying the search actions to that state, generating ALL of its successor states.

These successors are in the next level down of the search tree

The order in which we choose states for expansion is determined by the search strategy— Different strategies result in different behavior

KEY: We want to find the solution while realizing in memory as few as possible of the nodes in the search space.

More Examples

The 8-squares puzzle

— Initial state:

— Goal state:

— Actions: up, down, left, right

Maze:

— Initial state: entrance

— Goal state: exit

— Actions: north, south, east, west

7 2 4

5 6

8 3 1

1 2 3

4 5 6

7 8

A quick review on graph

Graph: G = {V, E}— V: a set of vertices (nodes)— E: a set of edges

Directed graph Path, loop Connected graph and disconnected graph Complete graph Root Tree:

— Root, leaf, parent, child, siblings, ancestors, decedents, branching factor

How to represent a graph?

Adjacency list

— Intuitive

— Less memory for sparse graph

Adjacency matrix

— Faster access: O(1) vs. O(n)

A

BC

E

D

Graph Problem Examples

Konigsberg bridges problem:

— a walk that crosses each bridge once?

Traveling salesperson:

— a route that visits each city once?

Map coloring:

— can map be painted so no two adjacent countries have the same color?

Data-Driven or Goal-Driven Search

Data-driven search:— Start from an initial state and move forward until a

goal is reached— Top-down approach— A.K.A. forward chaining— When initial data is available and goal is not clear

Goal-driven search:— Start at the goal and work back toward a start state— Bottom-up approach— A.K.A. backward chaining— When goal is clear: exit a maze, medical diagnosis

Example: The Towers of Hanoi

Initial State: (123)()()Actions:

Op1: Move disk from peg 1 to peg 2Op2: Move disk from peg 1 to peg 3Op3: Move disk from peg 2 to peg 1Op4: Move disk from peg 2 to peg 3Op5: Move disk from peg 3 to peg 1Op6: Move disk from peg 3 to peg 2

Goal test: ()()(123)Path cost: 1 per step

Example: The Towers of Hanoi

First five levels of search tree:

Goal Tree

A.K.A. and-or tree— Goal: solution

— Subgoal: each step along the way

— And-node: a goals can be achieved only by solving all its subgoals.

— Or-node: a goals can be achieved by solving any of its subgoals.

Leaf nodes are either success nodes or failure nodes.

Example: The Towers of Hanoi

with 4 disks

Example: The Towers of Hanoi

with 4 disks

Properties of Search Methods

Complexity— Time and space

Completeness— Is it guaranteed to find a goal state if one exists?

Optimality (often used to mean admissibility)— Is it guaranteed to find a solution in the quickest time?

Admissibility— Is it guaranteed to find the best solution?

Irrevocability— No backtracking

— Called tentative if there is backtracking

P, NP and NP-hard

In real-world, the search problem can be classified to two classes: P and NP.— class P is the set of all problems for which solutions

with polynomial time behavior have been found.

— class NP is the set of all problems for which solutions with exponential behavior have been found.

If an optimization of the problem cannot be solved in polynomial time, it is called NP-hard.

If a decision problem is both NP and NP-hard, it is NP-complete.

Uninformed Search

Brute-force search (exhaustive search, blind search, generate and test)— Examine every node until it finds a goal

— Simplest form of search

— Assume no additional knowledge other than how to traverse the search tree and detect a leaf and goal node

How many possible states do we have?— TSP of n cities

— Sliding-block puzzle (8-squares)

— Rubik’s cube (3 by 3)

Breadth-First Search

Check all siblings before children;; breadth_first_search: StartState->SUCCESS|FAILURE

Open <- [Start] // states to be considered

Closed <- [] // states that have been considered

while open != []

Next ← first(Open)

Open ← rest(Open) // remove first item from open

if isgoal(Next), return SUCCESS

let Kids = children(Next) - (Open union Closed)

Closed ← Closed union [Next]

Open ← append (Open, Kids)

end-while

return FAILURE

Use a queue

Depth-First Search

Check all decedents before siblings;; depth_first_search: StartState->SUCCESS|FAILURE

Open <- [Start] // states to be considered

Closed <- [] // states that have been considered

while open != []

Next ← first(Open)

Open ← rest(Open) // remove first item from open

if isgoal(Next), return SUCCESS

let Kids = children(Next) - (Open union Closed)

Closed ← Closed union [Next]

Open ← append (Kids, Open)

end-while

return FAILURE

Use a stack

Example: TSP

Appleton Brookfield

Cuba City

Dodgeville

Eau Claire

100

150

180

200150120

240

190

40180

Can we solve TSP using BFS and DFS?

Initial State: ?Actions:

Travel from one city to anotherTransition model:

roadsGoal test:

visit each city exactly once & return to APath cost:

traveling distance

How to order siblings?

Breadth-first vs. Depth-first

BFS DFS

Complexity

Completeness

Optimality

Admissibility

Irrevocability

b is the branching factor of the tree d is the depth of the shallowest goal state reachedm is the depth of the deepest goal state reached

Breadth-first vs. Depth-first

BFS DFS

Complexity Time: O(bd)Space: O(bd)

Time: O(bm)Space: O(bm)

Completeness Yes No

Optimality No No

Admissibility Yes (if no weight) No

Irrevocability Yes Yes

b is the branching factor of the tree d is the depth of the shallowest goal state reachedm is the depth of the deepest goal state reached

Breadth-first vs. Depth-first

When to use which?

Some paths are extremely long

All path are of similar length

All path are of similar length and all lead to a goal state

High branching factor: a state may lead to many different states

Internet search engine?

Variations of BFS and DFS

Uniform-cost search:— Instead of expanding the shallowest node, expand

the node with the smallest path cost.— e.g. Dijkstra’s Algorithm— If all steps are equal, it is the same as BFS— The first goal found is an optimal solution

Depth-limited search:— DFS with depth limit x— Avoid getting stuck in infinitely deep path or loops— will find a solution if it is within the depth limit

Review: Dijkstra’s algorithm

Dijkstra’s algorithm: single source shortest path algorithm

G = (V,E} S = {vertices whose shortest paths from the source is determined} di = best estimate of shortest path to vertex i pi = predecessors

Initialize di and pi, Set S to empty, While there are still vertices in V-S,

— Sort the vertices in V-S according to the current best estimate of their distance from the source,

— Add u, the closest vertex in V-S, to S, — Update all the vertices still in V-S connected to u to a better estimation if

possible

Dijkstra’s algorithm example

1

2

3

4

5

6

7

10

10

5

4

6 1

2

80

Dijkstra’s algorithm example

1

2

3

4

5

6

7

10

10

5

4

6 1

2

80

0+7=7

0+10=10

Dijkstra’s algorithm example

1

2

3

4

5

6

7

10

10

5

4

6 1

2

80

0+7=7

0+10=10 7+6=13

7+10=17

Dijkstra’s algorithm example

1

2

3

4

5

6

7

10

10

5

4

6 1

2

80

0+7=7

0+10=10 7+6=13

7+10=17

Dijkstra’s algorithm example

1

2

3

4

5

6

7

10

10

5

4

6 1

2

80

0+7=7

0+10=10 7+6=13

7+6+1=14 7+6+8=21

Dijkstra’s algorithm example

1

2

3

4

5

6

7

10

10

5

4

6 1

2

80

0+7=7

0+10=10 7+6=13

7+6+1=14 7+6+1+2=16

Dijkstra’s algorithm example

1

2

3

4

5

6

7

10

10

5

4

6 1

2

80

0+7=7

0+10=10 7+6=13

7+6+1=14 7+6+1+2=16

Depth-First Iterative Deepening

DFID, a.k.a. Iterative Deepening Search or IDS Exhaustive search technique that combines depth-first

with breadth-first search:— repeatedly carrying out depth-limit search on the tree, — starting with a depth-first search limited to a depth of 1,

then a depth-first search of depth 2, 3, and so on, until a goal node is found.

Combines the benefit of BFS that it will always find the shortest-step path and of DFS in the efficiency of memory use.

Avoid the problem of DFS that it may be trapped in infinitely deep path.

Is DFID too time consuming?

It is almost as efficient as DFS and BFS because for most trees, the majority of nodes are in the deepest level— All three approaches spend most of their time

examining these node

For a tree of depth d and with a branching factor of b, the total number of nodes is

1 + b + b^2 + b^3 + . . . + b^d = (1-bd+1)/(1-b) O(bd)

The total # of nodes DFID checks is

(d + 1) + b(d) + b^2 (d-1) + b^3(d-2) + . . . + b^d O(bd)

Properties of DFID

Complete?

Optimal?

Time complexity

Space complexity

Backtracking?

Yes

Yes if no weight

O(bd)

O(bd)

Yes