Download presentation
Presentation is loading. Please wait.
1
COMP 261 Lecture 17 Test Revision
2
Outline Data Structures for Graphs Tries Dijkstra’s Algorithm
A* Search Articulation Points Minimum Spanning Tree 3D Graphics
3
Data Structures for Graphs
Traditional Adjacency Matrix Traditional Adjacency List Object-based structure (Adjacency list-like)
4
Adjacency Matrix Complexity of actions Cannot handle multi-graph
Find all nodes O(numNodes) Find all edges O(numNodes2) Outgoing edges of a node Incoming edges of a node Outgoing neighbours of a node Incoming neighbours of a node Edge from vertex v to u? O(1) B 7 5 6 C 1 D 4 E 16 F 19 G 11 H 10 A 3 9 17 8 15 2 13 18 14 12 I J 20 Cannot handle multi-graph
5
Adjacency List Complexity of actions Cannot handle multi-graph either
Find all nodes O(numNodes) Find all edges O(numEdges) Outgoing edges of a node Incoming edges of a node Outgoing neighbours of a node O(numOutNeighbours) Incoming neighbours of a node Edge from vertex v to u? A B C D E F G H I J Cannot handle multi-graph either
6
Object-based Data Structure
1 2 25 key A A B C D E F G H I J 7 10 24 5 6 3 1 4 11 23 17 25 18 20 13 8 14 9 2 16 B C J
7
Tries
8
Tries Tree: set of strings/keys (if map string → value )
children indexed by elements of the key nodes corresponding to complete key are marked ( contain a value) tree under a node = set of all keys with the prefix so far. h j i had ice has iced hat in hats ink have irk he iron heal jab health jabs heat job a e c n r a o a e k k b b d s t v s e l t s s t h
9
Tries nodes Each node contains
marker (if set) or associated value (if hashmap) a map: char → node value a b c d e f g h i j k z _ children nodes
10
Tries How to add a word into the trie?
How to get a given word from the trie? How to get all words that match a given prefix from the trie?
11
Dijkstra’s Algorithm and A* Search
Path finding
12
A General Graph Search Strategy
Start a “fringe" with one node Repeat until fringe is empty or other criteria is met: Choose a fringe node to visit Add its neighbours to fringe Used by both Dijkstra’s Algorithm and A* Search
13
Dijkstra’s Algorithm Find the shortest paths from a start node to all other nodes Early stop if the goal node is given Use a priority queue Priority: costFromStart Always select the unvisited node with minimal costFromStart
14
A* Search Find the shortest path from a start node to a goal node
Use a priority queue Priority: estimated total cost = costFromStart + estCostToGoal Use heuristic to estimate estCostToGoal Admissible and Consistent heuristic Always select the unvisited node with minimal estTotalCost What if the heuristic is not admissible/not consistent?
15
Articulation Points What is an articulation point?
How to find articulations points in a graph? B A C D F G E
16
Find Articulation Points
DFS of the graph, set node count number For each node, split other nodes into Subtree nodes Non-subtree nodes Whether there is an alternative path (edges in the graph but not in the tree) from each subtree node to the non-subtree nodes? Min count number each node can reach back to? Articulation point if a subtree node has reach back no smaller than its count number Root node A 1/1 B 2/1 C 3/1 D 4/1 G 7/7 5/4 E F 6/4
17
Minimum Spanning Tree What is a MST? Prim’s Algorithm
Kruskal’s Algorithm Union-Find Data Structure
18
Prim’s Algorithm Grow from a start node
At each step, the min-weight edge that connects a node in the tree and a node not in the tree Stop when all the nodes in the tree
19
Kruskal’s Algorithm Start from a lot of single-node trees (forest)
At each step, merge two trees into one tree by adding a min-weight edge Stop when there is a single tree
20
Union–Find Data Structure
MakeSet(x), Find(x), Union(x,y) How to never merge longer tree into shorter tree? Z0 X3 J2 A4 T0 D2 C3 G0 U1 E2 S0 K0 R1 Y1 R0 Q1 N0 W0 H0
21
3D Graphics Right-hand rule Thumb pointing to z axis
Curl of other fingers from x to y (anti-clockwise viewing from z axis) Left-handed Right-handed wikipedia
22
Object Transformation
A unified [matrix * vector] transformation operator Convert 3D vectors to 4D vectors Use 1 for the value in the 4th dimension Translation Scaling Rotation
23
Rendering a Polygon Calculate EdgeList x (x3,y3,z3) (x1,y1,z1)
Need to keep only the closest pixels (small z) ⇒ work out z value of each pixel (x2,y2,z2) y
24
EdgeList with Z-buffer
Anti-clockwise for each edge (a, b) in {(v1,v2), (v2, v3), (v3, v1)} slopex ← (b.x – a.x) / (b.y – a.y), slopez ← (b.z – a.z) / (b.y – a.y) x ← a.x, z ← a.z, y ← round(a.y) if a.y < b.y then // going down, visit left while y <= round(b.y) [increase] xleft(y) ← x, zleft(y) ← z, x ← x + slopex , z ← z + slopez y++ else // going up, visit right while y >= round(b.y) [decrease] xright(y) ← x, zright(y) ← z, x ← x – slopex , z ← z – slopez y-- EdgeList Left Right x z x z ymin ymin+1 … ymax
25
Use EdgeList Two interpolations 1: get x and z bounds from y
2, get z from x and bounds (xleft(y), zleft(y)) (xright(y), zright(y)) (x, z) x z (xleft(y), y, zleft(y)) (xright(y), y, zright(y))
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.