TREES WITHIN GRAPHS

Fred Agbo

2026-04-13

Announcements

  • Welcome to week 14!
    • Be encouraged despite the challenges!
  • Mini Project #3 due date is extended to next week Monday April 20 at 10 pm.
  • Grading of PS4 and PS5 are published.
  • No class this Wednesday April 15. It is SSRD Day!

Trees within Graphs

  • Recall the function traverseFromVertex shown in the Psedocode, it implicitly yields a tree rooted at the vertex from which the traversal starts and includes all the vertices reached during the traversal
  • Suppose dfs has just been called using vertex v:
    • If a recursive call using vertex w now occurs, you can consider w to be a child of v The edge (v , w) corresponds to the parent-child relationship, or edge, between v and w
    • The starting vertex is the root of this tree
    • The tree is called a depth-first search tree

Spanning Trees

  • A spanning tree of a connected, undirected graph is a subgraph that includes all the vertices of the graph and is a tree.
  • Every connected graph has at least one spanning tree.
  • Spanning trees are useful for minimizing the number of edges while maintaining connectivity.
  • Minimum Spanning Tree (MST):

Minimum Spanning Tree

  • A Minimum Spanning Tree (MST) is a spanning tree of a weighted, connected, undirected graph with the smallest possible total edge weight.
  • MSTs are used in network design, clustering, and other optimization problems.
  • Common algorithms for finding MSTs include Kruskal’s and Prim’s algorithms.-

Minimum Spanning Tree

  • The graph on the left has the maximum number of edges, 10, for a five-vertex graph.
  • The graph on the right has the same five vertices but with the minimum number of edges necessary to connect them, 4.
  • This constitutes a minimum spanning tree (MST) for the graph.

Minimum Spanning Tree Algorithms

  • Kruskal’s Algorithm:
    • Sort all edges by weight.
    • Add edges one by one to the tree, skipping those that would form a cycle, until all vertices are connected.
  • Prim’s Algorithm:
    • Start from any vertex.
    • Grow the tree by repeatedly adding the smallest edge that connects a vertex in the tree to a vertex outside.
  • Both algorithms run in \(O(E \log V)\) time, where \(E\) is the number of edges and \(V\) is the number of vertices.

Minimum Spanning Tree

  • Robert C. Prim’s algorithm for finding a minimum spanning tree
minimumSpanningTree(graph):
    mark all vertices and edges as unvisited
    mark some vertex, say v, as visited
    for all the vertices:
        find the least weight edge from a visited vertex to an
        unvisited vertex, say w
        mark the edge and w as visited

Minimum Spanning Tree

  • Here is a weighted graph with six vertices. Each edge has a weight, shown by a number alongside the edge.

  • How can you pick a subgraph that minimizes the cost of connecting vertices into a network?

Minimum Spanning Tree

  • The answer is to calculate a minimum spanning tree.
  • It will have five edges (one fewer than the number of vertices), it will connect all six vertices
  • It will minimize the total cost of the links.

Recall Minimum Spanning Tree

  • The MST for the graph above

  • The minimum spanning tree consists of the edges AB, AC, CD, DE, and EF, for a total edge weight of 28

  • The order in which the edges are specified is unimportant.

Topological Sort

  • A topological sort of a directed acyclic graph (DAG) is a linear ordering of its vertices such that for every directed edge u → v, vertex u comes before v in the ordering.
  • Topological sorting is useful for scheduling tasks, resolving dependencies, and organizing data with precedence constraints.
  • Example Scenario:
    • Imagine a university course catalog where each course may have prerequisites.
    • Each course is a vertex, and a directed edge from course A to course B means “A is a prerequisite for B.”
    • For example:
      • Math 101 → Math 201 (Math 101 is a prerequisite for Math 201)
      • CS 101 → CS 201 (CS 101 is a prerequisite for CS 201)
      • CS 201 → CS 301
      • Math 201 → CS 301
  • Only DAGs have valid topological orders; graphs with cycles do not.

Topological Sort

  • The first topological ordering of the graph

  • The second topological ordering of the graph

Class Worksheet Activity:

Activity 1: Spanning Tree Basics

Question 1: A connected graph has 8 vertices. How many edges must its spanning tree have? Why?

Question 2: True or False? A graph can have multiple different spanning trees. If true, provide an example scenario.

Question 3: Can a disconnected graph have a spanning tree? Explain your reasoning.

Activity 2: Minimum Spanning Tree - Prim’s Algorithm

Given the following weighted graph, apply Prim’s Algorithm starting from vertex A:

    A ---5--- B
    |    \    |
    |     \   |
    3      7  6
    |       \ |
    C ---4--- D ---2--- E
         |             |
         8             1
         |             |
         F -----2------G

Tasks: 1. List the order in which edges are added to the MST 2. What is the total weight of the MST? 3. Draw the resulting MST

Activity 3: MST Algorithm Comparison

Question 1: What is the key difference between Kruskal’s and Prim’s algorithm approaches?

Question 2: Both algorithms have the same time complexity. What is it, and what do E and V represent?

Question 3: When would you prefer Kruskal’s over Prim’s algorithm (or vice versa)?

Activity 4: Topological Sort

Given the following course prerequisite graph:

  • CS 101 is a prerequisite for CS 201 and CS 202
  • CS 201 is a prerequisite for CS 301
  • CS 202 is a prerequisite for CS 301 and CS 302
  • Math 101 is a prerequisite for CS 201
  • Math 101 is a prerequisite for Math 201
  • Math 201 is a prerequisite for CS 301

Tasks: 1. Draw the directed graph representing these dependencies 2. Provide at least TWO valid topological orderings 3. Explain why CS 301 cannot come before CS 201 in any valid ordering

Activity 5: Cycle Detection

Question 1: Can a graph with a cycle have a valid topological sort? Why or why not?

Question 2: Consider these course dependencies: - Course A requires Course B - Course B requires Course C
- Course C requires Course A

What problem exists here? How would you detect this programmatically?

Activity 6: Application Problem

Scenario: You are designing a network to connect 5 cities with fiber optic cables. The cost (in millions) to connect each pair of cities is:

  • City A to B: $4M, A to C: $3M, A to D: $7M
  • City B to C: $2M, B to E: $5M
  • City C to D: $4M, C to E: $6M
  • City D to E: $3M

Tasks: 1. Which algorithm would you use to minimize total cost? Why? 2. Find the minimum cost to connect all cities 3. Which connections should be built? 4. What is the total cost of your solution?

Bonus Challenge: DFS Tree

Given an undirected graph with vertices {A, B, C, D, E, F} and edges: - A-B, A-C, B-D, B-E, C-F, D-E, E-F

Question: If you perform DFS starting from vertex A and always visit neighbors in alphabetical order, draw the resulting DFS tree. Which edges from the original graph are NOT in the DFS tree?