graph algorithms gam 376 robin burke fall 2006. outline graphs theory data structures graph search...

46
Graph Algorithms GAM 376 Robin Burke Fall 2006

Upload: simon-randall

Post on 06-Jan-2018

221 views

Category:

Documents


0 download

DESCRIPTION

Homework #2 Graded Yes, I am way behind in grading

TRANSCRIPT

Page 1: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Graph Algorithms

GAM 376Robin BurkeFall 2006

Page 2: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Outline

Graphs Theory Data structures Graph search

Algorithms DFS BFS

Project #1 Soccer

Break Lab

Page 3: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Homework #2

Graded Yes, I am way behind in grading

Page 4: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Graph Algorithms

Very important for real world problems: The airport system is a graph. What is the

best flight from one city to another? Class prerequisites can be represented as a

graph. What is a valid course order? Traffic flow can be modeled with a graph.

What are the shortest routes? Traveling Salesman Problem: What is the

best order to visit a list of cities in a graph?

Page 5: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Graph Algorithms in Games

Many problems reduce to graphspath findingtech trees in strategy gamesstate space search

• problem solving• "game trees"

Page 6: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

What is a Graph? A graph G = (V,E) consists of a set of vertices V

and a set of edges E. Each edge is a pair (v,w) where v and w are vertices.

If the edges are ordered (indicated with arrows in a picture of a graph), the graph is “directed” and (v,w) != (w,v).

Edges can also have weights associated with them.

Vertex w is “adjacent” to v if and only if (v,w) is an edge in E.

Page 7: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

An Example Graph

v1 v2

v3 v4 v5

v6 v7

v1, v2, v3, v4, v5, v6, and v7 are vertices. (v1,v2) is an edge in thegraph and thus v2 is adjacent to v1. The graph is directed.

Page 8: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Definitions

A “path” is a sequence of vertices w1, w2, w3, …, wn such that (wi, wi+1) are edges in the graph.

The “length” of the path is the number of edges (n-1).

A “simple” path is one where all vertices are distinct, except perhaps the first and last.

Page 9: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

An Example Graph

v1 v2

v3 v4 v5

v6 v7

The sequence v1, v2, v5, v4, v3, v6 is a path. The length is 5.It is a simple path.

Page 10: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

More Definitions

A “cycle” in a directed graph is a path such that the first and last vertices are the same.

A directed graph is “acyclic” if it has no cycles. This is sometimes referred to as a DAG (directed acyclic graph).

The previous graph is a DAG (convince yourself of this!).

Page 11: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

A Modified Graph

v1 v2

v3 v4 v5

v6 v7

The sequence v1, v2, v5, v4, v3, v1 is a cycle. We had tomake one change to this graph to achieve this cycle. So, thisgraph is not acyclic.

Page 12: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

More Definitions…

An undirected graph is “connected” if there is a path from every vertex to every other vertex. A directed graph with this property is called

“strongly connected”. If the directed graph is not strongly connected, but the underlying undirected graph is connected, then the graph is “weakly connected”.

A “complete” graph is a graph in which there is an edge between every pair of vertices.

The prior graphs have been weakly connected and have not been complete.

Page 13: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Graph Representation

v1 v2

v3 v4 v5

v6 v7 v1v2v3v4v5v6v7

v1 v2 v3 v4 v5 v6 v70 1 1 1 0 0 00 0 0 1 1 0 0

We can use an “adjacencymatrix” representation.

For each edge (u,v) we set A[u][v] to true;else it is false. If there are weights associatedwith the edges, insert those instead.

Page 14: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Representation

The adjacency matrix representation requires O(V2) space. This is fine if the graph is complete, or nearly complete.

But what if it is sparse (has few edges)? Then we can use an “adjacency list”

representation instead. This will require O(V+E) space.

Page 15: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Adjacency List

v1 v2

v3 v4 v5

v6 v7 v1 v2 v4 v3v2 v4 v5v3 v6v4 v6 v7 v3v5 v4 v7v6v7 v6

We can use an “adjacencylist” representation.

For each vertex we keep a list of adjacent vertices.If there are weights associated with the edges, that information must be stored as well.

Page 16: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Graph search

Problemis there a path from v to w?what is the shortest / best path?

• optimalitywhat is a plausible path that I can

compute quickly?• bounded rationality

Page 17: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

General search algorithm

Start with "frontier" = { (v,v) }

Until frontier is empty remove an edge (n,m) from the frontier set mark n as parent of m mark m as visited if m = w,

• return otherwise

• for each edge <i,j> from m• add (i, j) to the frontier

• if j not previously visited

Page 18: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Note

We don't say how to pick a node to "expand"

We don't find the best path, some path

Page 19: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Depth First Search

Last-in first-out We continue expanding the most

recent edge until we run out of edgesno edges out orall edges point to visited nodes

Then we "backtrack" to the next edge and keep going

Page 20: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

DFS

v1 v2

v3 v4 v5

v6 v7

start

target

Page 21: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Characteristics

Can easily get side-tracked into non-optimal paths

Very sensitive to the order in which edges are added

Guaranteed to find a path if one exists Low memory costs

only have to keep track of current path nodes fully explored can be discarded

Complexity Time: O(E) Space: O(1)

Page 22: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Optimal DFS

Really expensive Start with

bestPath = { } bestCost = "frontier" = { <{ }, (v,v)>}

Repeat until frontier is empty remove a pair <P, > from the frontier set if n = w Add w to P If cost of P is less than bestCost

• bestPath = P record n as "visited" add n to the path P for each edge <n,m> from n

• add <P, m> to the frontier• if m not previously visited• or if previous path to m was longer

Page 23: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Iterative Deepening DFS

Add a parameter k Only search for path of lengths <= k Start with k = 1

while solution not found• do DFS to depth k

Sounds wasteful searches repeated over and over but actually not too bad

• more nodes on the frontier finds optimal path less memory than BFS

Page 24: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Buckland's implementation

Page 25: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Breadth-first search

First-in first-out Expand nodes in the order in which

they are addeddon't expand "two steps" awayuntil you've expanded all of the "one

step" nodes

Page 26: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

BFS

v1 v2

v3 v4 v5

v6 v7

start

target

Page 27: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Characteristics

Will find shortest path Won't get lost in deep trees Can be memory-intensive

frontier can become very largeespecially if branching factor is high

ComplexityTime: O(E)Space: O(E)

Page 28: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Buckland implementation

Page 29: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

What if edges have weight?

If edges have weight then we might want the lowest weight path a path with more nodes might have lower

weight Example

a path around the lava pit has more steps but you have more health at the end compared to the path that goes through the

lava pit We will cover this next week

Page 30: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

What if edges have weight?

If edges have weight then we might want the lowest weight path a path with more nodes might have lower

weight Example

a path around the lava pit has more steps but you have more health at the end compared to the path that goes through the

lava pit We will cover this next week

Page 31: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Weighted graph

v1 v2

v3 v4 v5

v6 v7

1

1

1

2

21

5 3

3

23

1

Page 32: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Edge relaxation

It is not enough to knownode n is reachable via path P

We need to know the cost to reach node n via path Pbecause path Q might be cheaper

In which casewe discard path Pit can't enter into a solution

Page 33: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Djikstra's Algorithm

Use a priority queue a data structure in which the item with the smallest

"value" is always first items can be added in any order

Use the "value" of an edge as the total cost of the path through that edge always expand the node with the least cost so far

If an edge leads to a previously expanded node compare costs

• if greater, ignore edge• if lesser, replace path and estimate at node with new

value "Greedy" algorithm

Page 34: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Djikstra's algorithm

v1 v2

v3 v4 v5

v6 v7

1

1

1

2

21

5 3

3

23

1

3 1

4

34

65

5

5

Page 35: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Characteristics

We have discovered the cheapest route to every node nice side effect

Can be deceived by early gains garden-path phenomenon

Guaranteed to find the shortest path Complexity

O(|E| log |E|) not too bad

Page 36: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Priority Queue

This algorithm depends totally on the priority queue

Various techniques to implementsorted list

• yuckheap

• bettermany proposed variants

Page 37: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Different Example

Problem: Visit too many nodes, some clearly out of the question

Page 38: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Better Solution: Heuristic

Use heuristics to guide the search Heuristic: estimation or “hunch” of how to

search for a solution We define a heuristic function:

h(n) = “estimate of the cost of the cheapest path from the starting node to the goal node"

We could use this instead of our greedy "lowest cost so far" technique

Page 39: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Use a Heuristic for cost

Heuristic: minimize h(n) = “Euclidean distance to destination” Problem: not optimal (through Rimmici Viicea and Pitesti is shorter)

Page 40: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

The A* Search

Difficulty: we want to still be able to generate the path with minimum cost

A* is an algorithm that: Uses heuristic to guide search While ensuring that it will compute a path

with minimum cost

• A* computes the function f(n) = g(n) + h(n)

“actual cost”

“estimated cost”

Page 41: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

A* f(n) is the priority (controls which node to expand) f(n) = g(n) + h(n)

g(n) = “cost from the starting node to reach n” h(n) = “estimate of the cost of the cheapest path

from n to the goal node”

1015

20

2015

518

25

33

ng(n)

h(n)

Page 42: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Example

A*: minimize f(n) = g(n) + h(n)

Page 43: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Properties of A*

A* generates an optimal solution if h(n) is an admissible heuristic and the search space is a tree: h(n) is admissible if it never overestimates the

cost to reach the destination node A* generates an optimal solution if h(n) is a consistent

heuristic and the search space is a graph: h(n) is consistent if for every node n and for every

successor node n’ of n: h(n) ≤ c(n,n’) + h(n’) n

n’

dh(n)

c(n,n’) h(n’)

Page 44: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Admissible Heuristics

A heuristic is admissible if it is too optimistic, estimating the cost to be smaller than it actually is.

Example: for maps

• Euclidean distance• no path can be shorter than this

• but this requires a square root for grid maps

• Manhattan distance is sometimes used

Page 45: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Inadmissable Heuristics

If a heuristic sometimes overestimates the cost of a path then A* is not guaranteed to be optimal it might miss paths that are valid

On the other hand a stronger (higher-valued) heuristic is better it focuses the search more Djikstra is just A* with h(n) = 0 for all n

Some path planners use inadmissable heuristics on purpose if benefits of quicker planning are worth more than the

cost of the occasional missed opportunity

Page 46: Graph Algorithms GAM 376 Robin Burke Fall 2006. Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab

Buckland implementation