Untitled

 avatar
unknown
plain_text
2 years ago
2.4 kB
4
Indexable
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
 
vector<vector<bool>> initLogicTable(int min_Vertext, int max_Vertext) {
	vector<vector<bool>> adjMatrix;
	for (int i = min_Vertext; i <= max_Vertext; i++) {
		vector<bool> vs;
		for (int j = min_Vertext; j <= max_Vertext; j++) {
			vs.push_back(false);
		}
		adjMatrix.push_back(vs);
	}
	return adjMatrix;
}

void createAdjacencyMatrix(vector<vector<int>> v, vector<vector<bool>> &adjMatrix, int min_Vertext, int max_Vertext) {
	for (int i = min_Vertext; i <= max_Vertext; i++) {
		for (int j = min_Vertext; j <= max_Vertext; j++) {
			bool isExist = false;
			for (vector<int> x : v) {
				if (x[0] == i && x[1] == j) {
					isExist = true;
					break;
				}
			}
			adjMatrix[i][j] = isExist ? true : false; 
		} 
	}
}

bool isExistInList(int vertex, queue<int> list) {
	while (!list.empty()) {
		int vertex_del = list.front();
		if (vertex == vertex_del) { return true;  }
		list.pop();
	}
	return false;
}

void findPath(int vertex, vector<vector<bool>> adjMatrix, vector<vector<int>> v, int min_Vertext, int max_Vertext) {
	queue<int> list,list2;
	list.push(vertex);
	list2.push(vertex);
	while (!list.empty()) {
		int y = list.front();
		list.pop();
		cout << y << "	 ";
		for (int i = min_Vertext; i <= max_Vertext; i++) {
			if (adjMatrix[y][i] && !isExistInList(i,list2)) {
				list.push(i);
				list2.push(i);
				adjMatrix[y][i] = adjMatrix[i][y] = false;
			}
		} 
	}
}

int main() {
	vector<vector<int>> v = { {0,1}, {0,2}, {0,3},{1,4}, {1,0},{2,0},{2,4},{2,5},{3,0},{3,5},{4,1},{4,2},{4,6},
		{5,2},{5,3},{5,6},{5,7},{6,4},{6,5} ,{6,7} ,{6,8} ,{7,5} ,{7,6} ,{7,8} ,{8,6},{8,7} };
	int min_Vertext = 0;
	int max_Vertext = 8;
	vector<vector<bool>> adjMatrix= initLogicTable(min_Vertext, max_Vertext);
	createAdjacencyMatrix(v, adjMatrix, min_Vertext,max_Vertext);
	for (int i = min_Vertext; i <= max_Vertext; i++) { 
		for (int j = min_Vertext; j <= max_Vertext; j++) { 
			cout << adjMatrix[i][j] << "   ";
		}
		cout << endl;
	}
	cout << endl << "The path:  " << endl;
	findPath(0, adjMatrix, v, min_Vertext, max_Vertext);
}


https://www.google.com/search?q=tim+duong+di+ngan+nhat&sxsrf=ALiCzsagOqEFRm9wqbjxfrXJN5LLT6Im-w:1669166657542&source=lnms&tbm=isch&sa=X&ved=2ahUKEwiRhsatksP7AhUY1nMBHXyBBB0Q_AUoAXoECAIQAw&biw=1454&bih=762&dpr=1.25#imgrc=IZOlYYBRWfNcUM
Editor is loading...