CSCI-UA.0380-001 Programming Challenges Sean McIntyre Class 10: - - PowerPoint PPT Presentation

csci ua 0380 001 programming challenges
SMART_READER_LITE
LIVE PREVIEW

CSCI-UA.0380-001 Programming Challenges Sean McIntyre Class 10: - - PowerPoint PPT Presentation

CSCI-UA.0380-001 Programming Challenges Sean McIntyre Class 10: Graphs II Maximum Flow Given a network graph Connected, weighted, directed Edges act as pipes Weight is the capacity of the pipe Vertices act as splitting


slide-1
SLIDE 1

CSCI-UA.0380-001 Programming Challenges

Sean McIntyre Class 10: Graphs II

slide-2
SLIDE 2

Maximum Flow

  • Given a “network” graph
  • Connected, weighted, directed
  • Edges act as pipes
  • Weight is the capacity of the pipe
  • Vertices act as splitting points
  • Two special nodes
  • Source node s
  • Sink node t
slide-3
SLIDE 3

Maximum Flow

  • Given a “network” graph, what is the maximum

flow from the source to the sink?

  • How much water can travel through the pipes

without bursting?

  • Two methods we'll talk about today to solve this

problem

  • Ford-Fulkerson and Edmonds-Karp
slide-4
SLIDE 4

Maximum Flow

  • Ford-Fulkerson
  • Send a flow down a path p whenever there

exists an augmenting path p from s to t

  • An augmenting path is any path that has capacity
  • Find augmenting paths by using DFS
slide-5
SLIDE 5

Maximum Flow

3 2 1

30 5 25 70 70

3 2 1

30 5 25 70 70 70 70 5 25 30

Take the original graph, make it directed 3 2 1

30 5 25 70 70 70 70 5 25 30

3 2 1

30 5 50 45 70 95 70 5 30

Find an augmenting path Reduce capacity on path mf = 0 mf = 0 mf = 25 mf = 0

slide-6
SLIDE 6

Maximum Flow

Find an augmenting path Reduce capacity on path 3 2 1

30 5 50 45 70 95 70 5 30

3 2 1

30 50 40 75 100 65 10 30

Reduce capacity on path 3 2 1

30 50 40 75 100 65 10 30

3 2 1

60 50 40 105 100 35 10

mf = 25 mf = 30 mf = 30 mf = 60 Done!

slide-7
SLIDE 7

Maximum Flow

  • Ford-Fulkerson algorithm

1)mf ← 0 2)while (exists an augmenting path p from s to t)

1)Send flow along p = s → … → i → j → … → t 2)Find f, the minimum edge weight along p 3)Decrease weight of forward edges i → j by f 4)Increase weight of backward edges j → i by f 5)mf ← mf + f

3)Output mf

slide-8
SLIDE 8

Maximum Flow

  • Ford-Fulkerson algorithm
  • Use DFS to find an augmenting path
  • Runtime: O(E mf)
  • mf is the maximum flow in the graph
  • So if the solution is large, the runtime could be

large!

  • More on Wikipedia
slide-9
SLIDE 9

Maximum Flow

  • Edmonds-Karp algorithm
  • Use BFS to find an augmenting path
  • Runtime: O(VE2)
  • Provable that after O(VE) iterations, all

augmenting paths are exhausted

  • So it's limited by the number of edges!
  • More on Wikipedia
  • Code on class website
slide-10
SLIDE 10

Maximum Flow

  • Dinic's algorithm
  • Another technique
  • Runtime can be O(V2E)
  • Useful when the graph is edge-saturated
  • More on Wikipedia
slide-11
SLIDE 11

Maximum Flow

  • UVa 259 – Software Application
  • A number of batch jobs are to be run on a

number of computers

  • Batch jobs take a day
  • No multitasking on the computer
  • Certain computers are set up to run certain

batch jobs

  • Two or more of the same batch may be run
  • What is a possible allocation of jobs →

computers so that all jobs run in one day?

slide-12
SLIDE 12

Maximum Flow

s A B C Z 1 2 9 t Batch jobs Computers Sink Source Capacities specified by number of jobs Edges all capacity 1, specified by which computers can handle which jobs Edges all capacity 1, meaning each computer can handle 1 job

slide-13
SLIDE 13

Maximum Flow

  • UVa 259 – Software Application
  • Example data:
  • A4 01234;

Q1 5; P4 56789;

  • A4 01234;

Q1 5; P5 56789;

slide-14
SLIDE 14

Minimum Cut

3 2 1

60 50 40 105 100 35 10

mf = 60 A consequence of computing the maximum flow is computing the minimum cut Min cut: The smallest cost (or cut set) for removing edges so that the graph is split into two disconnected components 3 2 1

60 50 40 105 100 35 10

mf = 60

slide-15
SLIDE 15

Maximum Flow

  • Exercise:

– UVa 11418 Clever Naming Patterns – Figure out how to build the max flow graph – 5 minutes

slide-16
SLIDE 16

Maximum Flow

  • Exercise:

– UVa 10511 Counciling – Figure out how to build the max flow graph – 5 minutes

slide-17
SLIDE 17

Maximum Flow

  • Other max flow concepts covered in the book
  • Bipartite graph matching
  • Min-cost max flow
  • For graphs with each edge having capacity and

cost

  • Maximum independent paths
  • Multi-source maximum flow
  • More on TopCoder
slide-18
SLIDE 18

Math!

  • A big component in programming contests
  • Besides common algorithms and patterns, math

problems tend to be hard to teach

  • The problem-setter finds a problem that he or

she is comfortable with and creates a problem around it

  • “Complete search” is usually useful
  • Except, for example, number theory problems
slide-19
SLIDE 19

Math!

  • Exercise:
  • UVa 11723 Numbering Roads
  • Example of a math problem
  • 5 minutes
slide-20
SLIDE 20

Math!

  • Exercise:
  • UVa 11130 Billiard Bounces
  • Example where math insight helps find the

solution!

slide-21
SLIDE 21

Math!

  • Computing logarithms, base b
  • In Java, we have Math.log(), which is base e
  • How do we compute logb(a)?
  • Math.log(a) / Math.log(b)
slide-22
SLIDE 22

Math!

  • BigInteger class
  • Arbitrarily large integer type
  • But has to fit in memory of the program
  • If you're asked to work with very large numbers

(larger than 2^63), use BigInteger!

  • Basic operations: +, -, /, *, %, pow
  • Advanced operations: gcd, modulo arithmetic,

base conversion

slide-23
SLIDE 23

Math!

  • Combinatorics
  • Given a problem description, find some nice

formula to count something

  • Code is short, and you know if your code is right

if there's a nice example in the sample input/output

  • Use longs or BigIntegers if necessary!
  • Finding the formula is harder and requires some

thinking

  • If there are overlapping subproblems, then DP

will come in handy

slide-24
SLIDE 24

Math!

  • Exercise:
  • Tiling a Grid With Dominos
  • Combinatorics
slide-25
SLIDE 25

Math!

  • Prime Numbers
  • First and only even prime: 2

– First 10 primes: {2 3 5 7 11 13 17 19 23 29} – Prime counts:

  • 1-100: 25 primes
  • 1-1000: 168 primes
  • 1-10,000: 1229 primes

– Useful for factoring!

slide-26
SLIDE 26

Math!

  • Testing if a number is prime, isPrime(n)
  • Check if n is divisible by 2, …, n-1
  • O(n)
  • Check if n is divisible by 2, 3, …, sqrt(n)
  • O(sqrt(n))
  • Check if n is divisible by 3, 5, 7, …, sqrt(n)
  • O(sqrt(n)/2) = O(sqrt(n)) – plus check of 2
  • Check if n is divisible by primes <= sqrt(n)
  • O(π(sqrt(n)) = O(sqrt(n) / log(sqrt(n)))

– π(m) = number of primes up to m

slide-27
SLIDE 27

Math!

  • Generating primes from 1 to N
  • Naive algorithm:
  • for (int i = 0; i <= N; i++) { if (isPrime(i)) print(i); }
  • Better algorithm:
  • Sieve of Eratosthenes
slide-28
SLIDE 28

Math!

  • Sieve of Eratosthenes

1 2 3 4 5 6 7 8 9 ... 51 52 53 54 55 ... 75 76 77 F F T T T T T T T T T T T T T T T T

slide-29
SLIDE 29

Math!

  • Sieve of Eratosthenes

1 2 3 4 5 6 7 8 9 ... 51 52 53 54 55 ... 75 76 77 F F T T T T T T T T T T T T T T T T F F T T F T F T F T T F T F T T F T

slide-30
SLIDE 30

Math!

  • Sieve of Eratosthenes

1 2 3 4 5 6 7 8 9 ... 51 52 53 54 55 ... 75 76 77 F F T T T T T T T T T T T T T T T T F F T T F T F T F T T F T F T T F T F F T T F T F T F F F F T F T F F T

slide-31
SLIDE 31

Math!

  • Sieve of Eratosthenes

1 2 3 4 5 6 7 8 9 ... 51 52 53 54 55 ... 75 76 77 F F T T T T T T T T T T T T T T T T F F T T F T F T F T T F T F T T F T F F T T F T F T F F F F T F T F F T F F T T F T F T F F F F T F F F F T

slide-32
SLIDE 32

Math!

  • Sieve of Eratosthenes

1 2 3 4 5 6 7 8 9 ... 51 52 53 54 55 ... 75 76 77 F F T T T T T T T T T T T T T T T T F F T T F T F T F T T F T F T T F T F F T T F T F T F F F F T F T F F T F F T T F T F T F F F F T F F F F T F F T T F T F T F F F F T F F F F F

slide-33
SLIDE 33

Math!

  • Sieve of Eratosthenes

1)A 1D boolean array P from 0 to N

  • Initialized to true except 0 and 1

2)Pick i, the next True element in the array

1)Mark P[ j ] = False for j = i*2, i*3, …, i*k where i*(k+1) > N

  • Code on website
  • More on Wikipedia
slide-34
SLIDE 34

Math!

  • Finding prime factors for a number N
  • Check if N is divisible by the primes generated

by the sieve

  • N = P * N'
  • P is a prime factor
  • If N' = 1, then all primes have been discovered
  • Otherwise repeat on N'
slide-35
SLIDE 35

Math!

  • GCD, greatest common divisor of two numbers
  • Euclidean algorithm
  • gcd(a, 0) = a
  • gcd(a, b) = gcd(b, a % b)
  • In code:
  • int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  • LCM, least common multiple of two numbers
  • lcm(a, b) = a*b / gcd(a, b)
slide-36
SLIDE 36

Math!

  • Lots of stuff not covered in this class
  • Number theory
  • modulo arithmetic, Chinese Remainder Theorem
  • Probability theory
  • What's the probability of an event occuring
  • Uses DP
  • Game theory
  • Given a game, and with the perfect strategy, who

wins? Uses DP

  • Matrix power, Gaussian elimination
slide-37
SLIDE 37

Practice

slide-38
SLIDE 38

For next class

  • Readings:
  • Section 4.6
  • Max flow
  • Chapter 5
  • Read the first couple of sections, and dive deeper

into topics that interest you

  • Exercises:
  • Homework on the website
  • Pick a final problem – email me this weekend

what you choose