Balanced Programming - SJTU

Preview:

Citation preview

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Balanced Programming

Group Static

Shanghai Jiao Tong University

December 27, 2016

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Overview

1 Introduction

2 Baby step giant step

3 Another Example

4 Meet in the middle

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Features

Kind of a useful designing idea.

Easy implementation and good performance in practice.

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Features

Kind of a useful designing idea.Easy implementation and good performance in practice.

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Balances

Balanced in learning from each each other.

Balanced in the problem-solving processes.

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Balances

Balanced in learning from each each other.Balanced in the problem-solving processes.

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Available occiasion

There are two algorithms, both of which have differentfeatures, such as one can answer queries very quickly and theother cam handle modification swiftly.

There is an algorithm to solve the problems, but is quitecomplicated. Actually this algorithm is not the best choice inpractice. The idea of balanced programming might be used toproduced a suitable algorithm.

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Available occiasion

There are two algorithms, both of which have differentfeatures, such as one can answer queries very quickly and theother cam handle modification swiftly.There is an algorithm to solve the problems, but is quitecomplicated. Actually this algorithm is not the best choice inpractice. The idea of balanced programming might be used toproduced a suitable algorithm.

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Overview

1 Introduction

2 Baby step giant step

3 Another Example

4 Meet in the middle

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Problem

Find a smallest non-negative number x satisfying

Ax ≡ B (mod P)

which P is a prime, A,B ∈ [0,P).

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Naive Approach

According to Fermat Theorem, when A,P are coprime, we have:

AP ≡ A (mod P)

the only special case is A = 0, and in this case, B must be zero.

For A ̸= 0, we can just iterate x from 0 to P− 1 to check ifAx ≡ B (mod P). The complexity is O(P).

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Yet Another Naive Algorithm

We mapped Ax → x, x ∈ [0,P) by using a Hash-Table.

In order to find B, we just need to query B in this Hash-Table,which only takes O(1) time.

Precalculating this Hash-Table costs O(P) time, and the totalcomplexity of which is O(P + 1) = O(P).

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Balanced Programming

First we choose a number S ∈ [1,P− 1].We mapped Ax → x, x ∈ [0,S) by using a Hash-Table.Calculate A−S by using Fast Exponentiation.

In this step, we needs S + log P operations.

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Balanced Programming

Ax ≡ B (mod P)

x can be represented as i× S + j, we can do such transforming:

Ai×S+j ≡ B⇔ Aj ≡ B× (A−S)i (mod P)

which j ∈ [0,S), and i < PS .

We can just iterate i from 0 to PS to check if B× (A−S)i is in

Hash-Table. In the worst occasion, we need to iterate PS times.

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Total complexity evaluation

As you can see, the total operations we need to do are

S + log P +PS

we want to choose S optimally to get the best total complexity.

when S =√

P, the total operations are:

S + log P +PS = 2

√P + log P = O(

√P)

thus we get an O(√

P) algorithm by choosing S optimally.

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Overview

1 Introduction

2 Baby step giant step

3 Another Example

4 Meet in the middle

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Another Problem

an undirected graph

add x to uquery neighbors’sumO(m) = n

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Another Problem

an undirected graphadd x to u

query neighbors’sumO(m) = n

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Another Problem

an undirected graphadd x to u

query neighbors’sumO(m) = n

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Another Problem

an undirected graphadd x to u

query neighbors’sumO(m) = n

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Another Problem

an undirected graphadd x to uquery neighbors’sum

O(m) = n

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Another Problem

an undirected graphadd x to uquery neighbors’sum

O(m) = n

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Another Problem

an undirected graphadd x to uquery neighbors’sumO(m) = n

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Naive Approach 1

store the value v[x]

O(n) spaceO(1) time for addO(n) time for query

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Naive Approach 1

store the value v[x]O(n) space

O(1) time for addO(n) time for query

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Naive Approach 1

store the value v[x]O(n) spaceO(1) time for add

O(n) time for query

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Naive Approach 1

store the value v[x]O(n) spaceO(1) time for addO(n) time for query

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Naive Approach 2

store the sum s[x]

O(n) spaceO(n) time for addO(1) time for query

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Naive Approach 2

store the sum s[x]O(n) space

O(n) time for addO(1) time for query

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Naive Approach 2

store the sum s[x]O(n) spaceO(n) time for add

O(1) time for query

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Naive Approach 2

store the sum s[x]O(n) spaceO(n) time for add

O(1) time for query

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Naive Approach 2

store the sum s[x]O(n) spaceO(n) time for add

O(1) time for query

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Naive Approach 2

store the sum s[x]O(n) spaceO(n) time for addO(1) time for query

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Observation

bad when large deg(x)

”heavy” when deg(x) > B

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Observation

bad when large deg(x)”heavy” when deg(x) > B

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Heavy-Light Divide

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Heavy-Light Divide

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Combined Approach

s[x] =∑v[heavy neighbors] +∑v[light neighbors]

for∑

v[heavy neighbors]use approach 1for

∑v[light neighbors]

use approach 2

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Combined Approach

s[x] =∑v[heavy neighbors] +∑v[light neighbors]

for∑

v[heavy neighbors]use approach 1for

∑v[light neighbors]

use approach 2

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Add(u, x) details

if u is heavy,vh[u]← vh[u] + x

if u is light,sl[v]← sl[v] + x,u, v are neighborsO(1) time for heavyO(B) time for light

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Add(u, x) details

if u is heavy,vh[u]← vh[u] + x

if u is light,sl[v]← sl[v] + x,u, v are neighborsO(1) time for heavyO(B) time for light

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Add(u, x) details

if u is heavy,vh[u]← vh[u] + xif u is light,sl[v]← sl[v] + x,u, v are neighbors

O(1) time for heavyO(B) time for light

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Add(u, x) details

if u is heavy,vh[u]← vh[u] + xif u is light,sl[v]← sl[v] + x,u, v are neighborsO(1) time for heavy

O(B) time for light

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Add(u, x) details

if u is heavy,vh[u]← vh[u] + xif u is light,sl[v]← sl[v] + x,u, v are neighborsO(1) time for heavyO(B) time for light

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Query(x) details

∑v[heavy neighbors] =∑y is heavy neighbor vh[y]

∑v[light neighbors] = sl[x]

O(1 + cnt_heavy) timecnt_heavy · B ≤ 2m = O(n)O(n/B) time

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Query(x) details

∑v[heavy neighbors] =∑y is heavy neighbor vh[y]∑v[light neighbors] = sl[x]

O(1 + cnt_heavy) timecnt_heavy · B ≤ 2m = O(n)O(n/B) time

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Query(x) details

∑v[heavy neighbors] =∑y is heavy neighbor vh[y]∑v[light neighbors] = sl[x]

O(1 + cnt_heavy) timecnt_heavy · B ≤ 2m = O(n)O(n/B) time

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Query(x) details

∑v[heavy neighbors] =∑y is heavy neighbor vh[y]∑v[light neighbors] = sl[x]

O(1 + cnt_heavy) time

cnt_heavy · B ≤ 2m = O(n)O(n/B) time

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Query(x) details

∑v[heavy neighbors] =∑y is heavy neighbor vh[y]∑v[light neighbors] = sl[x]

O(1 + cnt_heavy) timecnt_heavy · B ≤ 2m = O(n)

O(n/B) time

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Query(x) details

∑v[heavy neighbors] =∑y is heavy neighbor vh[y]∑v[light neighbors] = sl[x]

O(1 + cnt_heavy) timecnt_heavy · B ≤ 2m = O(n)O(n/B) time

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Balance

O(n) space

O(B) time for addO(n/B) time for querymake B =

√n

O(√

n) for each operation

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Balance

O(n) spaceO(B) time for add

O(n/B) time for querymake B =

√n

O(√

n) for each operation

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Balance

O(n) spaceO(B) time for addO(n/B) time for query

make B =√

nO(√

n) for each operation

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Balance

O(n) spaceO(B) time for addO(n/B) time for querymake B =

√n

O(√

n) for each operation

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Balance

O(n) spaceO(B) time for addO(n/B) time for querymake B =

√n

O(√

n) for each operation

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Overview

1 Introduction

2 Baby step giant step

3 Another Example

4 Meet in the middle

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Search algorithm

Search algorithmexponential :O(xdepth)

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Meet in the middle

Given an undirectedgraph whose nodes’degree is 5 and finda path from S to T.

dist(S, T) = LO(4L)

Meet in the middleO(4L/2)

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Meet in the middle

Given an undirectedgraph whose nodes’degree is 5 and finda path from S to T.dist(S, T) = L

O(4L)

Meet in the middleO(4L/2)

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Meet in the middle

Given an undirectedgraph whose nodes’degree is 5 and finda path from S to T.dist(S, T) = LO(4L)

Meet in the middleO(4L/2)

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Meet in the middle

Given an undirectedgraph whose nodes’degree is 5 and finda path from S to T.dist(S, T) = LO(4L)

Meet in the middleO(4L/2)

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Some other applications

Mo’s algorithm

Blocked listDinic(capacity is 1)......

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Some other applications

Mo’s algorithmBlocked list

Dinic(capacity is 1)......

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Some other applications

Mo’s algorithmBlocked listDinic(capacity is 1)

......

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Some other applications

Mo’s algorithmBlocked listDinic(capacity is 1)......

Group Static Balanced Programming

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

IntroductionBaby step giant step

Another ExampleMeet in the middle

Thanks

Thanks

Thanks for listening.Questions are welcomed.

Group Static Balanced Programming