Reading 04

Readings

The focus of the reading is graphs, specifically adjacency list and adjacency matrix representation, and depth-first search

The readings for Thursday, February 14 are:

  1. Data Structures and Other Objects Using C++:

    • 15.1 - 15.3

Alternative

If you do not have the primary textbook, you can use the following free alternative texts instead:

  1. Data Structures & Algorithm Analysis

    • 11.1 - 11.3
  2. Open Data Structures

  3. Algorithms

Questions (due 2/14, 10:30am)

Once you have completed the readings, answer the following questions:

  1. Briefly explain what it means for a graph to be:

    A. Connected?

    B. Acyclic?

    C. Directed?

    D. Simple?

    E. Weighted?

  2. What is a reason for using an adjacency matrix instead of an adjacency list to represent a graph?

  3. Conversely, what is a reason for using an adjacency list instead of an adjacency matrix to represent a graph?

  4. Given the following non-recursive implementation of depth-first search:

    struct Graph {
        map<string, set<string>> adj; // Adjacency List/Set
    };
    
    void traversal(Graph &g, const string &s) {
        stack<string> q;
        set<string>   v;
    
        // TODO
    
        while (!q.empty()) {
            // TODO
        }
    }
    

    A. Complete the implementation of depth-first search by filling in the TODO sections with the appropriate C++ code. Remember to:

    B. What is the purpose of stack<string> q?

    C. What is the purpose of set<string> v?

  5. Submission

    To submit your reading assignment, you must upload a text/PDF file onto Canvas prior to class