1 of 50

CSE 373 AX SP25 Section 7

Graphs!!! Woo!

2 of 50

Graph Types - Warm Up Question

a

b

d

c

a

b

d

c

e

a

b

d

c

a

b

d

c

Word Bank:

  • Acyclic
  • Cyclic
  • Undirected
  • Directed
  • Vertex of degree 3

A

B

C

D

Which graphs are associated

with each category below?

3 of 50

Check-in

What's one place you want to visit? Alternatively, can you name all these places?

4 of 50

Announcements

  • Two-Stage Exam 2 Revisions: Due tonight (05/15) at 11:59 PM!
  • Autocomplete Resubs: Due tomorrow (05/16) night at 11:59 PM! (Resub Policies)
    • Remember to create a shared doc for graphs!
    • Remember to read TA feedback before providing new answers!
    • Create a regrade request for every portion you want regraded
    • AI/Citation Violations - need to show considerable work that is your own and deeper explanations
  • OH Reminders
    • TAs are here as a guide to facilitate your problem-solving process, we cannot pre-grade or give explicit answers - please be patient with us :)
    • We may need to pivot to Ed or other resources if we cannot help during OH, especially for setup issues or random bugs in VSCode

5 of 50

Resources

  • Graph Terminology
  • BFS
  • DFS

6 of 50

Graph Traversal (DFS)

7 of 50

Graph Traversal: DFS

  • A way to traverse a graph
  • Starts at the the root node and explores as far as possible along each branch and then backtracking (all the way down)

8 of 50

DFS Pseudocode (Iterative)

dfs(Graph graph, Vertex start) {

// stores the remaining vertices to visit in the DFS

Stack<Vertex> perimeter = new Stack<>();

// stores the set of visited vertices so we don't revisit them multiple times

Set<Vertex> visited = new Set<>();

// kicking off our starting point by adding it to the perimeter

perimeter.add(start);

while (!perimeter.isEmpty()) {

Vertex from = perimeter.remove();

if (!visited.contains(from)) {

for (E edge : graph.outgoingEdgesFrom(from)) {

Vertex to = edge.to();

perimeter.add(to);

}

visited.add(from);

}

}

}

9 of 50

Graph Traversal: DFS (HTML Graph)

Stack:��T��Current Node:��Visited:��

H1

T

L1

L2

H2

L3

P1

(More-recently inserted items go on the right)

T: Title

H: Heading

L: Link

P: Paragraph

Note: We have not provided any sequential ordering relation, but you may opt to process as: T < H < L < P and 1 < 2 < 3.

10 of 50

Graph Traversal: DFS

Stack:����Current Node:�TVisited:��

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

T is popped from the stack to then process its unvisited neighbors.

11 of 50

Graph Traversal: DFS

Stack:��H2 H1 ��Current Node:�T��Visited:�T

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

H1 and H2 are unvisited, here they are added in such a way that we will process the one in lower sequential order first.

Done with T = add to visited

If no ordering relation is provided, it is up to you to choose!

12 of 50

Graph Traversal: DFS (ON YOUR WORKSHEET NOW!)

Stack:��H2 ��Current Node:H1Visited:T

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

Process H1 to then add in its unvisited neighbors. (Pop from stack)

13 of 50

Graph Traversal: DFS

Stack:��H2 L2 L1��Current Node:�H1��Visited:�T H1

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

Add in the unvisited nodes, to choose L1 next (sequential)

Done with H1: Add to visited

14 of 50

Graph Traversal: DFS

Stack:��H2 L2��Current Node:�L1��Visited:�T H1

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

Process L1 and its unvisited neighbors (none), popped from stack

15 of 50

Graph Traversal: DFS

Stack:��H2 L2 ��Current Node:�L1Visited:�T H1 L1

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

Mark L1 as visited (done processing)

16 of 50

Graph Traversal: DFS

Stack:��H2��Current Node:�L2Visited:�T H1 L1

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

Process L2 and its unvisited neighbors (none), popped from stack

17 of 50

Graph Traversal: DFS

Stack:��H2��Current Node:�L2Visited:�T H1 L1 L2

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

Mark L2 as visited (done processing)

18 of 50

Graph Traversal: DFS

Stack:����Current Node:�H2Visited:�T H1 L1 L2

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

Process H2 to then add in its unvisited neighbors. (Pop from stack)

19 of 50

Graph Traversal: DFS

Stack:��P1 L3��Current Node:�H2��Visited:�T H1 L1 L2 H2

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

Add in the unvisited nodes, to choose L3 next (sequential)

Done with H2: Add to visited

20 of 50

Graph Traversal: DFS

Stack:��P1��Current Node:�L3��Visited:�T H1 L1 L2 H2

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

Pop L3 from stack to process unvisited neighbors (none)

21 of 50

Graph Traversal: DFS

Stack:��P1��Current Node:�L3Visited:�T H1 L1 L2 H2 L3

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

Done with L3: Add to visited

22 of 50

Graph Traversal: DFS

Stack:����Current Node:�P1Visited:�T H1 L1 L2 H2 L3

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

Pop P1 from stack to process unvisited neighbors (none)

23 of 50

Graph Traversal: DFS

Stack:����Current Node:�P1Visited:�T H1 L1 L2 H2 L3 P1

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

Done with P1: Add to visited

24 of 50

Graph Traversal: DFS

Stack:����Current Node:���Visited:�T H1 L1 L2 H2 L3 P1

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

25 of 50

Graph Traversal: DFS

Other Possible Visited orders:�T H1 L1 L2 H2 L3 P1

T H1 L1 L2 H2 P1 L3

T H1 L2 L1 H2 L3 P1

T H1 L2 L1 H2 P1 L3

T H2 L3 P1 H1 L1 L2

T H2 L3 P1 H1 L2 L1

T H2 P1 L3 H1 L1 L2

T H2 P1 L3 H1 L2 L1

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

26 of 50

Graph Traversal (BFS)

27 of 50

Graph Traversal: BFS

  • Level to Level exploration of the graph
  • Like onion layers

28 of 50

BFS Pseudocode

bfs(Graph graph, Vertex start) {

// stores the remaining vertices to visit in the BFS

Queue<Vertex> perimeter = new Queue<>();

// stores the set of visited vertices so we don't revisit them multiple times

Set<Vertex> visited = new Set<>();

// kicking off our starting point by adding it to the perimeter

perimeter.add(start); //

visited.add(start); //

while (!perimeter.isEmpty()) {

Vertex from = perimeter.remove();

for (E edge : graph.outgoingEdgesFrom(from)) {

Vertex to = edge.to();

if (!visited.contains(to)) {

perimeter.add(to);

visited.add(to);

}

}

}

}

29 of 50

Graph Traversal: BFS (HTML Graph)

Perimeter (Queue):�T��Current Node:�T��Visited:��

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

We start at T; it helps establish our “perimeter” as a ‘from’ vertex.

Note: We have not provided any sequential ordering relation, but you may opt to process as: T < H < L < P and 1 < 2 < 3.

For visibility, we will add to visited after processing a node’s neighbors! In the code, you would add to both concurrently.

30 of 50

Graph Traversal: BFS

Perimeter Queue:H1 H2��Current Node:�T��Visited:�T

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

Since T was found and we’re moving forward, we de-queue it and want to look at its neighbors (“to” vertices)

H1 + H2 = Vertex to

So we add H1 and H2 to perimeter.

�Done with T = visited

31 of 50

Graph Traversal: BFS (ON YOUR WORKSHEET NOW!)

Queue:�H2��Current Node:�H1��Visited:�T

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

We go level by level.

Here, we choose to look from H1.

Since H1 was found and we’re moving forward to new edges, we de-queue it and want to look at its neighbors (“to” vertices)

32 of 50

Graph Traversal: BFS

Perim. Queue:�H2 L1 L2��Current Node:�H1��Visited:�T H1

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

We discover H1’s neighbors are L1 and L2. They are new, so we add them to the queue!

We’re done pursuing H1 (no more edges found), so it is added to visited.

We now want to look from H2 (next in queue)

33 of 50

Graph Traversal: BFS

Perim. Queue:�L1 L2��Current Node:�H2��Visited:�T H1

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

We go level by level.

Here, we choose to look from H2.

Since H2 was found and we’re moving forward to new edges, we de-queue it and want to look at its neighbors (“to” vertices)

34 of 50

Graph Traversal: BFS

Perim. Queue:�L1 L2 L3 P1��Current Node:�H2��Visited:�T H1 H2

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

We discover H2’s neighbors are L3 and P1. They are new, so we add them to the queue!

We’re done pursuing H2 (no more edges found), so it is added to visited.

We now want to look from L1 (next in queue)

35 of 50

Graph Traversal: BFS

Perim. Queue:�L2 L3 P1��Current Node:�L1��Visited:�T H1 H2

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

We de-queue L1 from the Queue to look at its neighbors (“to” vertices)

We see there are none, so we’re going to move onto L2 and eventually mark it visited.

36 of 50

Graph Traversal: BFS

Perim. Queue:�L2 L3 P1��Current Node:�L1��Visited:�T H1 H2 L1

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

L1 gets marked as visited as there nothing else to explore (no more children to it and seen before)

37 of 50

Graph Traversal: BFS

Queue:�L3 P1��Current Node:�L2��Visited:�T H1 H2 L1

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

We de-queue L2 from the Queue to look at its neighbors (“to” vertices)

We see there are none, so we’re going to move onto L3 and eventually mark it visited.

38 of 50

Graph Traversal: BFS

Queue:�L3 P1��Current Node:�L2��Visited:�T H1 H2 L1 L2

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

L2 gets marked as visited as there nothing else to explore (no more children to it and seen before)

39 of 50

Graph Traversal: BFS

Queue:�P1��Current Node:�L3��Visited:�T H1 H2 H2 L1

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

We de-queue L3 from the Queue to look at its neighbors (“to” vertices)

We see there are none, so we’re going to move onto P1 and eventually mark it visited.

40 of 50

Graph Traversal: BFS

Queue:�P1��Current Node:�L3��Visited:�T H1 H2 L1 L2 L3

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

L3 gets marked as visited as there nothing else to explore (no more children to it and seen before)

41 of 50

Graph Traversal: BFS

Queue:���Current Node:�P1��Visited:�T H1 H2 L1 L2 L3

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

We de-queue P1 from the Queue to look at its neighbors (“to” vertices)

We see there are none, so we’re going to eventually mark it visited.

42 of 50

Graph Traversal: BFS

Queue:���Current Node:�P1��Visited:�T H1 H2 L1 L2 L3 P1

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

P1 gets marked as visited as there nothing else to explore (no more children to it and seen before)

43 of 50

Graph Traversal: BFS

Queue:���Current Node:���Visited:�T H1 H2 L1 L2 L3 P1

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

DONE!

44 of 50

Graph Traversal: BFS

Other Possible Visited orders:�T H1 H2 L1 L2 L3 P1

T H1 H2 L1 L2 P1 L3

T H1 H2 L2 L1 L3 P1

T H1 H2 L2 L1 P1 L3

T H2 H1 L3 P1 L1 L2

T H2 H1 L3 P1 L2 L1

T H2 H1 P1 L3 L1 L2

T H2 H1 P1 L3 L2 L1

H1

T

L1

L2

H2

L3

P1

T: Title

H: Heading

L: Link

P: Paragraph

45 of 50

Graph Traversal BFS and DFS Summary

  • BFS:
    • Uses a Queue, FIFO
    • Works by prioritizing the items that are “siblings”, or in the same graph layer (those go in the queue first, which we take off first)
    • Layer by layer
    • Used for single-source shortest paths in unweighted graphs
  • DFS:
    • Uses a Stack/Recursion, LIFO
    • Works by prioritizing the items that are deeper down that branch (those go on top of the stack, which we pop off first)
      • Don’t look at next layer until that whole branch is done
    • “origin-to-dead-end” by “origin-to-dead-end”

46 of 50

Summary

  • Both:
    • Algorithm for graph traversal
      • Think about the for-loop for linear data structures
    • Works for both directed and undirected graphs
    • Is not guaranteed to find every single node on directed graphs (unless the graph is strongly connected)

Strongly connected

Not strongly connected

No way to reach 1

47 of 50

Graph Traversal: Design Problem

48 of 50

Multiple source shortest paths

Given a directed graph and multiple possible starting vertices, the multiple source shortest paths problem finds the shortest paths from any starting vertex to every vertex in the graph.

For example, consider starting S = {1, 5, 7}.

  • Shortest path from S to 0 is 760.
  • Shortest path from S to 1 is 1 (no edges).
  • Shortest path from S to 2 is 52.
  • Shortest path from S to 4 is 524.

Ideally, we want the queue to include {1, 5, 7}. Without changing the code for BFS, describe a modification to the graph (add, remove, or change vertices/edges) so that BFS solves the multiple source shortest paths problem.

5

0

1

4

6

3

2

7

8

9

49 of 50

Approach:

  1. Create a new source node (s) as a vertex in the graph
  2. Connect s to all vertices in the original set
  3. S = {1, 5, 7}, inserting 3 new edges

Question for thought: What if this was a weighted graph, and we used a different shortest paths algorithm? What weights should we use?

  1. Now, to find the shortest path from any start vertex, only run BFS starting at node s.
  2. Since it is connected to all nodes in our set, it is guaranteed to find paths that include 1, 5, or 7
  3. When tracing back shortest paths, stop at 1/5/7 to prevent including “s” in result.

5

0

1

4

6

3

2

7

8

9

s

50 of 50

Project Activity: Priority Queues!