Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
shahalam22 authored Jun 6, 2023
0 parents commit a526c77
Show file tree
Hide file tree
Showing 61 changed files with 1,835 additions and 0 deletions.
125 changes: 125 additions & 0 deletions SCC.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include <stdio.h>
#include <string.h>

char color[100][10];
int n, time = 0, size = 0;
int prev[100], d[100], f[100], val[100];

// code for dfs
void dfs(int graph[][n], int k){

strcpy(color[k], "Grey");
time = time + 1;
d[k] = time;

//storing values in a array
val[size] = k;
size++;

for(int j=0; j<n; j++){
if((graph[k][j] == 1) && (!strcmp(color[j], "White"))){
prev[j] = k;
dfs(graph, j);
}
}

strcpy(color[k], "Black");
time = time + 1;
f[k] = time;

}

//topological sort code
void topologicalSort(int f[], int ts[]){
int min = max;
int minIndx = 0, size = 0;

for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(f[j]<min){
min = f[j];
minIndx = j;
}
}
f[minIndx] = max;
min = max;
ts[size] = minIndx;
size++;
}

printf("Topological sort - \n");
for(int i=0; i<n; i++){
printf("%d ", ts[i]);
}
printf("\n");
}

// transposing matrix
void transpose(int in[][n], int out[n][n]){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
out[i][j] = in[j][i];
}
}
}

// sstongly connected component
void SCC(int arr[][n], int ts[]){
int out[n][n];
transpose(arr, out);

// printing the SCC
printf("\nStrongly Connected components are -\n");
for(int i=0; i<n; i++){
strcpy(color[i], "White");
}

for(int i=0; i<n; i++){
if(!strcmp(color[i], "White")){
dfs(out, i);
for(int j=0; j<n; j++){
if(!strcmp(color[j], "Black")){
printf("%d ", j);
strcpy(color[j], "Blue");
}
}
printf("\n");
}
}
}

// main function

int main(void){

printf("Enter number of node - ");
scanf("%d", &n);

for(int i=0; i<n; i++){
strcpy(color[i], "White");
prev[i] = max;
f[i] = max;
d[i] = max;
}

int arr[n][n];

//creating graph
printf("Enter Graph [in matrix form] - \n");
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
scanf("%d", &arr[i][j]);
}
}

dfs(arr, 0);

// topological sort code
int ts[n];
topologicalSort(f, ts);

// transpose matrix
SCC(arr, ts);

return 0;
}
Binary file added SCC.exe
Binary file not shown.
89 changes: 89 additions & 0 deletions bfs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <stdio.h>
#include <string.h>

#define max 10000

int n, size = 0, size2 = 0;
char color [100][10];
int q[100], prev[100], val[100];

void bfs(int graph[][n], int s){
for(int i=0; i<n; i++){
strcpy(color[i], "White");
prev[i] = max;
}

strcpy(color[s], "Grey");
q[size] = s;
size++;

while(size!=0){
int a = q[0];
for(int i=1; i<size; i++){
q[i-1] = q[i];
}
size--;

for(int i=0; i<n; i++){
if((graph[a][i] == 1) && (!strcmp(color[i], "White"))){
strcpy(color[i], "Grey");
prev[i] = a;
q[size] = i;
size++;
}

}
strcpy(color[a], "Black");
val[size2] = a;
size2++;
}

printf("Printing bfs array - ");
for(int p=0; p<n; p++){
printf("%d ", val[p]);
}
}

// //path printing code
void printPath(int prev[], int a, int b){
if(a==b){
printf("%d\t", a);
}
else if(prev[b]==max){
printf("");
}
else {
printPath(prev, a, prev[b]);
printf("%d\t", b);
}
}

int main(void){

printf("Enter number of node - ");
scanf("%d", &n);

int arr[n][n];

//creating graph
printf("Enter Graph [in matrix form] - \n");
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
scanf("%d", &arr[i][j]);
}
}

//printing graph
printf("Graph is [in matrix form] - \n");
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
printf("%d ", arr[i][j]);
}
printf("\n");
}

//bfs run
bfs(arr, 0);

return 0;
}
Binary file added bfs.exe
Binary file not shown.
21 changes: 21 additions & 0 deletions binarySearch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>

int binarySearch(int arr[10], int a, int s, int e){
int start = s;
int end = e;
int mid = (s+e)/2;
if(arr[mid]==a) return mid;
else if(arr[mid]>a){
binarySearch(arr, a, s, mid-1);
}
else {
binarySearch(arr, a, mid+1, e);
}
}

int main(void){
int arr[10] = {1,2,3,4,5,6,7,8,9};
int ans = binarySearch(arr, 8, 0, 10);
printf("%d\n", ans);
return 0;
}
Binary file added binarySearch.exe
Binary file not shown.
Binary file added binarySearch.o
Binary file not shown.
80 changes: 80 additions & 0 deletions binaryTTraversal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <stdio.h>
#include <stdlib.h>

struct node *root;

struct node{
int data;
struct node *left;
struct node *right;
};

//creating binary tree
//creating node
struct node* createNode(int val){
struct node *newN;
newN = malloc(sizeof(struct node));
newN->data = val;
newN->left = NULL;
newN->right = NULL;

return newN;
}
//inserting value at left of parent
void insertLeft(struct node *p, int val){
p->left = createNode(val);
}
//inserting value at right of parent
void inserRight(struct node *p, int val){
p->right = createNode(val);
}

//traversal
//inorder traversal
void inOrder(struct node *p){
if(p == NULL){
return;
}
inOrder(p->left);
printf("%d\t", p->data);
inOrder(p->right);
}
//preorder traversal
void preOrder(struct node *p){
if(p == NULL){
return;
}
printf("%d\t", p->data);
preOrder(p->left);
preOrder(p->right);
}
//postorder traversal
void postOrder(struct node *p){
if(p == NULL){
return;
}
postOrder(p->left);
postOrder(p->right);
printf("%d\t", p->data);
}

int main(void){

root = createNode(1);

insertLeft(root, 2);
inserRight(root, 3);
insertLeft(root->left, 4);
inserRight(root->left, 5);
insertLeft(root->right, 6);
inserRight(root->right, 7);

inOrder(root);
printf("\n");
preOrder(root);
printf("\n");
postOrder(root);
printf("\n");

return 0;
}
Binary file added binaryTTraversal.exe
Binary file not shown.
43 changes: 43 additions & 0 deletions binaryTree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>

struct node *root;

struct node{
int data;
struct node *left;
struct node *right;
};

struct node* createNode(int val){
struct node *newN;
newN = malloc(sizeof(struct node));
newN->data = val;
newN->left = NULL;
newN->right = NULL;

return newN;
}

void insertLeft(struct node *p, int val){
p->left = createNode(val);
}

void inserRight(struct node *p, int val){
p->right = createNode(val);
}

int main(void){

root = createNode(1);

insertLeft(root, 2);
inserRight(root, 3);
insertLeft(root->left, 4);
inserRight(root->left, 5);
insertLeft(root->right, 6);
inserRight(root->right, 7);


return 0;
}
Binary file added binaryTree.exe
Binary file not shown.
29 changes: 29 additions & 0 deletions bubbleSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include<stdio.h>

void bubbleSort(int arr[], int n){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(arr[i]<arr[j]){
swap(arr, i, j);
}
}
}
}

void swap(int arr[], int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

int main(){
int arr[] = {5,6,4,1,2,8,7,3,9};
int n = sizeof(arr)/sizeof(arr[0]);

bubbleSort(arr, n);

//printing sorted array
for(int i=0; i<n; i++){
printf("%d ", arr[i]);
}
}
Binary file added bubbleSort.exe
Binary file not shown.
Binary file added bubbleSort.o
Binary file not shown.
Loading

0 comments on commit a526c77

Please sign in to comment.