diff --git a/graph/adjList.sublime-snippet b/graph/adjList.sublime-snippet new file mode 100644 index 0000000..161add7 --- /dev/null +++ b/graph/adjList.sublime-snippet @@ -0,0 +1,30 @@ + + > n >> m; + vector> 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; + } +} +]]> + adjList + Adjacency List in Graph + + + \ No newline at end of file diff --git a/graph/adjMatrix.sublime-snippet b/graph/adjMatrix.sublime-snippet new file mode 100644 index 0000000..ba2b6c9 --- /dev/null +++ b/graph/adjMatrix.sublime-snippet @@ -0,0 +1,36 @@ + + > 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; + } +} +]]> + adjMatrix + Adjacency Matrix in Graph + + + \ No newline at end of file diff --git a/graph/bellmanFord.sublime-snippet b/graph/bellmanFord.sublime-snippet new file mode 100644 index 0000000..8da7163 --- /dev/null +++ b/graph/bellmanFord.sublime-snippet @@ -0,0 +1,54 @@ + + > N >> m; + vector 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 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; + } + } +} +]]> + bellmanFord + Bellman Ford Algorithm + + + \ No newline at end of file diff --git a/graph/dijsktra.sublime-snippet b/graph/dijsktra.sublime-snippet new file mode 100644 index 0000000..fc472ce --- /dev/null +++ b/graph/dijsktra.sublime-snippet @@ -0,0 +1,57 @@ + + > n >> m; + vector> 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, vector>, greater>> pq; + vector 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>::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; +} +]]> + dijsktra + Dijsktra Algorithm + + + \ No newline at end of file diff --git a/graph/isCycleBfs.sublime-snippet b/graph/isCycleBfs.sublime-snippet new file mode 100644 index 0000000..0a4c7db --- /dev/null +++ b/graph/isCycleBfs.sublime-snippet @@ -0,0 +1,53 @@ + + > n >> m; + vector> adj(n); + vector indeg(n, 0); + for (int i = 0; i < m; i++) + { + int u, v; + cin >> u >> v; + adj[u].pb(v); + indeg[v]++; + } + queue 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; + } +} +]]> + isCycleBfs + Is Cycle is Present in Graph using BFS + + + \ No newline at end of file diff --git a/graph/isCycleDfs.sublime-snippet b/graph/isCycleDfs.sublime-snippet new file mode 100644 index 0000000..1afd6ad --- /dev/null +++ b/graph/isCycleDfs.sublime-snippet @@ -0,0 +1,51 @@ + + > &adj, vector &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 vis(n, false); + vector> 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; +} +]]> + isCycleDfs + Is Cycle is Present in Graph using DFS + + + \ No newline at end of file diff --git a/graph/shortesPathDAG.sublime-snippet b/graph/shortesPathDAG.sublime-snippet new file mode 100644 index 0000000..93971cf --- /dev/null +++ b/graph/shortesPathDAG.sublime-snippet @@ -0,0 +1,67 @@ + + > adj[], vector &vis, stack &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 vis(n + 1, false); + vector> 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 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] << " "; +} +]]> + shortesPathDAG + Shortest Path Direct Acyclic Graph + + + \ No newline at end of file diff --git a/graph/topoBfs.sublime-snippet b/graph/topoBfs.sublime-snippet new file mode 100644 index 0000000..1d2d004 --- /dev/null +++ b/graph/topoBfs.sublime-snippet @@ -0,0 +1,46 @@ + + > n >> m; + vector> adj(n); + vector indeg(n, 0); + for (int i = 0; i < m; i++) + { + int u, v; + cin >> u >> v; + adj[u].pb(v); + indeg[v]++; + } + queue q; + for (int i = 0; i < indeg.size(); i++) + { + if (indeg[i] == 0) + { + q.push(i); + } + } + + while (!q.empty()) + { + int node = q.front(); + + q.pop(); + cout << node << " "; + for (auto x : adj[node]) + { + indeg[x]--; + if (indeg[x] == 0) + { + q.push(x); + } + } + } +} +]]> + topoBfs + Topological BFS in Graph + + + \ No newline at end of file diff --git a/graph/topoDfs.sublime-snippet b/graph/topoDfs.sublime-snippet new file mode 100644 index 0000000..7b7fbc6 --- /dev/null +++ b/graph/topoDfs.sublime-snippet @@ -0,0 +1,52 @@ + + > adj, vector &vis, stack &st) +{ + vis[node] = true; + for (auto x : adj[node]) + { + if (vis[x]) + { + ; + } + else + { + Topodfs(x, adj, vis, st); + } + } + st.push(node); +} +void TopoDFS() +{ + int n, m; + cin >> n >> m; + vector vis(n + 1, false); + vector> adj(n + 1); + for (int i = 0; i < m; i++) + { + int u, v; + cin >> u >> v; + adj[u].pb(v); + } + stack st; + for (int i = 0; i < n; i++) + { + if (!vis[i]) + { + + Topodfs(i, adj, vis, st); + } + } + while (!st.empty()) + { + int ans = st.top(); + cout << ans << " "; + st.pop(); + } +} +]]> + topoDfs + Topological DFS in Graph + + + \ No newline at end of file