Balanced Programming Group Static Shanghai Jiao Tong University - - PowerPoint PPT Presentation

balanced programming
SMART_READER_LITE
LIVE PREVIEW

Balanced Programming Group Static Shanghai Jiao Tong University - - PowerPoint PPT Presentation

. . . . . . . . . . . . . . Introduction Baby step giant step Another Example Meet in the middle Thanks Balanced Programming Group Static Shanghai Jiao Tong University December 27, 2016 Group Static . . . . . . . . .


slide-1
SLIDE 1

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Balanced Programming

Group Static

Shanghai Jiao Tong University

December 27, 2016

Group Static Balanced Programming

slide-2
SLIDE 2

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Overview

1

Introduction

2

Baby step giant step

3

Another Example

4

Meet in the middle

Group Static Balanced Programming

slide-3
SLIDE 3

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Features

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

Group Static Balanced Programming

slide-4
SLIDE 4

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Features

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

Group Static Balanced Programming

slide-5
SLIDE 5

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Balances

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

Group Static Balanced Programming

slide-6
SLIDE 6

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Balances

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

Group Static Balanced Programming

slide-7
SLIDE 7

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Available occiasion

There are two algorithms, both of which have difgerent features, such as one can answer queries very quickly and the

  • ther cam handle modifjcation swiftly.

There is an algorithm to solve the problems, but is quite

  • complicated. Actually this algorithm is not the best choice in
  • practice. The idea of balanced programming might be used to

produced a suitable algorithm.

Group Static Balanced Programming

slide-8
SLIDE 8

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Available occiasion

There are two algorithms, both of which have difgerent features, such as one can answer queries very quickly and the

  • ther cam handle modifjcation swiftly.

There is an algorithm to solve the problems, but is quite

  • complicated. Actually this algorithm is not the best choice in
  • practice. The idea of balanced programming might be used to

produced a suitable algorithm.

Group Static Balanced Programming

slide-9
SLIDE 9

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Overview

1

Introduction

2

Baby step giant step

3

Another Example

4

Meet in the middle

Group Static Balanced Programming

slide-10
SLIDE 10

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet 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

slide-11
SLIDE 11

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet 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 if Ax ≡ B (mod P). The complexity is O(P).

Group Static Balanced Programming

slide-12
SLIDE 12

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Yet Another Naive Algorithm

We mapped Ax → x, x ∈ [0, P) by using a Hash-Table. In order to fjnd 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 total complexity of which is O(P + 1) = O(P).

Group Static Balanced Programming

slide-13
SLIDE 13

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet 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

slide-14
SLIDE 14

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet 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 < P

S.

We can just iterate i from 0 to P

S to check if B × (A−S)i is in

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

S times.

Group Static Balanced Programming

slide-15
SLIDE 15

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Total complexity evaluation

As you can see, the total operations we need to do are S + log P + P S we want to choose S optimally to get the best total complexity. when S = √ P, the total operations are: S + log P + P S = 2 √ P + log P = O( √ P) thus we get an O( √ P) algorithm by choosing S optimally.

Group Static Balanced Programming

slide-16
SLIDE 16

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Overview

1

Introduction

2

Baby step giant step

3

Another Example

4

Meet in the middle

Group Static Balanced Programming

slide-17
SLIDE 17

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Another Problem

an undirected graph add x to u query neighbors’ sum O m n

Group Static Balanced Programming

slide-18
SLIDE 18

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Another Problem

an undirected graph add x to u query neighbors’ sum O m n

Group Static Balanced Programming

slide-19
SLIDE 19

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Another Problem

an undirected graph add x to u query neighbors’ sum O m n

Group Static Balanced Programming

slide-20
SLIDE 20

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Another Problem

an undirected graph add x to u query neighbors’ sum O m n

Group Static Balanced Programming

slide-21
SLIDE 21

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Another Problem

an undirected graph add x to u query neighbors’ sum O m n

Group Static Balanced Programming

slide-22
SLIDE 22

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Another Problem

an undirected graph add x to u query neighbors’ sum O m n

Group Static Balanced Programming

slide-23
SLIDE 23

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Another Problem

an undirected graph add x to u query neighbors’ sum O(m) = n

Group Static Balanced Programming

slide-24
SLIDE 24

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Naive Approach 1

store the value v[x] O n space O time for add O n time for query

Group Static Balanced Programming

slide-25
SLIDE 25

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Naive Approach 1

store the value v[x] O(n) space O time for add O n time for query

Group Static Balanced Programming

slide-26
SLIDE 26

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Naive Approach 1

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

Group Static Balanced Programming

slide-27
SLIDE 27

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Naive Approach 1

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

Group Static Balanced Programming

slide-28
SLIDE 28

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Naive Approach 2

store the sum s[x] O n space O n time for add O time for query

Group Static Balanced Programming

slide-29
SLIDE 29

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Naive Approach 2

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

Group Static Balanced Programming

slide-30
SLIDE 30

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Naive Approach 2

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

Group Static Balanced Programming

slide-31
SLIDE 31

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Naive Approach 2

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

Group Static Balanced Programming

slide-32
SLIDE 32

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Naive Approach 2

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

Group Static Balanced Programming

slide-33
SLIDE 33

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Naive Approach 2

store the sum s[x] O(n) space O(n) time for add O(1) time for query

Group Static Balanced Programming

slide-34
SLIDE 34

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Observation

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

Group Static Balanced Programming

slide-35
SLIDE 35

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Observation

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

Group Static Balanced Programming

slide-36
SLIDE 36

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Heavy-Light Divide

Group Static Balanced Programming

slide-37
SLIDE 37

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Heavy-Light Divide

Group Static Balanced Programming

slide-38
SLIDE 38

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Combined Approach

s[x] = ∑ v[heavy neighbors] + ∑ v[light neighbors] for v heavy neighbors use approach 1 for v light neighbors use approach 2

Group Static Balanced Programming

slide-39
SLIDE 39

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Combined Approach

s[x] = ∑ v[heavy neighbors] + ∑ v[light neighbors] for ∑ v[heavy neighbors] use approach 1 for ∑ v[light neighbors] use approach 2

Group Static Balanced Programming

slide-40
SLIDE 40

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet 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 neighbors O time for heavy O B time for light

Group Static Balanced Programming

slide-41
SLIDE 41

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet 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 neighbors O time for heavy O B time for light

Group Static Balanced Programming

slide-42
SLIDE 42

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet 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 neighbors O time for heavy O B time for light

Group Static Balanced Programming

slide-43
SLIDE 43

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet 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 neighbors O(1) time for heavy O B time for light

Group Static Balanced Programming

slide-44
SLIDE 44

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet 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 neighbors O(1) time for heavy O(B) time for light

Group Static Balanced Programming

slide-45
SLIDE 45

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Query(x) details

∑ v[heavy neighbors] = ∑

y is heavy neighbor vh[y]

v light neighbors sl x O cnt_heavy time cnt_heavy B m O n O n B time

Group Static Balanced Programming

slide-46
SLIDE 46

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Query(x) details

∑ v[heavy neighbors] = ∑

y is heavy neighbor vh[y]

∑ v[light neighbors] = sl[x] O cnt_heavy time cnt_heavy B m O n O n B time

Group Static Balanced Programming

slide-47
SLIDE 47

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Query(x) details

∑ v[heavy neighbors] = ∑

y is heavy neighbor vh[y]

∑ v[light neighbors] = sl[x] O cnt_heavy time cnt_heavy B m O n O n B time

Group Static Balanced Programming

slide-48
SLIDE 48

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet 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 m O n O n B time

Group Static Balanced Programming

slide-49
SLIDE 49

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet 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

slide-50
SLIDE 50

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet 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

slide-51
SLIDE 51

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Balance

O(n) space O B time for add O n B time for query make B n O n for each operation

Group Static Balanced Programming

slide-52
SLIDE 52

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Balance

O(n) space O(B) time for add O n B time for query make B n O n for each operation

Group Static Balanced Programming

slide-53
SLIDE 53

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Balance

O(n) space O(B) time for add O(n/B) time for query make B n O n for each operation

Group Static Balanced Programming

slide-54
SLIDE 54

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Balance

O(n) space O(B) time for add O(n/B) time for query make B = √n O n for each operation

Group Static Balanced Programming

slide-55
SLIDE 55

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Balance

O(n) space O(B) time for add O(n/B) time for query make B = √n O(√n) for each operation

Group Static Balanced Programming

slide-56
SLIDE 56

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Overview

1

Introduction

2

Baby step giant step

3

Another Example

4

Meet in the middle

Group Static Balanced Programming

slide-57
SLIDE 57

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Search algorithm

Search algorithm exponential : O(xdepth)

Group Static Balanced Programming

slide-58
SLIDE 58

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Meet in the middle

Given an undirected graph whose nodes’ degree is 5 and fjnd a path from S to T. dist(S, T) = L O

L

Meet in the middle O

L

Group Static Balanced Programming

slide-59
SLIDE 59

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Meet in the middle

Given an undirected graph whose nodes’ degree is 5 and fjnd a path from S to T. dist(S, T) = L O

L

Meet in the middle O

L

Group Static Balanced Programming

slide-60
SLIDE 60

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Meet in the middle

Given an undirected graph whose nodes’ degree is 5 and fjnd a path from S to T. dist(S, T) = L O(4L) Meet in the middle O

L

Group Static Balanced Programming

slide-61
SLIDE 61

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Meet in the middle

Given an undirected graph whose nodes’ degree is 5 and fjnd a path from S to T. dist(S, T) = L O(4L) Meet in the middle O(4L/2)

Group Static Balanced Programming

slide-62
SLIDE 62

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Some other applications

Mo’s algorithm Blocked list Dinic(capacity is 1) ......

Group Static Balanced Programming

slide-63
SLIDE 63

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Some other applications

Mo’s algorithm Blocked list Dinic(capacity is 1) ......

Group Static Balanced Programming

slide-64
SLIDE 64

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Some other applications

Mo’s algorithm Blocked list Dinic(capacity is 1) ......

Group Static Balanced Programming

slide-65
SLIDE 65

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Some other applications

Mo’s algorithm Blocked list Dinic(capacity is 1) ......

Group Static Balanced Programming

slide-66
SLIDE 66

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Introduction Baby step giant step Another Example Meet in the middle Thanks

Thanks

Thanks for listening. Questions are welcomed.

Group Static Balanced Programming