Skip to content

Commit 402eb3f

Browse files
committed
Merge branch 'master' of https://github.com/ephremdeme/data-structure-and-algorithms into issue-128__implement-fibonacci-series
2 parents 2533008 + 66b57f2 commit 402eb3f

File tree

9 files changed

+588
-63
lines changed

9 files changed

+588
-63
lines changed

.all-contributorsrc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,33 @@
288288
"contributions": [
289289
"code"
290290
]
291+
},
292+
{
293+
"login": "Palak-137",
294+
"name": "Lethal_Moana",
295+
"avatar_url": "https://avatars0.githubusercontent.com/u/54790525?v=4",
296+
"profile": "https://github.com/Palak-137",
297+
"contributions": [
298+
"code"
299+
]
300+
},
301+
{
302+
"login": "nikunjagarwal321",
303+
"name": "Nikunj Agarwal",
304+
"avatar_url": "https://avatars0.githubusercontent.com/u/35420775?v=4",
305+
"profile": "http://www.nikunjagarwal.social",
306+
"contributions": [
307+
"code"
308+
]
309+
},
310+
{
311+
"login": "Druffl3",
312+
"name": "Goutham R",
313+
"avatar_url": "https://avatars2.githubusercontent.com/u/28187687?v=4",
314+
"profile": "https://github.com/Druffl3",
315+
"contributions": [
316+
"code"
317+
]
291318
}
292319
],
293320
"contributorsPerLine": 7,

Algorithms/KadaneAlgorithm.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
6+
class Kadane{
7+
public:
8+
int max_sum(int p[],int n){
9+
int max_sum=p[0],max_in=p[0];
10+
for(int i=1;i<n;i++){
11+
max_sum = max(p[i],max_sum+p[i]);
12+
max_in = max(max_in,max_sum);
13+
}
14+
return max_in;
15+
}
16+
17+
void display(int p[],int n){
18+
cout<<"Element in array "<<endl;
19+
for(int i=0;i<n;i++){
20+
cout<<p[i]<<" ";
21+
}
22+
}
23+
};
24+
25+
26+
int main()
27+
{
28+
Kadane algo;
29+
int n;
30+
cout<<"Enter the size of array ";
31+
cin>>n;
32+
int p[n];
33+
for (int i=0;i<n;i++){
34+
cout<<"Enter the element in array ";
35+
cin>>p[i];
36+
}
37+
algo.display(p,n);
38+
int a = algo.max_sum(p,n);
39+
cout<<"\n Largest sum of subarray using kadane algorithm is "<<a;
40+
return 0;
41+
}

Algorithms/dijkstra_algo.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <limits.h>
2+
#include <stdio.h>
3+
4+
#define V 9
5+
6+
int minDistance(int dist[], bool sptSet[])
7+
{
8+
// Initialize min value
9+
int min = INT_MAX, min_index;
10+
11+
for (int v = 0; v < V; v++)
12+
if (sptSet[v] == false && dist[v] <= min)
13+
min = dist[v], min_index = v;
14+
15+
return min_index;
16+
}
17+
18+
// A utility function to print the constructed distance array
19+
int printSolution(int dist[], int n)
20+
{
21+
printf("Vertex Distance from Source\n");
22+
for (int i = 0; i < V; i++)
23+
printf("%d tt %d\n", i, dist[i]);
24+
}
25+
26+
27+
void dijkstra(int graph[V][V], int src)
28+
{
29+
int dist[V];
30+
bool sptSet[V];
31+
for (int i = 0; i < V; i++)
32+
dist[i] = INT_MAX, sptSet[i] = false;
33+
34+
dist[src] = 0;
35+
36+
for (int count = 0; count < V - 1; count++) {
37+
38+
int u = minDistance(dist, sptSet);
39+
40+
// Mark the picked vertex as processed
41+
sptSet[u] = true;
42+
43+
// Update dist value of the adjacent vertices of the picked vertex.
44+
for (int v = 0; v < V; v++)
45+
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX
46+
&& dist[u] + graph[u][v] < dist[v])
47+
dist[v] = dist[u] + graph[u][v];
48+
}
49+
50+
printSolution(dist, V);
51+
}
52+
53+
// driver program to test above function
54+
int main()
55+
{
56+
57+
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
58+
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
59+
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
60+
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
61+
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
62+
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
63+
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
64+
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
65+
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
66+
67+
dijkstra(graph, 0);
68+
69+
return 0;
70+
}

Arrays/maxHourGlass.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# This python3 script accepts a nxn two dimensional matrix
2+
# and calculates the maximum hourglass sum possible from it.
3+
4+
'''
5+
Example:
6+
In this 6x6 Matrix:
7+
8+
1 1 1 0 0 0
9+
0 1 0 0 0 0
10+
1 1 1 0 0 0
11+
0 0 0 1 1 1
12+
0 0 0 0 1 0
13+
0 0 0 1 1 2
14+
15+
The pattern:
16+
1 1 1
17+
0 1 0
18+
1 1 1
19+
20+
makes 1 hour glass. And the sum of this hour glass is:
21+
1 + 1 + 1 + 1 + 1 + 1 + 1 = 7
22+
Similarly we need to find sum of all hourglasses in the Matrix
23+
And print the maximum sum.
24+
'''
25+
26+
#Taking rank of the matrix from user
27+
n = int(input())
28+
l = []
29+
30+
#Converting each string of row values into list and appending it to The
31+
#main two dimensional list 'l'
32+
for i in range(n):
33+
il = list(map(int, input().split()))
34+
l.append(il)
35+
36+
#This will store our expected result
37+
finalSum = 0
38+
39+
#Creating a 3x3 grid index to iterate over the two dimensional list And
40+
#calculate sum of the hourglasses.
41+
#-2 is added to make sure not to index beyond the actual list range.
42+
for r1 in range(n-2):
43+
r2 = r1+1
44+
r3 = r2+1
45+
for c1 in range(n-2):
46+
#to store sum of all hourglasses
47+
res = 0
48+
c2 = c1+1
49+
c3 = c2+1
50+
res = l[r1][c1] + l[r1][c2] + l[r1][c3] + l[r2][c2] + l[r3][c1] + l[r3][c2] + l[r3][c3]
51+
#Maybe the first element is -ve, therefore need to store it no matter what.
52+
if r1 == 0 and c1 == 0:
53+
finalSum = res
54+
#will always store the maximum result
55+
if res > finalSum:
56+
finalSum = res
57+
58+
#print the maximum hourglass sum.
59+
print(finalSum)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
5959
</tr>
6060
<tr>
6161
<td align="center"><a href="https://github.com/swastikchugh"><img src="https://avatars0.githubusercontent.com/u/17880938?v=4" width="100px;" alt=""/><br /><sub><b>swastikchugh</b></sub></a><br /><a href="https://github.com/ephremdeme/data-structure-and-algorithms/commits?author=swastikchugh" title="Code">💻</a></td>
62+
<td align="center"><a href="https://github.com/Palak-137"><img src="https://avatars0.githubusercontent.com/u/54790525?v=4" width="100px;" alt=""/><br /><sub><b>Lethal_Moana</b></sub></a><br /><a href="https://github.com/ephremdeme/data-structure-and-algorithms/commits?author=Palak-137" title="Code">💻</a></td>
63+
<td align="center"><a href="http://www.nikunjagarwal.social"><img src="https://avatars0.githubusercontent.com/u/35420775?v=4" width="100px;" alt=""/><br /><sub><b>Nikunj Agarwal</b></sub></a><br /><a href="https://github.com/ephremdeme/data-structure-and-algorithms/commits?author=nikunjagarwal321" title="Code">💻</a></td>
64+
<td align="center"><a href="https://github.com/Druffl3"><img src="https://avatars2.githubusercontent.com/u/28187687?v=4" width="100px;" alt=""/><br /><sub><b>Goutham R</b></sub></a><br /><a href="https://github.com/ephremdeme/data-structure-and-algorithms/commits?author=Druffl3" title="Code">💻</a></td>
6265
</tr>
6366
</table>
6467

graphs/dfs.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
void dfsUtil(int currentNode, vector< vector<int> > &adj, vector<bool> &visited) {
5+
visited[currentNode] = true;
6+
cout << currentNode << " ";
7+
for(int i = 0; i < adj[currentNode].size(); i++) {
8+
int neighbour = adj[currentNode][i];
9+
if(!visited[neighbour])
10+
dfsUtil(neighbour, adj, visited);
11+
}
12+
return;
13+
}
14+
15+
void dfs(int nodes, vector< vector<int> > &adj) {
16+
vector<bool> visited(nodes+1, false);
17+
for(int i = 1; i <= nodes; i++) {
18+
if(!visited[i])
19+
dfsUtil(i, adj, visited);
20+
}
21+
return;
22+
}
23+
24+
25+
int main() {
26+
int nodes, edges, x, y;
27+
cout << "Input number of nodes and edges: ";
28+
cin >> nodes >> edges;
29+
cout << "Input edges: \n";
30+
vector< vector<int> > adj(nodes+1);
31+
for(int i = 1; i <= edges; i++) {
32+
cin >> x >> y;
33+
adj[x].push_back(y);
34+
adj[y].push_back(x);
35+
}
36+
dfs(nodes, adj);
37+
return 0;
38+
}
File renamed without changes.

sorting-algorithms/heapsort.cpp

Lines changed: 70 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,74 @@
1-
#include<bits/stdc++.h>
2-
3-
using namespace std ;
4-
5-
6-
7-
8-
void heapify(int arr[], int n, int i)
9-
{
10-
int largest = i;
11-
int l = 2*i + 1;
12-
int r = 2*i + 2;
13-
14-
if (l < n && arr[l] > arr[largest])
15-
largest = l;
16-
17-
if (r < n && arr[r] > arr[largest])
18-
largest = r;
19-
20-
if (largest != i)
21-
{
22-
swap(arr[i], arr[largest]);
23-
heapify(arr, n, largest);
24-
}
1+
#include <iostream>
2+
using namespace std;
3+
#include <iostream>
4+
using namespace std;
5+
6+
7+
void heapify(int n,int p[],int i)
8+
{
9+
//intilizing root
10+
int root = i;
11+
int left = i*2+1;
12+
int right = i*2+2;
13+
14+
15+
//finding largest element
16+
if(p[right]>p[root]&& right<n){
17+
root = right;
18+
}
19+
if(p[left]>p[root]&& left<n){
20+
root = left;
21+
}
22+
if(root != i){
23+
int temp = p[i];
24+
p[i] = p[root];
25+
p[root] = temp;
26+
27+
28+
heapify(n,p,root);
29+
30+
}
31+
}
32+
33+
void heap_sort(int p[],int n)
34+
{
35+
//starting heapify from bottom
36+
for(int i=n/2-1;i>=0;i--){
37+
heapify(n,p,i);
38+
}
39+
//deleting element from heap
40+
for(int i= n-1;i>0;i--)
41+
{
42+
//swaping to the root
43+
int temp= p[0];
44+
p[0] = p[i];
45+
p[i] = temp;
46+
//now heapify again
47+
heapify(i,p,0);
48+
}
49+
2550
}
2651

27-
void heapSort(int arr[], int n)
28-
{
29-
30-
for (int i = n / 2 - 1; i >= 0; i--)
31-
heapify(arr, n, i);
32-
33-
34-
for (int i=n-1; i>0; i--)
35-
{
36-
37-
swap(arr[0], arr[i]);
38-
39-
40-
heapify(arr, i, 0);
41-
}
42-
}
4352
int main()
4453
{
45-
int n ;
46-
cout<<"Enter Size of Array\n" ;
47-
cin>>n ;
48-
cout<<"Enter Numbers\n" ;
49-
int arr[n] ;
50-
for(int i = 0 ; i<n ; i++)
51-
{
52-
cin>>arr[i] ;
53-
}
54-
cout<<"Unsorted Array: " ;
55-
for(int i = 0 ; i<n ; i++)
56-
{
57-
cout<<arr[i]<<" " ;
58-
}
59-
cout<<endl ;
60-
cout<<"Sorted Array: " ;
61-
heapSort(arr ,n ) ;
62-
for(int i = 0 ; i<n ; i++)
63-
{
64-
cout<<arr[i]<<" " ;
65-
}
66-
67-
}
54+
int n;
55+
cout <<"enter the size of array";
56+
cin >>n;
57+
int p[n];
58+
for(int i=0;i<n;i++){
59+
cout <<"Enter the element in array : " ;
60+
cin >>p[i];
61+
}
62+
63+
heap_sort(p,n);
64+
65+
for(int i=0;i<n;i++){
66+
cout<<p[i]<<" ";
67+
}
68+
return 0;
69+
}
70+
71+
72+
73+
74+

0 commit comments

Comments
 (0)