Question

In: Computer Science

I wanted c++ program for the question Problem 6 - Weighted Graph In the vertex and...

I wanted c++ program for the question

Problem 6 - Weighted Graph
In the vertex and edge structure defined below
Vertex        Edge   Vertex
SFO        2702   BOS
SFO        1846   ORD
ORD         867   BOS
ORD         742   JFK
JFK         189   BOS
SFO        1464   DFW
DFW         802   ORD
DFW        1123   MIA
MIA        1092   JFK
MIA        1258   BOS
SFO         339   LAX
LAX        1235   DFW
LAX        2342   MIA

a) Find the shortest distance between
ORD and LAX.

b) Find the shortest distance between
JFK and SFO.

c) Find the minimum spanning tree.

Solutions

Expert Solution

Please find the C++ program source code below. Given the small and static number of vertices and edges, the program uses the Floyd Warshall Algorithm to compute the shortest distance between vertices and uses Kruskal's Algorithm to find the minimum spanning tree. Also added comments for a clear understanding of the program and the steps involved.

C++ Program

#include<bits/stdc++.h>
#define pp pair < long long, long long >
#define MAX_D 100001
#define edge pair < int, int >

using namespace std;

int dist[10][10];
vector < pair < int, edge > > Graph, MST;
int n, e, parent[100];

void initialiseGraph() {
/* Map the cities to indexes for simplification
SFO -0
BOS -1
ORD -2
JFK -3
DFW -4
MIA -5
LAX -6
*/

// Intialise distance between cities to MAX_D initially for FloydWarshall Graph Algorithm
for (int i = 0; i <= 6; i++) {
for (int j = 0; j <= 6; j++) {
if (i == j)
dist[i][j] = 0;
else
dist[i][j] = MAX_D;
}
}

//Add edges as given in the problem and update the dist array
dist[0][1] = 2702;
dist[0][2] = 1846;
dist[2][1] = 867;
dist[2][3] = 742;
dist[3][1] = 189;
dist[0][4] = 1464;
dist[4][2] = 802;
dist[4][5] = 1123;
dist[5][3] = 1092;
dist[5][1] = 1258;
dist[0][6] = 339;
dist[6][4] = 1235;
dist[6][5] = 2342;

//Assuming its an undirected graph so for any edge u-->v there exists edge v-->u with same weight
dist[1][0] = 2702;
dist[2][0] = 1846;
dist[1][2] = 867;
dist[3][2] = 742;
dist[1][3] = 189;
dist[4][0] = 1464;
dist[2][4] = 802;
dist[5][4] = 1123;
dist[3][5] = 1092;
dist[1][5] = 1258;
dist[6][0] = 339;
dist[4][6] = 1235;
dist[5][6] = 2342;

//Initialise parent and add edges as given in the problem to Graph for finding Minimum Spanning Tree(MST)
for (int i = 0; i <= 6; i++) {
parent[i] = i;
}
MST.clear();
Graph.clear();

Graph.push_back(pair < int, edge > (2702, edge(0, 1)));
Graph.push_back(pair < int, edge > (1846, edge(0, 2)));
Graph.push_back(pair < int, edge > (867, edge(2, 1)));
Graph.push_back(pair < int, edge > (742, edge(2, 3)));
Graph.push_back(pair < int, edge > (189, edge(3, 1)));
Graph.push_back(pair < int, edge > (1464, edge(0, 4)));
Graph.push_back(pair < int, edge > (802, edge(4, 2)));
Graph.push_back(pair < int, edge > (1123, edge(4, 5)));
Graph.push_back(pair < int, edge > (1092, edge(5, 3)));
Graph.push_back(pair < int, edge > (1258, edge(5, 1)));
Graph.push_back(pair < int, edge > (339, edge(0, 6)));
Graph.push_back(pair < int, edge > (1235, edge(6, 4)));
Graph.push_back(pair < int, edge > (2342, edge(6, 5)));

Graph.push_back(pair < int, edge > (2702, edge(1, 0)));
Graph.push_back(pair < int, edge > (1846, edge(2, 0)));
Graph.push_back(pair < int, edge > (867, edge(1, 2)));
Graph.push_back(pair < int, edge > (742, edge(3, 2)));
Graph.push_back(pair < int, edge > (189, edge(1, 3)));
Graph.push_back(pair < int, edge > (1464, edge(4, 0)));
Graph.push_back(pair < int, edge > (802, edge(2, 4)));
Graph.push_back(pair < int, edge > (1123, edge(5, 4)));
Graph.push_back(pair < int, edge > (1092, edge(3, 5)));
Graph.push_back(pair < int, edge > (1258, edge(1, 5)));
Graph.push_back(pair < int, edge > (339, edge(6, 0)));
Graph.push_back(pair < int, edge > (1235, edge(4, 6)));
Graph.push_back(pair < int, edge > (2342, edge(5, 6)));
}

//This method calculates shortest distance from each vertex to every other vertex and stores in the dist array
void floydWarshall() {
for (int k = 0; k <= 6; k++) {
for (int i = 0; i <= 6; i++) {
for (int j = 0; j <= 6; j++) {
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}

//Returns the parent of vertex x
int findSet(int x, int * parent) {
if (x != parent[x]) {
parent[x] = findSet(parent[x], parent);
}
return parent[x];
}

//Constructs a Minimum Spanning tree and store results in MST using Kruskal's Algorithm
void kruskal() {
sort(Graph.begin(), Graph.end());
for (int i = 0; i < Graph.size(); i++) {
int pu = findSet(Graph[i].second.first, parent);
int pv = findSet(Graph[i].second.second, parent);
if (pu != pv) {
MST.push_back(Graph[i]);
parent[pu] = parent[pv];
}
}
}

int main() {
initialiseGraph();
floydWarshall();
cout << "Distance between ORD and LAX is " << dist[2][6] << endl;
cout << "Distance between JFK and SFO is " << dist[3][0] << endl;

/*Apply Kruskal's algorithm and print the minimum spanning tree as u-->v w where u,v are vertices and w denotes the
edge weight */
kruskal();
for (int i = 0; i < MST.size(); i++)
cout << MST[i].second.first << " " << MST[i].second.second << " " << MST[i].first << endl;

}


Related Solutions

Python 3 question Suppose that a directed and weighted graph represented as vertex-list, how could it...
Python 3 question Suppose that a directed and weighted graph represented as vertex-list, how could it perform uniform cost search by using priority queue?
Python3 question. Suppose I have a undirect and unweighted graph with vertex list type, here is...
Python3 question. Suppose I have a undirect and unweighted graph with vertex list type, here is the code of the graph. graph = { "a" : ["c"], "b" : ["c", "e"], "c" : ["a", "b", "d", "e"], "d" : ["c"], "e" : ["c", "b"], "f" : [] } I want to use this to create a DFS method. How can I use recursion only to finish the work?
Python3 question. Suppose I have a undirect and unweighted graph with vertex list type, here is...
Python3 question. Suppose I have a undirect and unweighted graph with vertex list type, here is the code of the graph. graph = { "a" : ["c"], "b" : ["c", "e"], "c" : ["a", "b", "d", "e"], "d" : ["c"], "e" : ["c", "b"], "f" : [] } I want to use this to create a DFS method. How can I use stack only to finish the work?
Python3 question. Suppose I have a undirect and unweighted graph with vertex list type, here is...
Python3 question. Suppose I have a undirect and unweighted graph with vertex list type, here is the code of the graph. graph = { "a" : ["c"], "b" : ["c", "e"], "c" : ["a", "b", "d", "e"], "d" : ["c"], "e" : ["c", "b"], "f" : [] } I want to use this to create a BFS method. How can I use queue only to finish the work?
Suppose you have an all positive edge-weighted graph G and a start vertex a and asks...
Suppose you have an all positive edge-weighted graph G and a start vertex a and asks you to find the lowest-weight path from a to every vertex in G. Now considering the weight of a path to be the maximum weight of its edges, how can you alter Dijkstra’s algorithm to solve this?
C Programming Question: Q) Write a C - program to check whether a given graph is...
C Programming Question: Q) Write a C - program to check whether a given graph is Bipartite using Breadth-first search (adjacency list) Do take your time but please do post full code and also please do it in C.
Let S = {a, b, c}. Draw a graph whose vertex set is P(S) and for...
Let S = {a, b, c}. Draw a graph whose vertex set is P(S) and for which the subsets A and B of S are adjacent if and only if A ⊂ B and |A| = |B| − 1. (a) How many vertices and edges does this graph have? (b) Can you name this graph? (c) Is this graph connected? (d) Does it have a perfect matching? If yes, draw a sketch of the matching. (e) Does it have a...
I need assistance on this problem in Pseudocode and in C++ Program Program 3: Give a...
I need assistance on this problem in Pseudocode and in C++ Program Program 3: Give a baby $5,000! Did you know that, over the last century, the stock market has returned an average of 10%? You may not care, but you’d better pay attention to this one. If you were to give a newborn baby $5000, put that money in the stock market and NOT add any additional money per year, that money would grow to over $2.9 million by...
Using C++: Create a function to search an undirected weighted graph and find the highest weighted...
Using C++: Create a function to search an undirected weighted graph and find the highest weighted edge between two specific values. This should include a class declaration and the definition for all required members that are needed to support the search. NO NEED to code those members. You can assume any other data structures such as stack, heap, or linked list is defined (so you can use their standard functions without declaring or defining them).
hey I have this program written in c++ and I have a problem with if statement...
hey I have this program written in c++ and I have a problem with if statement {} brackets and I was wondering if someone can fix it. //Name: Group1_QueueImplementation.cpp //made by ibrahem alrefai //programming project one part 4 //group members: Mohammad Bayat, Seungmin Baek,Ibrahem Alrefai #include <iostream> #include<stdlib.h> using namespace std; struct node { string data; struct node* next; }; struct node* front = NULL; struct node* rear = NULL; struct node* temp; void Insert() {     string val;    ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT