CSCI-UA.0380-001 Programming Challenges Sean McIntyre Class 10: - - PowerPoint PPT Presentation
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
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
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
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
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
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!
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
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
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
Maximum Flow
- Dinic's algorithm
- Another technique
- Runtime can be O(V2E)
- Useful when the graph is edge-saturated
- More on Wikipedia
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?
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
Maximum Flow
- UVa 259 – Software Application
- Example data:
- A4 01234;
Q1 5; P4 56789;
- A4 01234;
Q1 5; P5 56789;
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
Maximum Flow
- Exercise:
– UVa 11418 Clever Naming Patterns – Figure out how to build the max flow graph – 5 minutes
Maximum Flow
- Exercise:
– UVa 10511 Counciling – Figure out how to build the max flow graph – 5 minutes
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
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
Math!
- Exercise:
- UVa 11723 Numbering Roads
- Example of a math problem
- 5 minutes
Math!
- Exercise:
- UVa 11130 Billiard Bounces
- Example where math insight helps find the
solution!
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)
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
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
Math!
- Exercise:
- Tiling a Grid With Dominos
- Combinatorics
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!
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
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
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
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
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
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
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
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
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'
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)
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
Practice
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