CSE 421 Section 2
Graph algorithms
Administrivia
Announcements & Reminders
The “extremal principle”
Problem 2 - Write it Better: extremal principle
Starting point:
Problem 2 - Write it Better: extremal principle
Starting point:
We will construct a simple path in 𝐺 (i.e. a path with no repeated vertices) as follows:
Problem 2 - Write it Better: extremal principle
Starting point:
We will construct a simple path in 𝐺 (i.e. a path with no repeated vertices) as follows:
Now finish the proof with the people around you, and we’ll go over it together.
Problem 2 - Write it Better: extremal principle
Problem 2 - Write it Better: extremal principle
It’s common in proofs by contradiction to have cases like we’ve seen here; “Option A: we’re done with our proof! Option B: do something else.” Here, though, that “do something else” has us basically where we started (a new end-vertex on our path where we’ve used one edge), and it’s tempting to say “repeat indefinitely, eventually you hit the other case.” That’s mathematically correct! But not particularly elegant. And you have to write down enough steps that your reader knows what the pattern is, which could be a lot. � �The more elegant version is to use the “extremal principle”. Instead of slowly building an object (here, the path), just start with the most extreme version of the object at the beginning (usually the biggest one or the first one). Starting with the right object lets us eliminate Option A and jump right to Option B.�
Problem 2 - Write it Better: extremal principle
NEW, EXTREME Starting point: what kind of path should we consider?
Problem 2 - Write it Better: extremal principle
Problem 2 - Write it Better: extremal principle
Problem 2 - Write it Better: extremal principle
Graph Modeling
Modeling a Problem
Graph Modeling Steps
Writing Algorithms Using Existing Algorithms
Problem 4 – Graph Modeling
In this problem we’re going to solve a classic riddle.
Work on this problem with the people around you, and then we’ll go over it together!
Problem 4 – Graph Modeling
Problem 4 – Graph Modeling
Fill the 5 gallon jug, pour into the 3 gallon jug until it is full (the jugs now contain 2 and 3 gallons respectively). Pour from the larger jug into the large bucket (it now has 2 gallons). Empty the 3 gallon jug, and repeat all these steps to get the desired 4 gallons. ��
Problem 4 – Graph Modeling
Fill the 5 gallon jug, pour into the 3 gallon jug until it is full (the jugs now contain 2 and 3 gallons respectively). Pour from the larger jug into the large bucket (it now has 2 gallons). Empty the 3 gallon jug, and repeat all these steps to get the desired 4 gallons. ��Alternatively, Fill the 3 gallon jug, pour into the 5 gallon jug. Fill the 3 gallon jug again, pour until the 5 gallon jug is full (they now contain 5 gallons and 1 gallon respectively) . Pour the 1 gallon from the 3 gallon jug into the bucket. Refill the 3 gallon jug and pour into the bucket to bring the total to 4 gallons. ��There may be other solutions.
Problem 4 – Graph Modeling
Problem 4 – Graph Modeling
Intuition:
The “state” of the puzzle can be represented as the number of gallons in each of the jugs and the bucket.
We encode the rules of the puzzle such that each possible step is an edge.
Problem 4 – Graph Modeling
Intuition:
The “state” of the puzzle can be represented as the number of gallons in each of the jugs and the bucket.
We encode the rules of the puzzle such that each possible step is an edge.
Work on this problem with the people around you. First see if you can model it as a graph, and then think about how you could use that graph in an algorithm. Then we’ll go over it together!
Problem 4 – Graph Modeling
What are my fundamental objects?:
How are they related to each other?:
What am I looking for, is it encoded in the graph?:
How do I find the object from 3:
Problem 4 – Graph Modeling
What are my fundamental objects?: How much water is in each jug and how much is in the final bucket. We need a vertex for each possible state the jugs and bucket can be in.
How are they related to each other?:
What am I looking for, is it encoded in the graph?:
How do I find the object from 3:
Problem 4 – Graph Modeling
What are my fundamental objects?: How much water is in each jug and how much is in the final bucket. We need a vertex for each possible state the jugs and bucket can be in.
How are they related to each other?: We can fill a jug, empty a jug, or pour one jug into another or the bucket. So, we need edges that connect the different states for each of these valid operations.
What am I looking for, is it encoded in the graph?:
How do I find the object from 3:
Problem 4 – Graph Modeling
Problem 4 – Graph Modeling
Problem 4 – Graph Modeling
Problem 4 – Graph Modeling
Problem 4 – Graph Modeling
Problem 4 – Graph Modeling
Problem 4 – Graph Modeling
Problem 4 – Graph Modeling
Problem 4 – Graph Modeling
Correctness
Problem 4 – Graph Modeling
Problem 4 – Graph Modeling
Running Time
Problem 4 – Graph Modeling
Big-O
Big-O Review
Big-O Tips for Comparing
Problem 1 – Big-O-No
Work on this problem with the people around you, and then we’ll go over it together!
Problem 1 – Big-O-No
Problem 1 – Big-O-No
Problem 1 – Big-O-No
Problem 1 – Big-O-No
Problem 1 – Big-O-No
Problem 1 – Big-O-No
Problem 1 – Big-O-No
Problem 1 – Big-O-No
Problem 1 – Big-O-No
Problem 1 – Big-O-No
That’s All, Folks!
Thanks for coming to section this week!
Any questions?