CSCI 3080 - Discrete Structures

Lab 5 - Finding the Shortest Path with Dijkstra’s Algorithm

1. Objective

In this lab, students will build an interactive program in C++ that loads a graph, displays the adjacency matrix, and computes the shortest path between cities using Dijkstra’s algorithm. The program includes a menu for user interaction. By the end of this lab, students will be able to:

2. Lab Instructions

Step 1: Initialize Graph and City List

Step 2: Map City Names to Indices

            unordered_map<string, int> cityToIndex;
            for (int i = 0; i < cityNames.size(); ++i) {
                string lowercase = cityNames[i];
                transform(lowercase.begin(), lowercase.end(), lowercase.begin(), ::tolower);
                cityToIndex[lowercase] = i;
            }
    

Step 3: Build an Interactive Menu

Step 4: Implement Option 1 - Display Matrix

Step 5: Implement Option 2 - Dijkstra's Algorithm

Step 6: Exit Program

3. Requirements

4. Sample Input/Output

        ===== TN Shortest Path Finder =====
        1. Display Adjacency Matrix
        2. Find Shortest Path (Dijkstra)
        3. Exit
        Enter your choice (1-3): 1
        
        Adjacency Matrix (Distances in miles):
                      Murfreesboro   Nashville Chattanooga   Knoxville     Memphis    Franklin Clarksville     Johnson     Jackson  Cookeville
          Murfreesboro           0          35         150         INF         INF          40         INF         INF         INF          80
             Nashville          35           0         130         INF         210          20          50         INF         INF          70
           Chattanooga         150         130           0          80         310         INF         INF         INF         INF          90
             Knoxville         INF         INF          80           0         INF         INF         INF          90         INF          70
               Memphis         INF         210         310         INF           0         190         230         INF          85         INF
              Franklin          40          20         INF         INF         190           0          60         INF         INF         INF
           Clarksville         INF          50         INF         INF         230          60           0         INF         140         INF
               Johnson         INF         INF         INF          90         INF         INF         INF           0         INF         INF
               Jackson         INF         INF         INF         INF          85         INF         140         INF           0         INF
            Cookeville          80          70          90          70         INF         INF         INF         INF         INF           0
        
        ===== TN Shortest Path Finder =====
        1. Display Adjacency Matrix
        2. Find Shortest Path (Dijkstra)
        3. Exit
        Enter your choice (1-3): 2
        Enter starting city: Murfreesboro
        Enter ending city: Memphis
        
        Shortest path: Murfreesboro → Franklin → Memphis
        Total distance: 230 miles
        
        ===== TN Shortest Path Finder =====
        1. Display Adjacency Matrix
        2. Find Shortest Path (Dijkstra)
        3. Exit
        Enter your choice (1-3): 2
        Enter starting city: Nashville
        Enter ending city: Boston
        Error: "Boston" is not found in the graph.
        
        ===== TN Shortest Path Finder =====
        1. Display Adjacency Matrix
        2. Find Shortest Path (Dijkstra)
        3. Exit
        Enter your choice (1-3): 3
        Exiting program. Goodbye!
    

5. Grading Criteria

Grading Criteria (Total Points: 100)
Category Criteria Points
Graph Setup
(10 points)
The adjacency matrix and city list are properly defined 10
Menu Loop
(15 points)
Menu is functional with input handling 15
Dijkstra’s Algorithm
(30 points)
Correct and complete algorithm logic 30
Path Output
(10 points)
Displays correct path and distance 10
Error Handling
(10 points)
Handles invalid inputs gracefully 10
Main Function Implementation
(10 points)
All functions are correctly invoked in the main function 10
Code Readability & Comments
(7 points)
Maintain consistent indentation and spacing throughout the code. 2
Include comments that explain the purpose of key sections and logic. 5
Test Cases & Output Accuracy
(5 points)
The output for different test cases is correct and consistent. 5
AI disclaimer
(3 points)
Please put an AI disclaimer at the top of your source code as comments. 3
Errors
Your program has syntax errors, compiling errors, running errors, or infinite loops. -50 points

6. Comments

7. Submit

Please submit your Lab 5 source code file: lab5.cpp in D2L!

8. Jupyter Hub for Programming

Jupyter Hub for Programming
By logging in to Jupyter Hub with your MTSU credentials, you can create, compile, and run your source code there.

9. Compile and Run the Source Code


Congratulations! You have finished your Lab5!