Lecture 6
Graph modeling, Greedy algorithms
CSE 421 Autumn 2025
1
Previously…
2
Graph traversal applications
3
BFS
Shortest path (unweighted graphs)
DFS
Strongly connected components
Topological sort
Either one:
Bipartiteness testing (aka 2-coloring)
Connected components
Cycle detection
Takeaways
4
Today
5
Graph modeling
6
Most of the time, you don’t need a new graph algorithm. What you need to figure out is what graph to make and what graph algorithm to use!
“Graph modeling” is the process of going from a word problem to a graph problem. Often finding a way to turn your requirements into graph features.
This is a mix of “standard bag of tricks” and creativity.
Graph modeling process
7
Example #1
8
What are the vertices?
What are the edges?
What are we looking for?
What do we run?
You made a new social networking app, PurpleGraph. Users on PurpleGraph can have “asymmetric” following (I can follow you, without you following me). You decide to allow people to form multi-user direct messages, but only if people are probably in a similar social circle.
You’ll allow a messaging channel to form only if for every pair of users A,B in the channel: A must follow B or follow someone who follows B or follow someone who follows someone who follows B, or … �And the same for B to A.
You’d like to be able to quickly check if any new proposed channel whether it meets this condition.
Example #1
9
What are the vertices?
What are the edges?
What are we looking for?
What do we run?
Users
Directed - from u to v if �u follows v
If everyone in the proposed channel is in the same SCC
Find SCCs to test a new channel. Make sure all are in the same component
You made a new social networking app, PurpleGraph. Users on PurpleGraph can have “asymmetric” following (I can follow you, without you following me). You decide to allow people to form multi-user direct messages, but only if people are probably in a similar social circle.
You’ll allow a messaging channel to form only if for every pair of users A,B in the channel: A must follow B or follow someone who follows B or follow someone who follows someone who follows B, or … �And the same for B to A.
You’d like to be able to quickly check if any new proposed channel whether it meets this condition.
Example #2
10
Sports fans often use the “transitive law” to predict sports outcomes: in general, if you think A is better than B, and B is also better than C, then you expect that A is better than C.
However, teams don’t all play each other – from data of games that have been played, determine if the “transitive law” is realistic, or misleading about at least one outcome.
What are the vertices?
What are the edges?
What are we looking for?
What do we run?
Example #2
11
Sports fans often use the “transitive law” to predict sports outcomes: in general, if you think A is better than B, and B is also better than C, then you expect that A is better than C.
However, teams don’t all play each other – from data of games that have been played, determine if the “transitive law” is realistic, or misleading about at least one outcome.
What are the vertices?
What are the edges?
What are we looking for?
What do we run?
Teams
Directed - from u to v if �u beat v
A cycle would say it’s not realistic. A topological sort would say it is.
Cycle-detection DFS, or topological sort (with error detection)
Example #3
12
You are at Indiana Jones. Your friend is at Space Mountain. You have to tell each other about your experiences in person as soon as possible. Where do you meet and how quickly can you get there?
What are the vertices?
What are the edges?
What are we looking for?
What do we run?
5
3
5
6
3
4
5
4
8
3
5
5
4
6
5
5
4
Example #3
13
You are at Indiana Jones. Your friend is at Space Mountain. You have to tell each other about your experiences in person as soon as possible. Where do you meet and how quickly can you get there?
What are the vertices?
What are the edges?
What are we looking for?
What do we run?
Rides
Walkways weighted by time it takes to walk
The “midpoint”.
5
3
5
6
3
4
5
4
8
3
5
5
4
6
5
5
4
Example #4
14
You are going to a party at a friend’s house. You want to pick up ice-cream along the way. There are several stores. Which path is quickest?
What are the vertices?
What are the edges?
What are we looking for?
What do we run?
The vertices
The edges (weighted)
A
B
3
7
2
5
4
3
4
3
2
2
3
5
4
3
3
4
3
1
2
Example #4
15
An alternative clever idea for this problem:
Think of the extra bit as keeping track of whether the ice-cream has been picked up or not
Example #4
16
An alternative clever idea for this problem:
Think of the extra bit as keeping track of whether the ice-cream has been picked up or not
Example #4
17
You are going to a party at a friend’s house. You want to pick up ice-cream along the way. There are several stores. Which path is quickest?
What are the vertices?
What are the edges?
What are we looking for?
What do we run?
Two copies of the vertices! Now a vertex also stores information about whether ice-cream has been picked up or not!
Two copies of the original ones + edges between each ice-cream store and its copy.
Shortest path from (A, 0) to (B,1)
A
B
3
7
2
5
4
3
4
3
2
2
3
5
4
3
3
4
3
1
2
Dijkstra’s: one run is enough (on a graph that is twice the size)!
Greedy algorithms
18
This week..
19
Another abrupt change of topic.
In fact, the whole course is a sequence of seemingly abrupt changes of topic!
We’re building up a toolkit! A bag of common techniques to approach a variety of algorithmic problems.
Think of each week as a new tool in your toolkit.
Greedy algorithms
20
What’s a greedy algorithm?
An algorithm that builds a solution by:
In a nutshell: greedily do what appears best for you right here, right now.
Greedy algorithms
21
Pros
Cons
Simple
Rarely works
When it works, it’s typically very efficient
When it doesn’t work, often gives good approximations
greedily do what appears best for you right here, right now.
Other considerations:
Interval scheduling
22
Largest number of jobs�(length doesn’t matter)
Interval scheduling
23
Largest number of jobs�(length doesn’t matter)
Interval scheduling
24
Largest number of jobs�(length doesn’t matter)
This is the maximal one
Greedy algorithm for interval scheduling
Greedy algorithm: Pick the next job to include in the set based on a simple rule.
25
Possible rules?
i.e. pick the job with earliest start time, and include it in the set. Then, pick the job with the next earliest start time (that doesn’t overlap), etc.
Greedy algorithm for interval scheduling
Greedy algorithm: Pick the next job to include in the set based on a simple rule.
26
Possible rules?
Greedy algorithm for interval scheduling
Greedy algorithm: Pick the next job to include in the set based on a simple rule.
27
Possible rules?
?
Notice that there is a symmetry:�“Earliest start time” is the same as�“Latest end time” if we look at the jobs from right to left!
Greedy algorithm for interval scheduling
Greedy algorithm: Pick the next job to include in the set based on a simple rule.
28
Possible rules?
?
Notice that there is a symmetry:�“Earliest start time” is the same as�“Latest end time” if we look at the jobs from right to left!
Greedy algorithm for interval scheduling
Greedy algorithm: Pick the next job to include in the set based on a simple rule.
29
Possible rules?
Greedy algorithm for interval scheduling
Greedy algorithm: Pick the next job to include in the set based on a simple rule.
30
Possible rules?
?
?
Again notice that, by symmetry, these�two rules are essentially the same (they don’t necessarily pick the same sets, but one picks an optimal set iff the other does)
Greedy algorithm for interval scheduling
31
Proof of correctness:
Greedy algorithm for interval scheduling
32
Greedy algorithm for interval scheduling
33
Proof of correctness:
Now, that we have the claim, let’s complete the proof..
Greedy algorithm for interval scheduling
34
Greedy algorithm proof strategies
35
Summary
36
You’ll probably have a few ideas for “simple rules” for greedy algorithms! Check what happens on some simple examples to rule out some of them.
Greedy algorithms rarely work. But when they work, and you can prove so, they tend to be very efficient (and simple to implement)!
Proofs of correctness may not be easy: generally, try to find a reason why the greedy algorithm is staying ahead of all other algorithms at every step.