Skip to content

Added dijkstra algorithm implementation in c++ #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions graph/c++/dijkstra.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

#define fst first
#define snd second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
typedef long long ll;

const int N = 1e5 + 5;
int v, y, cost, best[N];
vector<pair<int, int>> graph[N];

void dijkstra(int x) {
memset(best, -1, sizeof best);
priority_queue<pair<int, int>> queue;
best[x] = 0; // First node.
queue.push({x, -best[x]}); // Now it's {x, 0}.

while(queue.size()){ // While it's not empty.
y = queue.top().fst; // The node we're now.
cost = -queue.top().snd; // The cost to that node.
queue.pop(); // We don't need that node anymore.

if(cost > best[y]) continue;
for(int i = 0; i < graph[x].size(); i++){ // For every neighbour of x...
v = graph[x][i].fst; // Get the node.
cost = graph[x][i].snd; // Get it's cost.

// If it's unvisited, or we found a better path...
if(best[v] < 0 or best[y] + cost < best[v]){
best[v] = best[y] + cost; // Change the cost.
queue.push(mp(v, -best[v])); // Push that node to the queue to visit it's neighbours.
}
}
}
}
54 changes: 54 additions & 0 deletions graph/c++/kruskal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ll long long
const int N = 1000;
vector<pair<ll,pair<ll,ll>>> g; // Graph (cost, (x, y))
ll n, m, a, b, c, mst_weight, mst_vertex, mst_edges, ancestors[N];

void set_fathers(){
for(ll i = 0; i < N; i++){
ancestors[i] = i;
}
}

ll father(ll x){
if(ancestors[x] == x) return x;
else return father(ancestors[x]);
}

void unite(ll a, ll b){
ll father_a = father(a);
ll father_b = father(b);
ancestors[father_a] = father_b;
}

void kruskal(){
set_fathers();
sort(g.begin(), g.end());

while(mst_vertex < n - 1 or mst_edges < m){
a = g[mst_edges].snd.fst;
b = g[mst_edges].snd.snd;
c = g[mst_edges].fst;
if(father(a) != father(b)){
unite(a, b);
mst_weight += c;
mst_vertex++;
}
mst_edges++;
}
}

int main(){
cin >> n >> m;
for(ll i = 0; i < m; i++){
cin >> a >> b >> c;
g.pb(mp(c, mp(a,b)));
}
kruskal();
return 0;
}