enh(CPlusPlus): memory usage on Dijksta algorithm (#1061)

Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com>
pull/1079/head
DenisO 2022-10-30 18:02:34 +02:00 committed by GitHub
parent ec8bdb7c84
commit 978a119d9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 161 additions and 83 deletions

View File

@ -1,99 +1,177 @@
//Dijkstra's algorithm #include <cstddef>
//implemented in the context of a directed graph #include <limits>
#include <bits/stdc++.h>
using namespace std;
int dijkstra(vector<vector<pair<int,int>>>& graph, int start, int end){ //I highly recommend to create matrix class
//return value(-1 if endpoint is unreachable) template <typename T>
int ret=-1; inline T& getMatrixElement( T* matrix, size_t size,
size_t row, size_t column)
{
return *(matrix + row * size + column);
}
//storing cost(distance) of each vertex, set initial value as -1 template <typename T>
vector<int> dist(graph.size(),-1); inline void setMatrixElement ( T* matrix, size_t size,
size_t row, size_t column, T element)
{
*(matrix + row * size + column) = element;
}
//priority queue to store traversing vertices and cost values template <typename T>
//data will be stored in the format of: {cost, current vertex} size_t minDistance(T* vector, bool* states, size_t size)
//entry with minimum cost will always be on top {
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq; size_t index;
pq.push({0,start}); T min = std::numeric_limits<T>::max();
while(!pq.empty()){ for(size_t i = 0; i < size; i++)
int cVertex, cCost; {
tie(cCost,cVertex) = pq.top(); if (states[i] == false && vector[i] <= min)
pq.pop(); {
min = vector[i];
index = i;
}
}
//vertex already visited with lower cost -> continue return index;
if(dist[cVertex]!=-1&&dist[cVertex]<=cCost)continue; }
//otherwise we update our current cost(distance)
dist[cVertex]=cCost;
if(cVertex==end){ template <typename T>
ret=cCost; T* dijkstra (T* matrix, size_t matrix_size, size_t start_pos)
{
T* result = new T [matrix_size];
// I recoment use dynamic bitset from boost library
bool* states = new bool[matrix_size];
//Set All elements to max value and all state to False
for(size_t i = 0; i < matrix_size; i++)
{
result[i] = std::numeric_limits<T>::max();
states[i] = false;
}
result[0] = 0;
for(size_t i = 0; i < matrix_size; i++)
{
size_t index = minDistance(result, states, matrix_size);
states[index] = true;
for(size_t j = 0; j < matrix_size; j++)
{
if (
!states[j] &&
getMatrixElement(matrix, matrix_size, index, j) &&
result[index] + getMatrixElement(matrix, matrix_size, index, j) < result[j]
//result.get(index) != std::numeric_limits<T>::max()
)
{
T new_val = result[index] + getMatrixElement(matrix, matrix_size, index, j);
result[j] = new_val;
}
}
}
return result;
}
#include <iostream>
//function declaration below
//Generate and std::cout << matrix
void getExampleMatrix(int*& matrix_out, size_t& size_out);
int main()
{
//I highly recommend to create matrix class
size_t size;
std::cout << "Graph Size: ";
std::cin >> size;
int* user_graph_matrix = new int [size*size];
for(size_t i = 0; i < size; i++)
{
for(size_t j = 0; j < size; j++)
{
int temp;
std::cout << "(" << j << ", " << i << ") = ";
std::cin >> temp;
setMatrixElement(user_graph_matrix, size, i, j, temp);
}
}
size_t start_element = std::numeric_limits<size_t>::max();
while (true)
{
std::cout << "Choose First Element: ";
std::cin >> start_element;
if(start_element < size)
break; break;
std::cout << "[Warning] Number of element is greater that matrix size\n";
} }
for(pair<int,int> nPair : graph[cVertex]){ auto ex_result = dijkstra(user_graph_matrix, size, start_element);
int nVertex, nCost; for(size_t i = 0; i < size; i++)
tie(nVertex,nCost) = nPair; {
if(dist[nVertex]!=-1&&dist[nVertex]<=cCost+nCost){ std::cout << ex_result[i] << " ";
//the next vertex has already been traversed with lower cost
continue;
} }
//otherwise we add a new entry to the priority queue std::cout << "\n";
pq.push({nCost+cCost,nVertex});
} // !!! Unkoment for example matrix
}
return ret; // size_t ex_size;
// int* ex_matrix;
// getExampleMatrix(ex_matrix, ex_size);
// auto ex_result = dijkstra(ex_matrix, ex_size, 0);
// for(size_t i = 0; i < ex_size; i++)
// {
// std::cout << ex_result[i] << " ";
// }
// std::cout << "\n";
} }
int main(){ void getExampleMatrix(int*& matrix_out, size_t& size_out)
//number of vertices(V) and edges(E) {
int V, E; size_t size = 4;
cout << "Enter the number of vertices: "; int* graph_matrix = new int[size*size];
cin >> V;
cout << "Enter the number of edges: "; setMatrixElement(graph_matrix, size, 0, 0, 0);
cin >> E; setMatrixElement(graph_matrix, size, 0, 1, 0);
cout << "Enter each edge information in the format of: \n"; setMatrixElement(graph_matrix, size, 0, 2, 3);
cout << "(Source vertex number) (Destination vertex number) (cost)\n"; setMatrixElement(graph_matrix, size, 0, 3, 1);
//Adjacency list
//data will be stored in the format of: {destination,cost} setMatrixElement(graph_matrix, size, 1, 0, 0);
//with the first index as the source setMatrixElement(graph_matrix, size, 1, 1, 0);
vector<vector<pair<int,int>>> graph(V+1,vector<pair<int,int>>()); setMatrixElement(graph_matrix, size, 1, 2, 0);
while(E--){ setMatrixElement(graph_matrix, size, 1, 3, 5);
int source, dest, cost;
cin >> source >> dest >> cost; setMatrixElement(graph_matrix, size, 2, 0, 3);
graph[source].push_back({dest,cost}); setMatrixElement(graph_matrix, size, 2, 1, 0);
setMatrixElement(graph_matrix, size, 2, 2, 0);
setMatrixElement(graph_matrix, size, 2, 3, 1);
setMatrixElement(graph_matrix, size, 3, 0, 1);
setMatrixElement(graph_matrix, size, 3, 1, 5);
setMatrixElement(graph_matrix, size, 3, 2, 1);
setMatrixElement(graph_matrix, size, 3, 3, 0);
for(size_t i = 0; i < size; i++)
{
for (size_t j = 0; j < size; j++)
{
std::cout << getMatrixElement(graph_matrix, size, i, j) << " ";
} }
//starting point(start), ending point(end) std::cout << "\n";
int start, end;
cout << "Enter the starting point: ";
cin >> start;
cout << "Enter the ending point: ";
cin >> end;
int answer = dijkstra(graph,start,end);
if(answer==-1){
cout << "Shortest path from " << start << " to " << end << " does not exist." << endl;
} }
else std::cout << "\n";
cout << "The minimum cost for the shortest path is: " << answer << endl;
size_out = size;
matrix_out = graph_matrix;
} }
//Time complexity: O(ElogV)
//Space complexity: O(V+E)
/*
Sample Input
5
8
1 2 2
1 3 3
1 4 1
1 5 10
2 4 2
3 4 1
3 5 1
4 5 3
1
5
Output(minimum cost)
4
*/