primer on dinosaurs

36
Lab Demo 08 22 October 2015

Upload: hubert-wong

Post on 05-Jan-2016

223 views

Category:

Documents


1 download

DESCRIPTION

One of the best book on dinosaurs you will ever see

TRANSCRIPT

Page 1: primer on dinosaurs

Lab Demo 0822 October 2015

Page 2: primer on dinosaurs

PS4 Debrief• Precompute the MST of the original graph using

Prim or Kruskal

• Precompute all MiniMax to all vertices from the first ten vertices by running DFS/BFS

• Answer each query in O(1)

• Total time complexity O(E log V + 10 x V)

Intended solution for subtask D:

Build MST

Traverse from the first ten

vertices

Page 3: primer on dinosaurs

PS4 Debrief• Precompute the MST of the original graph using

Prim or Kruskal

• Precompute all MiniMax to all vertices from the first ten vertices by running DFS/BFS

• Answer each query in O(1)

• Total time complexity O(E log V + 10 x V)

Intended solution for subtask D:

Why traversal only took O(V) time ?

Page 4: primer on dinosaurs

PS4 Debrief

What if there is no constraint on the source vertex ?

Page 5: primer on dinosaurs

PS4 Debrief1

2 3

10 4 6

5 7

9

8

Rooted tree at 1

Page 6: primer on dinosaurs

PS4 Debrief1

2 3

10 4 6

5 7

9

8

Lowest Common Ancestor of 5 and 9

Page 7: primer on dinosaurs

PS4 Debrief1

2 3

10 4 6

5 7

9

8

Lowest Common Ancestor (LCA) of 5 and 9

Ancestor of 5Ancestor of 9

Page 8: primer on dinosaurs

PS4 Debrief

• Finding LCA of two vertices can be done in

• Finding maximum edge in path from a vertex to its ancestor can be done in

• Time complexity for each query becomes

• Total complexity O(E log V + Q log V)

O(log V)

O(log V)

O(log V)

Page 9: primer on dinosaurs

PS5 is out :O

• Subtask C is quite tough to AC

• Need to use different codes for each subtask

• Try to start early

Page 10: primer on dinosaurs

PS5 StatusName A B C D

- AC AC AC AC

Hubert AC AC AC

Rui Bin, Jiahao, Yun Shian, Danwen AC AC

Cindy, Bao Jun AC

The rest?

Page 11: primer on dinosaurs

PS5• Given a graph, and Q queries

• Each query asks for shortest path from s to t that uses no more than k vertices

Page 12: primer on dinosaurs

PS5 Subtask A• Given graph is an undirected weighted tree

• 1 ≤ V ≤ 1 000

• 1 ≤ Q ≤ 100 000

• k=V

Page 13: primer on dinosaurs

PS5 Subtask A• Given graph is an undirected weighted tree

• 1 ≤ V ≤ 1 000

• 1 ≤ Q ≤ 100 000

• k=V Just ignore k

Page 14: primer on dinosaurs

PS5 Subtask A• Given graph is an undirected weighted tree

• 1 ≤ V ≤ 1 000

• 1 ≤ Q ≤ 100 000

• k=V

Classic shortest path problem, use Dijkstra ?

Just ignore k

Page 15: primer on dinosaurs

PS5 Subtask A• Given graph is an undirected weighted tree

• 1 ≤ V ≤ 1 000

• 1 ≤ Q ≤ 100 000

• k=V

Classic shortest path problem, use Dijkstra ?

Total complexity : O(Q * E log V) ~ 109 processes

Just ignore k

TLE👎

Page 16: primer on dinosaurs

PS5 Subtask A• Given graph is an undirected weighted tree

• 1 ≤ V ≤ 1 000

• 1 ≤ Q ≤ 100 000

• k=V

Use property of a tree

Total complexity : O(Q * (V + E)) ~ 108 processes

run BFS/DFS from u to v

Just ignore k

TLE👎

Page 17: primer on dinosaurs

PS5 Subtask A• Given graph is an undirected weighted tree

• 1 ≤ V ≤ 1 000

• 1 ≤ Q ≤ 100 000

• k=V

Precompute from each vertex

Total complexity : O(V * (V + E) + Q) ~ 106 processes

Notice the small V

AC👍

O(1) for each query

Page 18: primer on dinosaurs

PS5 Subtask B• Given graph is a directed weighted graph

• 1 ≤ V ≤ 1 000

• 0 ≤ E ≤ 200 000

• 1 ≤ Q ≤ 10 000

• 0 ≤ s ≤ 9

• k=V

Page 19: primer on dinosaurs

PS5 Subtask B• Given graph is a directed weighted graph

• 1 ≤ V ≤ 1 000

• 0 ≤ E ≤ 200 000

• 1 ≤ Q ≤ 10 000

• 0 ≤ s ≤ 9

• k=V

ignore k again

Shortest path on general graph

Dijkstra

Total complexity : O(Q * E log V) ~ 1010 processes

TLE👎

Page 20: primer on dinosaurs

PS5 Subtask B• Given graph is a directed weighted graph

• 1 ≤ V ≤ 1 000

• 0 ≤ E ≤ 200 000

• 1 ≤ Q ≤ 10 000

• 0 ≤ s ≤ 9

• k=V

Shortest path on general graph

Dijkstra

Total complexity : O(Q * E log V) ~ 1010 processes

Looks like PS4 Subtask D

TLE👎

ignore k again

Page 21: primer on dinosaurs

PS5 Subtask B• Given graph is a directed weighted graph

• 1 ≤ V ≤ 1 000

• 0 ≤ E ≤ 200 000

• 1 ≤ Q ≤ 10 000

• 0 ≤ s ≤ 9

• k=V

Precompute dijkstra from first ten vertices

Total complexity : O(10 * E log V + Q) ~ 2*107 processes

Looks like PS4 Subtask D

Query done in O(1)

AC👍

ignore k again

Page 22: primer on dinosaurs

PS5 Subtask C• Given graph is a directed weighted graph

• 1 ≤ V ≤ 1 000

• 0 ≤ E ≤ 200 000

• 1 ≤ Q ≤ 20

• 1 ≤ k ≤ min(V, 20)

Page 23: primer on dinosaurs

PS5 Subtask C• Given graph is a directed weighted graph

• 1 ≤ V ≤ 1 000

• 0 ≤ E ≤ 200 000

• 1 ≤ Q ≤ 20

• 1 ≤ k ≤ min(V, 20)

Need to use k now, cannot directly use Dijkstra

Page 24: primer on dinosaurs

PS5 Subtask C• Given graph is a directed weighted graph

• 1 ≤ V ≤ 1 000

• 0 ≤ E ≤ 200 000

• 1 ≤ Q ≤ 20

• 1 ≤ k ≤ min(V, 20)

Need to somehow attach a counter to vertices

1

1,1 1,2 1,3 1,k-1 1,k…

Split each vertex to k new vertices

Page 25: primer on dinosaurs

PS5 Subtask C• Given graph is a directed weighted graph

• 1 ≤ V ≤ 1 000

• 0 ≤ E ≤ 200 000

• 1 ≤ Q ≤ 20

• 1 ≤ k ≤ min(V, 20)

Need to somehow attach a counter to vertices

1

1,1 1,2 1,3 1,k-1 1,k…

Split each vertex to k new vertices

(u, k) moves to (v, k+1)

Page 26: primer on dinosaurs

PS5 Subtask C0

1 2 3

Query from 0 to 3, when k = 3

Page 27: primer on dinosaurs

PS5 Subtask C0

1 2 3

Query from 0 to 3, when k = 3

0,1 0,2 0,3

1,1

1,2

1,3

2,1

2,2

2,3

3,1

3,2

3,3

source

destination

Page 28: primer on dinosaurs

Optimisation• Modified Dijkstra algorithm should run faster

• Constructing a new adjacency list with 20V vertices probably will get TLE

• Stop Dijkstra when it reach the destination vertex (important!)

• Use primitive int type on IntegerPair object

Subtask C has a very strict time limit

AC👌

Page 29: primer on dinosaurs

PS5 Subtask D

• Subtask D is beyond CS2010

• It is a classical problem that can be solved using maximum flow.

• Take CS3233 next sem if you are interested.

Page 30: primer on dinosaurs

UVa 926

• This is part of CS2010 final exam question in the past 4 years

Page 31: primer on dinosaurs

UVa 926• Given a grid, start position

and end position

• Find number of ways to go from start to end

• Cannot move south or west.

• Some roads are discontinued. (1,2) to (2,2) and (2,2) to (2,3)

Page 32: primer on dinosaurs

UVa 926• Represent each intersection as a vertex

• There are no cycle in this graph, so this is a DAG (Directed Acyclic Graph)

Page 33: primer on dinosaurs

UVa 926• Counting number of path is usually a DP problem.

• Since it is a DAG, so it is okay to use DP.

Let start position be (xs, ys)

Let end position be (xe, ye)

Page 34: primer on dinosaurs

DP solutioncountPath(x, y): number of path from (x,y) to (xe, ye)

countPath(x,y) = 1

if (x,y) = (xe, ye) Base Case

Page 35: primer on dinosaurs

DP solutioncountPath(x, y): number of path from (x,y) to (xe, ye)

countPath(x,y) =

if (x,y) <> (xe, ye)

countPath(x+1, y) countPath(x, y+1)

0 0

if (x,y) to (x+1,y) not under construction

+

otherwise otherwise

if (x,y) to (x,y+1) not under construction

Page 36: primer on dinosaurs