I recently just started to learn c++ and for my next homework assignment I have to implement a bidirectional version of dijkstra's algorithm. I'm supposed to build on my last assignment which makes a graph using vectors. I'm wondering what the best way to setup this assignment using my code. Here is the actual assignment:
Machine Problem 3: Bidirectional shortest path algorithm : Ira Pohl Jan 24, 2014
Objective : Improve your Graph Class and add Dijkstra and Bidirectional algorithm
Graph algorithms and graph representation is a critical tool in CS. The basic problem will be to write Dijkstra’s algorithm as a class member function (method in OO speak). You should already know Dijkstra’s algorithm for the shortest path problem from prior experience, but it will be reviewed in class. It is the basis for many route calculations and optimizations programs.
There are 2 basic implementations used for graphs – one is edge lists, and the other is connectivity matrices. You can decide which to use, but comment on your choice.
Basic problem: Write a set of constructors for declaring and initializing a graph or use your previous implementation of graph. An edge will have a positive cost that is its distance. Have a procedure that can for a graph of at least size 1000 produce a randomly generated set of edges with positive distances. Assume the graphs are undirected. The random graph procedure should have edge density as a parameter and distance range as a parameter. So a graph whose density is 0.1 would have 10% of its edges picked at random and its edge distance would be selected at random from the distance range. This of course was already developed in problem 2.
The Dijkstra bi-directional algorithm should re-use code from the Dijkstra unidirectional algorithm.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <cmath>
double probability(){ return 1.0*rand()/RAND_MAX;}
using namespace std;
//class that has make_graph as constructor
class Graph{
public:
Graph(int s, double density);
void print_graph();
private:
int size;
vector<vector<bool> > g1; //A vector of vectors of bools
//Think of a vector as a first in, last out Data Structure
};
//make_graph altered to work in c++
Graph::Graph(int s, double density){
this->size = s;
for (int i = 0; i < s; ++i){
// We push a new vector of bool onto the initial vector s times
// The * is there to dereference the new vector that we insert
this->g1.push_back( *( new vector<bool>() ) );
for (int j = 0; j < s; ++j){
//then, on each of those vectors, we push a default "false" s times
this->g1.at(i).push_back(false);
}
}
//Don't have to change this part much
for (int i = 0; i < s; ++i){
for (int j = 0; j < s; ++j){
if (probability()< density) this->g1[i][j] = true;
}
}
}
//simple conversion, just needed 'this'
void Graph::print_graph(){
cout << "graph size " << this->size << "\n";
for(int i = 0; i < this->size; ++i){
for (int j = 0; j < this->size; ++j){
cout << this->g1[i][j] << "\t";
}
cout << "\n";
}
}
int main(){
srand(time(0));
cout << "Test simple graph generation\n";
Graph* test1 = new Graph(10, 0.7);
test1->print_graph();
cout << "\nEND of TEST 1\n\n";
Graph* test2 = new Graph(8, 0.5);
test2->print_graph();
cout << "\nEND of TEST 2\n\n";
return 0;
}