0% found this document useful (0 votes)
23 views7 pages

Dsa Assign 7

The document discusses implementing the Floyd-Warshall algorithm to find the shortest paths between all pairs of vertices in a graph. It defines a Graph class with methods to create an adjacency matrix representation of a graph, run Floyd-Warshall to calculate shortest distances, and display the original and processed distance matrices.

Uploaded by

Riaz Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views7 pages

Dsa Assign 7

The document discusses implementing the Floyd-Warshall algorithm to find the shortest paths between all pairs of vertices in a graph. It defines a Graph class with methods to create an adjacency matrix representation of a graph, run Floyd-Warshall to calculate shortest distances, and display the original and processed distance matrices.

Uploaded by

Riaz Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

#include <iostream>

#include "templateDatastructs.h"

#include <iomanip>

using namespace std;

//////////////////////

class Graph

int V;

int E, INF;

int **Adj; //2D Array

public:

Graph():INF(99999) {}

void createGraph();

void showGraphMatrix();

void floyd_warshall();

void graph_dist_matrix();

};

//////////////////////

void Graph :: createGraph()

int u, v;

// Getting No of Nodes & Edges

ifstream inFile("a1.txt");
inFile>>ch;

V = static_cast<int>(ch) - 48;

//Creating 2D Array

Adj = new int*[V];

for(int i = 0; i < V; ++i) {

Adj[i] = new int[V];

//Filling Array with '0'

for(u = 0; u < V; u++)

for(v = 0; v < V; v++)

Adj[u][v] = 0;

//Now Fillling Array with right Values

while (!inFile.eof())

inFile >> ch;

u = static_cast<int>(ch) - 48;

inFile >> ch;

v = static_cast<int>(ch) - 48;

/* inFile >> ch;

e1.weight = static_cast<int>(ch) - 48;*/

Adj[u][v] = 1;
Adj[v][u] = 1; //As Graph is Undirected, So BOTH has to be Filled

inFile.close();

/////////////////////////

void Graph :: showGraphMatrix()

cout<<endl;

// for(int i = 0; i < V; i++) cout<<"("<<i<<")\t";

for(int u = 0; u < V; u++)

// cout<<"\n("<<u<<") ";

for(int v = 0; v < V; v++)

if(Adj[u][v] == INF) cout<<setw(7)<<"INF";

else cout<<setw(7)<<Adj[u][v];

// cout<<*(Adj+((V*u)+v)*sizeof(int))<<"\t";

cout<<endl;

////////////////////////

void Graph ::graph_dist_matrix()

cout<<"\nEnter No of NODES: "; cin>>this->V;


int matrix[4][4] = { {0, 5, INF, INF},

{INF, 0, INF, 2},

{1, INF, 0, INF},

{6, INF, 3, 0 }};

//Creating 2D Array

Adj = new int*[V];

for(int i = 0; i < V; ++i) {

Adj[i] = new int[V];

for(int i = 0; i < V; i++)

for(int j = 0; j < V; j++)

Adj[i][j] = matrix[i][j];

cout<<"\nOriginal Distance!";

showGraphMatrix();

////////////////////////

void Graph :: floyd_warshall()

this->graph_dist_matrix();

int dist[V][V];

for(int i = 0; i < V; i++)


{

for(int j = 0; j < V; j++)

dist[i][j] = Adj[i][j];

for(int k = 0; k < V; k++)

for(int i = 0; i < V; i++)

for(int j = 0; j < V; j++)

if(dist[i][k] + dist[k][j] < dist[i][j])

dist[i][j] = dist[i][k] + dist[k][j];

for(int i = 0; i < V; i++)

for(int j = 0; j < V; j++)

Adj[i][j] = dist[i][j];

cout<<"\nFloyd-Warshall shortest distant pairs in Graph!";

showGraphMatrix();

}
////////////////////////

int main()

Graph myG;

short ch;

do

cout<<"\n------MENU------\n"

<<"\n1. Create Graph (ADJACENCY MATRIX)"

<<"\n2. Test Out Floyd-Warshall Algorithm"

<<"\n3. Show Graph"

<<"\n4. Ya Fer Exit\n";

cout<<"\nEnter A Choice: "; cin>>ch;

switch(ch)

case 1:

myG.createGraph();

break;

case 2:

myG.floyd_warshall();

break;

case 3:

myG.showGraphMatrix();

break;

case 4:
return 0;

break;

}while(1);

cout<<endl;

return 0;

You might also like