-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from PratyushaThumiki/master
Added C++ Programming Files
- Loading branch information
Showing
5 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include<iostream> | ||
#include<list> | ||
using namespace std; | ||
class Graph{ | ||
int v; | ||
list<int> *adj; | ||
public: Graph(int v); | ||
void add_edge(int v,int num); | ||
void bfs(int source); | ||
}; | ||
Graph::Graph(int v){ | ||
this->v=v; | ||
adj=new list<int>[v]; | ||
} | ||
void Graph::add_edge(int v,int num){ | ||
adj[v].push_back(num); | ||
} | ||
void Graph::bfs(int source){ | ||
bool *visited=new bool[v]; | ||
for(int i=0;i<v;i++){ | ||
visited[i]=false; | ||
} | ||
list<int> queue; | ||
visited[source]=true; | ||
queue.push_back(source); | ||
list<int>::iterator it; | ||
while(!queue.empty()){ | ||
source=queue.front(); | ||
cout<<source<<" "; | ||
queue.pop_front(); | ||
for(it=adj[source].begin();it!=adj[source].end();++it){ | ||
if(!visited[*it]){ | ||
visited[*it]=true; | ||
queue.push_back(*it); | ||
} | ||
} | ||
} | ||
} | ||
int main(){ | ||
Graph g(4); | ||
g.add_edge(0, 1); | ||
g.add_edge(0, 2); | ||
g.add_edge(1, 2); | ||
g.add_edge(2, 0); | ||
g.add_edge(2, 3); | ||
g.add_edge(3, 3); | ||
g.bfs(2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include<iostream> | ||
#include<list> | ||
using namespace std; | ||
class Graph{ | ||
int n; | ||
list<int> *adj; | ||
public: Graph(int n); | ||
void dfsUtil(int v,bool visited[]); | ||
void add_edge(int v,int num); | ||
void dfs(int v); | ||
}; | ||
Graph::Graph(int n){ | ||
this->n=n; | ||
adj=new list<int>[n]; | ||
} | ||
void Graph::add_edge(int v,int num){ | ||
adj[v].push_back(num); | ||
} | ||
void Graph::dfsUtil(int v,bool visited[]){ | ||
visited[v]=true; | ||
cout<<v<<" "; | ||
list<int>::iterator it; | ||
for(it=adj[v].begin();it!=adj[v].end();it++) | ||
if(!visited[*it]) | ||
dfsUtil(*it,visited); | ||
} | ||
void Graph::dfs(int v){ | ||
bool *visited=new bool[n]; | ||
for(int i=0;i<n;i++) | ||
visited[i]=false; | ||
dfsUtil(v,visited); | ||
} | ||
|
||
int main(){ | ||
Graph g(4); | ||
g.add_edge(0, 1); | ||
g.add_edge(0, 2); | ||
g.add_edge(1, 2); | ||
g.add_edge(2, 0); | ||
g.add_edge(2, 3); | ||
g.add_edge(3, 3); | ||
g.dfs(2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// Returns Length of the required subarray | ||
int maxLen(int arr[], int n) | ||
{ | ||
// Map to store the previous sums | ||
unordered_map<int, int> presum; | ||
|
||
int sum = 0; // Initialize the sum of elements | ||
int max_len = 0; // Initialize result | ||
|
||
// Traverse through the given array | ||
for (int i = 0; i < n; i++) { | ||
// Add current element to sum | ||
sum += arr[i]; | ||
|
||
if (arr[i] == 0 && max_len == 0) | ||
max_len = 1; | ||
if (sum == 0) | ||
max_len = i + 1; | ||
|
||
// Look for this sum in Hash table | ||
if (presum.find(sum) != presum.end()) { | ||
// If this sum is seen before, then update max_len | ||
max_len = max(max_len, i - presum[sum]); | ||
} | ||
else { | ||
// Else insert this sum with index in hash table | ||
presum[sum] = i; | ||
} | ||
} | ||
|
||
return max_len; | ||
} | ||
int main() | ||
{ | ||
int arr[] = { 15, -2, 2, -8, 1, 7, 10, 23 }; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
cout << "Length of the longest 0 sum subarray is " | ||
<< maxLen(arr, n); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
void sortArr(int arr[], int n) | ||
{ | ||
multimap<int, int> map; | ||
|
||
for (int i = 0; i < n; i++) { | ||
int count = 0; | ||
int k = arr[i]; | ||
|
||
// Counting no of setBits in arr[i] | ||
while (k) { | ||
k = k & k - 1; | ||
count++; | ||
} | ||
|
||
// The count is subtracted from 32 | ||
// because the result needs | ||
// to be in descending order | ||
map.insert(make_pair(32 - count, arr[i])); | ||
} | ||
|
||
// Printing the numbers in descending | ||
// order of set bit count | ||
for (auto it = map.begin(); it != map.end(); it++) { | ||
cout << (*it).second << " "; | ||
} | ||
} | ||
int main() | ||
{ | ||
int arr[] = { 5, 2, 3, 9, 4, 6, 7, 15, 32 }; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
|
||
sortArr(arr, n); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#define v 9 | ||
void min_distance(int dist[v],bool trset[v]){ | ||
int min_dis=INT_MAX, min_index; | ||
for(int i=0;i<v;i++){ | ||
if(dist[i]<=min && sprset[i]==false){ | ||
min_dis=dist[i]; | ||
min_index=i; | ||
} | ||
} | ||
return min_index; | ||
} | ||
void print_sol(int dis[v]){ | ||
cout<<”Vertex”<<”\t\tDistance from source”; | ||
for(int i=0;i<v;i++) | ||
cout<<i<<” “<<dist[i]<<endl; | ||
} | ||
void dijkstra(int graph[v][v],int src){ | ||
int dist[v]; | ||
bool trset[v]; | ||
for(int i=0;i<v;i++){ | ||
dist[v]=INT_MAX; | ||
trset[i]=false; | ||
} | ||
dist[src]=0; | ||
for(int i=0;i<v-1;i++){ | ||
int u=min_distance(dist,trset); | ||
trset[u]=true; | ||
for(int j=0;j<v;j++){ | ||
if(!trset[j] && graph[u][j] && dist[u]!=INT_MAX&& dist[u]+graph[u][j]<dist[j]) | ||
dist[j] = dist[u]+graph[u][j]; | ||
} | ||
print_sol(dist); | ||
} | ||
int main(){ | ||
int graph[v][v]={ { 0, 4, 0, 0, 0, 0, 0, 8, 0 }, | ||
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 }, | ||
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 }, | ||
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 }, | ||
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 }, | ||
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 }, | ||
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 }, | ||
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 }, | ||
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } }; | ||
|
||
dijkstra(graph, 0); | ||
} |