Skip to content
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

Added CPP codes for Lecture 78, 85, 94, 97 #543

Open
wants to merge 8 commits into
base: main
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.exe
/.vscode
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"ostream": "cpp"
}
}
31 changes: 31 additions & 0 deletions Lecture078 Hashmaps/maxFreqElement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <bits/stdc++.h>
using namespace std;
int maximumFrequency(vector<int> &arr, int n)
{
map<int, int> m;
int maxFreq = INT_MIN;
int ans = 0;
for (int i = 0; i < n; i++)
{
m[arr[i]]++;
maxFreq = max(maxFreq, m[arr[i]]);
}
for (int i = 0; i < n; i++)
{
if (maxFreq == m[arr[i]])
{
ans = arr[i];
break;
}
}

return ans;
}
int main()
{
vector<int> arr = {1, 2, 1, 4, 3, 4, 2, 6};
int n = arr.size();
int ans = maximumFrequency(arr, n);
cout << "Answer is: " << ans << endl;
return 0;
}
45 changes: 45 additions & 0 deletions Lecture085 Intro to Graphs/createGraph-I.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <bits/stdc++.h>
using namespace std;
class graph
{
public:
void addEdge(int &u, int &v, int n, int m, vector<vector<int>> &vec)
{
vec[u][v] = 1;
}
void printGraph(int &u, int &v, int n, int m, vector<vector<int>> &vec)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (vec[i][j] == 1)
{
cout << i << " - " << j << ":";
}
}
cout << endl;
}
}
};

int main()
{
int n, m;
cout << "Enter no of nodes: ";
cin >> n;
cout << "Enter no of edges: ";
cin >> m;
graph g;
int u, v;
vector<vector<int>> vec(n, vector<int>(n, 0));
for (int i = 0; i < m; i++)
{

cin >> u >> v;
g.addEdge(u, v, n, m, vec);
}

g.printGraph(u, v, n, m, vec);
return 0;
}
52 changes: 52 additions & 0 deletions Lecture085 Intro to Graphs/createGraph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <bits/stdc++.h>
using namespace std;
class graph
{
public:
unordered_map<int, vector<int>> gr;
void addEdge(int u, int v, bool direction)
{
if (find(gr[u].begin(), gr[u].end(), v) == gr[u].end())
{
gr[u].push_back(v);
}
if (direction == 0)
{
if (find(gr[v].begin(), gr[v].end(), u) == gr[v].end())
{
gr[v].push_back(u);
}
}
}
void printGraph()
{
for (const auto &i : gr)
{
cout << i.first << " -> ";
for (const auto &j : i.second)
{
cout << j << " ,";
}
cout << endl;
}
}
};

int main()
{
int n, m;
cout << "Enter no of nodes: ";
cin >> n;
cout << "Enter no of edges: ";
cin >> m;
graph g;
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
g.addEdge(u, v, 0);
}

g.printGraph();
return 0;
}
127 changes: 127 additions & 0 deletions Lecture094 Shortest Distance in a DAG/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include <bits/stdc++.h>
using namespace std;
/**
* @class graph
* @brief Represents a directed graph using an adjacency list.
*/
class graph
{
public:
unordered_map<int, list<pair<int, int>>> adj;

/**
* @brief Adds a directed edge from node u to node v with weight w to the adjacency list.
* @param u The source node.
* @param v The destination node.
* @param w The weight of the edge.
*/
void addEdge(int u, int v, int w)
{
pair<int, int> p = make_pair(v, w);
adj[u].push_back(p);
}

/**
* @brief Prints the adjacency list of the graph.
*/
void printAdj()
{
for (auto i : adj)
{
cout << i.first << " -> ";
for (auto j : i.second)
{
cout << "(" << j.first << " , " << j.second << ")";
}
cout << endl;
}
}

/**
* @brief Performs a depth-first search (DFS) traversal of the graph starting from the given node.
* @param node The starting node for the DFS traversal.
* @param vis A map to keep track of visited nodes.
* @param s A stack to store the visited nodes in the order they are visited.
* @param adj The adjacency list of the graph.
*/
void dfs(int node, unordered_map<int, bool> &vis, stack<int> &s)
{
vis[node] = 1;
for (auto neigh : adj[node])
{
if (!vis[neigh.first])
{
dfs(neigh.first, vis, s);
}
}
s.push(node);
}

/**
* @brief Calculates the shortest path from the source node to all other nodes using a topological sort and dynamic programming approach.
* @param dist A vector to store the shortest distances from the source node to each node.
* @param src The source node.
* @param s A stack containing the nodes in topological order.
*/
void getShortestPath(vector<int> &dist, int src, stack<int> &s)
{
dist[src] = 0;

while (!s.empty())
{
int node = s.top();
s.pop();

if (dist[node] != INT_MAX)
{
for (auto neigh : adj[node])
{
if (dist[node] + neigh.second < dist[neigh.first])
dist[neigh.first] = dist[node] + neigh.second;
}
}
}
}
};

int main()
{
graph g;
g.addEdge(0, 1, 5);
g.addEdge(0, 2, 3);
g.addEdge(1, 2, 2);
g.addEdge(1, 3, 6);
g.addEdge(2, 3, 7);
g.addEdge(2, 4, 4);
g.addEdge(2, 5, 2);
g.addEdge(3, 4, -1);
g.addEdge(4, 5, -2);

// topological sort

int n = 6;
unordered_map<int, bool> vis;
stack<int> s;
for (int i = 0; i < n; i++)
{
if (!vis[i])
{
g.dfs(i, vis, s);
}
}

int src = 1;
vector<int> dist(n, INT_MAX);

g.getShortestPath(dist, src, s);
cout << "Printing Ans:" << endl;
for (auto ele : dist)
{
if (ele == INT_MAX)
cout << "INF, ";
else
cout << ele << " , ";
}
cout << endl;
return 0;
}
87 changes: 87 additions & 0 deletions Lecture097 Disjoint Set_Union by rank/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <bits/stdc++.h>
using namespace std;
/**
* @class DisjointSet
* @brief A class that implements the disjoint-set data structure.
*
* The disjoint-set data structure is used to keep track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets.
*/
class DisjointSet
{
vector<int> rank, parent;

public:
/**
* @brief Constructor that creates a new disjoint-set data structure with a given number of elements.
* @param n The number of elements in the disjoint-set data structure.
*/
DisjointSet(int n)
{
rank.resize(n + 1, 0);
parent.resize(n + 1);
for (int i = 0; i <= n; i++)
{
parent[i] = i;
}
}

/**
* @brief Finds the parent of a given element in the disjoint-set data structure.
* @param node The element for which to find the parent.
* @return The parent of the given element.
*/
int findParent(int node)
{
if (parent[node] == node)
return node;
return parent[node] = findParent(parent[node]);
}

/**
* @brief Merges two subsets together by their rank, ensuring that the smaller subset is merged into the larger subset.
* @param u The first element of the subset to merge.
* @param v The second element of the subset to merge.
*/
void unionByRank(int u, int v)
{
int pu = findParent(u);
int pv = findParent(v);
if (pu == pv)
return;

if (rank[pu] < rank[pv])
{
parent[pu] = pv;
}
else if (rank[pu] > rank[pv])
{
parent[pv] = pu;
}
else
{
parent[pv] = pu;
rank[pu]++;
}
}
};

int main()
{
DisjointSet ds(7);
ds.unionByRank(1, 2);
ds.unionByRank(2, 3);
ds.unionByRank(4, 5);
ds.unionByRank(6, 7);
ds.unionByRank(5, 6);
if (ds.findParent(3) == ds.findParent(7))
cout << "Same" << endl;
else
cout << "Not Same" << endl;
ds.unionByRank(3, 7);
if (ds.findParent(3) == ds.findParent(7))
cout << "Same" << endl;
else
cout << "Not Same" << endl;

return 0;
}
Loading