Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions graphs/count_diconnected_components.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include<iostream>
#include<list>
using namespace std;

int count=1;
// Graph class represents a directed graph
// using adjacency list representation
class Graph
{
int V;
list<int> *adj;
void DFSUtil(int v, bool visited[]);
public:
Graph(int V);
void addEdge(int v, int w);
void DFS(int v);
};

Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}

void Graph::addEdge(int v, int w)
{
adj[v].push_back(w); // Add w to v’s list.
}

void Graph::DFSUtil(int v, bool visited[])
{
visited[v] = true;
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFSUtil(*i, visited);
}
void Graph::DFS(int v)
{
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
DFSUtil(v, visited);
for (int i = 0; i < V; i++)
if(visited[i] == false)
{
count++;
DFSUtil(i, visited);
}
}

int main()
{
Graph g(9);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
g.addEdge(4, 5);
g.addEdge(6, 7);
g.addEdge(6, 8);

g.DFS(2);
cout << "Number of disconnected components in the given graph is : " <<count;;

return 0;
}
51 changes: 51 additions & 0 deletions graphs/cycle_detect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//cycle detection in graph implemented in C++
//Author : Rituparno Biswas


#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;

int check=0;
int visited[1000]={0};
vector<int > v[1000];

void dfs(int curr , int p)
{
visited[curr]=1;
for(auto i:v[curr])
{
if(visited[i]==1)
{
if(i!=p)
check=1;
}
else
{
dfs(i, curr);
}
}
}


void addEdge(int a, int b)
{
v[a].push_back(b); // Add w to v’s list.
v[b].push_back(a); // Add v to w’s list.
}

int main()
{
addEdge(1, 0);
addEdge(0, 2);
addEdge(1, 2);
addEdge(0, 3);
addEdge(3, 4);
dfs(0,-1);
if(check)
cout<<"Cycle present";
else
cout<<"Cycle not present";
return 0;
}
49 changes: 49 additions & 0 deletions sorting/selection_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// C++ implementation of selection sort
//
// Author: Rituparno Biswas

#include <iostream>

// Swap elements
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}

// Implement selection sort
void selectionSort(int arr[], int n)
{
int i, j, min_id;
for (i = 0; i < n-1; i++)
{
min_id=i;
for (j = i+1; j < n; j++)
if (arr[min_id] > arr[j])
min_id=j;
swap(&arr[i], &arr[min_id]);
}
}

// Function to print elements
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

// test
int main()
{
int arr[] = {46, 24, 33, 10, 2, 81, 50};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Unsorted array: \n");
printArray(arr, n);
selectionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}