Tabij
Tabij
#include <iostream>
#include <queue>
#include <vector>
while (!q.empty()) {
int currentNode = q.front();
q.pop();
cout << currentNode << " ";
int main() {
int vertices, edges;
cout << "Enter the number of vertices: ";
cin >> vertices;
vector<vector<int>> adjList(vertices);
cout << "Enter the edges (source and destination pairs):" << endl;
for (int i = 0; i < edges; ++i) {
int u, v;
cin >> u >> v;
addEdge(adjList, u, v);
}
int startVertex;
cout << "Enter the starting vertex for BFS: ";
cin >> startVertex;
cout << "Breadth First Traversal starting from vertex " << startVertex << ": ";
bfs(adjList, startVertex, visited);
return 0;
}
DFS:
#include <iostream>
#include <vector>
int main() {
int vertices, edges;
cout << "Enter the number of vertices: ";
cin >> vertices;
vector<vector<int>> adj(vertices);
cout << "Enter the edges (source and destination pairs):" << endl;
for (int i = 0; i < edges; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int startVertex;
cout << "Enter the starting vertex for DFS: ";
cin >> startVertex;
cout << "Depth First Traversal starting from vertex " << startVertex << ": ";
DFS(startVertex, adj);
return 0;
}
Maximum Subarray:
#include <iostream>
#include <climits>
start = 0;
end = 0;
int s = 0; // Temporary start index
if (max_ending_here < 0) {
max_ending_here = 0;
s = i + 1; // Update temporary start index
}
}
return max_so_far;
}
int main() {
const int SIZE = 9;
int arr[SIZE] = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
std::cout << "Maximum subarray sum is " << max_sum << std::endl;
std::cout << "The subarray is: [";
for (int i = start; i <= end; i++) {
std::cout << arr[i];
if (i < end) {
std::cout << ", ";
}
}
std::cout << "]" << std::endl;
return 0;
}
User Input:
Dijkstra:
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
void dijkstra(const vector<vector<int>>& graph, int source, vector<int>& dist, vector<int>& parent)
{
int n = graph.size();
dist.assign(n, INF);
parent.assign(n, -1);
vector<bool> visited(n, false);
dist[source] = 0;
pq.push({0, source});
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
if (visited[u])
continue;
visited[u] = true;
int main() {
int n, source;
char choice;
while (true) {
cout << "Enter the number of vertices (enter -1 to exit): ";
cin >> n;
if (n == -1)
break;
cout << "Enter the adjacency matrix (use 0 for no direct edge):\n";
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
cin >> graph[i][j];
return 0;
}
User Input:
10 0 50 0 0
0 50 0 20 10
30 0 20 0 60
100 0 10 60 0
Floyd Warshal:
#include <iostream>
#include <vector>
#include <climits>
// 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 (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];
}
}
}
}
int main() {
int n;
std::cout << "Enter the number of vertices: ";
std::cin >> n;
int graph[100][100];
std::cout << "Enter the adjacency matrix (use 0 for no edge and input 0 for diagonal
elements):\n";
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
std::cin >> graph[i][j];
}
}
floydWarshall(graph, n);
return 0;
}
User Input:
4
Enter the adjacency matrix (use 0 for no edge and input 0 for diagonal elements):
0 3 INF 5
2 0 INF 4
INF 1 0 INF
INF INF 2 0
Merge Sort:
#include <iostream>
const int MAX_SIZE = 100; // Define a constant for the maximum array size
int main() {
int n;
int array[MAX_SIZE];
if (n > MAX_SIZE) {
std::cout << "Error: Maximum number of elements is " << MAX_SIZE << std::endl;
return 1;
}
return 0;
}
Quick Sort:
#include <iostream>
const int MAX_SIZE = 100; // Define a constant for the maximum array size
int main() {
int n;
int array[MAX_SIZE];
if (n > MAX_SIZE) {
std::cout << "Error: Maximum number of elements is " << MAX_SIZE << std::endl;
return 1;
}
quickSort(array, 0, n - 1);
return 0;
}
Knapsack:
#include <iostream>
#include <algorithm>
const int MAX_ITEMS = 100;
const int MAX_CAPACITY = 1000;
return dp[n][capacity];
}
int main() {
int n, capacity;
int weights[MAX_ITEMS], values[MAX_ITEMS];
std::cout << "The maximum value that can be put in a knapsack of capacity " << capacity << " is "
<< max_value << std::endl;
return 0;
}
User Input:
10
Enter the weights of the items:
2345
3456
LCS:
#include <iostream>
#include <cstring>
#include <algorithm>
int i = m, j = n;
while (i > 0 && j > 0) {
if (X[i - 1] == Y[j - 1]) {
lcsString[index - 1] = X[i - 1];
i--;
j--;
index--;
} else if (L[i - 1][j] > L[i][j - 1])
i--;
else
j--;
}
return L[m][n];
}
int main() {
char X[MAX_LENGTH + 1];
char Y[MAX_LENGTH + 1];
char lcsString[MAX_LENGTH + 1];
int m = std::strlen(X);
int n = std::strlen(Y);
std::cout << "The length of the Longest Common Subsequence is " << length << std::endl;
std::cout << "The Longest Common Subsequence is " << lcsString << std::endl;
return 0;
}
AGGTAB
Enter the second string:
GXTXAYB