Skip to content
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
30 changes: 30 additions & 0 deletions graph/adjList.sublime-snippet
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<snippet>
<content><![CDATA[
void adjList()
{
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n);
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
for (int i = 0; i < adj.size(); i++)
{
cout << i << "->";
for (auto x : adj[i])
{
cout << x << " ";
}
cout << endl;
}
}
]]></content>
<tabTrigger>adjList</tabTrigger>
<description>Adjacency List in Graph</description>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope >source.python</scope > -->
</snippet>
36 changes: 36 additions & 0 deletions graph/adjMatrix.sublime-snippet
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<snippet>
<content><![CDATA[
void adjMatrix()
{
int n, m;
cin >> n >> m;
int adj[n + 1][n + 1];
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= n; j++)
{
adj[i][j] = 0;
}
}
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
adj[u][v] = 1;
adj[v][u] = 1;
}
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= n; j++)
{
cout << adj[i][j] << " ";
}
cout << endl;
}
}
]]></content>
<tabTrigger>adjMatrix</tabTrigger>
<description>Adjacency Matrix in Graph</description>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope >source.python</scope > -->
</snippet>
54 changes: 54 additions & 0 deletions graph/bellmanFord.sublime-snippet
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<snippet>
<content><![CDATA[
void bellmanFord()
{
int N, m;
cin >> N >> m;
vector<node> edges;
for (int i = 0; i < m; i++)
{
int u, v, wt;
cin >> u >> v >> wt;
edges.push_back(node(u, v, wt));
}
int src;
cin >> src;

int inf = 1e9;
vector<int> dist(N, inf);
dist[src] = 0;

for (int i = 0; i <= N - 1; i++)
{
for (auto it : edges)
{
if (dist[it.u] + it.wt < dist[it.v])
{
dist[it.v] = dist[it.u] + it.wt;
}
}
}
int fl = 0;
for (auto it : edges)
{
if (dist[it.u] + it.wt < dist[it.v])
{
cout << "NegativeCycle" << endl;
fl = 1;
break;
}
}
if (!fl)
{
for (int i = 0; i < N; i++)
{
cout << i << " " << dist[i] << endl;
}
}
}
]]></content>
<tabTrigger>bellmanFord</tabTrigger>
<description>Bellman Ford Algorithm</description>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope >source.python</scope > -->
</snippet>
57 changes: 57 additions & 0 deletions graph/dijsktra.sublime-snippet
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<snippet>
<content><![CDATA[
void dijsktra()
{
int n, m, source;
cin >> n >> m;
vector<pair<int, int>> g[n + 1];

int a, b, wt;
for (int i = 0; i < m; i++)
{
cin >> a >> b >> wt;
g[a].push_back(make_pair(b, wt));
g[b].push_back(make_pair(a, wt));
}
cin >> source;

priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
vector<int> disTo(n + 1, INT_MAX);

disTo[source] = 0;

pq.push(make_pair(0, source));

while (!pq.empty())
{
int dist = pq.top().first;
int prev = pq.top().second;
pq.pop();

vector<pair<int, int>>::iterator it;

for (it = g[prev].begin(); it != g[prev].end(); it++)
{
int next = it->first;
int nextDist = it->second;

if (disTo[next] > disTo[prev] + nextDist)
{
disTo[next] = disTo[prev] + nextDist;
pq.push(make_pair(disTo[next], next));
}
}
}
cout << "The distances from source, " << source << ", are : " << endl;
for (int i = 1; i <= n; i++)
{
cout << disTo[i] << " ";
}
cout << endl;
}
]]></content>
<tabTrigger>dijsktra</tabTrigger>
<description>Dijsktra Algorithm</description>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope >source.python</scope > -->
</snippet>
53 changes: 53 additions & 0 deletions graph/isCycleBfs.sublime-snippet
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<snippet>
<content><![CDATA[
void CycleBFS()
{
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n);
vector<int> indeg(n, 0);
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
adj[u].pb(v);
indeg[v]++;
}
queue<int> q;
for (int i = 0; i < indeg.size(); i++)
{
if (indeg[i] == 0)
{
q.push(i);
}
}
int count = 0;
while (!q.empty())
{
int node = q.front();

q.pop();
for (auto x : adj[node])
{
indeg[x]--;
if (indeg[x] == 0)
{
q.push(x);
}
}
}
if (count != n)
{
cout << true << endl;
}
else
{
cout << false << endl;
}
}
]]></content>
<tabTrigger>isCycleBfs</tabTrigger>
<description>Is Cycle is Present in Graph using BFS</description>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope >source.python</scope > -->
</snippet>
51 changes: 51 additions & 0 deletions graph/isCycleDfs.sublime-snippet
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<snippet>
<content><![CDATA[
bool isCycle(int s, vector<vector<int>> &adj, vector<bool> &vis, int parent)
{
vis[s] = true;
for (auto x : adj[s])
{
if (x != parent)
{
if (vis[x])
{
return true;
}
if (!vis[x] && isCycle(x, adj, vis, s))
{
return true;
}
}
}
return false;
}
void CycleDFS()
{
int n, m;
cin >> n >> m;
vector<bool> vis(n, false);
vector<vector<int>> adj(n);
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
adj[u].pb(v);
}

for (int i = 0; i < n; i++)
{
if (!vis[i] && isCycle(i, adj, vis, -1))
{

cout << true << endl;
return;
}
}
cout << false << endl;
}
]]></content>
<tabTrigger>isCycleDfs</tabTrigger>
<description>Is Cycle is Present in Graph using DFS</description>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope >source.python</scope > -->
</snippet>
67 changes: 67 additions & 0 deletions graph/shortesPathDAG.sublime-snippet
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<snippet>
<content><![CDATA[
void TopodfsDAG(int node, vector<pair<int, int>> adj[], vector<bool> &vis, stack<int> &st)
{
vis[node] = true;
for (auto x : adj[node])
{
if (!vis[x.first])
{
TopodfsDAG(x.first, adj, vis, st);
}
}
st.push(node);
}
void shortesPathDAG()
{
int n, m;
cin >> n >> m;
vector<bool> vis(n + 1, false);
vector<pair<int, int>> adj[n];
for (int i = 0; i < m; i++)
{
int u, v, w;
cin >> u >> v >> w;
adj[u].pb({v, w});
}
int src;
cin >> src;
stack<int> st;
for (int i = 0; i < n; i++)
{
if (!vis[i])
{

TopodfsDAG(i, adj, vis, st);
}
}
int dist[n];
for (int i = 0; i < n; i++)
{
dist[i] = INT_MAX;
}
dist[src] = 0;
while (!st.empty())
{
int node = st.top();
st.pop();
if (dist[node] != INT_MAX)
{
for (auto x : adj[node])
{
if (dist[x.first] > dist[node] + x.second)
{
dist[x.first] = dist[node] + x.second;
}
}
}
}
for (int i = 0; i < n; i++)
(dist[i] == INT_MAX) ? cout << "INFINITY " : cout << dist[i] << " ";
}
]]></content>
<tabTrigger>shortesPathDAG</tabTrigger>
<description>Shortest Path Direct Acyclic Graph</description>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope >source.python</scope > -->
</snippet>
Loading