0% found this document useful (0 votes)
7 views4 pages

Do Thi

The document contains a C++ program that implements graph operations including saving a graph to a file, reading a graph from a file, displaying the graph, and finding the shortest path between two nodes using Dijkstra's algorithm. It defines functions for each operation and utilizes a main function to demonstrate these functionalities with a predefined graph. The program prompts the user to input start and end nodes to compute the shortest path.

Uploaded by

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

Do Thi

The document contains a C++ program that implements graph operations including saving a graph to a file, reading a graph from a file, displaying the graph, and finding the shortest path between two nodes using Dijkstra's algorithm. It defines functions for each operation and utilizes a main function to demonstrate these functionalities with a predefined graph. The program prompts the user to input start and end nodes to compute the shortest path.

Uploaded by

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

#include <iostream>

#include <fstream>
#include <iomanip>
#include <climits>

using namespace std;


#define MAX 100

void LuuDoThiVaoFile(int graph[MAX][MAX], int n, const string &filename) {


ofstream outFile(filename);
if (!outFile) {
cout << "Khong the mo file!" << endl;
return;
}
outFile << n << endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
outFile << graph[i][j] << " ";
}
outFile << endl;
}
outFile.close();
}

void NhapDoThiTuFile(int graph[MAX][MAX], int &n, const string &filename) {


ifstream inFile(filename);
if (!inFile) {
cout << "Khong the mo tep!" << endl;
return;
}
inFile >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
inFile >> graph[i][j];
}
}
inFile.close();
}

void HienThiDoThi(int graph[MAX][MAX], int n) {


cout << "Ma tran trong so cua do thi:" << endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (graph[i][j] == INT_MAX)
cout << setw(5) << "INF";
else
cout << setw(5) << graph[i][j];
}
cout << endl;
}
}

void TimDuongDi(int graph[MAX][MAX], int n, int start, int end) {


int dist[MAX], prev[MAX];
bool visited[MAX] = {false};
for (int i = 0; i < n; ++i) {
dist[i] = INT_MAX;
prev[i] = -1;
}
dist[start] = 0;

for (int i = 0; i < n - 1; ++i) {


int u = -1;
for (int j = 0; j < n; ++j) {
if (!visited[j] && (u == -1 || dist[j] < dist[u])) {
u = j;
}
}

if (dist[u] == INT_MAX) break;


visited[u] = true;

for (int v = 0; v < n; ++v) {


if (graph[u][v] != 0 && graph[u][v] != INT_MAX && !visited[v]) {
int newDist = dist[u] + graph[u][v];
if (newDist < dist[v]) {
dist[v] = newDist;
prev[v] = u;
}
}
}
}
if (dist[end] == INT_MAX) {
cout << "Khong co duong di tu " << char('A' + start) << " den " << char('A' + end) <<
"." << endl;
} else {
cout << "Do dai duong di ngan nhat: " << dist[end] << endl;
cout << "Duong di: ";
int path[MAX], count = 0;
for (int at = end; at != -1; at = prev[at]) {
path[count++] = at;
}
for (int i = count - 1; i >= 0; --i) {
cout << char('A' + path[i]);
if (i > 0) cout << " -> ";
}
cout << endl;
}
}

int main() {
int graph[MAX][MAX] = {
{0, 13, 14, 6, 0, 0},
{13, 0, 4, 0, 9, 0},
{14, 4, 0, 0, 2, 3},
{6, 0, 0, 0, 0, 5},
{0, 9, 2, 0, 0, 0},
{0, 0, 3, 5, 0, 0}
};
int n = 6;
string filename = "bai1.txt";
LuuDoThiVaoFile(graph, n, filename);
int inputGraph[MAX][MAX];
int inputSize;
NhapDoThiTuFile(inputGraph, inputSize, filename);
HienThiDoThi(inputGraph, inputSize);
int start, end;
cout<<"Nhap diem bat dau(0-5): "; cin>> start;
cout<<"Nhap diem den(0-5): "; cin>>end;
TimDuongDi(inputGraph, inputSize, start, end);
return 0;
}

You might also like