csci ua 0380 001 programming challenges
play

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


  1. CSCI-UA.0380-001 Programming Challenges Sean McIntyre Class 10: Graphs II

  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

  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

  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

  5. Maximum Flow 30 30 0 3 0 3 Take the original graph, 30 make it directed 5 5 70 70 70 70 70 70 5 mf = 0 mf = 0 25 25 2 1 2 1 25 Find an augmenting path 30 30 0 3 0 3 30 30 5 5 70 70 70 70 45 95 70 70 5 5 Reduce capacity on path mf = 0 mf = 25 25 50 2 1 2 1 25 0

  6. Maximum Flow 30 30 0 3 0 3 30 30 Reduce capacity on path 5 0 45 95 70 70 40 100 65 75 5 10 mf = 25 mf = 30 50 50 2 1 2 1 0 0 Find an augmenting path 30 60 0 3 0 3 30 0 0 0 40 100 65 75 40 100 35 105 10 10 Reduce capacity on path mf = 30 mf = 60 50 50 2 1 2 1 0 0 Done!

  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

  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

  9. Maximum Flow ● Edmonds-Karp algorithm ● Use BFS to find an augmenting path ● Runtime: O(VE 2 ) ● 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

  10. Maximum Flow ● Dinic's algorithm ● Another technique ● Runtime can be O(V 2 E) ● Useful when the graph is edge-saturated ● More on Wikipedia

  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?

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

  13. Maximum Flow ● UVa 259 – Software Application ● Example data: ● A4 01234; Q1 5; P4 56789; ● A4 01234; Q1 5; P5 56789;

  14. Minimum Cut 60 60 0 3 0 3 0 0 0 0 40 100 35 105 40 100 35 105 10 10 mf = 60 mf = 60 50 50 2 1 2 1 0 0 A consequence of computing the Min cut: The smallest cost (or cut maximum flow is computing the set) for removing edges so that minimum cut the graph is split into two disconnected components

  15. Maximum Flow ● Exercise: – UVa 11418 Clever Naming Patterns – Figure out how to build the max flow graph – 5 minutes

  16. Maximum Flow ● Exercise: – UVa 10511 Counciling – Figure out how to build the max flow graph – 5 minutes

  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

  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

  19. Math! ● Exercise: ● UVa 11723 Numbering Roads ● Example of a math problem ● 5 minutes

  20. Math! ● Exercise: ● UVa 11130 Billiard Bounces ● Example where math insight helps find the solution!

  21. Math! ● Computing logarithms, base b ● In Java, we have Math.log(), which is base e ● How do we compute log b (a)? ● Math.log(a) / Math.log(b)

  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

  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

  24. Math! ● Exercise: ● Tiling a Grid With Dominos ● Combinatorics

  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!

  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

  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

  28. Math! ● Sieve of Eratosthenes 0 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

  29. Math! ● Sieve of Eratosthenes 0 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

  30. Math! ● Sieve of Eratosthenes 0 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

  31. Math! ● Sieve of Eratosthenes 0 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

  32. Math! ● Sieve of Eratosthenes 0 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

  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

  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'

  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)

  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

  37. Practice

  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

Recommend


More recommend