diff --git a/CSE-205 DSA/Unit-6/Lecture-35/Lithish.cpp b/CSE-205 DSA/Unit-6/Lecture-35/Lithish.cpp new file mode 100644 index 00000000..b6bbd6ab --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-35/Lithish.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; +#define MAX_VERTICES 100 + +void addEdge(int adj[MAX_VERTICES][MAX_VERTICES], int v, int w) { + adj[v][w] = 1; +} + +void DFS(int adj[MAX_VERTICES][MAX_VERTICES], int visited[MAX_VERTICES], int V, int v) { + cout << v << " "; + visited[v] = 1; + for (int i = 0; i < V; ++i) { + if (adj[v][i] && !visited[i]) { + DFS(adj, visited, V, i); + } + } +} + +int main() { + int V, E; + cin >> V >> E; + int adj[MAX_VERTICES][MAX_VERTICES] = {0}; + int visited[MAX_VERTICES] = {0}; + + for (int i = 0; i < E; ++i) { + int v, w; + cin >> v >> w; + addEdge(adj, v, w); + } + + int startVertex; + cin >> startVertex; + + cout << "Depth First Traversal starting from vertex " << startVertex << ":\n"; + DFS(adj, visited, V, startVertex); + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-35/Raja.cpp b/CSE-205 DSA/Unit-6/Lecture-35/Raja.cpp new file mode 100644 index 00000000..4463bc8c --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-35/Raja.cpp @@ -0,0 +1,54 @@ +// You are using GCC +#include +#define MAXN 100 +using namespace std; + +int dfs(int node, int adj[][MAXN], int dp[], bool vis[], int n) { + if (vis[node]) { + return dp[node]; + } + vis[node] = true; + int maxPath = 0; + for (int i = 0; i < n; i++) { + if (adj[node][i]) { + maxPath = max(maxPath, 1 + dfs(i, adj, dp, vis, n)); + } + } + dp[node] = maxPath; + return maxPath; +} + +void addEdge(int adj[][MAXN], int u, int v) { + adj[u][v] = 1; +} + +int findLongestPath(int adj[][MAXN], int n) { + int dp[MAXN] = {0}; + bool vis[MAXN] = {false}; + int longestPath = 0; + + for (int i = 0; i < n; i++) { + if (!vis[i]) { + longestPath = max(longestPath, dfs(i, adj, dp, vis, n)); + } + } + + return longestPath; +} + +int main() { + int n, m; + cin >> n >> m; + int adj[MAXN][MAXN] = {0}; + + for (int i = 0; i < m; ++i) { + int u, v; + cin >> u >> v; + addEdge(adj, u - 1, v - 1); + } + + int result = findLongestPath(adj, n); + cout << result << endl; + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-35/Raju.cpp b/CSE-205 DSA/Unit-6/Lecture-35/Raju.cpp new file mode 100644 index 00000000..d2dfe256 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-35/Raju.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +bool dfs(int adjMatrix[][100], int n, int start, int end, bool visited[]) { + if (start == end) + return true; + visited[start] = true; + for (int i = 0; i < n; ++i) { + if (adjMatrix[start][i] && !visited[i] && dfs(adjMatrix, n, i, end, visited)) + return true; + } + return false; +} + +bool validPath(int n, int edges[][2], int m, int start, int end) { + int adjMatrix[100][100] = {0}; + + for (int i = 0; i < m; ++i) { + int u = edges[i][0]; + int v = edges[i][1]; + adjMatrix[u][v] = 1; + } + + bool visited[n] = {false}; + if (dfs(adjMatrix, n, start, end, visited)) { + cout << "There is a path from " << start << " to " << end << endl; + return true; + } else { + cout << "There is no path from " << start << " to " << end << endl; + return false; + } +} + +int main() { + int n, m; + cin >> n >> m; + + int edges[m][2]; + for (int i = 0; i < m; ++i) { + cin >> edges[i][0] >> edges[i][1]; + } + + int start, end; + cin >> start >> end; + + validPath(n, edges, m, start, end); + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-35/Rithu.cpp b/CSE-205 DSA/Unit-6/Lecture-35/Rithu.cpp new file mode 100644 index 00000000..03f3da29 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-35/Rithu.cpp @@ -0,0 +1,58 @@ +#include + +#define MAXN 100 + +using namespace std; + +int dfs(int node, int adj[][MAXN], int dp[], bool vis[], int n) { + if (vis[node]) { + return dp[node]; + } + + vis[node] = true; + int maxPath = 0; + + for (int i = 0; i < n; i++) { + if (adj[node][i]) { + maxPath = max(maxPath, 1 + dfs(i, adj, dp, vis, n)); + } + } + + dp[node] = maxPath; + return maxPath; +} + +void addEdge(int adj[][MAXN], int u, int v) { + adj[u][v] = 1; +} + +int findLongestPath(int adj[][MAXN], int n) +{ + int dp[MAXN] = {0}; + bool vis[MAXN] = {false}; + int maxPath = 0; + + for (int i = 0; i < n; i++) { + if (!vis[i]) { + maxPath = max(maxPath, dfs(i, adj, dp, vis, n)); + } + } + + return maxPath; +} + +int main() { + int n, m; + cin >> n >> m; + + int adj[MAXN][MAXN] = {0}; + + for (int i = 0; i < m; i++) { + int u, v; + cin >> u >> v; + addEdge(adj, u - 1, v - 1); + } + + cout << findLongestPath(adj, n); + return 0; +} diff --git a/CSE-205 DSA/Unit-6/Lecture-35/Rohan.cpp b/CSE-205 DSA/Unit-6/Lecture-35/Rohan.cpp new file mode 100644 index 00000000..0f53f144 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-35/Rohan.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +using namespace std; + +void add_edge(int adj[][100], int src, int dest) { + adj[src][dest] = 1; + adj[dest][src] = 1; +} + +bool BFS(int adj[][100], int src, int dest, int v, int pred[], int dist[]) { + queue q; + bool visited[v]; + for (int i = 0; i < v; i++) { + visited[i] = false; + dist[i] = INT_MAX; + pred[i] = -1; + } + + visited[src] = true; + dist[src] = 0; + q.push(src); + + while (!q.empty()) { + int u = q.front(); + q.pop(); + + for (int i = 0; i < v; i++) { + if (adj[u][i] && !visited[i]) { + visited[i] = true; + dist[i] = dist[u] + 1; + pred[i] = u; + q.push(i); + + if (i == dest) + return true; + } + } + } + + return false; +} + +void printShortestDistance(int adj[][100], int s, int dest, int v, int pred[], int dist[]) { + if (!BFS(adj, s, dest, v, pred, dist)) { + cout << "No path exists" << endl; + return; + } + + int path[v]; + int crawl = dest; + int pathLength = 0; + path[pathLength++] = crawl; + while (pred[crawl] != -1) { + path[pathLength++] = pred[crawl]; + crawl = pred[crawl]; + } + + cout << "Shortest path length is: " << dist[dest] << endl; + cout << "Path is: "; + for (int i = pathLength - 1; i >= 0; i--) { + cout << path[i] << " "; + } + cout << endl; +} + +int main() { + int v, e; + cin >> v >> e; + int adj[100][100] = {0}; // Assuming a maximum of 100 vertices + int src, dest; + + for (int i = 0; i < e; i++) { + cin >> src >> dest; + add_edge(adj, src, dest); + } + + int s; + cin >> s; // source + int destination; + cin >> destination; // destination + + int pred[v], dist[v]; + printShortestDistance(adj, s, destination, v, pred, dist); + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-35/Sanju.cpp b/CSE-205 DSA/Unit-6/Lecture-35/Sanju.cpp new file mode 100644 index 00000000..38ac8b4e --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-35/Sanju.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; +#define MAX_V 100 + +void enqueue(int* queue, int& rear, int vertex) { + queue[++rear] = vertex; +} + +int dequeue(int* queue, int& front) { + return queue[++front]; +} + +void bfsOfGraph(int V, int adjList[MAX_V][MAX_V]) { + int queue[MAX_V] = {0}; + int front = -1, rear = -1; + int visited[MAX_V] = {0}; + + int startVertex; + cin >> startVertex; + + enqueue(queue, rear, startVertex); + visited[startVertex] = 1; + + while (front != rear) { + int currentVertex = dequeue(queue, front); + cout << currentVertex << " "; + + for (int i = 0; i < V; ++i) { + if (adjList[currentVertex][i] && !visited[i]) { + enqueue(queue, rear, i); + visited[i] = 1; + } + } + } +} + +int main() { + int V, E; + cin >> V >> E; + int adjList[MAX_V][MAX_V] = {0}; + + for (int i = 0; i < E; ++i) { + int u, v; + cin >> u >> v; + adjList[u][v] = 1; + } + + bfsOfGraph(V, adjList); + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-35/Siddhu.cpp b/CSE-205 DSA/Unit-6/Lecture-35/Siddhu.cpp new file mode 100644 index 00000000..75d7e119 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-35/Siddhu.cpp @@ -0,0 +1,45 @@ +// You are using GCC +#include +#include +using namespace std; +#define MAX_V 100 + +void bfsOfGraph(int V, int adjList[MAX_V][MAX_V]) { + int queue[MAX_V] = {0}; + int front = -1, rear = -1; + int visited[MAX_V] = {0}; + + int startVertex; + cin >> startVertex; + + queue[++rear] = startVertex; + visited[startVertex] = 1; + + while (front != rear) { + int currentVertex = queue[++front]; + cout << currentVertex << " "; + + for (int i = 0; i < V; ++i) { + if (adjList[currentVertex][i] && !visited[i]) { + queue[++rear] = i; + visited[i] = 1; + } + } + } +} + +int main() { + int V, E; + cin >> V >> E; + int adjList[MAX_V][MAX_V] = {0}; + + for (int i = 0; i < E; ++i) { + int u, v; + cin >> u >> v; + adjList[u][v] = 1; + } + + bfsOfGraph(V, adjList); + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-35/gopi.cpp b/CSE-205 DSA/Unit-6/Lecture-35/gopi.cpp new file mode 100644 index 00000000..5c2d6c14 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-35/gopi.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +using namespace std; + +void add_edge(int adj[][100], int src, int dest) { + adj[src][dest] = 1; + adj[dest][src] = 1; +} + +bool BFS(int adj[][100], int src, int dest, int v, int pred[], int dist[]) { + queue q; + bool visited[v]; + + for (int i = 0; i < v; i++) { + visited[i] = false; + dist[i] = INT_MAX; + pred[i] = -1; + } + + visited[src] = true; + dist[src] = 0; + q.push(src); + + while (!q.empty()) { + int u = q.front(); + q.pop(); + + for (int i = 0; i < v; i++) { + if (adj[u][i] && !visited[i]) { + visited[i] = true; + dist[i] = dist[u] + 1; + pred[i] = u; + q.push(i); + + if (i == dest) + return true; + } + } + } + + return false; +} + +void printShortestDistance(int adj[][100], int s, int dest, int v, int pred[], int dist[]) { + if (BFS(adj, s, dest, v, pred, dist) == false) { + cout << "No path exists between " << s << " and " << dest << endl; + return; + } + + int path[v]; + int crawl = dest; + int pathLength = 0; + path[pathLength++] = crawl; + + while (pred[crawl] != -1) { + path[pathLength++] = pred[crawl]; + crawl = pred[crawl]; + } + + cout << "Shortest path length is: " << dist[dest] << endl; + cout << "Path is: "; + for (int i = pathLength - 1; i >= 0; i--) { + cout << path[i] << " "; + } + cout << endl; +} + +int main() { + int v, e; + cin >> v >> e; + + int adj[100][100] = {0}; + int pred[v], dist[v]; + + for (int i = 0; i < e; i++) { + int src, dest; + cin >> src >> dest; + add_edge(adj, src, dest); + } + + int source, destination; + cin >> source >> destination; + + printShortestDistance(adj, source, destination, v, pred, dist); + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-35/kalpana.cpp b/CSE-205 DSA/Unit-6/Lecture-35/kalpana.cpp new file mode 100644 index 00000000..ff1ff936 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-35/kalpana.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +#define MAX_VERTICES 100 + +void addEdge(int adj[MAX_VERTICES][MAX_VERTICES], int v, int w) { + adj[v][w] = 1; +} + +void DFS(int adj[MAX_VERTICES][MAX_VERTICES], int visited[MAX_VERTICES], int V, int v) { + cout << v << " "; + visited[v] = 1; + + for (int i = 0; i < V; i++) { + if (adj[v][i] && !visited[i]) { + DFS(adj, visited, V, i); + } + } +} + +int main() { + int V, E; + cin >> V >> E; + + int adj[MAX_VERTICES][MAX_VERTICES] = {0}; + int visited[MAX_VERTICES] = {0}; + + for (int i = 0; i < E; ++i) { + int v, w; + cin >> v >> w; + addEdge(adj, v, w); + } + + int startVertex; + cin >> startVertex; + + cout << "Depth First Traversal starting from vertex " << startVertex << ":\n"; + DFS(adj, visited, V, startVertex); + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-36/Harish.cpp b/CSE-205 DSA/Unit-6/Lecture-36/Harish.cpp new file mode 100644 index 00000000..c55dc876 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-36/Harish.cpp @@ -0,0 +1,64 @@ +//failing 1 test case + +#include +using namespace std; + +// A function to print the matrix of connections +void printMatrix(int** matrix, int n) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) + cout << matrix[i][j] << " "; + cout << "\n"; + } +} + +// A function to find the transitive closure using Warshall's Algorithm +void warshall(int** matrix, int n) { + // Loop through all nodes as intermediate nodes + for (int k = 0; k < n; k++) { + // Loop through all pairs of nodes + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + // If there is a path from i to k and from k to j, then there is a path from i to j + matrix[i][j] = matrix[i][j] || (matrix[i][k] && matrix[k][j]); + } + } + } +} + +// The main function +int main() { + // Read the number of nodes + int n; + cin >> n; + + // Allocate memory for the matrix of connections + int** matrix = new int*[n]; + for (int i = 0; i < n; i++) + matrix[i] = new int[n]; + + // Read the matrix of connections + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + cin >> matrix[i][j]; + + // Read the source and target nodes + int source, target; + cin >> source >> target; + + // Find the transitive closure using Warshall's Algorithm + warshall(matrix, n); + + // Check if there is a path from source to target + if (matrix[source][target]) + cout << "Yes\n"; + else + cout << "No\n"; + + // Free the memory allocated for the matrix + for (int i = 0; i < n; i++) + delete[] matrix[i]; + delete[] matrix; + + return 0; +} diff --git a/CSE-205 DSA/Unit-6/Lecture-36/Hemanth.cpp b/CSE-205 DSA/Unit-6/Lecture-36/Hemanth.cpp new file mode 100644 index 00000000..2b954532 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-36/Hemanth.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +#define MAX_N 100 + +int findProminentFigure(int acquaintances[MAX_N][MAX_N], int n) { + int *inDegree = new int[n](); // Initialize in-degrees to zero + + // Calculate in-degrees for each person + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + if (acquaintances[j][i] == 1) { + inDegree[i]++; + } + } + } + + // Check for a prominent figure + for (int i = 0; i < n; ++i) { + if (inDegree[i] == n - 1) { + delete[] inDegree; + return i; + } + } + + delete[] inDegree; + return -1; // No prominent figure found +} + +int main() { + int n; + cin >> n; + + int acquaintances[MAX_N][MAX_N]; + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + cin >> acquaintances[i][j]; + } + } + + int prominentFigureIndex = findProminentFigure(acquaintances, n); + if (prominentFigureIndex != -1) { + cout << "A prominent figure is present at index " << prominentFigureIndex << "."; + } else { + cout << "There is no prominent figure in the group."; + } + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-36/Megna.cpp b/CSE-205 DSA/Unit-6/Lecture-36/Megna.cpp new file mode 100644 index 00000000..f3a96350 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-36/Megna.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +#define MAX_TASKS 100 + +bool hasChainOfDependencies(int n, int dependencies[MAX_TASKS][MAX_TASKS], int task1, int task2) { + // Apply Warshall's Algorithm to find the transitive closure + for (int k = 0; k < n; ++k) { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + dependencies[i][j] = dependencies[i][j] || (dependencies[i][k] && dependencies[k][j]); + } + } + } + + // Check if there is a chain of dependencies from task1 to task2 + return dependencies[task1][task2]; +} + +int main() { + int n; + cin >> n; + + int dependencies[MAX_TASKS][MAX_TASKS] = {0}; + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + cin >> dependencies[i][j]; + } + } + + int task1, task2; + cin >> task1 >> task2; + + if (hasChainOfDependencies(n, dependencies, task1, task2)) { + cout << "There is a chain of dependencies."; + } else { + cout << "There is no chain of dependencies."; + } + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-36/Reachability.cpp b/CSE-205 DSA/Unit-6/Lecture-36/Reachability.cpp new file mode 100644 index 00000000..f4cf1898 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-36/Reachability.cpp @@ -0,0 +1,65 @@ +#include +using namespace std; + +// Function to check if there is a path from u to v +bool isReachable(int **graph, int u, int v, int n) { + return graph[u][v] == 1; +} + +// Function to compute reachability matrix using Warshall's Algorithm +void computeReachability(int **graph, int n) { + for (int k = 0; k < n; ++k) { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + graph[i][j] = graph[i][j] || (graph[i][k] && graph[k][j]); + } + } + } +} + +int main() { + int n, u, v; + cin >> n; + + // Dynamically allocate memory for the adjacency matrix + int **graph = new int *[n]; + for (int i = 0; i < n; ++i) { + graph[i] = new int[n]; + } + + // Input the adjacency matrix + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + cin >> graph[i][j]; + } + } + + // Compute reachability matrix + computeReachability(graph, n); + + // Output the reachability matrix + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + cout << graph[i][j] << " "; + } + cout << endl; + } + + // Input the pair of vertices + cin >> u >> v; + + // Check if there is a path from u to v + if (isReachable(graph, u, v, n)) { + cout << "There is a path from vertex " << u << " to vertex " << v << "." << endl; + } else { + cout << "There is no path from vertex " << u << " to vertex " << v << "." << endl; + } + + // Deallocate memory + for (int i = 0; i < n; ++i) { + delete[] graph[i]; + } + delete[] graph; + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-36/Rohit.cpp b/CSE-205 DSA/Unit-6/Lecture-36/Rohit.cpp new file mode 100644 index 00000000..89ed29d6 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-36/Rohit.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; + +#define MAX_FLIGHTS 100 + +bool canCreateFlightSchedule(int n, int constraints[][2]) { + // Create an adjacency matrix to represent the flight constraints + bool adjMatrix[MAX_FLIGHTS][MAX_FLIGHTS] = {false}; + + // Fill the adjacency matrix based on constraints + for (int i = 0; i < n; ++i) { + adjMatrix[constraints[i][1]][constraints[i][0]] = true; + } + + // Use Warshall's Algorithm to detect cycles + for (int k = 0; k < n; ++k) { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + adjMatrix[i][j] = adjMatrix[i][j] || (adjMatrix[i][k] && adjMatrix[k][j]); + } + } + } + + // Check for cycles + for (int i = 0; i < n; ++i) { + if (adjMatrix[i][i]) { + return false; // Cycle detected, cannot create flight schedule + } + } + + return true; // No cycle detected, can create flight schedule +} + +int main() { + int n; + cin >> n; + + int constraints[MAX_FLIGHTS][2]; + for (int i = 0; i < n; ++i) { + cin >> constraints[i][0] >> constraints[i][1]; + } + + if (canCreateFlightSchedule(n, constraints)) { + cout << "Yes"; + } else { + cout << "No"; + } + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-36/Sharon.cpp b/CSE-205 DSA/Unit-6/Lecture-36/Sharon.cpp new file mode 100644 index 00000000..9006f189 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-36/Sharon.cpp @@ -0,0 +1,56 @@ +#include +#include + +using namespace std; + +void printReachabilityMatrix(vector>& reachabilityMatrix) { + int N = reachabilityMatrix.size(); + + // cout << "Reachability Matrix:\n"; + for (int i = 0; i < N; ++i) { + for (int j = 0; j < N; ++j) { + cout << reachabilityMatrix[i][j] << " "; + } + cout << endl; + } +} + +void warshallAlgorithm(vector>& graph, vector>& reachabilityMatrix) { + int N = graph.size(); + + // Initialize the reachability matrix with the given graph + reachabilityMatrix = graph; + + // Applying Warshall's Algorithm + for (int k = 0; k < N; ++k) { + for (int i = 0; i < N; ++i) { + for (int j = 0; j < N; ++j) { + reachabilityMatrix[i][j] = reachabilityMatrix[i][j] || (reachabilityMatrix[i][k] && reachabilityMatrix[k][j]); + } + } + } +} + +int main() { + int N; + // cout << "Enter the number of vertices (N): "; + cin >> N; + + // Input adjacency matrix + vector> graph(N, vector(N, 0)); + // cout << "Enter the adjacency matrix (0 or 1):\n"; + for (int i = 0; i < N; ++i) { + for (int j = 0; j < N; ++j) { + cin >> graph[i][j]; + } + } + + // Compute reachability matrix using Warshall's Algorithm + vector> reachabilityMatrix; + warshallAlgorithm(graph, reachabilityMatrix); + + // Print the reachability matrix + printReachabilityMatrix(reachabilityMatrix); + + return 0; +} diff --git a/CSE-205 DSA/Unit-6/Lecture-37/Anu.cpp b/CSE-205 DSA/Unit-6/Lecture-37/Anu.cpp new file mode 100644 index 00000000..b73e125b --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-37/Anu.cpp @@ -0,0 +1,55 @@ +// You are using GCC +#include +using namespace std; + +void floydWarshall(int n, int graph[][100]) { + int INF = 1e9; + + // Applying Floyd-Warshall algorithm + for (int k = 0; k < n; k++) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (graph[i][k] && graph[k][j]) + graph[i][j] = 1; + } + } + } + + // Checking if the graph is strongly connected + bool isStronglyConnected = true; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (i != j && !graph[i][j]) { + isStronglyConnected = false; + break; + } + } + if (!isStronglyConnected) { + break; + } + } + + // Output + if (isStronglyConnected) { + cout << "The graph is strongly connected"; + } else { + cout << "The graph is not strongly connected"; + } +} + +int main() { + int n; + cin >> n; + int graph[100][100]; + + // Taking input for the adjacency matrix + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + cin >> graph[i][j]; + } + } + + floydWarshall(n, graph); + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-37/Latha.cpp b/CSE-205 DSA/Unit-6/Lecture-37/Latha.cpp new file mode 100644 index 00000000..4b08c34c --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-37/Latha.cpp @@ -0,0 +1,56 @@ +// You are using GCC +#include +using namespace std; + +#define V 4 +#define INF 99999 + +void printSolution(int dist[][V]); + +bool negCyclefloydWarshall(int graph[][V]) { + int dist[V][V]; + + // Initialize the distance matrix + for (int i = 0; i < V; i++) + for (int j = 0; j < V; j++) + dist[i][j] = graph[i][j]; + + // Applying Floyd-Warshall algorithm + for (int k = 0; k < V; k++) { + for (int i = 0; i < V; i++) { + for (int j = 0; j < V; j++) { + if (dist[i][k] != INF && dist[k][j] != INF && dist[i][k] + dist[k][j] < dist[i][j]) { + dist[i][j] = dist[i][k] + dist[k][j]; + } + } + } + } + + // Checking for negative cycles + for (int i = 0; i < V; i++) { + if (dist[i][i] < 0) { + return true; // Negative cycle detected + } + } + + return false; // No negative cycle found +} + +int main() { + int graph[V][V]; + + // Taking input for the weighted directed graph + for (int i = 0; i < V; i++) { + for (int j = 0; j < V; j++) { + cin >> graph[i][j]; + } + } + + if (negCyclefloydWarshall(graph)) { + cout << "Yes"; + } else { + cout << "No"; + } + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-37/Meena.cpp b/CSE-205 DSA/Unit-6/Lecture-37/Meena.cpp new file mode 100644 index 00000000..b296a0ed --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-37/Meena.cpp @@ -0,0 +1,50 @@ +// You are using GCC +#include +using namespace std; + +#define V 4 +#define INF 99999 + +void printSolution(int dist[][V]); + +bool negCyclefloydWarshall(int graph[][V]) { + int dist[V][V]; + + for (int i = 0; i < V; i++) + for (int j = 0; j < V; j++) + dist[i][j] = graph[i][j]; + + for (int k = 0; k < V; k++) { + for (int i = 0; i < V; i++) { + for (int j = 0; j < V; j++) { + if (dist[i][k] + dist[k][j] < dist[i][j]) { + dist[i][j] = dist[i][k] + dist[k][j]; + } + } + } + } + + for (int i = 0; i < V; i++) { + if (dist[i][i] < 0) + return true; + } + + return false; +} + +int main() { + int graph[V][V]; + + for (int i = 0; i < V; i++) { + for (int j = 0; j < V; j++) { + cin >> graph[i][j]; + } + } + + if (negCyclefloydWarshall(graph)) + cout << "Yes"; + else + cout << "No"; + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-37/Preethi.cpp b/CSE-205 DSA/Unit-6/Lecture-37/Preethi.cpp new file mode 100644 index 00000000..9ba48a5f --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-37/Preethi.cpp @@ -0,0 +1,54 @@ +#include +#include +using namespace std; + +#define INF 1000000000 +#define MAXN 100 + +int main() { + int n, m; + cin >> n >> m; + + int graph[MAXN][MAXN]; + + // Initialize the graph with INF distances + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (i == j) { + graph[i][j] = 0; + } else { + graph[i][j] = INF; + } + } + } + + // Input the directed edges with capacities + for (int i = 0; i < m; i++) { + int u, v, w; + cin >> u >> v >> w; + graph[u][v] = w; + } + + int source, sink; + cin >> source >> sink; + + // Floyd-Warshall Algorithm + for (int k = 0; k < n; k++) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (graph[i][k] != INF && graph[k][j] != INF && graph[i][k] + graph[k][j] < graph[i][j]) { + graph[i][j] = graph[i][k] + graph[k][j]; + } + } + } + } + + // Check if the sink node is unreachable from the source node + if (graph[source][sink] == INF) { + cout << "Nodes are unreachable. Maximum flow is 0." << endl; + } else { + cout << "Maximum flow from node " << source << " to " << sink << " is: " << graph[source][sink] << endl; + } + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-37/Reema.cpp b/CSE-205 DSA/Unit-6/Lecture-37/Reema.cpp new file mode 100644 index 00000000..7572e14e --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-37/Reema.cpp @@ -0,0 +1,71 @@ +// ONly 3 test case running + +#include +#include + +using namespace std; + +const int INF = 1e7; + +void printPath(vector>& next, int start, int dest) { + cout << "Shortest path from Earth to planet " << dest << ": "; + if (next[start][dest] == -1) { + cout << start ; + if(dest>0){ + cout<< " -> " << dest; + } + } else { + cout << start; + int current = start; + while (current != dest) { + cout << " -> " << next[current][dest]; + current = next[current][dest]; + } + cout << " -> " << dest; + } + cout << endl; +} + +void floydWarshall(vector>& graph, int V, int dest) { + vector> dist = graph; + vector> next(V, vector(V, -1)); + + // Applying Floyd-Warshall Algorithm + for (int k = 0; k < V; ++k) { + for (int i = 0; i < V; ++i) { + for (int j = 0; j < V; ++j) { + if (dist[i][k] != INF && dist[k][j] != INF && dist[i][j] > dist[i][k] + dist[k][j]) { + dist[i][j] = dist[i][k] + dist[k][j]; + next[i][j] = k; + } + } + } + } + + // Print the shortest path from Earth to the destination planet + printPath(next, 0, dest); +} + +int main() { + int V; + // cout << "Enter the number of planets in the galaxy (V): "; + cin >> V; + + // Input distance matrix + vector> graph(V, vector(V)); + // cout << "Enter the distance matrix (0 to 10^7):\n"; + for (int i = 0; i < V; ++i) { + for (int j = 0; j < V; ++j) { + cin >> graph[i][j]; + } + } + + int dest; + // cout << "Enter the index of the destination planet: "; + cin >> dest; + + // Apply Floyd-Warshall algorithm and print the result + floydWarshall(graph, V, dest); + + return 0; +} diff --git a/CSE-205 DSA/Unit-6/Lecture-37/Varsha-2.cpp b/CSE-205 DSA/Unit-6/Lecture-37/Varsha-2.cpp new file mode 100644 index 00000000..c990fde2 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-37/Varsha-2.cpp @@ -0,0 +1,57 @@ +#include +#include + +using namespace std; + +const int INF = 10000000; + +void printPath(vector>& next, int i, int j) { + if (next[i][j] == -1) { + cout << i << " -> " << j; + } else { + printPath(next, i, next[i][j]); + cout << " -> " << j; + } +} + +void floydWarshall(vector>& graph, int V) { + vector> dist = graph; + vector> next(V, vector(V, -1)); + + // Applying Floyd-Warshall Algorithm + for (int k = 0; k < V; ++k) { + for (int i = 0; i < V; ++i) { + for (int j = 0; j < V; ++j) { + if (dist[i][k] != INF && dist[k][j] != INF && dist[i][j] > dist[i][k] + dist[k][j]) { + dist[i][j] = dist[i][k] + dist[k][j]; + next[i][j] = k; + } + } + } + } + + // Print the shortest path from the top-left to the bottom-right + // cout << "Shortest Path from 0 to " << V - 1 << " : "; + printPath(next, 0, V - 1); + cout << endl; +} + +int main() { + int V; + // cout << "Enter the number of vertices (V): "; + cin >> V; + + // Input adjacency matrix + vector> graph(V, vector(V)); + // cout << "Enter the adjacency matrix (0 to 10000000):\n"; + for (int i = 0; i < V; ++i) { + for (int j = 0; j < V; ++j) { + cin >> graph[i][j]; + } + } + + // Apply Floyd-Warshall algorithm and print the result + floydWarshall(graph, V); + + return 0; +} diff --git a/CSE-205 DSA/Unit-6/Lecture-37/Varsha.cpp b/CSE-205 DSA/Unit-6/Lecture-37/Varsha.cpp new file mode 100644 index 00000000..b0b3d70e --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-37/Varsha.cpp @@ -0,0 +1,73 @@ +#include +using namespace std; + +#define INF 1e7 +#define MAXN 100 + +int dis[MAXN][MAXN]; +int Next[MAXN][MAXN]; + +void initialise(int V, int graph[MAXN][MAXN]) { + for (int i = 0; i < V; ++i) { + for (int j = 0; j < V; ++j) { + dis[i][j] = graph[i][j]; + if (graph[i][j] == INF) + Next[i][j] = -1; + else + Next[i][j] = j; + } + } +} + +void floydWarshall(int V) { + for (int k = 0; k < V; k++) { + for (int i = 0; i < V; i++) { + for (int j = 0; j < V; j++) { + if (dis[i][k] + dis[k][j] < dis[i][j]) { + dis[i][j] = dis[i][k] + dis[k][j]; + Next[i][j] = Next[i][k]; + } + } + } + } +} + +void printPath(int source, int destination) { + if (dis[source][destination] == INF) { + cout << "No path exists from source to destination." << endl; + return; + } + + cout << "Shortest path from Source to destination " << destination << ": "; + int currentNode = source; + + while (currentNode != destination) { + cout << currentNode << " -> "; + currentNode = Next[currentNode][destination]; + } + + cout << destination << endl; +} + +int main() { + int V; + cin >> V; + + int graph[MAXN][MAXN]; + + for (int i = 0; i < V; i++) { + for (int j = 0; j < V; j++) { + cin >> graph[i][j]; + } + } + + initialise(V, graph); + floydWarshall(V); + + int destination; + cin >> destination; + + printPath(0, destination); + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-37/kamal.cpp b/CSE-205 DSA/Unit-6/Lecture-37/kamal.cpp new file mode 100644 index 00000000..4cfc95c1 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-37/kamal.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; + +#define INF 10000000 +#define MAXN 100 + +int dis[MAXN][MAXN]; +int Next[MAXN][MAXN]; + +void initialise(int V, int graph[MAXN][MAXN]) { + for (int i = 0; i < V; i++) { + for (int j = 0; j < V; j++) { + dis[i][j] = graph[i][j]; + if (graph[i][j] == INF) + Next[i][j] = -1; + else + Next[i][j] = j; + } + } +} + +void floydWarshall(int V) { + for (int k = 0; k < V; k++) { + for (int i = 0; i < V; i++) { + for (int j = 0; j < V; j++) { + if (dis[i][k] == INF || dis[k][j] == INF) + continue; + + if (dis[i][j] > dis[i][k] + dis[k][j]) { + dis[i][j] = dis[i][k] + dis[k][j]; + Next[i][j] = Next[i][k]; + } + } + } + } +} + +void printPath(int path[], int n) { + for (int i = 0; i < n - 1; i++) + cout << path[i] << " -> "; + cout << path[n - 1] << endl; +} + +int main() { + int V; + cin >> V; + + int graph[MAXN][MAXN]; + for (int i = 0; i < V; i++) { + for (int j = 0; j < V; j++) { + cin >> graph[i][j]; + } + } + + initialise(V, graph); + floydWarshall(V); + + int u = 0; // Top-left corner + int v = V - 1; // Bottom-right corner + + int path[MAXN]; + path[0] = u; + int index = 1; + while (u != v) { + u = Next[u][v]; + path[index++] = u; + } + + printPath(path, index); + + return 0; +} diff --git a/CSE-205 DSA/Unit-6/Lecture-39/Deva.cpp b/CSE-205 DSA/Unit-6/Lecture-39/Deva.cpp new file mode 100644 index 00000000..b9985acd --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-39/Deva.cpp @@ -0,0 +1,39 @@ +// You are using GCC +#include +using namespace std; + +int SIZE = 100; + +int calHash(int key, int size) { + return key % size; +} + +int main() { + int size, numKeys; + cin >> size; + cin >> numKeys; + + int keys[numKeys]; + int hashTable[SIZE]; + + for (int i = 0; i < SIZE; i++) { + hashTable[i] = -1; + } + + for (int i = 0; i < numKeys; ++i) { + cin >> keys[i]; + } + + for (int i = 0; i < numKeys; ++i) { + int index = calHash(keys[i], size); + + while (hashTable[index] != -1) { + index = (index + 1) % size; // Linear probing + } + + hashTable[index] = keys[i]; + cout << index << " "; + } + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-39/Emma.cpp b/CSE-205 DSA/Unit-6/Lecture-39/Emma.cpp new file mode 100644 index 00000000..4ff3282a --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-39/Emma.cpp @@ -0,0 +1,31 @@ +// You are using GCC +#include +#include +using namespace std; + +#define HASH_TABLE_SIZE 1000 + +unsigned int customHash(string key) { + //Type your code here + unsigned int hashValue=0; + for(char ch:key){ + hashValue=(hashValue*37)+static_cast (ch); + } + return hashValue % HASH_TABLE_SIZE; +} + +int main() { + string key; + while (true) { + cin >> key; + + if (key == "exit") { + break; + } + + unsigned int hashValue = customHash(key); + cout << hashValue << endl; + } + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-39/Jessica.cpp b/CSE-205 DSA/Unit-6/Lecture-39/Jessica.cpp new file mode 100644 index 00000000..07780db1 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-39/Jessica.cpp @@ -0,0 +1,27 @@ +// You are using GCC +#include +#include +using namespace std; + +const int HASH_TABLE_SIZE = 10000; + +unsigned int customHash(string& key) { + unsigned int hashValue = 0; + for (char ch : key) { + hashValue = (hashValue * 29) + static_cast(ch); + } + return hashValue % HASH_TABLE_SIZE; +} + +int main() { + string key; + while (true) { + cin >> key; + if (key == "exit") { + break; + } + unsigned int hashValue = customHash(key); + cout << hashValue << endl; + } + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-39/John.cpp b/CSE-205 DSA/Unit-6/Lecture-39/John.cpp new file mode 100644 index 00000000..16c6f83d --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-39/John.cpp @@ -0,0 +1,23 @@ +#include +#include +using namespace std; + +unsigned int hashCalc(string key, int tableSize) { + unsigned int hashVal = 0; + for (char ch : key) { + hashVal = 37 * hashVal + ch; + } + return hashVal % tableSize; +} + +int main() { + string text; + int tSize; + + cin >> text; + cin >> tSize; + + cout << hashCalc(text, tSize); + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-39/Madhev.cpp b/CSE-205 DSA/Unit-6/Lecture-39/Madhev.cpp new file mode 100644 index 00000000..e3f43934 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-39/Madhev.cpp @@ -0,0 +1,52 @@ +// You are using GCC +#include +using namespace std; +#define SIZE 100 + +int calHash(int key, int size) { + return key % size; +} + +int main() { + int size, numKeys; + cin >> size; + cin >> numKeys; + int keys[numKeys]; + int hashTable[SIZE]; + + for (int i = 0; i < SIZE; i++) { + hashTable[i] = -1; + } + + for (int i = 0; i < numKeys; ++i) { + cin >> keys[i]; + } + + int minIndices[numKeys]; + + for (int i = 0; i < numKeys; ++i) { + int index = calHash(keys[i], size); + + while (hashTable[index] != -1) { + index = (index + 1) % size; + } + + hashTable[index] = keys[i]; + minIndices[i] = index; + } + + for (int i = 0; i < numKeys; ++i) { + for (int j = i + 1; j < numKeys; ++j) { + if (minIndices[i] > minIndices[j]) { + swap(minIndices[i], minIndices[j]); + swap(keys[i], keys[j]); + } + } + } + + for (int i = 0; i < numKeys; ++i) { + cout << "index: " << minIndices[i] << ", value: " << keys[i] << endl; + } + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-39/Ragul.cpp b/CSE-205 DSA/Unit-6/Lecture-39/Ragul.cpp new file mode 100644 index 00000000..513da212 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-39/Ragul.cpp @@ -0,0 +1,44 @@ +// You are using GCC +#include +using namespace std; + +int SIZE = 100; + +int calHash(int key, int size) { + return key % size; +} + +int main() { + int size, numKeys; + cin >> size; + cin >> numKeys; + + int keys[numKeys]; + int hashTable[SIZE]; + + for (int i = 0; i < SIZE; i++) { + hashTable[i] = -1; + } + + for (int i = 0; i < numKeys; ++i) { + cin >> keys[i]; + } + + for (int i = 0; i < numKeys; ++i) { + int hashIndex = calHash(keys[i], size); + + while (hashTable[hashIndex] != -1) { + hashIndex = (hashIndex + 1) % size; + } + + hashTable[hashIndex] = i; + + if (i < numKeys - 1) { + cout << hashIndex << " "; + } else { + cout << hashIndex << endl; + } + } + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-39/Ram.cpp b/CSE-205 DSA/Unit-6/Lecture-39/Ram.cpp new file mode 100644 index 00000000..7e6ff1b1 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-39/Ram.cpp @@ -0,0 +1,25 @@ +#include +#include +using namespace std; + +unsigned int hashCalc(string key, int tableSize) { + unsigned int hashVal = 0; + + for (int i = 0; i < key.length(); i++) { + hashVal = (hashVal * 37) + static_cast(key[i]); + } + + return hashVal % tableSize; +} + +int main() { + string text; + int tSize; + + cin >> text; + cin >> tSize; + + cout << hashCalc(text, tSize) << endl; + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-39/Sindhu.cpp b/CSE-205 DSA/Unit-6/Lecture-39/Sindhu.cpp new file mode 100644 index 00000000..dadbe00b --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-39/Sindhu.cpp @@ -0,0 +1,50 @@ +#include +#include +#include + +using namespace std; + +vector> linear_probing_hash(const vector& keys, int size) { + vector> hash_table(size, {-1, -1}); + + for (int key : keys) { + int index = key % size; + while (hash_table[index].first != -1) { + index = (index + 1) % size; + } + hash_table[index] = {key, index}; + } + + return hash_table; +} + +void display_hash_table(const vector>& hash_table) { + vector> sorted_keys = hash_table; + sort(sorted_keys.begin(), sorted_keys.end(), [](const auto& a, const auto& b) { + return a.second < b.second; + }); + + for (const auto& entry : sorted_keys) { + if (entry.first != -1) { + cout << "index: " << entry.second << ", value: " << entry.first << endl; + } + } +} + +int main() { + // Input + int size, n; + cin >> size >> n; + vector keys(n); + for (int i = 0; i < n; ++i) { + cin >> keys[i]; + } + + // Hashing with linear probing + vector> hash_table = linear_probing_hash(keys, size); + + // Displaying output + display_hash_table(hash_table); + + return 0; +} diff --git a/CSE-205 DSA/Unit-6/Lecture-40/Aaron.cpp b/CSE-205 DSA/Unit-6/Lecture-40/Aaron.cpp new file mode 100644 index 00000000..f01b43e9 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-40/Aaron.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +int divisionMethod(int key, int tableSize) +{ + return key % tableSize; +} + +int main() { + int tableSize = 10; + int numKeys; + cin >> numKeys; + int keys[numKeys]; + for (int i = 0; i < numKeys; i++) { + cin >> keys[i]; + } + int hashTable[tableSize]; + for (int i = 0; i < tableSize; i++) { + hashTable[i] = -1; + } + for (int i = 0; i < numKeys; i++) { + int key = keys[i]; + int hashIndex = divisionMethod(key, tableSize); + while (hashIndex < tableSize) { + if (hashTable[hashIndex] == -1) { + hashTable[hashIndex] = key; + break; + } else { + hashIndex = (hashIndex + 1) % tableSize; + } + } + } + for (int i = 0; i < tableSize; i++) { + if (hashTable[i] != -1) { + cout << "Table number " << i << ": Customer ID " << hashTable[i] << endl; + } + } + return 0; +} diff --git a/CSE-205 DSA/Unit-6/Lecture-40/Akil.cpp b/CSE-205 DSA/Unit-6/Lecture-40/Akil.cpp new file mode 100644 index 00000000..4a8df809 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-40/Akil.cpp @@ -0,0 +1,53 @@ +#include +#include + +using namespace std; + +int mid_square_hash(int key, int table_size) { + int square = key * key; + int middle_digits = (square / 10) % 100; + return middle_digits % table_size; +} + +vector linear_probing_hash(const vector& keys, int table_size) { + vector hash_table(table_size, -1); + + for (int key : keys) { + int index = mid_square_hash(key, table_size); + + while (hash_table[index] != -1) { + index = (index + 1) % table_size; + } + + hash_table[index] = key; + } + + return hash_table; +} + +void display_hash_table(const vector& hash_table) { + for (int index = 0; index < hash_table.size(); ++index) { + if (hash_table[index] != -1) { + cout << "Index " << index << ": Key " << hash_table[index] << endl; + } + } +} + +int main() { + // Input + int n; + cin >> n; + vector keys(n); + for (int i = 0; i < n; ++i) { + cin >> keys[i]; + } + const int TABLE_SIZE = 100; + + // Hashing with mid-square and linear probing + vector hash_table = linear_probing_hash(keys, TABLE_SIZE); + + // Displaying output + display_hash_table(hash_table); + + return 0; +} diff --git a/CSE-205 DSA/Unit-6/Lecture-40/Chandra.cpp b/CSE-205 DSA/Unit-6/Lecture-40/Chandra.cpp new file mode 100644 index 00000000..db658221 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-40/Chandra.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; + +int main() { + int tableSize = 10; + int numKeys, i; + cin >> numKeys; + int keys[numKeys]; + + for (i = 0; i < numKeys; i++) { + cin >> keys[i]; + } + + int hashTable[tableSize]; + + for (i = 0; i < tableSize; i++) { + hashTable[i] = -1; + } + + for (i = 0; i < numKeys; i++) { + int key = keys[i]; + int hashIndex = key % tableSize; + int j = 0; + + while (hashTable[hashIndex] != -1) { + j++; + hashIndex = (hashIndex + j * j) % tableSize; + } + + hashTable[hashIndex] = key; + } + + int min = hashTable[0]; + int in = 0; + + for (i = 1; i < tableSize; i++) { + if (hashTable[i] != -1) { + if (min < hashTable[i]) { + min = hashTable[i]; + in = i; + } + } + } + + cout << "Fastest bike speed: " << min << ", Index: " << in ; + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-40/Goa_trip.cpp b/CSE-205 DSA/Unit-6/Lecture-40/Goa_trip.cpp new file mode 100644 index 00000000..313aac4c --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-40/Goa_trip.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +int main() { + int tableSize = 10; + int numKeys; + cin >> numKeys; + int keys[numKeys]; + for (int i = 0; i < numKeys; i++) { + cin >> keys[i]; + } + int hashTable[tableSize]; + for (int i = 0; i < tableSize; i++) { + hashTable[i] = -1; + } + for (int i = 0; i < numKeys; i++) { + int key = keys[i]; + int hashIndex = key % tableSize; + int j = 0; + while (hashTable[hashIndex] != -1) { + j++; + hashIndex = (hashIndex + j * j) % tableSize; + } + hashTable[hashIndex] = key; + } + int f = 0; + for (int i = 0; i < tableSize; i++) { + if (hashTable[i] != -1 && hashTable[i] >= 60) { + cout << "Age: " << hashTable[i] <<", Seat: " << i <<"\n"; + f = 1; + } + } + if (f == 0) { + cout << "No senior citizens in the family"; + } + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-40/Harish.cpp b/CSE-205 DSA/Unit-6/Lecture-40/Harish.cpp new file mode 100644 index 00000000..20714d90 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-40/Harish.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; + +int myHash(int key, int tableSize) { + return key % tableSize; +} + +int main() { + int tableSize = 10; + int numKeys; + cin >> numKeys; + + int keys[numKeys]; + for (int i = 0; i < numKeys; i++) { + cin >> keys[i]; + } + + int hashTable[tableSize]; + for (int i = 0; i < tableSize; i++) { + hashTable[i] = -1; + } + + for (int i = 0; i < numKeys; i++) { + int key = keys[i]; + int hashIndex = myHash(key, tableSize); + + while (hashTable[hashIndex] != -1) { + hashIndex = (hashIndex + 1) % tableSize; + } + + hashTable[hashIndex] = key; + } + + int mid = (numKeys / 2) + 1; + int middleIndex, middleValue; + int c = 0; + + for (int i = 0; i < tableSize; i++) { + if (hashTable[i] != -1) { + c++; + if (c == mid) { + middleIndex = i; + middleValue = hashTable[i]; + } + cout << "Index " << i << ": " << hashTable[i] << endl; + } + } + + cout << "Middle Index: " << middleIndex << ", Middle Value: " << middleValue; + + return 0; +} diff --git a/CSE-205 DSA/Unit-6/Lecture-40/High-class_Hotel.cpp b/CSE-205 DSA/Unit-6/Lecture-40/High-class_Hotel.cpp new file mode 100644 index 00000000..35d3d901 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-40/High-class_Hotel.cpp @@ -0,0 +1,46 @@ +#include +#include +using namespace std; + +bool isPrime(int n) { + if (n <= 1) return false; + if (n <= 3) return true; + if (n % 2 == 0 || n % 3 == 0) return false; + for (int i = 5; i * i <= n; i += 6) { + if (n % i == 0 || n % (i + 2) == 0) return false; + } + return true; +} + +int main() { + int tableSize = 10; + int numDishes; + cin >> numDishes; + vector prices(tableSize, -1); + + for (int i = 0; i < numDishes; i++) { + int price; + cin >> price; + int index = price % tableSize; + int step = 1; + while (prices[index] != -1) { + index = (index + step * step) % tableSize; + step++; + } + prices[index] = price; + } + + bool found = false; + for (int i = 0; i < tableSize; i++) { + if (isPrime(i) && prices[i] != -1) { + found = true; + cout << "The price of the dish in prime position: " << i << " is Rs." << prices[i] << endl; + } + } + + if (!found) { + cout << "No dish is placed in prime position" << endl; + } + + return 0; +} diff --git a/CSE-205 DSA/Unit-6/Lecture-40/Ishu.cpp b/CSE-205 DSA/Unit-6/Lecture-40/Ishu.cpp new file mode 100644 index 00000000..dc3f82dd --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-40/Ishu.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +int divisionMethod(int key, int tableSize) +{ + return key % tableSize; +} + +int main() { + int tableSize = 10; + int numKeys, k; + cin >> numKeys; + + int keys[numKeys]; + for (int i = 0; i < numKeys; i++) { + cin >> keys[i]; + } + + cin >> k; + + int hashTable[tableSize]; + for (int i = 0; i < tableSize; i++) { + hashTable[i] = -1; + } + + for (int i = 0; i < numKeys; i++) { + int key = keys[i]; + int hashIndex = divisionMethod(key, tableSize); + + while (hashTable[hashIndex] != -1) { + hashIndex = (hashIndex + 1) % tableSize; + } + + hashTable[hashIndex] = key; + } + + int c = 0; + int kIndex = -1; + int kValue = -1; + + for (int i = 0; i < tableSize; i++) { + if (hashTable[i] != -1) { + if (c == k - 1) { + kIndex = i; + kValue = hashTable[i]; + } + + cout << "Index " << i << ": Value " << hashTable[i] << endl; + c++; + } + } + + if (kIndex != -1) { + cout << "Kth Position Index: " << kIndex << ", Kth Position Value: " << kValue; + } else { + cout << "Kth position not found"; + } + + return 0; +} diff --git a/CSE-205 DSA/Unit-6/Lecture-40/Josh.c b/CSE-205 DSA/Unit-6/Lecture-40/Josh.c new file mode 100644 index 00000000..038cd686 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-40/Josh.c @@ -0,0 +1,57 @@ +// You are using GCC +#include + +int midSquareHash(int key, int tableSize) +{ + int squared = key * key; + int middleDigit = (squared / 10) % 100; + return middleDigit % tableSize; +} + +int linearProbe(int hashTable[], int tableSize, int index) +{ + while (hashTable[index] != -1) + { + index = (index + 1) % tableSize; + } + return index; +} + + +int main() { + int tableSize = 100; + int numKeys; + scanf("%d", &numKeys); + int keys[numKeys]; + for (int i = 0; i < numKeys; i++) { + scanf("%d", &keys[i]); + } + int hashTable[tableSize]; + for (int i = 0; i < tableSize; i++) { + hashTable[i] = -1; + } + for (int i = 0; i < numKeys; i++) { + int key = keys[i]; + int hashIndex = midSquareHash(key, tableSize); + + hashIndex = linearProbe(hashTable, tableSize, hashIndex); + + hashTable[hashIndex] = key; + } + + int sum = 0; + for (int i = 0; i < tableSize; i++) { + if (hashTable[i] != -1) { + printf("Index %d: Key %d\n", i, hashTable[i]); + } + } + for (int i = 0; i < tableSize; i++) { + if (hashTable[i] != -1) { + if (i % 2 == 0) { + sum += hashTable[i]; + } + } + } + printf("Sum of even indexed soup price: %d", sum); +} + diff --git a/CSE-205 DSA/Unit-6/Lecture-40/Kishore.cpp b/CSE-205 DSA/Unit-6/Lecture-40/Kishore.cpp new file mode 100644 index 00000000..c96eb5ca --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-40/Kishore.cpp @@ -0,0 +1,57 @@ +#include +using namespace std; + +int midSquareHash(int key, int tableSize) +{ + int squared = key * key; + int middleDigit = (squared / 10) % 10; + int hashIndex = middleDigit % tableSize; + return hashIndex; +} + +int main() { + int tableSize = 100, in, min; + int numKeys; + cin >> numKeys; + int keys[numKeys]; + for (int i = 0; i < numKeys; i++) { + cin >> keys[i]; + } + int hashTable[tableSize]; + for (int i = 0; i < tableSize; i++) { + hashTable[i] = -1; + } + for (int i = 0; i < numKeys; i++) { + int key = keys[i]; + int hashIndex = midSquareHash(key, tableSize); + + while (hashTable[hashIndex] != -1) { + hashIndex = (hashIndex + 1) % tableSize; + } + hashTable[hashIndex] = key; + } + + for (int i = 0; i < tableSize; i++) { + if (hashTable[i] != -1) { + cout << "Index: " << i << ", Value: " << hashTable[i] << endl; + } + } + + int minElement = -1; + int minIndex = -1; + + for (int i = 0; i < tableSize; i++) { + if (hashTable[i] != -1) { + if (minElement == -1 || hashTable[i] < minElement) { + minElement = hashTable[i]; + minIndex = i; + } + } + } + + if (minIndex != -1) { + cout << "Lowest Fee: " << minElement << " Index " << minIndex; + } + + return 0; +} diff --git a/CSE-205 DSA/Unit-6/Lecture-40/Logistic_company.cpp b/CSE-205 DSA/Unit-6/Lecture-40/Logistic_company.cpp new file mode 100644 index 00000000..4c19bb07 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-40/Logistic_company.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; + +int main() { + int tableSize = 10; + int numKeys; + cin >> numKeys; + int keys[numKeys]; + + for (int i = 0; i < numKeys; i++) { + cin >> keys[i]; + } + + int hashTable[tableSize] = {0}; + + int maxCapacity = keys[0]; + int maxIndex = keys[0] % tableSize; // Initialize with the hash index of the first truck + + for (int i = 0; i < numKeys; i++) { + int key = keys[i]; + int hashIndex = key % tableSize; + int probeCount = 1; + + while (hashTable[hashIndex] != 0) { + hashIndex = (hashIndex + probeCount * probeCount) % tableSize; + probeCount++; + } + + hashTable[hashIndex] = key; + + if (keys[i] > maxCapacity) { + maxCapacity = keys[i]; + maxIndex = hashIndex; // Use the final hash index after insertion + } + } + + cout << "The lorry that has high load capacity is in index " << maxIndex << "."; + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-40/Reema.cpp b/CSE-205 DSA/Unit-6/Lecture-40/Reema.cpp new file mode 100644 index 00000000..1b0fb98a --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-40/Reema.cpp @@ -0,0 +1,45 @@ +// You are using GCC +#include +using namespace std; + +int midSquareHash(int key, int tableSize, int r) { + int squared = key * key; + int middleDigit = (squared / 10) % 10; + int hashIndex = (middleDigit + r) % tableSize; + return hashIndex; +} + +int main() { + int tableSize = 100; + int numKeys; + cin >> numKeys; + int keys[numKeys]; + + for (int i = 0; i < numKeys; i++) { + cin >> keys[i]; + } + + int hashTable[tableSize]; + for (int i = 0; i < tableSize; i++) { + hashTable[i] = -1; + } + + for (int i = 0; i < numKeys; i++) { + int key = keys[i]; + int r = 0; // Linear probing counter + + while (hashTable[midSquareHash(key, tableSize, r)] != -1) { + r++; + } + + hashTable[midSquareHash(key, tableSize, r)] = key; + } + + for (int i = 0; i < tableSize; i++) { + if (hashTable[i] != -1) { + cout << "Index " << i << ": Key " << hashTable[i] << endl; + } + } + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-40/Reema_2.cpp b/CSE-205 DSA/Unit-6/Lecture-40/Reema_2.cpp new file mode 100644 index 00000000..88dba9c9 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-40/Reema_2.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +int main() { + int tableSize = 10; + int numKeys; + cin >> numKeys; + + int keys[numKeys]; + for (int i = 0; i < numKeys; i++) { + cin >> keys[i]; + } + + int hashTable[tableSize]; + for (int i = 0; i < tableSize; i++) { + hashTable[i] = -1; + } + + for (int i = 0; i < numKeys; i++) { + int key = keys[i]; + int hashIndex = key % tableSize; + + while (hashTable[hashIndex] != -1) { + hashIndex = (hashIndex + 1) % tableSize; + } + + hashTable[hashIndex] = key; + } + + for (int i = 0; i < tableSize; i++) { + if (hashTable[i] != -1) { + cout << "Seat Number " << i << ": Ticket ID " << hashTable[i] << endl; + } + } + + return 0; +} diff --git a/CSE-205 DSA/Unit-6/Lecture-40/Rishi.cpp b/CSE-205 DSA/Unit-6/Lecture-40/Rishi.cpp new file mode 100644 index 00000000..13b7c641 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-40/Rishi.cpp @@ -0,0 +1,53 @@ +#include + +int main() { + int tableSize = 10; + int numKeys; + scanf("%d", &numKeys); + + int keys[numKeys]; + for (int i = 0; i < numKeys; i++) { + scanf("%d", &keys[i]); + } + + int hashTable[tableSize]; + for (int i = 0; i < tableSize; i++) { + hashTable[i] = -1; + } + + for (int i = 0; i < numKeys; i++) { + int index = keys[i] % tableSize; + while (hashTable[index] != -1) { + index = (index + 1) % tableSize; // Linear probing + } + hashTable[index] = keys[i]; + } + + int f = -1, l = -1; + int f_index = -1, l_index = -1; + + for (int i = 0; i < tableSize; i++) { + if (hashTable[i] != -1) { + if (f == -1) { + f = l = hashTable[i]; + f_index = l_index = i; + } else { + if (i < f_index) { + f = hashTable[i]; + f_index = i; + } + if (i > l_index) { + l = hashTable[i]; + l_index = i; + } + } + } + } + + if (f != -1 && l != -1) { + printf("First index: %d, Value: %d\n", f_index, f); + printf("Last index: %d, Value: %d\n", l_index, l); + } + + return 0; +} diff --git a/CSE-205 DSA/Unit-6/Lecture-40/Rithika.cpp b/CSE-205 DSA/Unit-6/Lecture-40/Rithika.cpp new file mode 100644 index 00000000..a92b69ab --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-40/Rithika.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; + +int midSquareHash(int key, int tableSize) { + int squared = key * key; + int middleDigit = (squared / 10) % 100; // Extracting the last two digits + int hashIndex = middleDigit % tableSize; + return hashIndex; +} + +int main() { + int tableSize = 100; + int numKeys; + cin >> numKeys; + int keys[numKeys]; + + for (int i = 0; i < numKeys; i++) { + cin >> keys[i]; + } + + int hashTable[tableSize]; + for (int i = 0; i < tableSize; i++) { + hashTable[i] = -1; + } + + for (int i = 0; i < numKeys; i++) { + int key = keys[i]; + int r = 0; // Linear probing counter + int hashIndex = midSquareHash(key, tableSize); + + // Find the next available slot using linear probing + while (hashTable[hashIndex] != -1) { + r++; + hashIndex = (midSquareHash(key, tableSize) + r) % tableSize; + } + + // Store the key in the hash table + hashTable[hashIndex] = key; + } + + // Find the key with the highest value and its index + int maxWeight = hashTable[0]; + int maxIndex = 0; + + for (int i = 1; i < tableSize; i++) { + if (hashTable[i] != -1 && hashTable[i] > maxWeight) { + maxWeight = hashTable[i]; + maxIndex = i; + } + } + + // Print the result + cout << "Weight: " << maxWeight << ", Index: " << maxIndex << endl; + + return 0; +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-40/Roshene.cpp b/CSE-205 DSA/Unit-6/Lecture-40/Roshene.cpp new file mode 100644 index 00000000..95b99f6a --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-40/Roshene.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; + +int main() { + int tableSize = 10; + int numMarks; + cin >> numMarks; + int marks[numMarks]; + + for (int i = 0; i < numMarks; i++) { + cin >> marks[i]; + } + + int hashTable[tableSize]; + for (int i = 0; i < tableSize; i++) { + hashTable[i] = -1; + } + + int bestMark = -1; + int bestIndex = -1; + + for (int i = 0; i < numMarks; i++) { + int mark = marks[i]; + int index = mark % tableSize; + int step = 1; + + while (hashTable[index] != -1) { + index = (index + step * step) % tableSize; + step++; + } + + hashTable[index] = mark; + + if (bestMark == -1 || mark > bestMark) { + bestMark = mark; + bestIndex = index; + } + } + + cout << "Best Mark: " << bestMark << ", Index: " << bestIndex << endl; + + return 0; +} diff --git a/CSE-205 DSA/Unit-6/Lecture-40/Sharon.cpp b/CSE-205 DSA/Unit-6/Lecture-40/Sharon.cpp new file mode 100644 index 00000000..dd413a9e --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-40/Sharon.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +int main() { + + int tableSize = 10; + int numKeys,i; + cin >> numKeys; + int keys[numKeys]; + for ( i = 0; i < numKeys; i++) { + cin >> keys[i]; + } + int hashTable[tableSize]; + for ( i = 0; i < tableSize; i++) { + hashTable[i] = -1; + } + for ( i = 0; i < numKeys; i++) { + int key = keys[i]; + int hashIndex = key % tableSize; + int j = 0; + while (hashTable[hashIndex] != -1) + { + j++; + hashIndex = (hashIndex + j * j) % tableSize; + } + hashTable[hashIndex] = key; + } + for ( i = 0; i < tableSize; i++) { + if(hashTable[i]!=-1) + cout << hashTable[i] << " "; + } +} \ No newline at end of file diff --git a/CSE-205 DSA/Unit-6/Lecture-40/Teju.cpp b/CSE-205 DSA/Unit-6/Lecture-40/Teju.cpp new file mode 100644 index 00000000..6de81171 --- /dev/null +++ b/CSE-205 DSA/Unit-6/Lecture-40/Teju.cpp @@ -0,0 +1,42 @@ +// You are using GCC +#include +using namespace std; + +int divisionMethod(int key, int tableSize) { + return key % tableSize; +} + +int main() { + int tableSize = 10; + int numKeys; + cin >> numKeys; + int keys[numKeys]; + + for (int i = 0; i < numKeys; i++) { + cin >> keys[i]; + } + + int hashTable[tableSize]; + for (int i = 0; i < tableSize; i++) { + hashTable[i] = -1; + } + + for (int i = 0; i < numKeys; i++) { + int key = keys[i]; + int hashIndex = divisionMethod(key, tableSize); + + while (hashTable[hashIndex] != -1) { + hashIndex = (hashIndex + 1) % tableSize; // Linear probing + } + + hashTable[hashIndex] = key; + } + + for (int i = 0; i < tableSize; i++) { + if (hashTable[i] != -1 && i % 2 == 0) { + cout << "Index: " << i << " Flight ID: " << hashTable[i] << endl; + } + } + + return 0; +} \ No newline at end of file