Lecture 20: Graph Modeling
CSE 373: Data Structures and Algorithms
CSE 373 21 SP – CHAMPION
1
Announcements
that’s it.
Warm Up
CSE 373 SP 18 - KASEY CHAMPION
3
1
6
3
weight = 1
4
2
10
5
7
0
9
8
11
15
13
weight = 8
14
12
17
16
18
weight = 10
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| | | | | | | | | | | | | | | | | | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
-1 | -1 | 1 | 2 | 2 | 2 | 1 | 6 | 7 | 7 | 6 | -1 | 11 | 12 | 12 | 11 | 15 | 15 | 17 |
Store (weight * -1)
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
-1 | -10 | 1 | 2 | 2 | 2 | 1 | 6 | 7 | 7 | 6 | -8 | 11 | 12 | 12 | 11 | 15 | 15 | 17 |
Each “node” now only takes 4 bytes of memory instead of 32
Fill in the array with the correct values representing this Disjoint Set forest
Use the indices that correspond with the values
tinyurl.com/Summer373L20
DP ClimbingStairs() Question - Recap
You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
How can we solve this problem in the most optimized way?
4
Implement - Java Code
public int climbStairs(int n) {
int secondLast = 1, last = 1, current = 1;
if (n == 1)
return last;
for (int i = 2; i <= n; i++) {
current = secondLast + last;
secondLast = last;
last = current;
}
return last;
}
5
Using Arrays for Up-Trees
Aileen (2)
Santino
Paul (4)
Joyce (0)
Ken
Sam
Alex
0 | 1 | 2 | 3 | 4 | 5 | 6 |
-1 | 0 | -1 | 6 | -1 | 2 | 0 |
Joyce | Sam | Aileen | Alex | Paul | Santino | Ken |
Using Arrays: Find
0 | 1 | 2 | 3 | 4 | 5 | 6 |
-1 | 0 | -1 | 6 | -1 | 2 | 0 |
Joyce | Sam | Aileen | Alex | Paul | Santino | Ken |
Aileen (2)
Santino
Paul (4)
Joyce (0)
Ken
Sam
Alex
Alex
Aileen
Sam
…
find(A):
index = jump to A node’s index
while array[index] > 0:
index = array[index]
path compression
return index
1
2
find(Alex)
1
2
= 0
0
3
3
Using Arrays: Union
0 | 1 | 2 | 3 | 4 | 5 | 6 |
-4 | 0 | -2 | 6 | -1 | 2 | 0 |
Joyce | Sam | Aileen | Alex | Paul | Santino | Ken |
Aileen (2)
Santino
Joyce (0)
Ken
Sam
Alex
union(A, B):
rootA = find(A)
rootB = find(B)
use -1 * array[rootA] and -1 *
array[rootB] to determine weights
put lighter root under heavier root
weight 4
weight 2
union(Ken, Santino)
Paul (4)
weight 1
Using Arrays: Union
0 | 1 | 2 | 3 | 4 | 5 | 6 |
-4 | 0 | -2 | 6 | -1 | 2 | 0 |
Joyce | Sam | Aileen | Alex | Paul | Santino | Ken |
union(A, B):
rootA = find(A)
rootB = find(B)
use -1 * array[rootA] and -1 *
array[rootB] to determine weights
put lighter root under heavier root
-6
0
Aileen (2)
Santino
Joyce (0)
Ken
Sam
Alex
weight 6
Paul (4)
weight 1
Aileen
union(Ken, Santino)
Practice
CSE 373 SP 18 - KASEY CHAMPION
10
3
0
weight = 1
4
11
1
5
2
13
12
weight = 8
10
9
14
15
8
weight = 6
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| | | | | | | | | | | | | | | | |
weight = 2
6
7
16
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
3 | 0 | 0 | -6 | 3 | -1 | -2 | 6 | 12 | 13 | 13 | 0 | 13 | -8 | 12 | 12 | 12 |
union(2, 16)
Practice
CSE 373 SP 18 - KASEY CHAMPION
11
3
0
weight = 1
4
11
1
5
2
13
12
weight = 8
10
9
14
15
8
weight = 6
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| | | | | | | | | | | | | | | | |
weight = 2
6
7
16
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
3 | 0 | 3 | 13 | 3 | -1 | -2 | 6 | 12 | 13 | 13 | 0 | 13 | -8 | 12 | 12 | 13 |
union(2, 16)
findSet(2) with path compression
findSet(16) with path compression
union(3,13) by weight
Using Arrays for WQU+PC
| (Baseline) | QuickFind | QuickUnion | WeightedQuickUnion | WQU + Path Compression | ArrayWQU+PC |
makeSet(value) | 𝚯(1) | 𝚯(1) | 𝚯(1) | 𝚯(1) | 𝚯(1) | 𝚯(1) |
find(value) | 𝚯(n) | 𝚯(1) | 𝚯(n) | 𝚯(log n) | 𝚯(log* n) | 𝚯(log* n) |
union(x, y) assuming root args | 𝚯(n) | 𝚯(n) | 𝚯(1) | 𝚯(1) | 𝚯(1) | 𝚯(1) |
union(x, y) | 𝚯(n) | 𝚯(n) | 𝚯(n) | 𝚯(log n) | 𝚯(log* n) | 𝚯(log* n) |
Implementing Dijkstra’s
Review Dijkstra’s Algorithm: Key Properties
dijkstraShortestPath(G graph, V start)
Set known; Map edgeTo, distTo;
initialize distTo with all nodes mapped to ∞, except start to 0
while (there are unknown vertices):
let u be the closest unknown vertex
known.add(u)
for each edge (u,v) to unknown v with weight w:
oldDist = distTo.get(v) // previous best path to v
newDist = distTo.get(u) + w // what if we went through u?
if (newDist < oldDist):
distTo.put(v, newDist)
edgeTo.put(v, u)�
Review Why Does Dijkstra’s Work?
X
KNOWN
8??
3
1
A
1
5
6??
Example:
Dijkstra’s Algorithm Invariant
All vertices in the “known” set have the correct shortest path
INVARIANT
Review Why Does Dijkstra’s Work?
Dijkstra’s Algorithm Invariant
All vertices in the “known” set have the correct shortest path
INVARIANT
X
KNOWN
7??
3
1
A
1
5
6
Example:
Implementing Dijkstra’s
dijkstraShortestPath(G graph, V start)
Set known; Map edgeTo, distTo;
initialize distTo with all nodes mapped to ∞, except start to 0
while (there are unknown vertices):
let u be the closest unknown vertex
known.add(u)
for each edge (u,v) to unknown v with weight w:
oldDist = distTo.get(v) // previous best path to v
newDist = distTo.get(u) + w // what if we went through u?
if (newDist < oldDist):
distTo.put(u, newDist)
edgeTo.put(u, v)�
MIN PRIORITY QUEUE ADT
Implementing Dijkstra’s: Pseudocode
dijkstraShortestPath(G graph, V start)
Map edgeTo, distTo;
initialize distTo with all nodes mapped to ∞, except start to 0
PriorityQueue<V> perimeter; perimeter.add(start);
while (!perimeter.isEmpty()):
u = perimeter.removeMin()
for each edge (u,v) to v with weight w:
oldDist = distTo.get(v) // previous best path to v
newDist = distTo.get(u) + w // what if we went through u?
if (newDist < oldDist):
distTo.put(v, newDist)
edgeTo.put(v, u)
if (perimeter.contains(v)):
perimeter.changePriority(v, newDist)
else:
perimeter.add(v, newDist)
�
Dijkstra’s Runtime
dijkstraShortestPath(G graph, V start)
Map edgeTo, distTo;
initialize distTo with all nodes mapped to ∞, except start to 0
PriorityQueue<V> perimeter; perimeter.add(start);
while (!perimeter.isEmpty()):
u = perimeter.removeMin()
for each edge (u,v) to v with weight w:
oldDist = distTo.get(v) // previous best path to v
newDist = distTo.get(u) + w // what if we went through u?
if (newDist < oldDist):
distTo.put(v, newDist)
edgeTo.put(v, u)
if (perimeter.contains(v)):
perimeter.changePriority(v, newDist)
else:
perimeter.add(v, newDist)
�
Dijkstra’s Runtime
dijkstraShortestPath(G graph, V start)
Map edgeTo, distTo;
initialize distTo with all nodes mapped to ∞, except start to 0
PriorityQueue<V> perimeter; perimeter.add(start);
while (!perimeter.isEmpty()):
u = perimeter.removeMin()
for each edge (u,v) to v with weight w:
oldDist = distTo.get(v) // previous best path to v
newDist = distTo.get(u) + w // what if we went through u?
if (newDist < oldDist):
distTo.put(v, newDist)
edgeTo.put(v, u)
if (perimeter.contains(v)):
perimeter.changePriority(v, newDist)
else:
perimeter.add(v, newDist)
�
Final result:
Why can’t we simplify further?
Topological Sort
Topological Sort
A
B
C
A before C
B before C
A before B
A
B
C
Topological Sort:
With original edges for reference:
A
B
C
Input:
Can We Always Topo Sort a Graph?
CSE 143
CSE 373
CSE 417
🤔 Where do I start?
Where do I end? 🤔
MATH 126
CSE 142
CSE 143
CSE 373
CSE 374
CSE 417
No 😭
DIRECTED ACYCLIC GRAPH
Problem 1: Ordering Dependencies
CSE 373 SP 18 - KASEY CHAMPION
24
Math 126
CSE 142
CSE 143
CSE 373
CSE 374
CSE 417
Problem 1: Ordering Dependencies
CSE 373 19 SP - KASEY CHAMPION
25
Given: a directed graph G
Find: an ordering of the vertices so all edges go from left to right (all the dependency arrows are satisfied and the vertices can be processed left to right with no problems) .
Topological Sort (aka Topological Ordering)
Ordering a DAG
CSE 373 19 WI - KASEY CHAMPION
26
A
B
C
E
D
If a vertex doesn’t have any edges going into it, we can add it to the ordering.
More generally, if the only incoming edges are from vertices already in the ordering, it’s safe to add.
0
1
2
1
1
A
C
B
D
E
0
1
0
0
0
Topological Ordering
CSE 373 19 SP - KASEY CHAMPION
27
Math 126
CSE 142
CSE 143
CSE 373
CSE 374
CSE 417
Math 126
CSE 142
CSE 143
CSE 373
CSE 374
CSE 417
Reductions
Reductions
1. Convert input for P into input for Q
2. Solve using algorithm for Q
3. Convert output from Q into output from P
Q INPUT
P INPUT
Q OUTPUT
P OUTPUT
PROBLEM Q
PROBLEM P
Reductions
1. Place note inside of envelope
2. Mail using US Postal Service
3. Take note out of envelope
Q INPUT
Q OUTPUT
Mail a letter
Get a note to Chicago
Chicago
Chicago
Seattle
Seattle
How To Perform Topo Sort?
0
1
3
4
7
2
5
6
👻
0
1
3
4
7
2
5
6
👻
0
1
3
4
7
2
5
6
1
2
3
4
5
6
7
0
1
2
3
4
5
6
7
0
👻
BFS
Reduce topo sort to BFS by modifying graph, running BFS, then modifying output back
Performing Topo Sort
Checking for Duplicates
containsDuplicates(array) {
for (int i = 0; i < array.length; i++):
for (int j = i; j < array.length; j++):
if (array[i] == array[j]):
return true
return false
}
0 | 1 | 2 | 3 | 4 |
2 | 4 | 8 | 3 | 8 |
Goal of a Reduction
0 | 1 | 2 | 3 | 4 |
2 | 4 | 8 | 3 | 8 |
Goal: Reduce the problem of “Contains Duplicates?” to another problem we have an algorithm for.
Try to identify each of the following:
Q INPUT
P INPUT
Q OUTPUT
P OUTPUT
PROBLEM Q
PROBLEM P
One Solution: Sorting!
Array
Array
Sorted Array
Boolean
Sorting
Contains Duplicates?
One Solution: Reduce “Contains Duplicates?” to the problem of sorting an array
Graph Modeling Review
Recap: Graph Modeling
SCENARIO
&
QUESTION TO ANSWER
ANSWER!
MODEL AS A GRAPH
RUN ALGORITHM
…
Often need to refine original model as you work through details of algorithm
Many ways to model any scenario with a graph, but question motivates which data is important
Graph Modeling Activity
Note Passing - Part I
Imagine you are an American High School student. You have a very important note to pass to your crush, but the two of you do not share a class so you need to rely on a chain of friends to pass the note along for you. A note can only be passed from one student to another when they share a class, meaning when two students have the same teacher during the same class period.
Unfortunately, the school administration is not as romantic as you, and passing notes is against the rules. If a teacher sees a note, they will take it and destroy it. Figure out if there is a sequence of handoffs to enable you to get your note to your crush.�
How could you model this situation as a graph?
| Period 1 | Period 2 | Period 3 | Period 4 |
You | Smith | Patel | Lee | Brown |
Anika | Smith | Lee | Martinez | Brown |
Bao | Brown | Patel | Martinez | Smith |
Carla | Martinez | Jones | Brown | Smith |
Dan | Lee | Lee | Brown | Patel |
Crush | Martinez | Brown | Smith | Patel |
�
Possible Design
You
Anika
Carla
Bao
Dan
Crush
Smith, 1
Martinez, 1
Patel, 2
Lee, 2
Martinez, 3
Brown, 3
Smith, 4
Patel, 4
You | | |
A | | |
B | | |
C | | |
D | | |
Crush | | |
A
B
B
D
You
A
C
You
D
Crush
B
C
Crush
A
C
D
More Design
Note Passing - Part II
Now that you know there exists a way to get your note to your crush, we can work on picking the best hand off path possible.
Thought Experiments:
2. What if you want to optimize for rick avoidance to make sure your note only gets passed in classes least likely for it to get intercepted?
- Some teachers are better at intercepting notes than others. The more notes a teacher has intercepted, the more likely it is they will take yours and it will never get to your crush. If we knew how many notes each teacher has intercepted how might we incorporate that into our graph to find the least risky route?
Optimize for Time
You
Anika
Carla
Bao
Dan
Crush
1
1
2
2
3
3
4
4
Vertex | Distance | Predecessor | Process Order |
You | 0 | -- | 0 |
Anika | 1 | You | 1 |
Bao | 2 | You | 5 |
Carla | 6 | Dan | 3 |
Dan | 3 | Anika | 2 |
Crush | 7 | Carla | 4* |
*The path found wraps around to a new school day because the path moves from a later period to an earlier one
- We can change our algorithm to check for wrap arounds and try other routes
“Distance” will represent the sum of which periods the note is passed in, because smaller period values are earlier in the day the smaller the sum the earlier the note gets there except in the case of a “wrap around”
Optimize for Risk
You
Anika
Carla
Bao
Dan
Crush
1
3
2
4
3
5
1
4
Vertex | Distance | Predecessor | Process Order |
You | 0 | -- | 0 |
Anika | 1 | You | 1 |
Bao | 4 | Anika | 2 |
Carla | 5 | Bao | 3 |
Dan | 10 | Carla | 5 |
Crush | 8 | Carla | 4 |
Teacher | Notes Intercepted |
Smith | 1 |
Martinez | 3 |
Lee | 4 |
Brown | 5 |
Patel | 2 |
“Distance” will represent the sum of notes intercepted across the teachers in your passing route. The smaller the sum of notes the “safer” the path.
Seam Carving
Content-Aware Image Resizing
Seam carving: A distortion-free technique for resizing an image by removing “unimportant seams”
Seam carving for content-aware image resizing (Avidan, Shamir/ACM); Broadway Tower (Newton2, Yummifruitbat/Wikimedia)
Original Photo
Horizontally-Scaled�(castle and person�are distorted)
Seam-Carved�(castle and person are undistorted; “unimportant” sky removed instead)
44
Seam Carving Reduces to Dijkstra’s!
Shortest Paths (Robert Sedgewick, Kevin Wayne/Princeton)
1.5
1.0
1.6
58.2
120.9
greater pixel difference = higher weight!
An Incomplete Reduction
Shortest Paths (Robert Sedgewick, Kevin Wayne/Princeton)
S
T
In Conclusion
Q INPUT
P INPUT
Q OUTPUT
P OUTPUT
PROBLEM Q
PROBLEM P
Appendix