I filled a 2dim vector as below:
vector< vector<double>> save;
for ( int i=0;i<5;i++ )
{
for ( int j=0;j<5;j++ )
{
save[i][j]=i*(i+j);
}
}
Now how can I sort it and keep their related indices ?
For example indices of 8 are 4 and 4
I have already used this code to save indices and sort them in two other array which 2dim and 1dim respectively
void sort(int index[5][2],int order[5])
{
int l=0;
while( l<5)
{
float max=-10000;
int c1=0;
int c2=0;
for(int i=0;i<5;i++)
{
for(int j=i+1;j<5;j++)
{
if(save[i][j]>max)
{
max=save[i][j];
c1=i+1;
c2=j+1;
}
}
}
order[l]=max;
index[l][0]=c1;
index[l][1]=c2;
l++;
}
}
I want to know can I save more time in case of computation time if I use vector and sort function instead of current solution when number of data is 100 instead of 5 ?
I would do something like this.
// Create a structure to manage co-ordinates, values, and 1d-indices.
// These are the components you're actually interested in.
struct PointValue
{
PointValue(int x, int y, int value)
: x(x), y(t), value(value), indexOneDimensional(x*(x+y))
{}
int x, y, indexOneDimensional;
int value;
// Sort predicate for sorting these PointValue objects.
static bool SortFunction(const PointValue& left, const PointValue& right)
{
return left.value < right.value;
}
};
typedef std::vector<PointValue> PointValueList;
int sizeX = 100; int sizeY = 100;
PointValueList myValues(sizeX * sizeY);
// Create a 100x100 array with random values for each point.
for (int i = 0; i < sizeX; i++)
{
for (int j = 0; j < sizeY; j++)
{
myValues.push_back(PointValue(i, j, rand()));
}
}
// Sort using our static predicate.
std::sort(myValues.begin(), myValues.end(), PointValue::SortFunction);
// Print 1d-indices.
std::for_each(m_values.begin(), m_values.end(), [](const PointValue& val) {
printf("Index is: %d. Value is: %d", val.indexOneDimensional, val.value);
});
You can use a hash map of numbers to their indices. Something like:
std::map< double, std::list<int> > indices;
Also, take into account the fact that the comparison between two floating point numbers is approximate.
Related
I have a map of a room that I have put into a vector of vectors of characters (vector>). The map will look something like this:
# * #
* * D
S * #
where # are walls, * are path areas, S is the start and D is the end. I will not know what the map looks like ahead of time so I want my program to be able to read any map with similar characteristics to the one above.
Thus, I would like to be able to search my vector of vectors to find the coordinates/location of S, so I know where the starting point of the maze is. I have only been able to find examples for just a single vector (one-dimension). Is this possible to do with a vector of vectors (two-dimensions)? If so, how can I do it?
Here is the code I used to create the matrix:
vector<vector<char>> GetMap(int& M, int& N) //function to get the map of a room
{
vector<vector<char>> matrix{}; //give a matrix
char char_buf;
for (int rows = 0; rows < M; rows++)
{
matrix.push_back(vector<char>()); //Put a new empty row in your matrix
for (int cols = 0; cols < N; cols++)
{
cin >> char_buf; //Here we get a char from cin
matrix.back().push_back(char_buf); //That you push back in your sub-vector
}
}
return matrix;
}
First of all, your GetMap function is constantly pushing back new elements. That's a big no no when you already have the size of the matrix available to you (M and N). Also, there is really no need for the size parameters to be of type int&. A simple int is fine and, in most cases, even more efficient.
Rule of thumb: Only use references for non-basic types like vector, string and pretty much all classes.
Also, the fact that you use int& and not const int& doesn't allow you to call the function by passing rvalues (variables without names). For example GetMap(5, 5).
Now, to finally answer your question. Since you already have an idea on how to parse the whole matrix in your GetMap function. I really don't see the problem in creating a similar function that would get the position of a desired character.
The full working code with some enhancements:
#include <iostream>
#include <vector>
using namespace std;
struct Pos{
Pos() : x(0), y(0) {}
Pos(int x, int y) : x(x), y(y) {}
int x;
int y;
};
vector<vector<char>> GetMap(const int height, const int width) //function to get the map of a room
{
//Create the matrix with the constructor (much more efficent than constantly push_back'ing elements)
vector<vector<char>> matrix(height, vector<char>(width));
//Go through every single char in the matrix
for (int rows = 0; rows < height; rows++)
{
for (int cols = 0; cols < width; cols++)
{
cin >> matrix[rows][cols];
}
}
return matrix;
}
Pos getElementPos(const vector<vector<char>>& matrix, const char toFind)
{
int height = matrix.size();
int width = matrix[0].size();
//Go through every single char in the matrix
for (int rows = 0; rows < height; rows++)
{
for (int cols = 0; cols < width; cols++)
{
if(matrix[rows][cols] == toFind){
return Pos(cols, rows);
}
}
}
// In the event that we couldn't find the element
return Pos(-1, -1);
}
int main(){
vector<vector<char>> map = GetMap(5, 5);
Pos dPos = getElementPos(map, 'D');
cout << "\nThe coordinates of D are " << dPos.x << " and " << dPos.y << '\n';
return 0;
}
I am trying to implement A* search for a N puzzle whose size is 15. My start state would be random. The goal state is the des array in the code. I can only swap tiles with 0 (blank state) in 4 directions in the puzzle to create a new state. To implement this I have used a priority_queue and 4 maps. For all of these , I have used 2 dimensional array. Compare is the comparator for the priority_queue and map_cmp is the comparator for the 4 maps. Vis is used to keep track of the visited states, Dis is used to keep count of the path , parent is used to keep the parent of the state and ab is used to keep the position of 0 (blank space) of each state. Here is the code:
enter code here
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
int fx[]={0,0,1,-1};
int fy[]={1,-1,0,0};
int des[4][4]={{0,1,2,3},{4,5,6,7},{8,9,10,11},{12,13,14,15}};
int func1(int node[][4])
{
int cnt=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(i==0 && j==0)
continue;
if(des[i][j]!=node[i][j])
cnt++;
}
}
return cnt;
}
double func2(int node[][4])
{
int a,b,x,y;
double sum=0.0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(node[i][j]==0)
continue;
a=node[i][j];
x=a/4;
y=a%4;
sum+=sqrt((i-x)*(i-x)+ (j-y)*(j-y));
}
}
}
struct map_cmp {
bool operator()(const int (&a)[4][4], const int (&b)[4][4]) const {
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(a[i][j]!=b[i][j])
return true;
else
continue;
}
}
return false;
}
};
map<int[4][4],int,map_cmp>vis;
map<int[4][4],int,map_cmp>dist;
map<int[4][4],int[][4],map_cmp>parent;
map< int[4][4],pair<int,int>,map_cmp >ab;
struct compare
{
bool operator()(const int (&a)[4][4],const int (&b)[4][4] )const{
return ((dist[a]+func1(a)) < (dist[b]+func1(b)));
}
};
bool isValid(int row, int col)
{
return (row >= 0) && (row < 4) && (col >= 0) && (col < 4);
}
int bfs( int src[][4],int a,int b)
{
int u[4][4];
int v[4][4];
int x,y;
vis[src]=1;
dist[src]=0;
parent[src]={0};
ab[src]=pii(a,b);
pii pos;
priority_queue < int[4][4], vector < int[4][4] > , compare > q;
q.push(src);
while(!q.empty())
{
u = q.top();
q.pop();
pos=ab[u];
for(int i=0;i<4;i++)
{
copy(u,u+16,v);
x=pos.first+fx[i];
y=pos.second+fy[i];
if(isValid(x,y))
{
swap(v[pos.first][pos.second],v[x][y]);
vis[v]=1;
dist[v]=dist[u]+1;
ab[v]=pii(x,y);
parent[v]=u;
if(memcmp(des,v,sizeof(des))==0)
return dist[v];
q.push(v);
}
}
}
}
int main()
{
int a,b,i,j,k,m,n,x,y;
int result[5];
int src[4][4]={{7,2,12,11},{10,14,0,6},{8,13,3,1},{9,5,15,4}};
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(src[i][j]==0)
{
x=i;
y=j;
break;
}
}
if(j!=4)
break;
}
a=bfs(src,x,y);
ab.clear();
}
The errors i am getting are for the comparator of maps and priority_queue.
They are:
1. no match for operator[] in dist[a]/vis/parent/ab[in short all the maps]
2. invalid array assignment
3. no matching function for call to 'std::priority_queue, compare>::push(int (*&)[4])'
This is my first post here. Sorry for any mistakes. Any help will be appreciated as i have already done whatever i can
Leave alone the unsized array issue.
Let's consider about the issue in question title, starting from the definition of std::priority_queue,
std::priority_queue<class _Tp, class _Container, class _Compare>
three parameters are element type, element container(default std::vector), comparator(It's a class with () comparator, default std::less).
class TpType {};
class TpTypeComparatator {
bool operator () (TpType &a, TpType &b) const {
return true;
}
};
std::priority_queue, TpTypeComparatator> q;
I'm trying to use clear functions to do a matrix multiplication with random generated values. Therefore I'm hoping to use a function(mat_def) to generate the matrices and another function(mat_mul) to multiply them when the matrices are sent as parameters.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
double mat_def(int n) //how to return the matrix
{
double a[n][n];
double f;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
f= rand();
cout<<f ;
a[i][j]=f;
}
}
return 0;
}
double mat_mul( int n, double a[n][n], double b[n][n]) //how to send matrix as parameter
{
return 0;
}
int main()
{
/* initialize random seed: */
srand (time(NULL));
mat_def(10);
}
Here's a nice, standard C++ Matrix template for you.
Matrix.h
#include <vector>
class Matrix
{
class InnerM
{
private:
int ydim;
double* values;
public:
InnerM(int y) : ydim(y)
{
values = new double[y];
}
double& operator[](int y)
{
return values[y];
}
};
private:
int xdim;
int ydim;
std::vector<InnerM> inner;
public:
Matrix(int x, int y) : xdim(x), ydim(y), inner(xdim, InnerM(ydim))
{
}
InnerM& operator[](int x)
{
return inner[x];
}
};
All the memory leaks are there for you but you get the idea. From here you can handle the multiplication by overiding ::operator*() in the Matrix class.
I assume your problem is to define 2-D array and then pass it to mat_mul function to multiply the matrices. And the rest will be quite simple.
Defining the 2-D array(considering memory needs are known at run time):
int rows,cols;
cin >> rows;
cin >> cols;
int **arr = new int*[rows]; // rows X cols 2D-array
for(int i = 0; i < rows; ++i) {
arr[i] = new int[cols];
}
You can define another 2-D array exactly the same way with required rows and column.
now, Passing the 2-D array to function:
void mat_mul(int **arr1, int **arr2, int m, int n, int p, int q){
//define a 2-D array to store the result
//do the multiplication operation
//you could store the result in one of the two arrays
//so that you don't have to return it
//or else the return type should be modified to return the 2-D array
}
example:
void display(int **arr, int row, int col){
for (int i=0; i<row; i++){
for(int j=0;j<col; j++){
cout << arr[i][j] << '\t';
}
cout << endl;
}
}
Delete the memory if not required anymore with the following syntax:
for(int i=0; i<rows; i++){
delete[] array[i];
}
delete[] array;
hope this will be sufficient to get your work done!
there is already an answer on how to return a 2-D array on SO. Check the link below.
https://stackoverflow.com/a/8618617/8038009
Returning the raw allocation is a sucker bet. You need to manage all of the memory allocated yourself and pass it around with the matrix size parameters.
Why suffer? Use a matrix class
template<class Type>
class Matrix{
int rows;
int cols;
std::vector<type> data;
public:
Matrix(int row, int col):rows(row), cols(col), data(rows*cols)
{
// does nothing. All of the heavy lifting was in the initializer
}
// std::vector eliminates the need for destructor, assignment operators, and copy
//and move constructors.
//add a convenience method for easy access to the vector
type & operator()(size_t row, size_t col)
{
return data[row*cols+col];
}
type operator()(size_t row, size_t col) const
{
return data[row*cols+col];
}
};
Usage would be
Matrix<double> mat_mul(const Matrix<double> &a, const Matrix<double> &b)
{
Matrix<double> result;
// do multiplication
return result;
}
int main()
{
/* initialize random seed: */
srand (time(NULL));
Matrix<double> matA(10, 10);
matA(0,0) = 3.14; // sample assignment
matA(9,9) = 2.78;
double x = matA(0,0) * matA(9,9)
Matrix<double> matB(10, 10);
Matrix<double> matC = mat_mul(matA, matB) ;
}
More functionality, such as construction from an initializer list, can be added to the class to make your life easier. You can also specify an operator * overload for Matrix and use that in place of mat_mul if you chose. Read Operator overloading for more on that option.
I'm a newbie to programming, especially to C++. I've got a task, and its part is to write a function using a struct.
struct S {
float m; //how many
int h; //where
float mx;
};
int main() {
S s;
s.m=0.5;
s.h=1;
vector<float> v(10);
for (int i=0;i<10;i++)
v[i]=sin(i);
S mx = max_search(v);
The function is ok, if (mx.m>0.98935 && mx.m<0.9894 && mx.h==8).
I came out with this code of function, but I know, it's quite defective.
float max_search(vector<float> v) {
int max=0;
for (int i=0; i<v.size(); i++) {
if (v[i]>max) {
max=v[i];
}
return max;
}
}
I don't know, what should I do with the type of function, and maybe the return value in also wrong.
Not sure I capture your main question correctly or not. You want to convert return value of max_search function which is float to struct S? I'll massage on KarithikT's answer and add more details:
To enable implicit conversion (from float to struct S), need to add conversion functions to S
struct S {
S():m(0.0), h(0), mx(0.0){ } //
S(float x):m(0.0), h(0), mx(x){ } // to enalbe convert float to S
float m; //how many
int h; //where
float mx;
};
float max_search(const vector<float>& v) { // pass by const reference
float max=0.0f;
for (int i=0; i<v.size(); i++) {
if (v[i]>max) {
max=v[i];
}
}
return max;
}
You could also use std::max_element to find max element from a container:
vector<float> v(10);
for (int i=0;i<10;i++) {
v[i]=sin(i);
}
S mx = *std::max_element(v.begin(), v.end());
You want your return max; In the outer most level. Right now it returns every iteration of the for loop, which means you get only 1 iteration.
float max_search(vector<float> v) {
float max=0.0f; <------------
for (int i=0; i<v.size(); i++) {
if (v[i]>max) {
max=v[i];
}
--------------
}
return max; <------------
}
And i think you want to call it like this s.mx = max_search(v);
You can also use std::max_element
s.mx = std::max_element(v.begin(),v.end()); // (begin(v),end(v)) in c++11
If you declare a function as float, why are you returning an int?
float max_search(vector<float> v) {
float max = v[0]; //this way you avoid an iteration
for (int i = 1; i < v.size() - 1; i++)
if (v[i] > max) max = v[i];
return max;
}
You can also use an iterator to do that:
float max_search(vector<float> v) {
float max = .0;
for (vector<float>::iterator it = v.begin(); it != v.end(); ++it)
if (*it > max) max = *it;
return max;
}
In the first code block it's important to substract 1 to v.size, other way you will try to access to an element that does not exists. If your code isn't returning you a segmentation fault, that's because std::vector is access safe. That means that std::vector try to access to the element, but anyway, you are doing one last innecesary iteration. That's why it's better to use iterators.
It's also true what #KarthikT says: you are trying to return max in each iteration, so, after first iteration, function return the value and stop the execution, always retrieving you the first value of the vector (if this value is greater than 0).
I hope this help.
For example suppose there are 3 nodes A,B,C and A links to B and C, B links to A and C, and C links to B and A. In visual form its like this
C <- A -> B //A links to B & C
A <- B -> C //B links to A & C
B <- C -> A //C links to B & A
Assume the A,B,C are held in an array like so [A,B,C] with index starting at 0. How can I efficiently sort the array [A,B,C] according to the value held by each node.
For example if A holds 4, B holds -2 and C holds -1, then sortGraph([A,B,C]) should return [B,C,A]. Hope its clear. Would it be possible if I can somehow utilize std::sort?
EDIT: Not basic sort algorithm. Let me clarify a bit more. Assume I have a list of Nodes [n0,n1...nm]. Each ni has a left and right neighbor index. For example, n1 left neight is n0 and its right neighbor is n2. I use index to represent the neighbor. If n1 is at index 1, then its left neighbor is at index 0 and its right neighbor is at index 2. If I sort the array, then I need to update the neighbor index as well. I don't want to really implement my own sorting algorithm, any advice on how to proceed?
If I understand the edited question correctly your graph is a circular linked list: each node points to the previous and next nodes, and the "last" node points to the "first" node as its next node.
There's nothing particularly special you need to do the sort that you want. Here are the basic steps I'd use.
Put all the nodes into an array.
Sort the array using any sorting algorithm (e.g. qsort).
Loop through the result and reset the prev/next pointers for each node, taking into account the special cases for the first and last node.
Here is a C++ implementation, hope is useful (it includes several algorithms like dijkstra, kruskal, for sorting it uses depth first search, etc...)
Graph.h
#ifndef __GRAPH_H
#define __GRAPH_H
#include <vector>
#include <stack>
#include <set>
typedef struct __edge_t
{
int v0, v1, w;
__edge_t():v0(-1),v1(-1),w(-1){}
__edge_t(int from, int to, int weight):v0(from),v1(to),w(weight){}
} edge_t;
class Graph
{
public:
Graph(void); // construct a graph with no vertex (and thus no edge)
Graph(int n); // construct a graph with n-vertex, but no edge
Graph(const Graph &graph); // deep copy of a graph, avoid if not necessary
public:
// #destructor
virtual ~Graph(void);
public:
inline int getVertexCount(void) const { return this->numV; }
inline int getEdgeCount(void) const { return this->numE; }
public:
// add an edge
// #param: from [in] - starting point of the edge
// #param: to [in] - finishing point of the edge
// #param: weight[in] - edge weight, only allow positive values
void addEdge(int from, int to, int weight=1);
// get all edges
// #param: edgeList[out] - an array with sufficient size to store the edges
void getAllEdges(edge_t edgeList[]);
public:
// topological sort
// #param: vertexList[out] - vertex order
void sort(int vertexList[]);
// dijkstra's shortest path algorithm
// #param: v[in] - starting vertex
// #param: path[out] - an array of <distance, prev> pair for each vertex
void dijkstra(int v, std::pair<int, int> path[]);
// kruskal's minimum spanning tree algorithm
// #param: graph[out] - the minimum spanning tree result
void kruskal(Graph &graph);
// floyd-warshall shortest distance algorithm
// #param: path[out] - a matrix of <distance, next> pair in C-style
void floydWarshall(std::pair<int, int> path[]);
private:
// resursive depth first search
void sort(int v, std::pair<int, int> timestamp[], std::stack<int> &order);
// find which set the vertex is in, used in kruskal
std::set<int>* findSet(int v, std::set<int> vertexSet[], int n);
// union two sets, used in kruskal
void setUnion(std::set<int>* s0, std::set<int>* s1);
// initialize this graph
void init(int n);
// initialize this graph by copying another
void init(const Graph &graph);
private:
int numV, numE; // number of vertices and edges
std::vector< std::pair<int, int> >* adjList; // adjacency list
};
#endif
Graph.cpp
#include "Graph.h"
#include <algorithm>
#include <map>
Graph::Graph()
:numV(0), numE(0), adjList(0)
{
}
Graph::Graph(int n)
:numV(0), numE(0), adjList(0)
{
this->init(n);
}
Graph::Graph(const Graph &graph)
:numV(0), numE(0), adjList(0)
{
this->init(graph);
}
Graph::~Graph()
{
delete[] this->adjList;
}
void Graph::init(int n)
{
if(this->adjList){
delete[] this->adjList;
}
this->numV = n;
this->numE = 0;
this->adjList = new std::vector< std::pair<int, int> >[n];
}
void Graph::init(const Graph &graph)
{
this->init(graph.numV);
for(int i = 0; i < numV; i++){
this->adjList[i] = graph.adjList[i];
}
}
void Graph::addEdge(int from, int to, int weight)
{
if(weight > 0){
this->adjList[from].push_back( std::make_pair(to, weight) );
this->numE++;
}
}
void Graph::getAllEdges(edge_t edgeList[])
{
int k = 0;
for(int i = 0; i < numV; i++){
for(int j = 0; j < this->adjList[i].size(); j++){
// add this edge to edgeList
edgeList[k++] = edge_t(i, this->adjList[i][j].first, this->adjList[i][j].second);
}
}
}
void Graph::sort(int vertexList[])
{
std::pair<int, int>* timestamp = new std::pair<int, int>[this->numV];
std::stack<int> order;
for(int i = 0; i < this->numV; i++){
timestamp[i].first = -1;
timestamp[i].second = -1;
}
for(int v = 0; v < this->numV; v++){
if(timestamp[v].first < 0){
this->sort(v, timestamp, order);
}
}
int i = 0;
while(!order.empty()){
vertexList[i++] = order.top();
order.pop();
}
delete[] timestamp;
return;
}
void Graph::sort(int v, std::pair<int, int> timestamp[], std::stack<int> &order)
{
// discover vertex v
timestamp[v].first = 1;
for(int i = 0; i < this->adjList[v].size(); i++){
int next = this->adjList[v][i].first;
if(timestamp[next].first < 0){
this->sort(next, timestamp, order);
}
}
// finish vertex v
timestamp[v].second = 1;
order.push(v);
return;
}
void Graph::dijkstra(int v, std::pair<int, int> path[])
{
int* q = new int[numV];
int numQ = numV;
for(int i = 0; i < this->numV; i++){
path[i].first = -1; // infinity distance
path[i].second = -1; // no path exists
q[i] = i;
}
// instant reachable to itself
path[v].first = 0;
path[v].second = -1;
while(numQ > 0){
int u = -1; // such node not exists
for(int i = 0; i < numV; i++){
if(q[i] >= 0
&& path[i].first >= 0
&& (u < 0 || path[i].first < path[u].first)){ //
u = i;
}
}
if(u == -1){
// all remaining nodes are unreachible
break;
}
// remove u from Q
q[u] = -1;
numQ--;
for(int i = 0; i < this->adjList[u].size(); i++){
std::pair<int, int>& edge = this->adjList[u][i];
int alt = path[u].first + edge.second;
if(path[edge.first].first < 0 || alt < path[ edge.first ].first){
path[ edge.first ].first = alt;
path[ edge.first ].second = u;
}
}
}
delete[] q;
return;
}
// compare two edges by their weight
bool edgeCmp(edge_t e0, edge_t e1)
{
return e0.w < e1.w;
}
std::set<int>* Graph::findSet(int v, std::set<int> vertexSet[], int n)
{
for(int i = 0; i < n; i++){
if(vertexSet[i].find(v) != vertexSet[i].end()){
return vertexSet+i;
}
}
return 0;
}
void Graph::setUnion(std::set<int>* s0, std::set<int>* s1)
{
if(s1->size() > s0->size()){
std::set<int>* temp = s0;
s0 = s1;
s1 = temp;
}
for(std::set<int>::iterator i = s1->begin(); i != s1->end(); i++){
s0->insert(*i);
}
s1->clear();
return;
}
void Graph::kruskal(Graph &graph)
{
std::vector<edge_t> edgeList;
edgeList.reserve(numE);
for(int i = 0; i < numV; i++){
for(int j = 0; j < this->adjList[i].size(); j++){
// add this edge to edgeList
edgeList.push_back( edge_t(i, this->adjList[i][j].first, this->adjList[i][j].second) );
}
}
// sort the list in ascending order
std::sort(edgeList.begin(), edgeList.end(), edgeCmp);
graph.init(numV);
// create disjoint set of the spanning tree constructed so far
std::set<int>* disjoint = new std::set<int>[this->numV];
for(int i = 0; i < numV; i++){
disjoint[i].insert(i);
}
for(int e = 0; e < edgeList.size(); e++){
// consider edgeList[e]
std::set<int>* s0 = this->findSet(edgeList[e].v0, disjoint, numV);
std::set<int>* s1 = this->findSet(edgeList[e].v1, disjoint, numV);
if(s0 == s1){
// adding this edge will make a cycle
continue;
}
// add this edge to MST
graph.addEdge(edgeList[e].v0, edgeList[e].v1, edgeList[e].w);
// union s0 & s1
this->setUnion(s0, s1);
}
delete[] disjoint;
return;
}
#define IDX(i,j) ((i)*numV+(j))
void Graph::floydWarshall(std::pair<int, int> path[])
{
// initialize
for(int i = 0; i < numV; i++){
for(int j = 0; j < numV; j++){
path[IDX(i,j)].first = -1;
path[IDX(i,j)].second = -1;
}
}
for(int i = 0; i < numV; i++){
for(int j = 0; j < this->adjList[i].size(); j++){
path[IDX(i,this->adjList[i][j].first)].first
= this->adjList[i][j].second;
path[IDX(i,this->adjList[i][j].first)].second
= this->adjList[i][j].first;
}
}
// dynamic programming
for(int k = 0; k < numV; k++){
for(int i = 0; i < numV; i++){
for(int j = 0; j < numV; j++){
if(path[IDX(i,k)].first == -1
|| path[IDX(k,j)].first == -1){
// no path exist from i-to-k or from k-to-j
continue;
}
if(path[IDX(i,j)].first == -1
|| path[IDX(i,j)].first > path[IDX(i,k)].first + path[IDX(k,j)].first){
// there is a shorter path from i-to-k, and from k-to-j
path[IDX(i,j)].first = path[IDX(i,k)].first + path[IDX(k,j)].first;
path[IDX(i,j)].second = k;
}
}
}
}
return;
}
If you are looking for sorting algorithms you should just ask google:
http://en.wikipedia.org/wiki/Sorting_algorithm
My personal favourite is the BogoSort coupled with parallel universe theory. The theory is that if you hook a machine up to the program that can destroy the universe, then if the list isn't sorted after one iteration it will destroy the universe. That way all the parallel universes except the one with the list sorted will be destroyed and you have a sorting algorithm with complexity O(1).
The best ....
Create a struct like this:
template<typename Container, typename Comparison = std::less<typename Container::value_type>>
struct SortHelper
{
Container const* container;
size_t org_index;
SortHelper( Container const* c, size_t index ):container(c), org_index(index) {}
bool operator<( SortHelper other ) const
{
return Comparison()( (*c)[org_index], (*other.c)[other.org_index] );
}
};
This lets you resort things however you want.
Now, make a std::vector<SortHelper<blah>>, sort it, and you now have a vector of instructions of where everything ends up going after you sort it.
Apply these instructions (there are a few ways). An easy way would be to reuse container pointer as a bool. Walk the sorted vector of helpers. Move the first entry to where it should go, moving what you found where it should go to where it should go, and repeat until you loop or the entire array is sorted. As you go, clear the container pointers in your helper struct, and check them to make sure you don't move an entry that has already been moved (this lets you detect loops, for example).
Once a loop has occurred, proceed down the vector looking for the next as-yet-not-in-right-place entry (with a non-null container pointer).