Skip to content

Commit 41762d1

Browse files
committed
-added DSA Unit-6
1 parent 521c31a commit 41762d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2412
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
using namespace std;
3+
#define MAX_VERTICES 100
4+
5+
void addEdge(int adj[MAX_VERTICES][MAX_VERTICES], int v, int w) {
6+
adj[v][w] = 1;
7+
}
8+
9+
void DFS(int adj[MAX_VERTICES][MAX_VERTICES], int visited[MAX_VERTICES], int V, int v) {
10+
cout << v << " ";
11+
visited[v] = 1;
12+
for (int i = 0; i < V; ++i) {
13+
if (adj[v][i] && !visited[i]) {
14+
DFS(adj, visited, V, i);
15+
}
16+
}
17+
}
18+
19+
int main() {
20+
int V, E;
21+
cin >> V >> E;
22+
int adj[MAX_VERTICES][MAX_VERTICES] = {0};
23+
int visited[MAX_VERTICES] = {0};
24+
25+
for (int i = 0; i < E; ++i) {
26+
int v, w;
27+
cin >> v >> w;
28+
addEdge(adj, v, w);
29+
}
30+
31+
int startVertex;
32+
cin >> startVertex;
33+
34+
cout << "Depth First Traversal starting from vertex " << startVertex << ":\n";
35+
DFS(adj, visited, V, startVertex);
36+
37+
return 0;
38+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// You are using GCC
2+
#include <iostream>
3+
#define MAXN 100
4+
using namespace std;
5+
6+
int dfs(int node, int adj[][MAXN], int dp[], bool vis[], int n) {
7+
if (vis[node]) {
8+
return dp[node];
9+
}
10+
vis[node] = true;
11+
int maxPath = 0;
12+
for (int i = 0; i < n; i++) {
13+
if (adj[node][i]) {
14+
maxPath = max(maxPath, 1 + dfs(i, adj, dp, vis, n));
15+
}
16+
}
17+
dp[node] = maxPath;
18+
return maxPath;
19+
}
20+
21+
void addEdge(int adj[][MAXN], int u, int v) {
22+
adj[u][v] = 1;
23+
}
24+
25+
int findLongestPath(int adj[][MAXN], int n) {
26+
int dp[MAXN] = {0};
27+
bool vis[MAXN] = {false};
28+
int longestPath = 0;
29+
30+
for (int i = 0; i < n; i++) {
31+
if (!vis[i]) {
32+
longestPath = max(longestPath, dfs(i, adj, dp, vis, n));
33+
}
34+
}
35+
36+
return longestPath;
37+
}
38+
39+
int main() {
40+
int n, m;
41+
cin >> n >> m;
42+
int adj[MAXN][MAXN] = {0};
43+
44+
for (int i = 0; i < m; ++i) {
45+
int u, v;
46+
cin >> u >> v;
47+
addEdge(adj, u - 1, v - 1);
48+
}
49+
50+
int result = findLongestPath(adj, n);
51+
cout << result << endl;
52+
53+
return 0;
54+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
bool dfs(int adjMatrix[][100], int n, int start, int end, bool visited[]) {
5+
if (start == end)
6+
return true;
7+
visited[start] = true;
8+
for (int i = 0; i < n; ++i) {
9+
if (adjMatrix[start][i] && !visited[i] && dfs(adjMatrix, n, i, end, visited))
10+
return true;
11+
}
12+
return false;
13+
}
14+
15+
bool validPath(int n, int edges[][2], int m, int start, int end) {
16+
int adjMatrix[100][100] = {0};
17+
18+
for (int i = 0; i < m; ++i) {
19+
int u = edges[i][0];
20+
int v = edges[i][1];
21+
adjMatrix[u][v] = 1;
22+
}
23+
24+
bool visited[n] = {false};
25+
if (dfs(adjMatrix, n, start, end, visited)) {
26+
cout << "There is a path from " << start << " to " << end << endl;
27+
return true;
28+
} else {
29+
cout << "There is no path from " << start << " to " << end << endl;
30+
return false;
31+
}
32+
}
33+
34+
int main() {
35+
int n, m;
36+
cin >> n >> m;
37+
38+
int edges[m][2];
39+
for (int i = 0; i < m; ++i) {
40+
cin >> edges[i][0] >> edges[i][1];
41+
}
42+
43+
int start, end;
44+
cin >> start >> end;
45+
46+
validPath(n, edges, m, start, end);
47+
48+
return 0;
49+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <iostream>
2+
3+
#define MAXN 100
4+
5+
using namespace std;
6+
7+
int dfs(int node, int adj[][MAXN], int dp[], bool vis[], int n) {
8+
if (vis[node]) {
9+
return dp[node];
10+
}
11+
12+
vis[node] = true;
13+
int maxPath = 0;
14+
15+
for (int i = 0; i < n; i++) {
16+
if (adj[node][i]) {
17+
maxPath = max(maxPath, 1 + dfs(i, adj, dp, vis, n));
18+
}
19+
}
20+
21+
dp[node] = maxPath;
22+
return maxPath;
23+
}
24+
25+
void addEdge(int adj[][MAXN], int u, int v) {
26+
adj[u][v] = 1;
27+
}
28+
29+
int findLongestPath(int adj[][MAXN], int n)
30+
{
31+
int dp[MAXN] = {0};
32+
bool vis[MAXN] = {false};
33+
int maxPath = 0;
34+
35+
for (int i = 0; i < n; i++) {
36+
if (!vis[i]) {
37+
maxPath = max(maxPath, dfs(i, adj, dp, vis, n));
38+
}
39+
}
40+
41+
return maxPath;
42+
}
43+
44+
int main() {
45+
int n, m;
46+
cin >> n >> m;
47+
48+
int adj[MAXN][MAXN] = {0};
49+
50+
for (int i = 0; i < m; i++) {
51+
int u, v;
52+
cin >> u >> v;
53+
addEdge(adj, u - 1, v - 1);
54+
}
55+
56+
cout << findLongestPath(adj, n);
57+
return 0;
58+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <iostream>
2+
#include <queue>
3+
#include <climits>
4+
using namespace std;
5+
6+
void add_edge(int adj[][100], int src, int dest) {
7+
adj[src][dest] = 1;
8+
adj[dest][src] = 1;
9+
}
10+
11+
bool BFS(int adj[][100], int src, int dest, int v, int pred[], int dist[]) {
12+
queue<int> q;
13+
bool visited[v];
14+
for (int i = 0; i < v; i++) {
15+
visited[i] = false;
16+
dist[i] = INT_MAX;
17+
pred[i] = -1;
18+
}
19+
20+
visited[src] = true;
21+
dist[src] = 0;
22+
q.push(src);
23+
24+
while (!q.empty()) {
25+
int u = q.front();
26+
q.pop();
27+
28+
for (int i = 0; i < v; i++) {
29+
if (adj[u][i] && !visited[i]) {
30+
visited[i] = true;
31+
dist[i] = dist[u] + 1;
32+
pred[i] = u;
33+
q.push(i);
34+
35+
if (i == dest)
36+
return true;
37+
}
38+
}
39+
}
40+
41+
return false;
42+
}
43+
44+
void printShortestDistance(int adj[][100], int s, int dest, int v, int pred[], int dist[]) {
45+
if (!BFS(adj, s, dest, v, pred, dist)) {
46+
cout << "No path exists" << endl;
47+
return;
48+
}
49+
50+
int path[v];
51+
int crawl = dest;
52+
int pathLength = 0;
53+
path[pathLength++] = crawl;
54+
while (pred[crawl] != -1) {
55+
path[pathLength++] = pred[crawl];
56+
crawl = pred[crawl];
57+
}
58+
59+
cout << "Shortest path length is: " << dist[dest] << endl;
60+
cout << "Path is: ";
61+
for (int i = pathLength - 1; i >= 0; i--) {
62+
cout << path[i] << " ";
63+
}
64+
cout << endl;
65+
}
66+
67+
int main() {
68+
int v, e;
69+
cin >> v >> e;
70+
int adj[100][100] = {0}; // Assuming a maximum of 100 vertices
71+
int src, dest;
72+
73+
for (int i = 0; i < e; i++) {
74+
cin >> src >> dest;
75+
add_edge(adj, src, dest);
76+
}
77+
78+
int s;
79+
cin >> s; // source
80+
int destination;
81+
cin >> destination; // destination
82+
83+
int pred[v], dist[v];
84+
printShortestDistance(adj, s, destination, v, pred, dist);
85+
86+
return 0;
87+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <iostream>
2+
using namespace std;
3+
#define MAX_V 100
4+
5+
void enqueue(int* queue, int& rear, int vertex) {
6+
queue[++rear] = vertex;
7+
}
8+
9+
int dequeue(int* queue, int& front) {
10+
return queue[++front];
11+
}
12+
13+
void bfsOfGraph(int V, int adjList[MAX_V][MAX_V]) {
14+
int queue[MAX_V] = {0};
15+
int front = -1, rear = -1;
16+
int visited[MAX_V] = {0};
17+
18+
int startVertex;
19+
cin >> startVertex;
20+
21+
enqueue(queue, rear, startVertex);
22+
visited[startVertex] = 1;
23+
24+
while (front != rear) {
25+
int currentVertex = dequeue(queue, front);
26+
cout << currentVertex << " ";
27+
28+
for (int i = 0; i < V; ++i) {
29+
if (adjList[currentVertex][i] && !visited[i]) {
30+
enqueue(queue, rear, i);
31+
visited[i] = 1;
32+
}
33+
}
34+
}
35+
}
36+
37+
int main() {
38+
int V, E;
39+
cin >> V >> E;
40+
int adjList[MAX_V][MAX_V] = {0};
41+
42+
for (int i = 0; i < E; ++i) {
43+
int u, v;
44+
cin >> u >> v;
45+
adjList[u][v] = 1;
46+
}
47+
48+
bfsOfGraph(V, adjList);
49+
50+
return 0;
51+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// You are using GCC
2+
#include <iostream>
3+
#include <queue>
4+
using namespace std;
5+
#define MAX_V 100
6+
7+
void bfsOfGraph(int V, int adjList[MAX_V][MAX_V]) {
8+
int queue[MAX_V] = {0};
9+
int front = -1, rear = -1;
10+
int visited[MAX_V] = {0};
11+
12+
int startVertex;
13+
cin >> startVertex;
14+
15+
queue[++rear] = startVertex;
16+
visited[startVertex] = 1;
17+
18+
while (front != rear) {
19+
int currentVertex = queue[++front];
20+
cout << currentVertex << " ";
21+
22+
for (int i = 0; i < V; ++i) {
23+
if (adjList[currentVertex][i] && !visited[i]) {
24+
queue[++rear] = i;
25+
visited[i] = 1;
26+
}
27+
}
28+
}
29+
}
30+
31+
int main() {
32+
int V, E;
33+
cin >> V >> E;
34+
int adjList[MAX_V][MAX_V] = {0};
35+
36+
for (int i = 0; i < E; ++i) {
37+
int u, v;
38+
cin >> u >> v;
39+
adjList[u][v] = 1;
40+
}
41+
42+
bfsOfGraph(V, adjList);
43+
44+
return 0;
45+
}

0 commit comments

Comments
 (0)