45
Graph Algorithms GAM 376 Robin Burke Winter 2006

Graph Algorithms

Embed Size (px)

DESCRIPTION

Graph Algorithms. GAM 376 Robin Burke Winter 2006. Outline. Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab. Admin. Homework #3 due Monday not today Careers in Technology Tomorrow 5 – 7 pm Midway Games will be there. Admin. - PowerPoint PPT Presentation

Citation preview

Page 1: Graph Algorithms

Graph Algorithms

GAM 376Robin BurkeWinter 2006

Page 2: Graph Algorithms

Outline

Graphs Theory Data structures Graph search

Algorithms DFS BFS

Project #1 Soccer

Break Lab

Page 3: Graph Algorithms

Admin

Homework #3due Monday not today

Careers in TechnologyTomorrow5 – 7 pmMidway Games will be there

Page 4: Graph Algorithms

Admin

Late policynot spelled out in syllabus10% per day for three days

Meaningtomorrow is the last day to turn in

Homework #230% off though

Page 5: Graph Algorithms

Grading

Yes, I am way behind in grading

Page 6: Graph Algorithms

Admin

Final projectfirst build

Same as the exercise from Lab #1but you have to decide what map

you're going to usewhat NPC you're going to create

Deliverablecompiled map

Page 7: Graph Algorithms

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 8: Graph Algorithms

Graph Algorithms in Games

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

• problem solving• "game trees"

Page 9: Graph Algorithms

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 10: Graph Algorithms

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 11: Graph Algorithms

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 12: Graph Algorithms

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 13: Graph Algorithms

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 14: Graph Algorithms

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 15: Graph Algorithms

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 16: Graph Algorithms

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 17: Graph Algorithms

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 18: Graph Algorithms

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 19: Graph Algorithms

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 20: Graph Algorithms

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 21: Graph Algorithms

Note

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

We don't find the best path, some path

Page 22: Graph Algorithms

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 23: Graph Algorithms

DFS

v1 v2

v3 v4 v5

v6 v7

start

target

Page 24: Graph Algorithms

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 25: Graph Algorithms

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 26: Graph Algorithms

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 27: Graph Algorithms

Buckland's implementation

Page 28: Graph Algorithms

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 29: Graph Algorithms

BFS

v1 v2

v3 v4 v5

v6 v7

start

target

Page 30: Graph Algorithms

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 31: Graph Algorithms

Buckland implementation

Page 32: Graph Algorithms

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 33: Graph Algorithms

Next week

More graph funDijkstra's algorithmA*

Scripting / Lua

Page 34: Graph Algorithms

Simple Soccer

Implementation of a 5-player soccer team

Two state machines"Team state""Player state"

Page 35: Graph Algorithms

Team state

kickoff everybody go to default position

offense look for opportunities to get a pass upfield

from the player with the ball defense

go to defensive position transition

offense / defense based on possession of ball

Page 36: Graph Algorithms

Player state

defensechase ball if you're the closest

offensemove toward goal with ball

• pass if possiblewithout ball,

• move to support spot• ask for pass

Page 37: Graph Algorithms

Steering behaviors

chasing the ball steering to support position goalie has special behavior to get in

blocking position

Page 38: Graph Algorithms

Demo

Page 39: Graph Algorithms

SteeringSoccerLab

Not the same as Buckland's Allows multiple team implementations Records the CPU time used by each

AI implementation Don't use Buckland's code

Page 40: Graph Algorithms

How to allow different opponents? Need students to make their own soccer

teams need to run tournament in which teams

compete don't want to recompile when adding a team

How to make extensible code that doesn't need recompilation?

In particular how can I create an instance if I don't know

the name of the class

Page 41: Graph Algorithms

AbstractFactory

Page 42: Graph Algorithms

Registration

How to know which factory object to use? Static instance that registers a name on

instantiation Table associating factories with names Result

dynamic object creation A bit easier in Java using reflection

Page 43: Graph Algorithms

Tournament rules

Round-robin 3 game matches 5 minutes / match

Scoring Lower scoring team

• get a bonus if they used less CPU time• 20% less CPU = 1 point

Ties go to the most efficient team Degenerate strategies disqualified

Page 44: Graph Algorithms

Teams

Team 1 Choryan Gilliam Wiemeyer

Team 2 Abero Gantchev McNulty

Team 3 Flaks Hall

Team 4 Chrostowski Hogan Kenley

Page 45: Graph Algorithms

Break