0% found this document useful (0 votes)
15 views11 pages

CC 2

Uploaded by

thithikshhh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views11 pages

CC 2

Uploaded by

thithikshhh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

DFS & BFS:

#include <iostream>

#include <vector>

#include <queue>

#include <unordered_set>

#include <unordered_map>

#include <stack>

using namespace std;

class Graph {

private:

int vertices;

unordered_map<int, vector<int>> adjacencyList;

public:

Graph(int V) : vertices(V) {}

void addEdge(int v, int w) {

adjacencyList[v].push_back(w);

adjacencyList[w].push_back(v);

void dfs(int startVertex) {

unordered_set<int> visited;

stack<int> dfsStack;

dfsStack.push(startVertex);

visited.insert(startVertex);

cout << "DFS Traversal: ";

while (!dfsStack.empty()) {

int currentVertex = dfsStack.top();

dfsStack.pop();

cout << currentVertex << " ";

for (int neighbor : adjacencyList[currentVertex]) {

if (visited.find(neighbor) == visited.end()) {

dfsStack.push(neighbor);
visited.insert(neighbor);

cout << endl;

void bfs(int startVertex) {

unordered_set<int> visited;

queue<int> bfsQueue;

bfsQueue.push(startVertex);

visited.insert(startVertex);

cout << "BFS Traversal: ";

while (!bfsQueue.empty()) {

int currentVertex = bfsQueue.front();

bfsQueue.pop();

cout << currentVertex << " ";

for (int neighbor : adjacencyList[currentVertex]) {

if (visited.find(neighbor) == visited.end()) {

bfsQueue.push(neighbor);

visited.insert(neighbor);

cout << endl;

};

int main() {

int V;

cin >> V;

Graph graph(V);

int edges;
cin >> edges;

for (int i = 0; i < edges; ++i) {

int v, w;

cin >> v >> w;

graph.addEdge(v, w);

int startVertex;

cin >> startVertex;

graph.dfs(startVertex);

graph.bfs(startVertex);

return 0;

Topological sorting of a directed acyclic graph:


#include <iostream>

#include <list>

#include <stack>

#include <vector>

using namespace std;

class Graph {

int V;

list<int>* adj;

public:

Graph(int V);

void addEdge(int source, int destination);

void topologicalSort();

void topologicalSortUtil(int v, vector<bool>& visited, stack<int>& result);

};

Graph::Graph(int V) {

this->V = V;

adj = new list<int>[V];

}
void Graph::addEdge(int source, int destination) {

adj[source].push_back(destination);

void Graph::topologicalSortUtil(int v, vector<bool>& visited, stack<int>& result) {

visited[v] = true;

for (const int& neighbor : adj[v]) {

if (!visited[neighbor]) {

topologicalSortUtil(neighbor, visited, result);

result.push(v);

void Graph::topologicalSort() {

stack<int> result;

vector<bool> visited(V, false);

for (int i = 0; i < V; ++i) {

if (!visited[i]) {

topologicalSortUtil(i, visited, result);

cout << "Topological Sorting Order: ";

while (!result.empty()) {

cout << result.top() << " ";

result.pop();

cout << endl;

int main() {

int numNodes, numEdges, source, destination;

cin >> numNodes >> numEdges;

Graph graph(numNodes);
for (int i = 0; i < numEdges; ++i) {

cin >> source >> destination;

graph.addEdge(source, destination);

graph.topologicalSort();

return 0;

Friendship matrix:
#include <iostream>

#include <vector>

using namespace std;

void createAdjacencyMatrix(int vertices, int edges, const vector<pair<int, int>>& edgeList) {

vector<vector<int>> adjacencyMatrix(vertices, vector<int>(vertices, 0));

for (const auto& edge : edgeList) {

int source = edge.first;

int destination = edge.second;

adjacencyMatrix[source][destination] = 1;

adjacencyMatrix[destination][source] = 1;

cout << "Adjacency Matrix:\n";

for (const auto& row : adjacencyMatrix) {

for (int value : row) {

cout << value << " ";

cout << "\n";

int main() {

int vertices, edges;

cin >> vertices;

cin >> edges;


vector<pair<int, int>> edgeList;

for (int i = 0; i < edges; ++i) {

int source, destination;

cin >> source >> destination;

edgeList.emplace_back(source, destination);

createAdjacencyMatrix(vertices, edges, edgeList);

return 0;

Dijistra’s algorithm:
#include <iostream>

#include <vector>

#include <queue>

#include <limits>

using namespace std;

class Graph {

int V;

vector<pair<int, int>>* adj;

public:

Graph(int V);

void addEdge(int source, int destination, int weight);

void dijkstra(int startNode);

};

Graph::Graph(int V) {

this->V = V;

adj = new vector<pair<int, int>>[V];

void Graph::addEdge(int source, int destination, int weight) {

adj[source].push_back({destination, weight});

void Graph::dijkstra(int startNode) {


priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;

vector<int> dist(V, numeric_limits<int>::max());

pq.push({0, startNode});

dist[startNode] = 0;

while (!pq.empty()) {

int u = pq.top().second;

pq.pop();

for (const auto& neighbor : adj[u]) {

int v = neighbor.first;

int weight = neighbor.second;

if (dist[v] > dist[u] + weight) {

dist[v] = dist[u] + weight;

pq.push({dist[v], v});

for (int i = 0; i < V; ++i) {

cout << "Shortest distance from node " << startNode << " to node " << i << ": ";

if (dist[i] == numeric_limits<int>::max()) {

cout << "INF" << endl;

else {

cout << dist[i] << endl;

int main() {

int numNodes, numEdges, source, destination, weight, startNode;

cin >> numNodes >> numEdges;

Graph graph(numNodes);

for (int i = 0; i < numEdges; ++i) {


cin >> source >> destination >> weight;

graph.addEdge(source, destination, weight);

cin >> startNode;

graph.dijkstra(startNode);

return 0;

Kruskal’s algorithm:
#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

struct Edge {

int src, dest, weight;

};

struct Subset {

int parent, rank;

};

class Graph {

private:

int vertices;

vector<Edge> edges;

public:

Graph(int v) : vertices(v) {}

void addEdge(int src, int dest, int weight) {

Edge edge = {src, dest, weight};

edges.push_back(edge);

int find(Subset subsets[], int i) {

if (subsets[i].parent != i)

subsets[i].parent = find(subsets, subsets[i].parent);


return subsets[i].parent;

void Union(Subset subsets[], int x, int y) {

int xroot = find(subsets, x);

int yroot = find(subsets, y);

if (subsets[xroot].rank < subsets[yroot].rank)

subsets[xroot].parent = yroot;

else if (subsets[xroot].rank > subsets[yroot].rank)

subsets[yroot].parent = xroot;

else {

subsets[yroot].parent = xroot;

subsets[xroot].rank++;

void kruskalMST() {

vector<Edge> result;

int e = 0;

int i = 0;

sort(edges.begin(), edges.end(), [](Edge a, Edge b) {

return a.weight < b.weight;

});

Subset* subsets = new Subset[vertices];

for (int v = 0; v < vertices; v++) {

subsets[v].parent = v;

subsets[v].rank = 0;

while (i < vertices - 1 && e < edges.size()) {

Edge next_edge = edges[e++];

int x = find(subsets, next_edge.src);

int y = find(subsets, next_edge.dest);

if (x != y) {
result.push_back(next_edge);

Union(subsets, x, y);

i++;

cout << "Minimum Spanning Tree using Kruskal's Algorithm:\n";

for (i = 0; i < result.size(); i++)

cout << result[i].src << " -- " << result[i].dest << " == " << result[i].weight << endl;

delete[] subsets;

};

int main() {

int vertices, edges, src, dest, weight;;

cin >> vertices;

Graph g(vertices);

cin >> edges;

for (int i = 0; i < edges; i++) {

cin>>src>>dest>>weight;

g.addEdge(src,dest,weight);

g.kruskalMST();

return 0;

Polynomial time:
#include<iostream>

#include<vector>

bool isPolynomialTime(int n,std::string timeComplexity) {

for(char &c : timeComplexity) {

c=std::tolower(c);

if(timeComplexity.find("n^")!=std::string::npos) {
return true;

else if(timeComplexity.find("n log n")!=std::string::npos) {

return true;

else if(timeComplexity.find("n")!=std::string::npos) {

return true;

return false;

int main() {

int inputSize;

std::string timeComplexity;

std::cin>>inputSize;

std::cin.ignore();

std::getline(std::cin,timeComplexity);

if(isPolynomialTime(inputSize,timeComplexity)) {

std::cout<<"The algorithm runs in polynomial time (P class).\n";

else {

std::cout<<"The algorithm does not run in polynomial time.\n";

return 0;

NP complete classes:
P vs NP:

You might also like