Skip to content

Commit 4fba501

Browse files
committed
first commit
0 parents  commit 4fba501

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+4050
-0
lines changed

**kadanes.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int maxSubArraySum(int a[], int size)
5+
{
6+
int max_so_far = a[0];
7+
int curr_max = a[0];
8+
9+
for (int i = 1; i < size; i++)
10+
{
11+
cout<<"i:"<<i<<"\n";
12+
curr_max = max(a[i], curr_max+a[i]);
13+
cout<<curr_max<<"\n";
14+
max_so_far = max(max_so_far, curr_max);
15+
cout<<max_so_far<<"\n";
16+
cout<<"@@@@@@"<<"\n";
17+
}
18+
return max_so_far;
19+
}
20+
21+
/* Driver program to test maxSubArraySum */
22+
int main()
23+
{
24+
int a[] = {-2, -3, 4, -1, -2, 1, 5, -3};
25+
int n = sizeof(a)/sizeof(a[0]);
26+
int max_sum = maxSubArraySum(a, n);
27+
cout << "Maximum contiguous sum is " << max_sum;
28+
return 0;
29+
}

**minimumsum.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<iostream>
2+
using namespace std;
3+
int a[4]={36 ,7 ,46 ,40};
4+
int findMinimumSum(int netsum,int sum,int i){
5+
if(i==3){
6+
return abs((netsum-sum)-sum);
7+
}
8+
9+
return min(findMinimumSum(netsum,sum+a[i],i+1),findMinimumSum(netsum,sum,i+1));
10+
}
11+
int main()
12+
{
13+
int sum=0;
14+
for (int i=0;i<5;i++){
15+
sum+=a[i];
16+
}
17+
int k=findMinimumSum(sum,0,0);
18+
cout<<"K:"<<k;
19+
return 0;
20+
}

.DS_Store

6 KB
Binary file not shown.

Graphs/articulation.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include<iostream>
2+
#include<list>
3+
using namespace std;
4+
5+
class Graph{
6+
int v;
7+
list<int> *adj;
8+
public:
9+
Graph(int v){
10+
this->v=v;
11+
this->adj=new list<int>[v];
12+
}
13+
void addEdge(int u,int w){
14+
this->adj[u].push_back(w);
15+
this->adj[w].push_back(u);
16+
}
17+
void articulationUtil(int v,bool *visited,int *parent,int *low,int *high,
18+
int & childcout,int root){
19+
static int t=0;
20+
low[v]=t;
21+
high[v]=t;
22+
visited[v]=true;
23+
list<int>::iterator it;
24+
for(it=this->adj[v].begin();it!=this->adj[v].end();it++){
25+
int u = *it;
26+
if(!visited[u]){
27+
if(v==root){
28+
childcout++;
29+
}
30+
parent[u]=v;
31+
t=t+1;
32+
articulationUtil(u,visited,parent,low,high,childcout,root);
33+
// low[v]=min(low[v],low[u]);
34+
if(v==root && childcout>=2){
35+
cout<<"ART: "<<v<<"\n";
36+
}
37+
if(parent[v]!=-1 && high[v]<=low[u]){
38+
cout<<"ART: "<<v<<"\n";
39+
}
40+
}else if(parent[v]!=u){
41+
low[v]=min(low[v],low[u]);
42+
}
43+
}
44+
}
45+
void articulation(){
46+
int root=3;
47+
int childcout=0;
48+
bool *visited=new bool[this->v];
49+
int *parent=new int[this->v];
50+
int *low=new int[this->v];
51+
int *high=new int[this->v];
52+
for(int i=0;i<this->v;i++){
53+
visited[i]=false;
54+
parent[i]=-1;
55+
low[i]=-1;
56+
high[i]=-1;
57+
}
58+
parent[root]=-1;
59+
articulationUtil(root,visited,parent,low,high,childcout,root);
60+
}
61+
};
62+
63+
int main(){
64+
65+
Graph g(8);
66+
g.addEdge(0,1);
67+
g.addEdge(0,2);
68+
g.addEdge(1,2);
69+
g.addEdge(2,3);
70+
g.addEdge(3,4);
71+
g.addEdge(4,6);
72+
g.addEdge(5,6);
73+
g.addEdge(5,4);
74+
g.addEdge(5,7);
75+
g.articulation();
76+
77+
return 0;
78+
}

Graphs/bfs_graph.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include<iostream>
2+
#include<list>
3+
using namespace std;
4+
5+
class Graph {
6+
int v;
7+
list<int> *adj;
8+
9+
public:
10+
void addEdge(int v,int w)
11+
{
12+
(this->adj)[v].push_back(w);
13+
}
14+
15+
Graph(int v){
16+
this->v=v;
17+
this->adj=new list<int>[v];
18+
}
19+
void bfs(int v){
20+
bool *visited=new bool[this->v];
21+
list<int> queue;
22+
list<int>::iterator i;
23+
list<int>::iterator j;
24+
25+
for(int i=0;i<this->v;i++){
26+
visited[i]=false;
27+
}
28+
29+
visited[v]=true;
30+
queue.push_back(v);
31+
j=queue.begin();
32+
33+
while(j!=queue.end()){
34+
for(i=(this->adj)[*j].begin();i!=(this->adj)[*j].end();i++){
35+
if(visited[(*i)]==false){
36+
visited[*i]=true;
37+
queue.push_back(*i);
38+
}
39+
}
40+
j++;
41+
}
42+
43+
for(j=queue.begin();j!=queue.end();j++){
44+
cout<<*j<<"\n";
45+
}
46+
}
47+
};
48+
49+
int main(){
50+
Graph g(4);
51+
g.addEdge(0, 1);
52+
g.addEdge(0, 2);
53+
g.addEdge(1, 2);
54+
g.addEdge(2, 0);
55+
g.addEdge(2, 3);
56+
g.addEdge(3, 3);
57+
g.bfs(0);
58+
return 0;
59+
}

Graphs/cycle_bellman_ford_graph.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include<iostream>
2+
#include<list>
3+
#include<limits>
4+
using namespace std;
5+
struct Edge{
6+
int src;
7+
int dest;
8+
int dis;
9+
};
10+
11+
class Graph{
12+
int v;
13+
int e;
14+
struct Edge * edge;
15+
public:
16+
Graph(int v,int e){
17+
this->v=v;
18+
this->e=e;
19+
this->edge=new Edge[e];
20+
}
21+
void addEdge(int i,int src,int dest,int dis){
22+
((this->edge)[i]).src=src;
23+
((this->edge)[i]).dest=dest;
24+
((this->edge)[i]).dis=dis;
25+
}
26+
bool isNegativeCycle(){
27+
int *dist=new int[this->v];
28+
for(int i=0;i<this->v;i++){
29+
dist[i]=INT_MAX;
30+
}
31+
32+
dist[0]=0;
33+
34+
for(int i=0;i<(this->v)-1;i++){
35+
for(int j=0;j<this->e;j++){
36+
int src=this->edge[j].src;
37+
int dest=this->edge[j].dest;
38+
int weight=this->edge[j].dis;
39+
if(dist[src]>dist[dest]+weight){
40+
dist[src]=dist[dest]+weight;
41+
}
42+
}
43+
}
44+
45+
int flag=0;
46+
for(int i=0;i<1;i++){
47+
for(int j=0;j<this->e;j++){
48+
int src=this->edge[j].src;
49+
int dest=this->edge[j].dest;
50+
int weight=this->edge[j].dis;
51+
if(dist[src]>dist[dest]+weight){
52+
int flag=1;
53+
dist[src]=dist[dest]+weight;
54+
break;
55+
}
56+
}
57+
}
58+
if(flag==1){
59+
return true;
60+
}
61+
return false;
62+
}
63+
};
64+
65+
int main()
66+
{
67+
Graph g(5,8);
68+
int t=0;
69+
g.addEdge(t++,0,1,-1);
70+
g.addEdge(t++,0,2,4);
71+
g.addEdge(t++,1,2,3);
72+
g.addEdge(t++,1,3,2);
73+
g.addEdge(t++,1,4,2);
74+
g.addEdge(t++,3,2,5);
75+
g.addEdge(t++,3,1,1);
76+
g.addEdge(t++,4,3,-3);
77+
if(g.isNegativeCycle()){
78+
cout<<"yes negative cycle exist"<<"\n";
79+
}else{
80+
cout<<"no negative cycle"<<"\n";
81+
}
82+
return 0;
83+
}

Graphs/cycle_color.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include<iostream>
2+
#include<list>
3+
#include<stack>
4+
// #include <stdc++.h>
5+
using namespace std;
6+
enum Color {white,grey,black};
7+
class Graph{
8+
int v;
9+
list<int> *adj;
10+
public:
11+
Graph(int v){
12+
this->v=v;
13+
(this->adj)=new list<int>[v];
14+
}
15+
16+
void addEdge(int u,int v){
17+
(this->adj)[u].push_back(v);
18+
}
19+
20+
bool isCylcicUtil(int i,int *visited){
21+
22+
visited[i]=grey;
23+
list<int>:: iterator k;
24+
for(k=this->adj[i].begin();k!=this->adj[i].end();k++){
25+
if(visited[*k]==grey){
26+
return true;
27+
}
28+
if(visited[*k]==white && isCylcicUtil(*k,visited)){
29+
return true;
30+
}
31+
}
32+
visited[i]=black;
33+
return false;
34+
}
35+
36+
bool isCylic(){
37+
int *visited=new int[this->v];
38+
for(int i=0;i<v;i++){
39+
visited[i]=white;
40+
}
41+
for(int i=0;i<v;i++){
42+
if(visited[i]==white){
43+
if(isCylcicUtil(i,visited)){
44+
return true;
45+
}
46+
}
47+
}
48+
return false;
49+
}
50+
};
51+
int main(){
52+
Graph g(4);
53+
g.addEdge(0, 1);
54+
g.addEdge(0, 2);
55+
g.addEdge(1, 2);
56+
g.addEdge(2, 0);
57+
g.addEdge(2, 3);
58+
g.addEdge(3, 0);
59+
if(g.isCylic()){
60+
cout<<"It is cylic"<<"\n";
61+
}else{
62+
cout<<"It is not cylic"<<"\n";
63+
}
64+
return 0;
65+
}

0 commit comments

Comments
 (0)