Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 – Find all nodes • O(numNodes) – Find all edges • O(numNodes2) – Outgoing edges of a node • O(numNodes) – Incoming edges of a node • O(numNodes) – Outgoing neighbours of a node • O(numNodes) – Incoming neighbours of a node • O(numNodes) – Edge from vertex v to u? • O(1) A B C D E F G H A B 7 7 C 5 6 1 1 E 4 F 10 I 15 3 J 16 6 17 19 19 9 2 13 17 2 13 16 H 3 9 4 G J 10 15 5 D I 18 11 8 14 11 12 8 20 18 14 12 20 Cannot handle multi-graph 5 Adjacency List • Complexity of actions – Find all nodes • O(numNodes) – Find all edges • O(numEdges) – Outgoing edges of a node • O(numEdges) – Incoming edges of a node • O(numEdges) – Outgoing neighbours of a node • O(numOutNeighbours) – Incoming neighbours of a node • O(numEdges) – Edge from vertex v to u? • O(numOutNeighbours) A B C D E F G H I J B A B C D E B A A H C D E F G F G B I G I J I J H J C I J E G J C D F G H I I J Cannot handle multi-graph either 6 Object-based Data Structure 1 25 2 key A B 5 C C B 7 A 7 5 6 10 24 1 9 2 3 1 D 9 I 4 11 3 J 4 E 2 J H 10 20 11 18 14 13 23 17 16 13 F G 6 25 20 8 14 23 25 18 Tries 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 had has hat hats have he heal health heat ice iced in ink irk iron jab jabs job a e d s t s j i c v a e l t h e t s n r a k k b s o b Tries • 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 children nodes z _ 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? Dijkstra’s Algorithm and A* Search • Path finding 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 G F 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? A 1/1 B 2/1 • Min count number each node can reach back to? D 4/1 • Articulation point if a subtree node has reach back no smaller than its count 5/4 E number F • Root node 6/4 C 3/1 G 7/7 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 Union–Find Data Structure • MakeSet(x), Find(x), Union(x,y) • How to never merge longer tree into shorter tree? Z0 A4 T0 C3 G0 E2 R0 X3 J2 D2 S0 U1 K0 R1 Y1 Q1 W0 H0 N0 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 y (x2,y2,z2) 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 EdgeList while y <= round(b.y) [increase] Right Left xleft(y) ← x, zleft(y) ← z, z x z x x ← x + slopex , z ← z + slopez ymin y++ ymin+1 else // going up, visit right while y >= round(b.y) [decrease] xright(y) ← x, zright(y) ← z, x ← x – slopex , z ← z – slopez y-- ymax 25 Use EdgeList • Two interpolations – 1: get x and z bounds from y – 2, get z from x and bounds z (xright(y), zright(y)) (x, z) (xleft(y), zleft(y)) x (xleft(y), y, zleft(y)) (xright(y), y, zright(y))