Advanced Dynamic Programming II HKOI Training Team 2004.

Slides:



Advertisements
Similar presentations
Great Theoretical Ideas in Computer Science
Advertisements

Shortest Paths and Treewidth Algorithms and Networks.
Algorithm Design Methodologies Divide & Conquer Dynamic Programming Backtracking.
Greedy Algorithms Greed is good. (Some of the time)
Two-Player Zero-Sum Games
Study Group Randomized Algorithms 21 st June 03. Topics Covered Game Tree Evaluation –its expected run time is better than the worst- case complexity.
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Artificial Intelligence in Game Design
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
This time: Outline Game playing The minimax algorithm
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Branch and Bound Similar to backtracking in generating a search tree and looking for one or more solutions Different in that the “objective” is constrained.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
Pertemuan 23 : Penerapan Dinamik Programming (DP) Mata kuliah : K0164-Pemrograman vers 01.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
1 Dynamic Programming Jose Rolim University of Geneva.
Backtracking.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Important Problem Types and Fundamental Data Structures
Binary Trees Chapter 6.
CSC 412: AI Adversarial Search
Game Trees: MiniMax strategy, Tree Evaluation, Pruning, Utility evaluation Adapted from slides of Yoonsuck Choe.
Dynamic Programming Part One HKOI Training Team 2004.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Minimax Trees: Utility Evaluation, Tree Evaluation, Pruning CPSC 315 – Programming Studio Spring 2008 Project 2, Lecture 2 Adapted from slides of Yoonsuck.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
Minimax.
HKOI2009 Training (Advanced Group) (Reference: Powerpoint of Dynamic Programming II, HKOI Training 2005, by Liu Chi Man, cx)
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Theory Topics to be covered:
Agents that can play multi-player games. Recall: Single-player, fully-observable, deterministic game agents An agent that plays Peg Solitaire involves.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
7 -1 Chapter 7 Dynamic Programming Fibonacci sequence Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, … F i = i if i  1 F i = F i-1 + F i-2 if.
Games. Adversaries Consider the process of reasoning when an adversary is trying to defeat our efforts In game playing situations one searches down the.
Dynamic Programming: Manhattan Tourist Problem Lecture 17.
MA/CSSE 473 Day 28 Dynamic Programming Binomial Coefficients Warshall's algorithm Student questions?
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
CS 3343: Analysis of Algorithms Lecture 18: More Examples on Dynamic Programming.
§5 Backtracking Algorithms A sure-fire way to find the answer to a problem is to make a list of all candidate answers, examine each, and following the.
Computer Sciences Department1.  Property 1: each node can have up to two successor nodes (children)  The predecessor node of a node is called its.
Games, page 1 CSI 4106, Winter 2005 Games Points Games and strategies A logic-based approach to games AND-OR trees/graphs Static evaluation functions Tic-Tac-Toe.
ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.
Adversarial Search. Game playing u Multi-agent competitive environment u The most common games are deterministic, turn- taking, two-player, zero-sum game.
Graph Connectivity This discussion concerns connected components of a graph. Previously, we discussed depth-first search (DFS) as a means of determining.
Chapter 13 Backtracking Introduction The 3-coloring problem
MA/CSSE 473 Day 30 B Trees Dynamic Programming Binomial Coefficients Warshall's algorithm No in-class quiz today Student questions?
Graph Search II GAM 376 Robin Burke. Outline Homework #3 Graph search review DFS, BFS A* search Iterative beam search IA* search Search in turn-based.
Explorations in Artificial Intelligence Prof. Carla P. Gomes Module 5 Adversarial Search (Thanks Meinolf Sellman!)
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Artificial Intelligence in Game Design Board Games and the MinMax Algorithm.
Honors Track: Competitive Programming & Problem Solving Seminar Topics Kevin Verbeek.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
1 Chapter 15-2: Dynamic Programming II. 2 Matrix Multiplication Let A be a matrix of dimension p x q and B be a matrix of dimension q x r Then, if we.
All-pairs Shortest paths Transitive Closure
CS330 Discussion 4 Spring 2017.
NIM - a two person game n objects are in one pile
Chapter 15: Dynamic Programming II
Ch. 15: Dynamic Programming Ming-Te Chi
Backtracking and Branch-and-Bound
Dynamic Programming II DP over Intervals
Important Problem Types and Fundamental Data Structures
GRAPH TRAVERSAL.
Presentation transcript:

Advanced Dynamic Programming II HKOI Training Team 2004

2 In the previous lesson...  What is DP?  Some examples of DP  Probably NOT enough for you to solve DP problems in IOI/NOI Except those classic ones  To identify a DP problem, the keys are Recurrence Optimal substructure Experience (Chinglish(?) - “ DP feel ” )

3 In this lesson...  Dimension reduction  DP on trees, graphs, etc.  Game strategy - Minimax

4 Dimension Reduction  Reduce the memory complexity by one (or more) dimension  Usually a “ rolling ” array is employed

5 Triangle revisited  Only a 2x5 array is needed AF

Rolling array F F’F’

7 LCS revisited  Recall the recurrence  F[i,j] = F[i-1,j-1]+1 if A[i]=B[j]  F[i,j] = max{F[i-1,j],F[i,j-1]} if A[i]B[j]  Note that F[i,?] only depends on F[i,?] and F[i-1,?]  Thus we can just keep 2 rows

8 Non-rectangular structures  DP can also be applied on graphs, trees, etc.  Usually structures with no cycles Recurrence should not contain cycles!  Rooted tree is a recursive structure  Notation C(v): the set of children of v (in a rooted tree)

9 Path Counting  A graph is a directed acyclic graph (DAG) if it is directed and has no cycles This is a DAG. This is not a DAG.

10 Path Counting  Given a DAG G, and two vertices of G, s and t, count the number of distinct paths from s to t What if I give you a graph with directed cycles?  How is the graph given to you? Adjacency matrix Adjacency list Other ways

11 Path (example)  s = A, t = E  Paths: ABDE, ACBDE, ACDE  Answer = 3 A B C F E D

12 Path (an attempt)  Use DFS to find out all paths from s to t Simple enough, but consider this graph How many paths from s to t?  2 4 = 16 s t

13 Path (solution)  Obviously the three paths shown below must be distinct Even if they meet at some intermediate vertices! C A t... s B

14 Path (solution)  Topological order s t

15 Path (solution)  Number of paths from vertex to t s t

16 Path (solution)  Algorithm Tsort the vertices Set F[v] = 0 for every vertex v Set F[t] = 1 Following topological order, for each vertex v For each outgoing edge (v, u) F[v] = F[v] + F[u]  Time complexity Tsort – O(V+E) DP – O(V+E) Total – O(V+E) recurrence relation

17 Path (extensions)  Longest path in DAG Given a weighted DAG G, find the length of a longest path from s to t  Shortest path counting Given a weighted graph G, find the number of shortest paths from s to t

18 Longest Path in Tree I  Given a weighted tree T, find the length of the longest path from a given node s s

19 Longest I (simple solution)  Make s the root s

20 Longest I (simple solution)  Calculate the nodes ’ distances from s (in pre-order/level-order) s

21 Longest I (another solution)  A longest path must end at one of the leaves s

22 Longest I (another solution)  Let F[v] be the longest distance between v to one of its descendant leaves For example, F[x] = 9 s x

23 Longest I (another solution)  Compute F in post-order  F[x] = 0 for every leaf x  F[v] = max {F[u]+length(v,u)} s u  C(v) answer

24 Longest I (another solution)  Algorithm Longest_One(vertex v) { if (v is a leaf) F[v]  0 else F[v]  0 for each child u of v do Longest_One(u) if (F[u]+length(v,u) F[v]  F[u]+length(v,u) }

25 Longest I (another solution)  Time complexity – O(V)  No overlapping subproblems  F[] is redundant!

26 Longest Path in Tree II  Given a weighted tree T (all weights positive), find the length of the longest path in T

27 Longest II (solution)  Take any node and make it root

28 Longest II (solution)  A longest path must be a leaf-to-leaf or a root-to-leaf path Must it pass the root?

29 Longest II (solution)  Let z be the uppermost node in the longest path  Only two cases z z the only common node is z

30 Longest II (solution)  As in Longest I, let F[v] be the longest distance between v to one of its descendant leaves  Define G as follows  G[v] = F[v] if v has less than 2 children  G[v] = max{F[u]+length(v,u)} + second_max {F[w]+length(v,w)}  Note that max may equal second_max u  C(v) w  C(v)

31 Longest II (demonstration)  Computing G from F (0+7)+(0+6) = 13 (7+5)+(0+4) = 16

32 Longest II (demonstration)  Computing G from F (again) (0+4)+(0+3) = 7 (4+5)+(0+7) = 16 14

33 Longest II (solution)  Time complexity Computing F – O(V) Computing G – O(V) Total – O(V)  F and G can be computed together  Not quite a DP problem

34 Simplified Gems  Given a tree T with N nodes  Each node is to be covered by a gemstone  Costs of gemstones: 1, 2, 3, …, M  There is an unlimited supply of gemstones  Two adjacent nodes must contain gemstones of different costs  What is the minimum total cost?

35 Gems (example)  N = 8, M =

36 Gems (attempt)  Make the tree a rooted one first

37 Gems (attempt)  Let G[v] be the minimum cost to cover all nodes in the subtree rooted at v  How to set up the recurrence?

38 Gems (solution)  Let F[v,c] be the minimum cost to cover all nodes in the subtree rooted at v and the cost of the gemstone covering v is c  Base cases F[x,c] = c for every leaf x and 1  c  M  Progress F[v,c] =  min {F[u,d]} + c  Post-order traversal u  C(v)1dM,dc

39 Gems (demostration)  M =

40 Gems (solution)  Algorithm (recursive, non-DP) Gems(vertex v,integer c) { if (v is leaf) return c value  c for each child u of v do temp  ∞ for d  1 to M do if (d  c) temp  min{temp, Gems(u,d)} value  value + temp return value }

41 Gems (solution)  Algorithm (DP) Gems_DP(vertex v) { if (v is a leaf) set base case and exit for each child u of v do Gems_DP(u) for c  1 to M do F[v,c]  c for each child u of v do temp  ∞ for d  1 to M do if (d  c) temp  min{temp, F[u,d]} F[v,c]  temp + c }

42 Gems (solution)  Time complexity Computing F[v,c] – O(M × #children of v) Computing F[v,c] for all vertices – O(MN) Computing all entries – (M 2 N)  The time complexity can be reduced to O(MN) with a “ trick ”  The original problem allows N to be as large as and M arbitrarily large Even O(N 2 ) is too slow How to solve it??

43 Game strategies  Not closely related to DP  Almost all game-type problems in IOI/BOI/CEOI requires the concept of Minimax  DP is needed in most of these problems

44 Game-type problems  Usually interactive problems  Write a program to play a simple two- player game with a judging program e.g. play tic-tac-toe with the judging program  Often the judging program uses an optimal strategy

45 Game tree  A (finite or infinite) rooted tree showing the movements of a game play O OOO … X O X O OXOX …… XOXO … … …… …

46 Card Picking  A stack of N cards with numbers on them  Two players take turns to take cards from the top of the stack  1, 2, or 3 cards can be taken in each turn  Game ends when all cards have been taken  The player with a higher total score (sum of numbers) wins

47 Card (example) A B 17 14

48 Card (game tree)  N = 4  Only 5 different states NULL 4 4 A ’ s move B ’ s move NULL

49 Minimax  A recursive algorithm for choosing the next move in a two-player game  A value is associated with each state e.g. in tic-tac-toe, all winning states may have value 1  We assume that the other player always chooses his best move

50 Minimax  Suppose A wants to maximize his final score (value), which move should he make? A ’ s moves B ’ s moves min max

51 Minimax  Again! A ’ s move B ’ s move 1

52 Minimax  Answer: left move A ’ s move B ’ s move 1

53 Tic-tac-toe  O wants to maximize the value  Is this a winning state for O? OX OOO XX OX OO OXX OX OO XX OOX OO XX value: 1 XOX OO OXX OX XOO OXX OOX XOO XX OOX OO XXX value: -1 OOX XOO OXX XOX OOO OXX OOX XOO OXX value: 0 value: 1 value: 0 O X

54 Card Picking revisited  Let the F-value of a state be the maximum difference (preserve +/- sign) between your score and your opponent ’ s score if the game starts from this state (assume that your opponent plays perfectly)

55 Card Picking revisited  A transition may alter the F-value  Two states that appear the same may have different F-values! 9 NULL F-value: 0 F-value: 9 9 NULL F-value: 0 F-value: -9

56 Card Picking revisited  We can still apply the concept of Minimax NULL NULL

57 Card Picking revisited  A recurrence can be set up  Many overlapping sub-problems, so DP!  Find the optimal move by backtracking  Most game-type problems in OI competitions are similar to this game

58 Conclusion  Many DP problems discussed are now classics  More and more atypical DP problems in competitions (esp. on trees)  Still not enough for solving some difficult IOI/NOI/BOI/CEOI DP problems  We hope that those problems can be covered in the summer vacation  Practice, practice and practice

59 The end  Prepare for TFT (19 June)....as well as your exam  Have a nice holiday!