// 二分查找 key的位置
// arr是排好序的数组
template<class T>
int binary_search(T * arr, int len, T& key){
int low = 0; hight = len - 1;
while(low <= hight){
int mid = (low + hight) >> 1;
if(arr[mid] == key) return mid;
else if(arr[mid] < key) low = mid + 1;
else hight = mid - 1;
}
return -1;
}
// ------------------------------最短路径-------------------------------------------
// Folyd算法: 计算各个结点之间的最短路径
template<class T>
void Folyd(T** dist, int n){
for(int k = 0; k < n; k++){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(dist[i][j] > dist[i][k] + dist[k][j]){
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
}
// can work better with minheap, here demostrate the algorithm only.
// 计算单源最短路径
template<class T>
void dijkstra(T** graph, T * dist, int n, int s){
bool * visited = new bool[n];
memset(visited, 0, sizeof(bool) * n);
for(int i = 0; i < n; i++){
dist[i] = INF;
}
dist[s] = 0;
for(int i = 0; i < n; i ++){
// find the vertex closest to s;
T min = INF;
int k = -1;
for(int j = 0; j < n; j++){
if(!visited[j] && dist[j] < min){
k = j;
min = dist[j];
}
}
// relax other vertex.
for(int j = 0; j < n; j++){
if(!visited[j] && dist[j] > dist[k] + graph[k][j]){
dist[j] = dist[k] + graph[k][j];
}
}
visted[k] = true;
}
delete [] visited;
}
//
template<class T>
void bellman-fold(T** graph, T * dist, int n, int s){
memset(dist, INF, n * sizeof(T));
dist[s] = 0;
for(int k = 0; k < n; k++)
for(int i = 0; i < n; i ++){
for(int j = 0; j < n; j++){
if(dist[j] > dist[i] + graph[i][j]){
dist[j] = dist[i] + graph[i][j];
}
}
}
}
// -------------------------------最小生成树-------------------------------------
// prime 算法, 每次取出最小的边,加入到生成树种
template<class T>
int prime(T** graph, int n, int start){
bool * visited = new bool[n];
memset(visited, 0, sizeof(bool) * n);
T * dist = new T[n];
for(int i = 0; i < n; i ++){
dist[i] = graph[start][i];
}
visted[start] = true;
int sum = 0;
for(int i = 0; i < n; i ++){
T min = INF;
int k = -1;
for(int j = 0; j < n; j++){
if(!visited[j] && dist[j] < min){
k = j;
min = dist[j];
}
}
visited[k] = true;
sum += dist[j];
for(int j = 0; j < n; j++){
if(!visited[j] && dist[j] > graph[k][j]){
dist[j] = graph[k][j];
}
}
}
delete [] visited;
delete [] dist;
return sum;
}
// -----kruskal----
// union set
class union_set{
public:
union_set(int n){
father = new int[n];
rank = new int[n];
size = n;
make_set();
}
void make_set(){
for(int i = 0; i < size; i ++){
father[i] = i;
rank[i] = 1;
}
}
void find_father(int x){
if(x != father[x]){
father[x] = find_father(father[x]);
}
return father[x];
}
void union(int x, int y){
int father_x = find_father(x);
int father_y = find_father(y);
if(father_x == father_y){
return;
}
if(rank[father_x] < rank[father_y]){
father[father_x] = father_y;
}else{
if(rank[father_x] == rank[father_y]){
rank[father_x] ++;
}
father[father_y] = father_x;
}
}
private:
int size;
int * father;
int * rank;
};
typedef struct{
int start;
int end;
int val;
} Edge;
// 使用并查集计算最短生成路径。
// 把每个节点当作一颗树,把边上的两个节点所在的树逐渐合并,直到形成一颗完整的树
// Edge是按val大小排好序的数组。
int kruskal(Edge * edge, int n, int vertex_size){
union_set uset(vertex_size);
int sum = 0;
for(int i = 0; i< n; i++){
if(uset.find_father(edge[i].start) != uset.find_father(edge[i].end)){
uset.union(edge[i].start, edge[i].end);
sum += edge[i].val;
}
}
return sum;
}
template<class T>
void bubble_sort(T * arr, int n){
for(int i = 1; i < n; i++)
for(int j = n - 1; j >= i; j--)
if(arr[j] < arr[j - 1])
swap(arr[j], arr[j - 1]);
}
template<class T>
void insert_sort(T * arr, int n){
T t;
int j;
for(int i = 1; i < n; i++){
t = arr[i];
for(j = i; j > 0 && arr[j - 1] > arr[j]; j --){
arr[j] = arr[j - 1];
}
arr[j] = t;
}
}
template<class T>
void select_sort(T * arr, int n){
int k;
for(int i = 0; i < n; i ++){
k = i;
for(int j = i + 1; j < n; j++){
if(arr[j] < arr[k]){
k = j;
}
}
swap(arr[i], arr[k]);
}
}
template<class T>
void hill_sort(T * t, int n){
for(int d = n/2; d >= 1; d /=2){
for(int i = 0; i < d; i++){
for(int j = n - 1; j >= i + d; j -= d){
if ( arr[j] < arr[j-d]){
swap(arr[j], arr[j-d]);
}
}
}
}
}
// 快排,比较短小精悍。
template<class T>
int partition(T * arr, int left, int right){
int x = left - 1;
for( int i = left; i < right; i++){
if(arr[i] < arr[right]){
x ++ ;
swap(arr[x], arr[i]);
}
}
x++;
swap(arr[x], arr[right]);
return x;
}
template<class T>
void quick_sort(T * arr, int left, int right){
if(left >= right) return;
int p = partition(arr, left, right);
quick_sort(arr, left, p - 1);
quick_sort(arr, p + 1, right);
}
常用算法-mark
最新推荐文章于 2024-01-27 03:56:34 发布