CSE 373 Au24 AX Section 9
MSTs and Project Sidewalk
Check-in
Any plans for break on Nov 28-29?
Announcements
MicroTeach: MSTs
MSTs
��� �
1
A
B
C
D
3
4
2
5
1
A
B
C
D
3
4
2
5
1
A
B
C
D
3
4
2
5
MSTs vs. SPTs: A Key Difference
A shortest paths tree depends on the source vertex. There is no source vertex for a minimum spanning tree. The SPT for every other vertex is different from the MST!
Minimizes each node’s distance from source
Minimizes tree’s total edge weight
A
2
4
3
1
D
B
C
SPT from A
A
2
4
3
1
D
B
C
MST
BFS | Dijkstra’s | Prim’s & Kruskal’s |
Both find Shortest Paths Trees (SPTs)! | Finds MST! | |
|
|
|
| | |
Y
S
Z
T
X
Y
S
Z
T
X
6
7
8
9
7
3
2
5
4
Y
S
Z
T
X
6
7
8
9
7
3
2
5
4
MST Finding Algorithms
Prim’s
Start a tree from a single node.��Each iteration, connect a node in the tree to a node not in the tree with the cheapest edge possible. ��(Slowly builds up one, connected tree).
Kruskal’s
Repeatedly add the smallest edge that doesn’t cause a cycle in the tree you are building. ��(Can build a few disconnected trees before they all merge). => Disjoint Sets/Union Find ADT
[Thinks about nodes!]
[Thinks about edges!]
Prim’s Walkthrough
Try the problem on your worksheet now!
Prim’s
A
C
B
D
2
7
1
4
3
E
5
G
F
9
10
0
6
8
| dist | edge | processed |
A | 0 | none | yes |
B | 2 | (AB) | |
C | 7 | (AC) | |
D | infinity | | |
E | 8 | (AE) | |
F | infinity | | |
G | infinity | | |
Note: The red lines represent a “cut”!
Prim’s
A
C
B
D
2
7
1
4
3
E
5
G
F
9
10
0
6
8
| dist | edge | processed |
A | 0 | none | yes |
B | 2 | (AB) | yes |
C | 3 | (BC) | |
D | 4 | (BD) | |
E | 8 | (AE) | |
F | infinity | | |
G | infinity | | |
Prim’s
A
C
B
D
2
7
1
4
3
E
5
G
F
9
10
0
6
8
| dist | edge | processed |
A | 0 | none | yes |
B | 2 | (AB) | yes |
C | 3 | (BC) | yes |
D | 1 | (CD) | |
E | 5 | (CE) | |
F | infinity | | |
G | infinity | | |
Prim’s
A
C
B
D
2
7
1
4
3
E
5
G
F
9
10
0
6
8
| dist | edge | processed |
A | 0 | none | yes |
B | 2 | (AB) | yes |
C | 3 | (BC) | yes |
D | 1 | (CD) | yes |
E | 5 | (CE) | |
F | 6 | (DF) | |
G | 10 | (DG) | |
Prim’s
A
C
B
D
2
7
1
4
3
E
5
G
F
9
10
0
6
8
| dist | edge | processed |
A | 0 | none | yes |
B | 2 | (AB) | yes |
C | 3 | (BC) | yes |
D | 1 | (CD) | yes |
E | 5 | (CE) | yes |
F | 6 | (DF) | |
G | 9 | (EG) | |
Prim’s
A
C
B
D
2
7
1
4
3
E
5
G
F
9
0
6
8
| dist | edge | processed |
A | 0 | none | yes |
B | 2 | (AB) | yes |
C | 3 | (BC) | yes |
D | 1 | (CD) | yes |
E | 5 | (CE) | yes |
F | 6 | (DF) | yes |
G | 0 | (FG) | |
Prim’s
A
C
B
D
2
7
1
4
3
E
5
G
F
9
10
0
6
8
| dist | edge | processed |
A | 0 | none | yes |
B | 2 | (AB) | yes |
C | 3 | (BC) | yes |
D | 1 | (CD) | yes |
E | 5 | (CE) | yes |
F | 6 | (DF) | yes |
G | 0 | (FG) | yes |
Resulting MST!
A
C
B
D
2
1
3
E
5
G
F
0
6
Kruskal’s Walkthrough
Try the problem on your worksheet now!
Kruskal’s
A
B
E
C
D
G
F
4
2
3
5
1
6
9
3
2
| Disjoint set components |
A | |
B | |
C | |
D | |
E | |
F | |
G | |
Kruskal’s pseudocode(Graph G):
Create disjoint sets for each vertex in G
Sort the edges by weight
for each edge(u,v) in sorted order:
If u is not in the same set as v:
Join set u and v)
Add edge(u,v) to mst
4
Kruskal’s
A
B
E
C
D
G
F
4
2
3
5
1
6
9
3
2
| Disjoint set components |
A | {A} |
B | {B} |
C | {C} |
D | {D} |
E | {E} |
F | {F} |
G | {G} |
Kruskal’s pseudocode(Graph G):
Create disjoint sets for each vertex in G
Sort the edges by weight
for each edge(u,v) in sorted order:
If u is not in the same set as v:
Join set u and v)
Add edge(u,v) to mst
4
Kruskal’s
A
B
E
C
D
G
F
4
2
3
5
1
6
9
3
2
| Disjoint set components |
A | {A} |
B | {B} |
C | {C, F} |
D | {D} |
E | {E} |
F | {C, F} |
G | {G} |
Kruskal’s pseudocode(Graph G):
Create disjoint sets for each vertex in G
Sort the edges by weight
for each edge(u,v) in sorted order:
If u is not in the same set as v:
Join set u and v)
Add edge(u,v) to mst
4
Note: We join C and F, so it is reflected under both in the table! (union)
Kruskal’s
A
B
E
C
D
G
F
4
2
3
5
1
6
9
3
2
| Disjoint set components |
A | {A} |
B | {B} |
C | {C, F} |
D | {D, G} |
E | {E} |
F | {C, F} |
G | {D, G} |
4
Kruskal’s pseudocode(Graph G):
Create disjoint sets for each vertex in G
Sort the edges by weight
for each edge(u,v) in sorted order:
If u is not in the same set as v:
Join set u and v)
Add edge(u,v) to mst
Note: choosing between D-G and A-E is arbitrary + up to you!
Kruskal’s
A
B
E
C
D
G
F
4
2
3
5
1
6
9
3
2
| Disjoint set components |
A | {A, E} |
B | {B} |
C | {C, F} |
D | {D, G} |
E | {E, A} |
F | {C, F} |
G | {D, G} |
4
Kruskal’s pseudocode(Graph G):
Create disjoint sets for each vertex in G
Sort the edges by weight
for each edge(u,v) in sorted order:
If u is not in the same set as v:
Join set u and v)
Add edge(u,v) to mst
Kruskal’s
A
B
E
C
D
G
F
4
2
3
5
1
6
9
3
2
| Disjoint set components |
A | {E, C, F, A} |
B | {B} |
C | {E, C, F, A} |
D | {D, G} |
E | {E, C, F, A} |
F | {E, C, F, A} |
G | {D, G} |
4
Kruskal’s pseudocode(Graph G):
Create disjoint sets for each vertex in G
Sort the edges by weight
for each edge(u,v) in sorted order:
If u is not in the same set as v:
Join set u and v)
Add edge(u,v) to mst
Note: choosing between B-D and E-C is arbitrary + up to you!
Kruskal’s
A
B
E
C
D
G
F
4
2
3
5
1
6
9
3
2
| Disjoint set components |
A | {E, C, F, A} |
B | {D, G, B} |
C | {E, C, F, A} |
D | {D, G, B} |
E | {E, C, F, A} |
F | {E, C, F, A} |
G | {D, G, B} |
4
Kruskal’s pseudocode(Graph G):
Create disjoint sets for each vertex in G
Sort the edges by weight
for each edge(u,v) in sorted order:
If u is not in the same set as v:
Join set u and v)
Add edge(u,v) to mst
Kruskal’s
A
B
E
C
D
G
F
4
2
3
5
1
6
9
3
2
| Disjoint set components |
A | {E, C, F, A} |
B | {D, G, B} |
C | {E, C, F, A} |
D | {D, G, B} |
E | {E, C, F, A} |
F | {E, C, F, A} |
G | {D, G, B} |
4
Kruskal’s pseudocode(Graph G):
Create disjoint sets for each vertex in G
Sort the edges by weight
for each edge(u,v) in sorted order:
If u is not in the same set as v:
Join set u and v)
Add edge(u,v) to mst
Note: There is a distinction between E-F and A-B! Cycles!
Kruskal’s
A
B
E
C
D
G
F
4
2
3
5
1
6
9
3
2
| Disjoint set components |
A | {E, C, F, A, D, G, B} |
B | {E, C, F, A, D, G, B} |
C | {E, C, F, A, D, G, B} |
D | {E, C, F, A, D, G, B} |
E | {E, C, F, A, D, G, B} |
F | {E, C, F, A, D, G, B} |
G | {E, C, F, A, D, G, B} |
4
Kruskal’s pseudocode(Graph G):
Create disjoint sets for each vertex in G
Sort the edges by weight
for each edge(u,v) in sorted order:
If u is not in the same set as v:
Join set u and v)
Add edge(u,v) to mst
By now, all vertices have been joined - the remaining larger weighted edges create cycles! (To optimize, stop at |V| - 1)
Kruskal’s
A
B
E
C
D
G
F
4
2
3
5
1
6
9
3
2
| Disjoint set components |
A | {E, C, F, A, D, G, B} |
B | {E, C, F, A, D, G, B} |
C | {E, C, F, A, D, G, B} |
D | {E, C, F, A, D, G, B} |
E | {E, C, F, A, D, G, B} |
F | {E, C, F, A, D, G, B} |
G | {E, C, F, A, D, G, B} |
4
Kruskal’s pseudocode(Graph G):
Create disjoint sets for each vertex in G
Sort the edges by weight
for each edge(u,v) in sorted order:
If u is not in the same set as v:
Join set u and v)
Add edge(u,v) to mst
Kruskal’s
A
B
E
C
D
G
F
4
2
3
5
1
6
9
3
2
| Disjoint set components |
A | {E, C, F, A, D, G, B} |
B | {E, C, F, A, D, G, B} |
C | {E, C, F, A, D, G, B} |
D | {E, C, F, A, D, G, B} |
E | {E, C, F, A, D, G, B} |
F | {E, C, F, A, D, G, B} |
G | {E, C, F, A, D, G, B} |
4
Kruskal’s pseudocode(Graph G):
Create disjoint sets for each vertex in G
Sort the edges by weight
for each edge(u,v) in sorted order:
If u is not in the same set as v:
Join set u and v)
Add edge(u,v) to mst
Kruskal’s
A
B
E
C
D
G
F
4
2
3
5
1
6
9
3
2
| Disjoint set components |
A | {E, C, F, A, D, G, B} |
B | {E, C, F, A, D, G, B} |
C | {E, C, F, A, D, G, B} |
D | {E, C, F, A, D, G, B} |
E | {E, C, F, A, D, G, B} |
F | {E, C, F, A, D, G, B} |
G | {E, C, F, A, D, G, B} |
4
Kruskal’s pseudocode(Graph G):
Create disjoint sets for each vertex in G
Sort the edges by weight
for each edge(u,v) in sorted order:
If u is not in the same set as v:
Join set u and v)
Add edge(u,v) to mst
Resulting MST!
A
B
E
C
D
G
F
4
2
3
1
3
2
Case Study: Project Sidewalk
Inaccessible Paths
Consider the following scenario…
Naive Approach
Naive Approach: Boolean Field for “inaccessible”
Additional input: A set of “inaccessible edges” decided by volunteers
As we construct the graph, explicitly check if an edge has been deemed as “inaccessible”
Inaccessible edges | S → Y | S → T | Z → X | … | … |
Shortest Paths Project: Naive Approach Implementation
Insert if-checks towards each edge here, where we add the edge information to build the graph
Naive Approach: Potential drawbacks to consider…
Draw out what this graph would look like without the inaccessible edges before we run Dijkstra’s from S, given the set of inaccessible edges below.
Are there any potential drawbacks to this algorithm to consider? How might this algorithm not work the way we expect it to?
Set of inaccessible edges:
S –> T
S –> Y
Naive Approach: Potential drawbacks to consider…
Set of inaccessible edges:
S –> T
S –> Y
A disconnected graph!
New Approach!
New Approach: Cost Refinements with accessibility score
Instead of completely relying on the “accessibility” condition of an edge, we are given an “accessibility score” between 0 to 1:
Refined cost of an edge = Original Cost / Accessibility Score
Accessibility Score Range
Accessibility Score = 0
The refined cost goes to infinity in this case, marking the edge as being “extremely inaccessible”.
Refined cost remains the same, indicating no extra cost arising from the accessible criterion when this edge is included
Accessibility Score = 1
New Approach: Cost Refinements
Instead of completely relying on the “accessibility” condition of an edge, we are given an “accessibility score” between 0 to 1:
Refined cost of an edge = Original Cost / Accessibility Score
Additional input data set: Edge set with associated accessibility score
Edge | e1 | e2 | e3 | e4 | e5 | … |
Original cost | 6 | 7 | 8 | 4 | 9 | … |
Accessibility Score | 0.4 | 0.5 | 0.2 | 0.4 | 1.0 | … |
Refined Cost | 6 / 0.4 = 15 | 7 / 0.5 = 14 | 8 / 0.2 = 40 | 4 / 0.4 = 10 | 9 / 1.0 = 9 | … |
New Approach: Example
This is what we’re seeing in the Shortest Paths Project!
For your analysis, consider alternatives! Would you maybe set an adjustable threshold, or use other diverse factors?
Diverse Factors; Diverse Weights
In the previous examples, we were only concerned about physical access as being a barrier to accessibility. What other factors or aspects of paths may be of interest to users? What are other possible barriers to accessibility?
Here’s one example, please come up with others:
Closing Announcements
Please don't hesitate to reach out if you have any
questions or concerns about the course, or if there's
anything else we can help you with! We're here to support
you however we can!