Question

Please answer this question. This is review for exam. Explain as possible and use C++ code.
A B F C E Using the attached graph, write a program that will find the shortest path from a given vertex to a given vertex. Y

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ code for above problem

#include<iostream>
using namespace std;

#define V 6
#define MAX 9999999

void floyd_warshall(int graph[][V],int distances[][V])
{
   for(int i=0;i<V;i++)
   {
        for(int j=0;j<V;j++)
        {
           distances[i][j]=graph[i][j];
           if(i!=j && distances[i][j]==0)
           {
               distances[i][j]=MAX;
           }
        }
    }

   for(int k=0;k<V;k++)
    {
        for(int i=0;i<V;i++)
       {
            for(int j=0;j<V;j++)
            {
               distances[i][j]=min(distances[i][j],distances[i][k]+distances[k][j]);
            }
        }
    }
}  

int main()
{
   int graph[V][V]={{0,1,0,0,1,0},
                   {1,0,1,0,0,1},
                   {0,1,0,1,0,0},
                   {0,0,1,0,1,0},
                   {1,0,0,1,0,1},
                   {0,1,0,0,1,0},
                   };
                  
   int distances[V][V];
   floyd_warshall(graph,distances);
  
   char src,dest;
   cout << "Enter source vertex: ";
   cin >> src;
  
   cout << "Enter destination vertex: ";
   cin >> dest;
  
   cout << "Distance from " << src << " to " << dest << ": " << distances[src-'A'][dest-'A'] << endl;
  
   return 0;
}

Sample output

Enter source vertex: A Enter destination vertex: D Distance from A to D: 2

Mention in comments if any mistakes or errors are found. Thank you.

Add a comment
Know the answer?
Add Answer to:
Please answer this question. This is review for exam. Explain as possible and use C++ code....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write a program that will find the shortest path from a given vertex to a given...

    write a program that will find the shortest path from a given vertex to a given vertex. Your program must allow the user to enter any start vertex and any destination vertex and output the shortest path. A B F E D Using the attached graph, write a program that will find the shortest path from a given vertex to a given vertex. Your program must allow the user to enter any start vertex and any destination vertex and output...

  • Please post following code with criteria below... please leave as many comments as possible Exam Four:...

    Please post following code with criteria below... please leave as many comments as possible Exam Four: the election Outcome: Student will demonstrate all core competencies from this class: Program Design (design tools) Variables Decision Error Checking Looping Functions Arrays Program Specifications: Let’s pretend that we are tracking votes for the next presidential election. There will be two candidates: Ivanka Trump and Michele Obama. You can assume that there are fifty states casting votes. You will do not need to deal...

  • ***** running late, mere 3 hours left for due time, please help ** #needed in c++...

    ***** running late, mere 3 hours left for due time, please help ** #needed in c++ #inputfile MinPath2.txt 0   SF 1   LA 2   DALLAS 3   CONCORD 4   PHOENIX 5   CHICAGO 6   ST LOUIS 7   BOSTON 8   NY 9   LONDON 10   PARIS 11   TOKYO 12   BANGKOK 13   MEXICO CITY 14   MONTREAL -1   0   1   40 0   2   100 0   4   130 0   8   200 0   9   800 0   10   900 1   2   50 1   3   80 1   4   70 1   8  ...

  • PLEASE READ AND CODE IN BASIC C++ CODE. ALSO, PLEASE CODE USING THIS DO WHILE LOOP...

    PLEASE READ AND CODE IN BASIC C++ CODE. ALSO, PLEASE CODE USING THIS DO WHILE LOOP AND FORMAT: #include <iostream> using namespace std; int main() { //variables here    do { // program here             }while (true);    } Objectives To learn to code, compile and run a program containing SELECTION structures Assignment Write an interactive C++.program to have a user input the length of three sides of a triangle. Allow the user to enter the sides...

  • Dijkstra’s Algorithm: You have to implement the Dijkstra’s algorithm and apply it on the graph provided below. You have to take the input from the user as an adjacency matrix representing the graph,...

    Dijkstra’s Algorithm: You have to implement the Dijkstra’s algorithm and apply it on the graph provided below. You have to take the input from the user as an adjacency matrix representing the graph, the source, the destination. Then you have to apply the Dijkstra’s algorithm to find the shortest path from the source and the destination, and  find the shortest route between the source and the destination. For the input you have to read it from a file. It will...

  • Assignment 14.3: Valid Email (10 pts) image source Write a program that takes as input an...

    Assignment 14.3: Valid Email (10 pts) image source Write a program that takes as input an email address, and reports to the user whether or not the email address is valid. For the purposes of this assignment, we will consider a valid email address to be one that contains an @ symbol The program must allow the user to input as many email addresses as desired until the user enters "q" to quit. For each email entered, the program should...

  • Please write a code in C that will allow the user to choose to calculate voltage,...

    Please write a code in C that will allow the user to choose to calculate voltage, resistance or current. Then the program will prompt the user to enter the necessary values and output the result.

  • CSC 430              GRAPH PROJECT Implement a graph class using an adjacency matrix or an adjacency list....

    CSC 430              GRAPH PROJECT Implement a graph class using an adjacency matrix or an adjacency list. The class should have a constructor, a copy constructor, a destructor and all the methods necessary to add/delete vertices, add/delete edges… Write a menu-driven program to THOROUGHLY check your class and all the functions included in your program. You can choose the data type. Allow the user to continue with operations as long as he/she wants to. Your program should check if an operation...

  • CAN SOMEONE PLEASE HELP ME WRITE THIS CODE IN C++, PLEASE HAVE COMMENTS IN THE CODE...

    CAN SOMEONE PLEASE HELP ME WRITE THIS CODE IN C++, PLEASE HAVE COMMENTS IN THE CODE IF POSSIBLE!! General Description: Write a program that allows the user to enter data on their friends list. The list holds a maximum of 10 friends, but may have fewer. Each friend has a name, phone number, and email address. The program first asks the user to enter the number of friends (1-10). You are not required to validate the entry, but you may...

  • C# Web Application, using ASP.Net Web Forms Site template. Please include any and all code for...

    C# Web Application, using ASP.Net Web Forms Site template. Please include any and all code for any and all files necessary for the program to run correctly! 7. Create a Web application that enables the user to enter first name, last name, and e-mail address. Accept those values and store them in a text file. Allow the user to input the path where the file should be stored. After retrieving the values from the user, display on the web page...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT