Как ускорить поиск в ширину?
Имеется программа, выполняющая обход в ширину в ориентированном графе. Граф в памяти предствален в виде матрицы. Граф читается из файла in.txt в виде
4 4 -число вершин и ребер
0 1
1 2
2 3 - пути между вершинами
#include <vector>
#include <locale.h>
#include <queue>
#include <Windows.h>
#include <set>
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
UINT breadthFirstSearch(vector<vector<UINT>> &graph, UINT begin, UINT vertex, UINT n)
{
queue<UINT> q;
q.push(begin);
set<int> gray;
while (!q.empty())
{
UINT i = 0;
q.pop();
gray.insert(q.front());
if (q.front() == vertex)
return 1;
while (i < n)
{
if (graph[q.front()][i])
q.push(i);
i++;
}
if (q.empty()) return 0;
}
}
void main()
{
UINT one = 0, two = 0;
setlocale(LC_ALL, "");
ifstream get_content("in.txt");
UINT vertex, edge;
get_content >> edge;
get_content >> vertex;
vector<vector<UINT>> graph;
graph.assign(vertex, vector<UINT>(vertex));;
UINT i = 0;
while (i < edge)
{
UINT one, two;
get_content >> one;
get_content >> two;
graph[one][two] = 1;
i++;
}
printf_s("Введите две вершины: ");
cin >> one;
cin >> two;
set<UINT> gray;
set <UINT>black;
unsigned int start_time = clock();
UINT b = breadthFirstSearch(graph, one, two, vertex);
unsigned int end_time = clock();
unsigned int search_time = end_time - start_time;
printf_s("%d", search_time);
b ? printf_s("Путь есть\n") : printf_s("Пути нет\n");
return;
}
Тестирование показало, что данный код работает в 2 раза медленнее поиска в глубину. Как ускорить работу программы?(нужно также исправление в коде)