Skip to content
Open
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
4 changes: 4 additions & 0 deletions CONTRIBUTOR.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@
#### Name: [Pratik Shirodkar](https://github.com/Pratik-Shirodkar)
- Place: Goa, India
- GitHub: [Pratik Shirodkar](https://github.com/Pratik-Shirodkar)

### Name: [Raza Khan](https://github.com/raza-khan0108)
- Place: Mumbai, India
- Github: [raza-khan0108](https://github.com/raza-khan0108)
28 changes: 28 additions & 0 deletions CPP/2Darray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include<stdio.h>
#include <iostream>
using namespace std;

int main(){
int A[3][4] = {{1,2,3,4},{2,4,6,8},{1,3,5,7}};
for(int i=0;i<3;i++){ //for rows
for(int j=0;j<4;j++){ // for columns
cout<<A[i][j]<<endl;
}
}

int *B[3];

B[0] = new int [4];
B[1] = new int [4];
B[2] = new int [4];

B[1][2] = 15;
cout<<B[1][2];

int **C;
C = new int*[3];
C[0] = new int [4];
C[1] = new int [4];
C[2] = new int [4];

}
108 changes: 108 additions & 0 deletions CPP/Graph/BFSandDFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#ifndef Queue_h
#define Queue_h
#include <stdlib.h>
#include <stdio.h>
struct Node
{
int data;
struct Node *next;

}*front=NULL,*rear=NULL;
void enqueue(int x)
{
struct Node *t;
t=(struct Node*)malloc(sizeof(struct Node));
if(t==NULL)
printf("Queue is FUll\n");
else
{
t->data=x;
t->next=NULL;
if(front==NULL)
front=rear=t;
else
{
rear->next=t;
rear=t;
}
}

}
int dequeue()
{
int x=-1;
struct Node* t;

if(front==NULL)
printf("Queue is Empty\n");
else
{
x=front->data;
t=front;
front=front->next;
free(t);
}
return x;
}
int isEmpty()
{
return front==NULL;
}
#endif /* Queue_h */
#include <stdio.h>
void BFS(int G[][7],int start,int n)
{
int i=start,j;

int visited[7]={0};

printf("%d ",i);
visited[i]=1;
enqueue(i);

while(!isEmpty())
{
i=dequeue();
for(j=1;j<n;j++)
{
if(G[i][j]==1 && visited[j]==0)
{
printf("%d ",j);
visited[j]=1;
enqueue(j);
}
}
}


}
void DFS(int G[][7],int start,int n)
{
static int visited[7]={0};
int j;

if(visited[start]==0)
{
printf("%d ",start);
visited[start]=1;

for(j=1;j<n;j++)
{
if(G[start][j]==1 && visited[j]==0)
DFS(G,j,n);
}
}
}
int main()
{
int G[7][7]={{0,0,0,0,0,0,0},
{0,0,1,1,0,0,0},
{0,1,0,0,1,0,0},
{0,1,0,0,1,0,0},
{0,0,1,1,0,1,1},
{0,0,0,0,1,0,0},
{0,0,0,0,1,0,0}};
DFS(G,4,7);

return 0;
}
94 changes: 94 additions & 0 deletions CPP/Graph/prims algo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <iostream>
#define V 8
#define I 32767

using namespace std;

void PrintMST(int T[][V-2], int G[V][V]){
cout << "\nMinimum Spanning Tree Edges (w/ cost)\n" << endl;
int sum {0};
for (int i {0}; i<V-2; i++){
int c = G[T[0][i]][T[1][i]];
cout << "[" << T[0][i] << "]---[" << T[1][i] << "] cost: " << c << endl;
sum += c;
}
cout << endl;
cout << "Total cost of MST: " << sum << endl;
}

void PrimsMST(int G[V][V], int n){
int u;
int v;
int min {I};
int track [V];
int T[2][V-2] {0};

// Initial: Find min cost edge
for (int i {1}; i<V; i++){
track[i] = I; // Initialize track array with INFINITY
for (int j {i}; j<V; j++){
if (G[i][j] < min){
min = G[i][j];
u = i;
v = j;
}
}
}
T[0][0] = u;
T[1][0] = v;
track[u] = track[v] = 0;

// Initialize track array to track min cost edges
for (int i {1}; i<V; i++){
if (track[i] != 0){
if (G[i][u] < G[i][v]){
track[i] = u;
} else {
track[i] = v;
}
}
}

// Repeat
for (int i {1}; i<n-1; i++){
int k;
min = I;
for (int j {1}; j<V; j++){
if (track[j] != 0 && G[j][track[j]] < min){
k = j;
min = G[j][track[j]];
}
}
T[0][i] = k;
T[1][i] = track[k];
track[k] = 0;

// Update track array to track min cost edges
for (int j {1}; j<V; j++){
if (track[j] != 0 && G[j][k] < G[j][track[j]]){
track[j] = k;
}
}
}
PrintMST(T, G);
}

int main() {

int cost [V][V] {
{I, I, I, I, I, I, I, I},
{I, I, 25, I, I, I, 5, I},
{I, 25, I, 12, I, I, I, 10},
{I, I, 12, I, 8, I, I, I},
{I, I, I, 8, I, 16, I, 14},
{I, I, I, I, 16, I, 20, 18},
{I, 5, I, I, I, 20, I, I},
{I, I, 10, I, 14, 18, I, I},
};

int n = sizeof(cost[0])/sizeof(cost[0][0]) - 1;

PrimsMST(cost, n);

return 0;
}
16 changes: 16 additions & 0 deletions CPP/Hanoi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
#include <stdio.h>
using namespace std;

void TOH(int n,int A,int B,int C){
if(n>0){
TOH(n-1,A,C,B);
cout << "("<<A<<","<<C<<")"<<endl;
TOH(n-1,B,A,C);
}
}

int main(){
TOH(3,1,2,3);
return 0;
}
49 changes: 49 additions & 0 deletions CPP/LinkedList(Doubly).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

struct Node
{
int data;
struct Node *next;
}*first=NULL,*second=NULL,*third=NULL;

void Display(struct Node *p)
{
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
}

void create(int A[],int n)
{
int i;
struct Node *t,*last;
first=new Node;
first->data=A[0];
first->next=NULL;
last=first;

for(i=1;i<n;i++)
{
t=new Node;
t->data=A[i];
t->next=NULL;
last->next=t;
last=t;
}

}
int main()
{

int A[]={10,20,40,50,60};
create(A,5);

Display(first);

return 0;
}
Loading