Skip to content

Commit

Permalink
-added DSA Unit-6
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-codes18 committed Nov 12, 2023
1 parent 521c31a commit 41762d1
Show file tree
Hide file tree
Showing 48 changed files with 2,412 additions and 0 deletions.
38 changes: 38 additions & 0 deletions CSE-205 DSA/Unit-6/Lecture-35/Lithish.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
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;
}
54 changes: 54 additions & 0 deletions CSE-205 DSA/Unit-6/Lecture-35/Raja.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// You are using GCC
#include <iostream>
#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;
}
49 changes: 49 additions & 0 deletions CSE-205 DSA/Unit-6/Lecture-35/Raju.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
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;
}
58 changes: 58 additions & 0 deletions CSE-205 DSA/Unit-6/Lecture-35/Rithu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <iostream>

#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;
}
87 changes: 87 additions & 0 deletions CSE-205 DSA/Unit-6/Lecture-35/Rohan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <iostream>
#include <queue>
#include <climits>
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<int> 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;
}
51 changes: 51 additions & 0 deletions CSE-205 DSA/Unit-6/Lecture-35/Sanju.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>
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;
}
45 changes: 45 additions & 0 deletions CSE-205 DSA/Unit-6/Lecture-35/Siddhu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// You are using GCC
#include <iostream>
#include <queue>
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;
}
Loading

0 comments on commit 41762d1

Please sign in to comment.