1 of 44

CSE 373 Au24 AX Section 9

MSTs and Project Sidewalk

2 of 44

Check-in

Any plans for break on Nov 28-29?

3 of 44

Announcements

  • Lectures: Code Challenges on Friday! Learn more about interviewing + LeetCode :)
  • Project: Shortest Paths Analysis due tomorrow night! Last project!

4 of 44

MicroTeach: MSTs

5 of 44

MSTs

  • Tree: (when talking about graphs) An connected acyclic graph.�
  • Spanning Tree: A set of edges connecting all nodes of the graph in a Tree.�
  • Minimum Spanning Tree: The Spanning Tree of lowest possible total weight.�
  • Multiple algorithms that are used to find 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

6 of 44

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

7 of 44

BFS

Dijkstra’s

Prim’s & Kruskal’s

Both find Shortest Paths Trees (SPTs)!

Finds MST!

  • Unweighted
  • Shortest path (number of edges) from S to every other node
  • Weighted
  • Shortest path (sum of edge weights) from S to every other node
  • Weighted, undirected
  • Lowest cumulative weight for whole tree

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

8 of 44

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!]

9 of 44

Prim’s Walkthrough

Try the problem on your worksheet now!

10 of 44

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”!

11 of 44

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

12 of 44

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

13 of 44

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)

14 of 44

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)

15 of 44

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)

16 of 44

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

17 of 44

Resulting MST!

A

C

B

D

2

1

3

E

5

G

F

0

6

18 of 44

Kruskal’s Walkthrough

Try the problem on your worksheet now!

19 of 44

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

20 of 44

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

21 of 44

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)

22 of 44

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!

23 of 44

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

24 of 44

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!

25 of 44

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

26 of 44

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!

27 of 44

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)

28 of 44

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

29 of 44

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

30 of 44

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

31 of 44

Resulting MST!

A

B

E

C

D

G

F

4

2

3

1

3

2

32 of 44

Case Study: Project Sidewalk

33 of 44

Inaccessible Paths

Consider the following scenario…

  • The grand staircase vs. ramp
    • Which one is shorter? Which one is more accessible?
  • Let's check out more examples from Project Sidewalk!

34 of 44

Naive Approach

35 of 44

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”

  • If yes, move on to other edges
    • Don’t allow it to be part of our graph setup for Dijkstra’s!
  • If no, add it into the edge set

Inaccessible edges

S → Y

S → T

Z → X

36 of 44

Shortest Paths Project: Naive Approach Implementation

Insert if-checks towards each edge here, where we add the edge information to build the graph

37 of 44

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

38 of 44

Naive Approach: Potential drawbacks to consider…

  • What if we accidentally disconnect the graph?
    • The criterion for shortest path could potentially eliminate all paths : (

  • Example:

Set of inaccessible edges:

S –> T

S –> Y

A disconnected graph!

39 of 44

New Approach!

40 of 44

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

41 of 44

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

42 of 44

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?

43 of 44

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:

  • Safety of the path

44 of 44

Closing Announcements

  • Thanks for coming to section! See you tomorrow in class! Stay safe and stay hydrated :)
  • Remember analysis for Shortest Paths is due tomorrow night!

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!