Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I ' m trying to find a solution to a complete undirectected graph. My idea is to run nearest neighbor on it then use a
Im trying to find a solution to a complete undirectected graph. My idea is to run nearest neighbor on it then use a chepest link clustering algortihm to improve that path. here is my code. However my cheapest link algorithm do
This describes a graph with three nodes.
am
clude
#include
#include
#include
using namespace std;
const int INF INTMAX;
Function to find the minimum value and corresponding indices in a matrix
void findMinValueconst vector& matrix, int& minValue, int& minRow, int& minCol
minValue INF;
for int i ; i matrix.size; i
for int j ; j matrixisize; j
if j minCol && matrixij minValue
minValue matrixij;
minRow i;
minCol j;
Function to apply Cheapest Link Algorithm on a graph
void cheapestLinkvector& graph
int n graph.size;
Initialize clusters with individual vertices
vector clusters;
for int i ; i n; i
clusters.pushbacki;
int totalDistance ;
vector nodesVisited;
while clusterssize
int minValue INF;
int minRow minCol ;
Find the minimum value in the graph
findMinValuegraph minValue, minRow, minCol;
Add a check for valid minRow and minCol
if minRow minCol
cout No valid minimum value found. Exiting." endl;
break;
Merge clusters minRow and minCol
vector mergedCluster;
mergedCluster.reserveclustersminRowsize clustersminColsize;
mergedCluster.insertmergedClusterend clustersminRowbegin clustersminRowend;
mergedCluster.insertmergedClusterend clustersminColbegin clustersminColend;
Update the overall distance
totalDistance graphminRowminCol
Update nodesVisited
nodesVisited.insertnodesVisitedend clustersminRowbegin clustersminRowend;
nodesVisited.insertnodesVisitedend clustersminColbegin clustersminColend;
Create a new graph to store the updated distances
vector updatedGraphn vectorn INF;
Update the new graph with merged clusters
for int i newI ; i n; i
if i minRow && i minCol
for int j newJ ; j n; j
if j minRow && j minCol
updatedGraphnewInewJ graphij;
newJ;
newI;
Add the distances from the merged cluster to other clusters in the new graph
for int i ; i n ; i
int minG mingraphminRowclustersi graphminColclustersi;
updatedGraphin minG;
updatedGraphn i minG;
Add a new row and column for the merged cluster in the new graph
updatedGraphn n ;
Update clusters with the merged clusters
clusters.pushbackmergedCluster;
Update the graph to the new graph
graph moveupdatedGraph;
Print the order of nodes traversed
cout "Order of nodes traversed: ;
for int node : nodesVisited
cout node ;
cout endl;
Print the overall distance traveled
cout "Overall distance traveled: totalDistance endl;
Function to apply Nearest Neighbor Algorithm on a graph
void nearestNeighborconst vector& graph, int startVertex
int n graph.size;
vector visitedn false;
vector tour;
tour.pushbackstartVertex;
visitedstartVertex true;
for int i ; i n; i
int currentVertex tour.back;
int nextVertex ;
int minDistance INF;
for int j ; j n; j
if visitedj && graphcurrentVertexj minDistance
minDistance graphcurrentVertexj;
nextVertex j;
if nextVertex
tour.pushbacknextVertex;
visitednextVertex true;
Print the tour obtained from Nearest Neighbor Algorithm
cout "Tour obtained from Nearest Neighbor Algorithm starting from vertex startVertex :
;
for int vertex : tour
cout vertex ;
cout
;
int main
Open the file containing the graph
ifstream inputFile;
if inputFile.isop
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started