Skip to content

Commit

Permalink
Merge pull request #1 from ACES-DYPCOE/master
Browse files Browse the repository at this point in the history
Update to original repo
  • Loading branch information
mohitkhedkar authored Oct 1, 2020
2 parents 2631add + bbe072a commit ba9ad04
Show file tree
Hide file tree
Showing 49 changed files with 3,386 additions and 3 deletions.
79 changes: 79 additions & 0 deletions Dynamic Programming/C++/BoxStackingProblem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include<stdio.h>
#include<stdlib.h>

struct Box
{
int h, w, d;
};

int min (int x, int y)
{ return (x < y)? x : y; }

int max (int x, int y)
{ return (x > y)? x : y; }

int compare (const void *a, const void * b)
{
return ( (*(Box *)b).d * (*(Box *)b).w ) -
( (*(Box *)a).d * (*(Box *)a).w );
}

int maxStackHeight( Box arr[], int n )
{
Box rot[3*n];
int index = 0;
for (int i = 0; i < n; i++)
{
rot[index].h = arr[i].h;
rot[index].d = max(arr[i].d, arr[i].w);
rot[index].w = min(arr[i].d, arr[i].w);
index++;

rot[index].h = arr[i].w;
rot[index].d = max(arr[i].h, arr[i].d);
rot[index].w = min(arr[i].h, arr[i].d);
index++;

rot[index].h = arr[i].d;
rot[index].d = max(arr[i].h, arr[i].w);
rot[index].w = min(arr[i].h, arr[i].w);
index++;
}

n = 3*n;

qsort (rot, n, sizeof(rot[0]), compare);

int msh[n];
for (int i = 0; i < n; i++ )
msh[i] = rot[i].h;

for (int i = 1; i < n; i++ )
for (int j = 0; j < i; j++ )
if ( rot[i].w < rot[j].w &&
rot[i].d < rot[j].d &&
msh[i] < msh[j] + rot[i].h
)
{
msh[i] = msh[j] + rot[i].h;
}


int max = -1;
for ( int i = 0; i < n; i++ )
if ( max < msh[i] )
max = msh[i];

return max;
}

int main()
{
Box arr[] = { {4, 6, 7}, {1, 2, 3}, {4, 5, 6}, {10, 12, 32} };
int n = sizeof(arr)/sizeof(arr[0]);

printf("The maximum possible height of stack is %d\n",
maxStackHeight (arr, n) );

return 0;
}
78 changes: 78 additions & 0 deletions Graph/C++/DFS_traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include<bits/stdc++.h>
using namespace std;




/* Function to do DFS of graph
g : adjacency list of graph
N : number of vertices
return a list containing the DFS traversal of the given graph
*/



void rec_dfs(vector<int> g[],vector<int> &result,int k,int *visit){

result.push_back(k);
visit[k] = 1;

for(i:g[k]){
if(visit[i] == 0){
rec_dfs(g,result,i,visit);
}
}


return;
}


vector <int> dfs(vector<int> g[], int N)
{
vector<int> result;

int visit[N] = {0};

rec_dfs(g,result,0,visit);



return result;

}



int main()
{
int T;
cin>>T;
while(T--)
{

int N, E;
cin>>N>>E;

vector<int> g[N];
bool vis[N];

memset(vis, false, sizeof(vis));

for(int i=0;i<E;i++)
{
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}

vector <int> res = dfs(g, N);
for (int i = 0; i < res.size (); i++)
cout << res[i] << " ";
cout<<endl;

}
}
67 changes: 67 additions & 0 deletions Graph/C++/DetectCycleInADirectedGraph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <bits/stdc++.h>
using namespace std;


/* Function to check if the given graph contains cycle
* V: number of vertices
* adj[]: representation of graph
*/
bool check_cycle(vector<int> g[],int *visit,int *cycle,int k){
cycle[k] = 1;
visit[k] = 1;

for(auto i = g[k].begin();i!=g[k].end();i++){
if(cycle[*i] == 1){
return true;
}

else if(cycle[*i] == 0)
if(visit[*i] != 0 && check_cycle(g,visit,cycle,*i))return true;

}
cycle[k] = 0;
return false;
}


bool isCyclic(int V, vector<int> adj[])
{
int visited[V] = {0};
int cycleCheck[V] = {0};
for(int i=0;i<V;i++){
if(visited[i] == 0){
if(check_cycle(adj,visited,cycleCheck,i))
return true;
}
}
return false;

}




int main() {

int t;
cin >> t;

while(t--){

int v, e;
cin >> v >> e;

vector<int> adj[v];

for(int i =0;i<e;i++){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
}

cout << isCyclic(v, adj) << endl;

}

return 0;
}
12 changes: 12 additions & 0 deletions Linked Lists/Check_Circularll.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//Given a singly linked list, find if the linked list is circular or not. A linked list is called circular if it not NULL terminated and all nodes are connected in the form of a cycle. An empty linked list is considered as circular.
//solution

bool isCircular(Node *head)
{
if(head==NULL || head->next==head) return true;
Node* temp=head;
while(temp->next!=NULL && temp->next!=head)
temp=temp->next;

return (temp->next==head)?true:false;
}
26 changes: 26 additions & 0 deletions Linked Lists/DeleteAtPos_Circularll.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//Given a linked list of size n, you have to delete the node at position pos of the linked list and return the new head. The position of initial node is 1.
//The tail of the circular linked list is connected to the head using next pointer.

//solution
Node * deleteAtPosition(Node *head,int pos)
{
if(pos==1)
{
if(head->next==head)
return NULL;
Node *temp=head->next;
head->next=head->next->next;
delete(temp);
return head;
}
else
{
Node *temp=head;
Node *cur;
for(int i=0;i<pos-2;i++) temp=temp->next;
cur=temp->next;
temp->next=temp->next->next;
delete(cur);
return head;
}
}
18 changes: 18 additions & 0 deletions Linked Lists/DeleteHead_Circularll.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//Given a circular linked list of size n, you have to delete the head of the linked list and return the new head.
//In the circular linked list the tail of the list is connected to the head using the next pointer.
//Note: Please also set the next of the original head to null.

//solution
Node * deleteHead(Node *head)
{
if(head==NULL)
return NULL;

Node* temp=head;
Node* temp1=head;
while(temp->next!=head) temp=temp->next;
temp->next=temp1->next;
head=temp1->next;
temp1->next=NULL;
return head;
}
14 changes: 14 additions & 0 deletions Linked Lists/DeleteHead_Doublyll.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//Given a doubly linked list of size n, you have to delete the head of the linked list and return the new head.
//Note: Please set the previous of new head to null, and set the next of old head to null, and then delete the old head.

Node *deleteHead(Node * head)
{
if(head==NULL) return NULL;
Node* temp=head;

head=temp->next;
head->prev=NULL;

delete temp;
return head;
}
12 changes: 12 additions & 0 deletions Linked Lists/DeleteHead_LinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//Given a linked list of size n, you have to delete the head of the linked list and return the new head.
//Note: Please also set the next of the original head to null.

Node* deleteHead(Node *head)
{
Node* temp=head->next;
head->next=NULL;

delete head;

return temp;
}
13 changes: 13 additions & 0 deletions Linked Lists/DeleteTail_Circular.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//Given a circular linked list of size n, you have to delete the tail (last element) in the linked list.
//In a circular linked list, the tail is connect to the head using the next pointer.

//solution
Node * deleteTail(Node * head)
{
if(head==NULL) return NULL;

Node* temp=head;
while(temp->next->next!=head) temp=temp->next;
temp->next=head;
return head;
}
15 changes: 15 additions & 0 deletions Linked Lists/DeleteTail_Doublyll.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//Given a doubly linked list of size n, you have to delete the tail (last element) in the linked list.

Node *deleteTail(Node * head)
{
if(head==NULL) return NULL;

Node* temp=head;

while(temp->next->next) temp=temp->next;

delete temp->next;
temp->next=NULL;

return head;
}
18 changes: 18 additions & 0 deletions Linked Lists/DeleteTail_LinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//Given a linked list of size n, you have to delete the tail (last element) in the linked list.
//solution

Node* deleteTail(Node *head)
{
//empty linked list
if(head==NULL)
return head;

Node* temp=head;
while(temp->next->next!=NULL)
temp=temp->next;

delete temp->next;
temp->next=NULL;

return head;
}
17 changes: 17 additions & 0 deletions Linked Lists/Display_DoublyCircularll.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
void displayList(Node *head)
{
vector<int>v;
Node* temp=head;
while(temp->next!=head)
{
v.push_back(temp->data);
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<temp->data<<" ";
v.push_back(temp->data);
cout<<endl;
for(int i=v.size()-1;i>=0;i--)
cout<<v[i]<<" ";

}
Loading

0 comments on commit ba9ad04

Please sign in to comment.