graph algorithms gam 376 robin burke winter 200. homework #2 no 10s most common mistake not handling...
TRANSCRIPT
![Page 1: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/1.jpg)
Graph Algorithms
GAM 376
Robin Burke
Winter 200
![Page 2: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/2.jpg)
Homework #2
No 10s Most common mistake
not handling the possibilities associated with damage
Big no-noStates with no exit conditionsHandling explosion without state blip
![Page 3: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/3.jpg)
My solution
States Patrol Combat-Normal Combat-HideSolid Combat-HideWeak Combat-Close Combat-Closer Combat-Damaged Combat-DamagedHide Combat-
DamagedCloser Hit Explode Dead
Conditions Player enters tank's field of view Player fires Player hides behind a non-destructible
object Player hides behind a dest. object Player is not hidden Tank / player distance > 50 meters Tank / player distance < 50 meters and >
10 Tank / player distance < 10 meters Player hits with grenade or RPG Player health < 0 Animation complete Tank health < 0 Tank health < 25%
![Page 4: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/4.jpg)
P
CN CHS
CHW
CC CC2
H
E
(S or F) and D50 F and HsF and Hw
D50 DMed D50D10
D10
DMed
V and D50 V and
D50
Hs
Hw
from any state
A
Aout and Dead
D
Aout
Aout and ~Dead
return toprevious
from any state except EKO
CDfrom any state
Dmg
CDH
CDC
D10
DMed
Hs orHw
V and (DMed or D50)
V and DMed
V and D10
V and D10
V and D10V and DMed
![Page 5: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/5.jpg)
Homework #3
Buckland’s APIyes, it isn’t very well documentedvery, very typical of production game
code Cannot wait for the world to become
better documented investigate the code and its usage find cluesapply logic
![Page 6: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/6.jpg)
Example PointToLocalSpace
look at a function call (Obstacle avoidance) //calculate this obstacle's position in local space Vector2D LocalPos = PointToLocalSpace((*curOb)->Pos(), m_pVehicle->Heading(), m_pVehicle->Side(), m_pVehicle->Pos());
look at the function itselfinline Vector2D PointToLocalSpace(const Vector2D &point, Vector2D &AgentHeading, Vector2D &AgentSide, Vector2D &AgentPosition){ //make a copy of the point Vector2D TransPoint = point; //create a transformation matrix C2DMatrix matTransform; double Tx = -AgentPosition.Dot(AgentHeading); double Ty = -AgentPosition.Dot(AgentSide); //create the transformation matrix matTransform._11(AgentHeading.x); matTransform._12(AgentSide.x); matTransform._21(AgentHeading.y); matTransform._22(AgentSide.y); matTransform._31(Tx); matTransform._32(Ty); //now transform the vertices matTransform.TransformVector2Ds(TransPoint); return TransPoint;}
![Page 7: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/7.jpg)
Clues
Calling conventionfirst the data being convertedthen information about the vehicle
• local space
Return valueconverted point
Vehicle APIincludes heading, side (?) and position
![Page 8: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/8.jpg)
Outline
Graphs Theory Data structures Graph search
Algorithms DFS BFS
![Page 9: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/9.jpg)
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 10: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/10.jpg)
Graph Algorithms in Games
Many problems reduce to graphspath findingtech trees in strategy gamesstate space search
• problem solving• "game trees"
![Page 11: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/11.jpg)
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 12: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/12.jpg)
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 13: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/13.jpg)
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 14: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/14.jpg)
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 15: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/15.jpg)
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 16: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/16.jpg)
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 17: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/17.jpg)
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 18: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/18.jpg)
Graph Representation
v1 v2
v3 v4 v5
v6 v7 v1v2v3
v4v5v6v7
v1 v2 v3 v4 v5 v6 v7
0 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.
![Page 19: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/19.jpg)
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 20: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/20.jpg)
Adjacency List
v1 v2
v3 v4 v5
v6 v7 v1 v2 v4 v3v2 v4 v5v3 v6
v4 v6 v7 v3v5 v4 v7
v6v7 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 21: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/21.jpg)
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 22: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/22.jpg)
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 23: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/23.jpg)
Note
We don't say how to pick a node to "expand"
We don't find the best path, some path
![Page 24: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/24.jpg)
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 25: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/25.jpg)
DFS
v1 v2
v3 v4 v5
v6 v7
start
target
![Page 26: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/26.jpg)
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
Typical Complexity Time: O(E/2) Space: O(1)
• assuming paths are short relative to the size of the graph
![Page 27: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/27.jpg)
Optimality
DFS does not find the shortest pathreturns the first path it encounters
If we want the shortest pathwe have to keep goinguntil we have expanded everything
![Page 28: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/28.jpg)
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 29: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/29.jpg)
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 30: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/30.jpg)
Buckland's implementation
![Page 31: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/31.jpg)
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 32: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/32.jpg)
BFS
v1 v2
v3 v4 v5
v6 v7
start
target
![Page 33: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/33.jpg)
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
Typical ComplexityTime: O(p*b)Space: O(E)
![Page 34: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/34.jpg)
Buckland implementation
![Page 35: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/35.jpg)
ExerciseNodes Edges1 1-4, 1-3, 1-223 3-4, 3-54 4-65 5-2, 5-66 6-3
1
65
432
Path from node1 to node6 depth-first breadth-first iterative deepening dfs
![Page 36: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/36.jpg)
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
![Page 37: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/37.jpg)
Weighted graph
v1 v2
v3 v4 v5
v6 v7
1
1
1
2
21
5 3
3
23
1
v1v2v3
v4v5v6v7
v1 v2 v3 v4 v5 v6 v7
0 1 1 5 0 0 00 0 0 3 1 0 00 0 0 0 0 1 0
0 0 1 0 0 3 2
![Page 38: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/38.jpg)
Uniformed algorithms
Can use DFS and BFS buthow to know when the shortest path
found? Problem condition
long paths of cheap linksmust examine whole network
![Page 39: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/39.jpg)
Midterm review
Midterm topicsFinite state machinesSteering behaviorsGraph search
![Page 40: Graph Algorithms GAM 376 Robin Burke Winter 200. Homework #2 No 10s Most common mistake not handling the possibilities associated with damage Big no-no](https://reader036.vdocument.in/reader036/viewer/2022062515/56649c755503460f94928873/html5/thumbnails/40.jpg)
Tuesday
Soccer Lab