S-111
  • Home
  • Lectures
  • Problem Sets
  • Sections
  • Syllabus
  • Schedule
  • Staff
  • Resources
  • Canvas
  • Ed Discussion
  • Gradescope

Section 24

  • Course Evaluations
  • Graphs
    • Graph basics
    • Graph traversals
    • Minimum spanning trees
    • Finding shortest paths

Course Evaluations

Before we start today’s section, we ask that you complete the course evaluations for this class. The evaluations can be found here. These submissions are anonymous, so please answer honestly about the course, Dr. Sullivan, and your TAs.

Graphs

Consider the following weighted, undirected graph representing the distances from city to city by way of the Autobahn in Germany.

Graph basics

  1. What are Berlin’s neighbors in the graph? 1

  2. Is this graph connected? Why or why not? 2

  3. Is it complete? Why or why not? 3

  4. Is this graph acyclic? If the graph is cyclic, provide an example of a cycle. 4

  5. If we used an adjacency matrix to represent this graph, what would it look like? Assume that the vertices are numbered alphabetically, starting from zero. 5

Graph traversals

For the following graph traversals, list the order in which the cities would be considered and draw the resulting spanning tree.

  1. Perform a depth-first traversal of the graph, starting from Düsseldorf. List the order in which cities are considered and draw the resulting spanning tree. 6

  2. Perform a breadth-first traversal of the graph, starting from Berlin. List the order in which cities are considered and draw the resulting spanning tree. 7

Minimum spanning trees

  1. Use Prim’s minimum spanning tree algorithm to obtain a minimum-cost spanning tree from the graph. Start by adding Hamburg to the singleton set. Then list the edges added to the tree as the algorithm runs, and draw the resulting tree. 8

Finding shortest paths

  1. Use Dijkstra’s shortest path algorithm to obtain the shortest paths starting from Cologne. 9

  1. Berlin’s neighbors are Hamburg, Düsseldorf, Cologne, and Stuttgart. ↩

  2. This graph is connected, since there is a path from one vertex to any other vertex in the graph. ↩

  3. This graph is not complete, since there is not an edge from each vertex to every other vertex in the graph. ↩

  4. This graph has at least one cycle. It is therefore cyclic, or not acyclic. An example of a cycle is the one formed among Hamburg, Berlin, and Düsseldorf. ↩

  5. Here’s the adjacency matrix. Each vertex is labeled with the first letter of its name.

    BCDFHMS
    B -580567293640
    C 580-39191
    D 56739-401
    F 191-204
    H 293401-
    M -233
    S 640204233-
     ↩

  6. A depth-first traversal from Düsseldorf results in the following sequence of visited vertices: Düsseldorf, Cologne, Frankfurt am Main, Stuttgart, Munich, Berlin, Hamburg. Here’s the spanning tree:

    The spanning tree obtained from depth-first search.
     ↩

  7. A breadth-first traversal from Berlin results in the following sequence of visited vertices: Berlin, Hamburg, Düsseldorf, Cologne, Stuttgart, Frankfurt, Munich.

    The spanning tree obtained from breadth-first search.
     ↩

  8. Here’s the table showing the edges added to set A from set B, where A initially contains Hamburg. Each city is denoted by the first letter in its name.

    edge added set A set B
    H B, C, D, F, M, S
    B–H B, H C, D, F, M, S
    D–H B, D, H C, F, M, S
    C–D B, C, D, H F, M, S
    C–F B, C, D, F, H M, S
    F–S B, C, D, F, H, S M
    M–S B, C, D, F, H, M, S

    ↩

  9. Here’s the table showing each step of Dijkstra’s algorithm. We start by finalizing Cologne’s distance of zero.

    city
    Berlin ∞ 580 580 580 580 580
    Cologne 0
    Düsseldorf ∞ 39
    Frankfurt ∞ 191 191
    Hamburg ∞ ∞ 440 440 440
    Munich ∞ ∞ ∞ ∞ 628 628 628
    Stuttgart ∞ ∞ ∞ 395

    ↩

Last updated on August 4, 2026.