Download ppt - 5 searching

Transcript
Page 1: 5 searching

Searching in a Graph

Jeff Edmonds

York University COSC 3101LectureLecture 55

Page 2: 5 searching

Generic SearchBreadth First Search

Dijkstra's Shortest Paths AlgorithmDepth First Search

Linear OrderNetwork Flow

Linear Programming

Page 3: 5 searching

Graph

a

c

b Node ~ city or computer

Edge ~ road or network

Undirected or Directed

A surprisingly large number of problems in computer science can be expressed as a graph theory problem.

Page 4: 5 searching

Graph Search

Specification: Reachability-from-single-source s•Preconditions: The input is a graph G (either directed or undirected) and a source node s. •Postconditions: Output all the nodes u that are reachable by a path in G from s.

Page 5: 5 searching

Graph Search

Basic Steps:

su

Suppose you know that u is reachable from s

v

& there is an edge from u to v

You know that v is reachable from s

Build up a set of reachable nodes.

Page 6: 5 searching

Graph Search

s

reachable

fuk reachable

dvw reachable

ehd reachable

bt

reachable

dvw reachable

ehd reachable

bt

•How do we keep track of all of this information?

•How do we avoid cycling?

Page 7: 5 searching

sa

c

hk

fi

lm

j

eb

gd

Graph Search

Page 8: 5 searching

sa

c

hk

fi

lm

j

eb

gd

Graph Search

Page 9: 5 searching

sa

c

hk

fi

lm

j

eb

gd

Graph Search

Page 10: 5 searching

sa

c

hk

fi

m

j

eb

gd

We know found nodes are reachable from s because we have traced out a path.

If a node has been handled, then all of its neighbors have been found.

Graph Search

l

Page 11: 5 searching

sa

c

hk

fi

m

j

eb

gd

We know found nodes are reachable from s because we have traced out a path.

If a node has been handled, then all of its neighbors have been found.

Graph Search

l

Handle some foundNotHandled nodei.e. find its neighborsDon’t refind a node.

Page 12: 5 searching

We know found nodes are reachable from s because we have traced out a path.

If a node has been handled, then all of its neighbors have been found.

Graph Search

Handle some foundNotHandled nodei.e. find its neighbors

sa

c

hk

fi

m

j

eb

gd

l

Page 13: 5 searching

We know found nodes are reachable from s because we have traced out a path.

If a node has been handled, then all of its neighbors have been found.

Graph Search

# of found nodes.

sa

c

hk

fi

m

j

eb

gd

l

measureprogress

# of handled nodes.Might not increase.

Page 14: 5 searching

We know found nodes are reachable from s because we have traced out a path.

If a node has been handled, then all of its neighbors have been found.

Graph Search

Node s is foundNotHandled

sa

c

hk

fi

lm

j

eb

gd

Other nodes NotFound

Page 15: 5 searching

We know found nodes are reachable from s because we have traced out a path.

If a node has been handled, then all of its neighbors have been found.

Graph Search

All nodes found.Exit

a

c

hk

fi

m

j

eb

gd

l

When can’t make any more progress.No. Might not find all.

When all found nodes are have been handled.Handle some foundNotHandled node

s

Page 16: 5 searching
Page 17: 5 searching

Graph Search

We know found nodes are reachable from s because we have traced out a path.

If a node has been handled, then all of its neighbors have been found.

a

c

hk

fi

m

j

eb

gd

lExit All found nodes are handled.

Exit

•Postconditions: Output all the nodes u that are reachable by a path in G from s.

Output Found nodes

Page 18: 5 searching

We know found nodes are reachable from s because we have traced out a path.

If a node has been handled, then all of its neighbors have been found.

Graph Search

Exit

a

c

hk

fi

m

j

eb

gd

lAll found nodes are handled.

Exit Found nodes are reachable from s. Reachable nodes have been Found.

•Postconditions:

Page 19: 5 searching

We know found nodes are reachable from s because we have traced out a path.

If a node has been handled, then all of its neighbors have been found.

Graph Search

Exit

a

c

hk

fi

m

j

eb

gd

lAll found nodes are handled.

Exit Reachable nodes have been Found. •Postconditions:

found = handled

handled

Notfound

[A B] = [B A]NotFound nodes not reachable.

Page 20: 5 searching

Graph SearchSpecification of Reachability-from-single-source s

•Preconditions: The input is a graph G (either directed or undirected) and a source node s. •Postconditions: Output all the nodes u that are reachable by a path in G from s.

EndingInitial ConditionsMake Progress

Maintain Loop InvDefine Exit ConditionDefine Step

Define Measure of Progress

Define Loop Invariants

Define Problem

km

79 km

to school

Exit

Exit

79 km 75 km

Exit

Exit

0 km Exit

Page 21: 5 searching

Graph Search# of handled nodes.

Handle some foundNotHandled node

Time = O(n)

fuk

O(n) iterations, but iteration takes more than O(1)

O(n) neighbors= O(n2)Could be fewer?

Each edge visited, times.2= O(E)

Size = O(E)Linear time.

Page 22: 5 searching

Graph Search

Handle some foundNotHandled node

•Queue: Handle in order found.

•Breadth-First Search

•Stack: Handle most recently found

•Depth-First Search

•Priority Queue: Handle node that seems to be closest to s.

•Shortest (Weighted) Paths:

Page 23: 5 searching

Breadth First Search

Pre & Post Conditions

Algorithm

Page 24: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

Page 25: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

s

Page 26: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

a

bgd

Page 27: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

bgd

cf

Page 28: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

bg

cfme

Page 29: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

bcfmej

Page 30: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

cfmej

Page 31: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

fmejh

i

Page 32: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

mejh

i

Page 33: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

ejh

il

Page 34: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

jh

il

Page 35: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

h

il

Page 36: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

ilk

Page 37: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

lk

Page 38: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

k

Page 39: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

Page 40: 5 searching

BFS

What order are the nodes found?

i.e. Breadth First Search

Algorithm

So far, the nodes have been found in order of length from s.

Page 41: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

Page 42: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

s

d=0

d=0

Page 43: 5 searching

BFS FoundNot Handled

Queue

d=0a

bgd

d=1

s

a

c

h

k

f

i

l

m

j

e

b

gd

d=0d=1

Page 44: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

a

bgd

d=0d=1

d=1

Page 45: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

bgd

cf

d=0d=1

d=2

d=1

d=2

Page 46: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

bg

cfme

d=0d=1

d=2

d=1

d=2

Page 47: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queued=0

d=1

d=2

b

j

cfme

d=1

d=2

Page 48: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queued=0

d=1

d=2

j

cfme

d=1

d=2

Page 49: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

cfmej

d=0d=1

d=2

d=2

Page 50: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

fmejh

i

d=0d=1

d=2

d=3

d=2

d=3

Page 51: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

mejh

i

d=0d=1

d=2

d=3

d=2

d=3

Page 52: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

ejh

il

d=0d=1

d=2

d=3

d=2

d=3

Page 53: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

jh

il

d=0d=1

d=2

d=3

d=2

d=3

Page 54: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

h

il

d=0d=1

d=2

d=3

d=2

d=3

Page 55: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

h

d=0d=1

d=2

d=3

il

d=3

Page 56: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

ilk

d=0d=1

d=2

d=3d=4

d=3

d=4

Page 57: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

lk

d=0d=1

d=2

d=3d=4

d=3

d=4

Page 58: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

k

d=0d=1

d=2

d=3d=4

d=3

d=4

Page 59: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queue

k

d=0d=1

d=2

d=3d=4

d=4

Page 60: 5 searching

BFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Queued=0

d=1

d=2

d=3d=4

d=4d=5

Page 61: 5 searching

BFS

What order are the nodes found?

So far, the nodes have been found in order of length from s.

Post Conditions: Finds a shortest path from sto each node v and its length.

To prove path is shortest:Prove there is a path of this length.Prove there are no shorter paths.

Give a path(witness)

Page 62: 5 searching

Basic Steps:

su

The shortest path to uhas length d

v

& there is an edge from u to v

There is a path to v with length d+1.

BFS

Page 63: 5 searching

BFS

What order are the nodes found?

So far, the nodes have been found in order of length from s.

Post Conditions: Finds a shortest path from sto each node v and its length.

When we find v, we know there isn't a shorter path to it because ?

Prove there are no shorter paths.

Otherwise, we would have found it already.

Page 64: 5 searching

BFSData structure for storing tree: For each node v, store (v) to be parent of v.

Page 65: 5 searching

Basic Steps:

su

Path to u

v

& there is an edge from u to v

Parent of v is

(v)

(v) = u.

BFS

Page 66: 5 searching
Page 67: 5 searching

Graph Search

Handle some foundNotHandled node

•Queue: Handle in order found.

•Breadth-First Search

•Stack: Handle most recently found

•Depth-First Search

•Priority Queue: Handle node that seems to be closest to s.

•Shortest (Weighted) Paths:

Page 68: 5 searching

Dijkstra's Shortest-Weighted Paths

Specification: Dijkstra's Shortest-Weighted Paths Reachability-from-single-source s

•Preconditions: The input is a graph G (either directed or undirected) with positive edge weights and a source node s. •Postconditions: Finds a shortest weighted path from s to each node v and its length.

Page 69: 5 searching

Dijkstra's Shortest-Weighted Paths

s

u

v

100

11

1

1w

r

Length of shortest path from s to u?

4

Page 70: 5 searching

So far, the nodes have been found in order of length from s.

BFS

s

u

v

100

11

1

1w

r

Which node is found first?

Is the same true for Dijkstra's Alg?

Page 71: 5 searching

So far, the nodes have been found in order of length from s.

s

u

v

100

11

1

1w

r

It has the longest path from s.Which node is found first?

Is the same true for Dijkstra's Alg?

BFS

Page 72: 5 searching

So far, the nodes have been found in order of length from s.

s

u

v

100

11

1

1w

r

In what order do we handle the FoundNotHandled nodes?

handled

Dijkstra's

Handle node that seems to be closest to s.

Page 73: 5 searching

To prove path is shortest:Prove there is a path of this length.Prove there are no shorter paths.

Give a path(witness)

Dijkstra'sSo far, the nodes have been handled in order of length from s.

Post Conditions: Finds a shortest weighted path from s to each node v and its length.

Page 74: 5 searching

To prove path is shortest:Prove there is a path of this length.Prove there are no shorter paths.

Dijkstra's

When we handle v, we know there isn't a shorter path to it because ?

Otherwise, we would have handled it already.

Post Conditions: Finds a shortest weighted path from s to each node v and its length.

So far, the nodes have been handled in order of length from s.

Page 75: 5 searching

Basic Steps:

u

The shortest of handled paths to u has length d(u)

Dijkstra's

& there is an edge from u to v

w<u,v>

vs

The shortest of handled paths to v has length d(v)

The shortest known path to v has length

min( d(v), d(u)+w<u,v> ).

Handle node that seems to be closest to s.Need to keep approximate shortest distances.

Page 76: 5 searching

Page 77: 5 searching

Dijkstra'sThe shortest of handled paths

to v has length d(v)

uvs

Page 78: 5 searching

Dijkstra's

uvs

Handled nodes Found nodes

Handled Edges?

The shortest of handled paths to v has length d(v)

Page 79: 5 searching

Dijkstra's

uvs

Handled Nodes Found Nodes

Handled Edges

The shortest of handled paths to v has length d(v)

Handled Paths?

Page 80: 5 searching

Dijkstra's

uvs

Handled Nodes Found Nodes

Handled Edges

The shortest of handled paths to v has length d(v)

Handled Paths

Page 81: 5 searching

Dijkstra's

uvs

Handled Nodes

The shortest of handled paths to v has length d(v)

d(v) is the length of the shortest handled paths to v.

For handled u,d(u) is the length of the

shortest paths to u.

NotFound

d(v’)=

Page 82: 5 searching

Exit

Page 83: 5 searching

Dijkstra'sFor handled w,

d(w) is the length of the shortest paths to w.

Handle node with smallest d(u).

For handled u, d(u) is the length of the shortest paths to u.

d(v) is the length of the shortest handled

path to v.

Page 84: 5 searching

Dijkstra'sFor handled w,

d(w) is the length of the shortest paths to w.

Handle node with smallest d(u).

For handled u, d(u) is the length of the shortest paths to u.

d(u) is the length of the shortest handled

path to u.

d(u)

Page 85: 5 searching

Dijkstra'sFor handled w,

d(w) is the length of the shortest paths to w.

Handle node with smallest d(u).

For handled u, d(u) is the length of the shortest paths to u.

d(u)

d(u) is the length of the shortest handled

path to u.

First not handled node in path.

Page 86: 5 searching

Dijkstra'sFor handled w,

d(w) is the length of the shortest paths to w.

Handle node with smallest d(u).

For handled u, d(u) is the length of the shortest paths to u.

d(u)

First not handled node in path.d(u’)

d(u) & d(u’) are the length of the shortest handled

paths to u and u’.

Page 87: 5 searching

Dijkstra'sd(v) is the length

of the shortest handled path to v.

For handled w,d(w) is the length of the

shortest paths to w.

Handle node with smallest d(u).

d(v) is the length of the shortest handled path to v.

d(u)

d(v)

w<u,v>

d(v) = min( d(v), d(u)+w<u,v> ).

Page 88: 5 searching

Time = O(E)Each edge visited, times.2

?

Dijkstra's

This number of times following an edge.

Must update Priority Queue.

Takes time O(log(n))

Time = O(E log(n))

For each update, d(v) = min( d(v), d(u)+w<u,v> ).

Heap

Page 89: 5 searching

Dijkstra's Shortest-Weighted PathsSpecification: Dijkstra's Shortest-Weighted Paths Reachability-from-single-source s

•Preconditions: The input is a graph G (either directed or undirected) with positive edge weights and a source node s. •Postconditions: Finds a shortest weighted path from s to each node v and its length.

EndingInitial ConditionsMake Progress

Maintain Loop InvDefine Exit ConditionDefine Step

Define Measure of Progress

Define Loop Invariants

Define Problem

km

79 km

to school

Exit

Exit

79 km 75 km

Exit

Exit

0 km Exit

Page 90: 5 searching

Dijkstra's

Page 91: 5 searching

Dijkstra's

Page 92: 5 searching

Graph Search

Handle some foundNotHandled node

•Queue: Handle in order found.

•Breadth-First Search

•Stack: Handle most recently found

•Depth-First Search

•Priority Queue: Handle node that seems to be closest to s.

•Shortest (Weighted) Paths:

Page 93: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Stack

Page 94: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Stack

s

Page 95: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Stack

a

bgd

Page 96: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Stack

a

mgd

e

Page 97: 5 searching

DFS

Page 98: 5 searching

DFS

The nodes in the stack form a path starting at s.

Page 99: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Stack <node,# edges>

Page 100: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Stack <node,# edges>

s,0

Page 101: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,0

Page 102: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,0

Page 103: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,1h,0

Page 104: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,1h,1k,0

Page 105: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,1h,1

Tree Edge

Path on Stack

Page 106: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,1

Page 107: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2i,0

Page 108: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2i,1

Cross Edgeto handled node

Page 109: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2i,2

Page 110: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2i,3l,0

Page 111: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2i,3l,1

Page 112: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2i,3

Page 113: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2i,4g,0

Page 114: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2i,4g,1j,0

Page 115: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2i,4g,1j,1

Back Edgeto node on Stack

Forms a cycle

Page 116: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2i,4g,1j,2m,0

Page 117: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2i,4g,1j,2m,1

Page 118: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2i,4g,1j,2

Page 119: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2i,4g,1

Page 120: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2i,4

Page 121: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2i,5f,0

Page 122: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2i,5f,1

Page 123: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2i,5

Page 124: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,2

Page 125: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1c,3

Page 126: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,1

Page 127: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

a,2

Page 128: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,1

FoundNot Handled

Stack <node,# edges>

Page 129: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,2

FoundNot Handled

Stack <node,# edges>

d,1

Page 130: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,2

FoundNot Handled

Stack <node,# edges>

d,2

Page 131: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,2

FoundNot Handled

Stack <node,# edges>

d,3e,0

Page 132: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,2

FoundNot Handled

Stack <node,# edges>

d,3e,1

Page 133: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,2

FoundNot Handled

Stack <node,# edges>

d,3

Page 134: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,2

FoundNot Handled

Stack <node,# edges>

Page 135: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,3

FoundNot Handled

Stack <node,# edges>

Page 136: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,4

FoundNot Handled

Stack <node,# edges>

b,0

Page 137: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,4

FoundNot Handled

Stack <node,# edges>

b,1

Page 138: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,4

FoundNot Handled

Stack <node,# edges>

b,2

Page 139: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,4

FoundNot Handled

Stack <node,# edges>

b,3

Page 140: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

s,4

FoundNot Handled

Stack <node,# edges>

Page 141: 5 searching

DFS

s

a

c

h

k

f

i

l

m

j

e

b

gd

FoundNot Handled

Stack <node,# edges>

Page 142: 5 searching
Page 143: 5 searching
Page 144: 5 searching

Linear Order

Page 145: 5 searching
Page 146: 5 searching

Ingredients:•Instances: The possible inputs to the problem. •Solutions for Instance: Each instance has an exponentially large set of solutions. •Cost of Solution: Each solution has an easy to compute cost or value.

Specification•Preconditions: The input is one instance.•Postconditions: An valid solution with optimal cost. (minimum or maximum)

Optimization Problems

Page 147: 5 searching

Specification: Network Flow•Preconditions: The input is a network G directed edges with capacities,a source node s, and a sink node t. •Postconditions: Finds a Maximum Flow F.

Network Flow

Page 148: 5 searching

Network Flow

No leaks, not extra flow

Value of flow = flow out of s = flow into t = flow across any cut

Page 149: 5 searching

Network Flow

Page 150: 5 searching

Network Flow

•Given Flow F•Construct Augmenting Graph GF

•Find path P using BFS, DFS, or generic search algorithm•Let w be the max amount flowcan increase along path P. •Increase flow along path P by w. i.e newF = oldF + w × P

Page 151: 5 searching

Network Flow

Stop

Page 152: 5 searching

Hill ClimbingWe have a valid solution.(not necessarily optimal)

Take a step that goes up.

measure

progressValue of our solution.

Problems:

Exit Cant take a step that goes up.

Running time?

Initially have the “zero” solution

Local Max

Global Max

Can our Network Flow Algorithm get stuck in a local maximum?

Make small local changes to your solution toconstruct a slightly better solution.

Page 153: 5 searching

Network Flow

Previous Input

Previous Output

Same Input

Yes! Our Network Flow Algorithm can get stuck.

Worse Output

Need only one example.

Page 154: 5 searching

Network Flow

Page 155: 5 searching

Network Flow

Page 156: 5 searching

Network Flow

Page 157: 5 searching

Network Flow

Page 158: 5 searching

Network Flow

Previous Input

Previous Output

Same Input

Yes! Our Network Flow Algorithm can get stuck.

Worse Output

Need only one example.

Page 159: 5 searching

Hill ClimbingAvoiding getting stuck

in a local maximum

Good ExecutionBad Execution

•Made better choices of direction•Hard

•Back up an retry•Exponential time

•Define a bigger step

Page 160: 5 searching

Hill ClimbingDifferent Solutions

Current Solution

Alg defines to where alg can stepi.e. what small local

changes can be made to current solution

This defines the topography

Page 161: 5 searching

Hill ClimbingDifferent Solutions

Current Solution

Define a slightlybigger step

This defines the topography

Perhaps removes some local maxima

Page 162: 5 searching

Network Flow

Previous Input

Previous Output

Same Input

Yes! Our Network Flow Algorithm can get stuck.

Worse Output

Need only one example.Mistake?

Putting 2 through this edge

Page 163: 5 searching

Network Flow

u v0/75

0/10

Flow Graph

u v75

10

Augmentation Graph75-21=54

21+10=31

u v21/75

10

Flow Graph

u v

Augmentation Graph

2

Page 164: 5 searching

Network Flow

Page 165: 5 searching

Network Flow

Page 166: 5 searching

Network Flow

Page 167: 5 searching

Network Flow

Page 168: 5 searching

Network Flow

Page 169: 5 searching

Network Flow

Page 170: 5 searching
Page 171: 5 searching

Hill Climbing

Page 172: 5 searching
Page 173: 5 searching

Network Flow

Page 174: 5 searching
Page 175: 5 searching
Page 176: 5 searching

Linear Programming

Page 177: 5 searching

A Hotdog

A combination of A combination of pork, grain, and pork, grain, and sawdust, …sawdust, …

Constraints: Constraints: •Amount of Amount of moisturemoisture•Amount of Amount of protean,protean,•……

Page 178: 5 searching

The Hotdog Problem

Given today’s prices,what is a fast algorithm to find the cheapest hotdog?

Page 179: 5 searching

There are deep ideas within the simplicity.

=

AbstractionAbstraction

Goal: Understand and think about complex things in simple ways.

There are deep ideas within the simplicity. Rudich www.discretemath.com

Page 180: 5 searching

Abstract Out Essential Details

Cost: 29, 8, 1, 2

Amount to add: x1, x2, x3, x4

pork

grai

nw

ater

saw

dust

3x1 + 4x2 – 7x3 + 8x4 122x1 - 8x2 + 4x3 - 3x4 24

-8x1 + 2x2 – 3x3 - 9x4 8x1 + 2x2 + 9x3 - 3x4 31

Constraints: Constraints: •moisturemoisture•protean,protean,•……

29x1 + 8x2 + 1x3 + 2x4Cost of Hotdog:Cost of Hotdog:

Page 181: 5 searching

3x1 + 4x2 – 7x3 + 8x4 122x1 - 8x2 + 4x3 - 3x4 24

-8x1 + 2x2 – 3x3 - 9x4 8x1 + 2x2 + 9x3 - 3x4 31

29x1 + 8x2 + 1x3 + 2x4

Subject to:Subject to:

Minimize:Minimize:

Abstract Out Essential Details

Page 182: 5 searching

A Fast Algorithm

For decades people thought that there was no fast algorithm.

Then one was found!

Theoretical Computer Sciencefinds new algorithms every day.

3x1 + 4x2 – 7x3 + 8x4 12

2x1 - 8x2 + 4x3 - 3x4 24

-8x1 + 2x2 – 3x3 - 9x4 8x1 + 2x2 + 9x3 - 3x4 31

29x1 + 8x2 + 1x3 + 2x4

Subject to:Subject to:

Minimize:Minimize:

Page 183: 5 searching
Page 184: 5 searching

End


Recommended