Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9b7f1a9

Browse files
Merge branch 'master' of https://github.com/codeIIEST/Algorithms
2 parents e63903a + 4ead738 commit 9b7f1a9

File tree

23 files changed

+667
-7
lines changed

23 files changed

+667
-7
lines changed

‎CONTRIBUTORS.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Thanks for all your contributions :heart: :octocat:
1414
|[Rahul Arulkumaran](https://github.com/rahulkumaran)| [#46](https://github.com/codeIIEST/Algorithms/pull/46), [#66](https://github.com/codeIIEST/Algorithms/pull/66), [#88](https://github.com/codeIIEST/Algorithms/pull/88), [#89](https://github.com/codeIIEST/Algorithms/pull/89), [#90](https://github.com/codeIIEST/Algorithms/pull/90), [#93](https://github.com/codeIIEST/Algorithms/pull/93), [#159](https://github.com/codeIIEST/Algorithms/pull/159) | CLOSED |
1515
|[Rahul Arulkumaran](https://github.com/rahulkumaran)| [#94](https://github.com/codeIIEST/Algorithms/pull/94), [#95](https://github.com/codeIIEST/Algorithms/pull/95), [#112](https://github.com/codeIIEST/Algorithms/pull/112), [#151](https://github.com/codeIIEST/Algorithms/pull/151), [#174](https://github.com/codeIIEST/Algorithms/pull/174) | MERGED |
1616
|[Rahul Arulkumaran](https://github.com/rahulkumaran)| [#206](https://github.com/codeIIEST/Algorithms/pull/206), [#207](https://github.com/codeIIEST/Algorithms/pull/207) | OPEN |
17-
|[Abhishek Nalla](https://github.com/abhisheknalla)| [#129](https://github.com/codeIIEST/Algorithms/pull/129)| MERGED |
18-
[#147](https://github.com/codeIIEST/Algorithms/pull/147)| MERGED | [#204](https://github.com/codeIIEST/Algorithms/pull/204)| MERGED |
19-
| [Zanark ( Debashish Mishra )](https://github.com/Zanark) | [PR #222](https://github.com/codeIIEST/Algorithms/pull/222) , [PR #146](https://github.com/codeIIEST/Algorithms/pull/146) , [PR #135](https://github.com/codeIIEST/Algorithms/pull/135)| OPEN , MERGED , CLOSED|
20-
17+
|[Abhishek Nalla](https://github.com/abhisheknalla)| [#129](https://github.com/codeIIEST/Algorithms/pull/129) ,[#147](https://github.com/codeIIEST/Algorithms/pull/147)[#204](https://github.com/codeIIEST/Algorithms/pull/204)| MERGED |
18+
|[Zanark ( Debashish Mishra )](https://github.com/Zanark)|[PR #222](https://github.com/codeIIEST/Algorithms/pull/222) , [PR #146](https://github.com/codeIIEST/Algorithms/pull/146) , [PR #225](https://github.com/codeIIEST/Algorithms/pull/225) , [PR #135](https://github.com/codeIIEST/Algorithms/pull/135)| CLOSED , MERGED , MERGED , CLOSED|
19+
| [d3v3sh5ingh](https://github.com/D3v3sh5ingh) | [PR #126](https://github.com/codeIIEST/Algorithms/pull/126) , [PR #161](https://github.com/codeIIEST/Algorithms/pull/161)| Closed , MERGED|
20+
|[imVivekGupta](https://github.com/imVivekGupta)|[#173](https://github.com/codeIIEST/Algorithms/pull/173), [#182](https://github.com/codeIIEST/Algorithms/pull/182), [#218](https://github.com/codeIIEST/Algorithms/pull/218), [#230](https://github.com/codeIIEST/Algorithms/pull/230)| MERGED |
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.*;
2+
3+
4+
class Knapsack
5+
{
6+
7+
// A function to return maximum of two integers
8+
static int maximum(int a, int b)
9+
{
10+
if(a>b)
11+
return a;
12+
else
13+
return b;
14+
15+
}
16+
17+
// Returns the maximum value that can be put in a knapsack of capacity W
18+
static int knapSack(int W, int wt[], int val[], int n)
19+
{
20+
int i, w;
21+
int K[][] = new int[n+1][W+1];
22+
23+
// Build table K[][] in bottom up manner
24+
for (i = 0; i <= n; i++)
25+
{
26+
for (w = 0; w <= W; w++)
27+
{
28+
if (i==0 || w==0)
29+
K[i][w] = 0;
30+
else if (wt[i-1] <= w)
31+
K[i][w] = maximum(val[i-1] + K[i-1][w-wt[i-1]] , K[i1][w]);
32+
else
33+
K[i][w] = K[i-1][w];
34+
}
35+
}
36+
37+
Return K[n][W];
38+
}
39+
40+
41+
42+
public static void main(String args[])
43+
{
44+
45+
Scanner s=new Scanner(System.in);
46+
System.out.println("Enter number of objects");
47+
int n=s.nextInt();
48+
int val[]=new int[n];
49+
int wt[] = new int[n];
50+
System.out.println("Enter values of objects");
51+
52+
53+
for(int i=0;i<n;i++)
54+
{
55+
val[i]=s.nextInt();
56+
}
57+
58+
System.out.println("Enter weights of object");
59+
for(int i=0;i<n;i++)
60+
{
61+
wt[i]=s.nextInt();
62+
}
63+
64+
65+
System.out.println("Enter the maximum weight");
66+
int W = s.nextInt();
67+
System.out.println(knapSack(W, wt, val, n));
68+
}
69+
}
70+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 0-1 Knapsack
2+
The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most valuable items.
3+
4+
**Time Complexity**
5+
Time Complexity: O(nW) where n is the number of items and W is the capacity of knapsack.
6+
### Applications
7+
Knapsack problems appear in real-world decision-making processes in a wide variety of fields, such as finding the least wasteful way to cut raw materials,[4] selection of investments and portfolios selection of assets for asset-backed securitization and generating keys for the Merkle–Hellman and other knapsack cryptosystems
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Problem :-
2+
===
3+
Given a value N and the values of different denomination,find the number of ways to make changes for N cents,if we have infinite supply of all denominations.
4+
5+
Input:-
6+
---
7+
Size of an array and all the values in the array and the number of cents.
8+
9+
Output :-
10+
---
11+
An integer that denotes the number of ways to make change.
12+
13+
#### Language : `C++`
14+
15+
#### Algorithm Paradigm : `Dynamic Programming`
16+
17+
#### Time Complexity : `O(N*M)`
18+
19+
#### Space Complexity : `O(N*M)`
20+
21+
Working :-
22+
---
23+
An arr array stores the different currency values.
24+
Another array temp stores the best values for sub-problems.For eg: `temp[3][4]` stores the optimal value for the number of ways
25+
to make change if the number of cents(N) were 4 and we only had `arr[0]`,`arr[1]` and `arr[2]` as the different values of currency.
26+
27+
By using dynamic programming we are bringing the time complexity down from exponentional(in case of brute force solution)
28+
to polynomial.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//Coin change problem using dynamic programming in C++.
2+
#include<bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
int M;//Variable to store the number of different currency values.
9+
cout<<"Enter the number of denominations : ";
10+
cin>>M;//Inputting the number of different currency value from user.
11+
cout<<endl;
12+
13+
int arr[M];//Array to store different currency values.
14+
for(int i=0;i<M;i++)
15+
{
16+
cout<<"Enter the value of denominaion "<<i+1<<" : ";
17+
cin>>arr[i];//Inputting the value of each currency from user.
18+
}
19+
cout<<endl;
20+
21+
int N;//Variable to store the number of cents whose number of ways to make change is to be found.
22+
cout<<"Enter the number of cents : ";
23+
cin>>N;//Inputting the number of cents from user.
24+
cout<<endl;
25+
26+
/*2D array to store the optimal value for sub-cases.*/
27+
int temp[M+1][N+1];
28+
29+
for(int i=0;i<M+1;i++)//Implementing the algorithm.
30+
{
31+
for(int j=0;j<N+1;j++)
32+
{
33+
if(i==0 || j==0)
34+
temp[i][j]=0;
35+
else if(j<arr[i-1])
36+
temp[i][j]=temp[i-1][j];
37+
else if(j==arr[i-1])
38+
temp[i][j]=1+temp[i-1][j];
39+
else
40+
temp[i][j]=temp[i-1][j]+temp[i][j-arr[i-1]];
41+
}
42+
}
43+
44+
cout<<"Number of ways to make change for "<<N<<" cent is "<<temp[M][N]<<endl;//Printing the final answer.
45+
46+
return 0;
47+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
4+
#define md 1000000007
5+
#define ll long long int
6+
#define vi vector<int>
7+
#define vll vector<i64>
8+
#define pb push_back
9+
#define all(c) (c).begin(),(c).end()
10+
template< class T > T max2(const T &a,const T &b) {return (a < b ? b : a);}
11+
template< class T > T min2(const T &a,const T &b) {return (a > b ? b : a);}
12+
template< class T > T max3(const T &a, const T &b, const T &c) { return max2(a, max2(b, c)); }
13+
template< class T > T min3(const T &a, const T &b, const T &c) { return min2(a, min2(b, c)); }
14+
template< class T > T gcd(const T a, const T b) { return (b ? gcd<T>(b, a%b) : a); }
15+
template< class T > T lcm(const T a, const T b) { return (a / gcd<T>(a, b) * b); }
16+
template< class T > T mod(const T &a, const T &b) { return (a < b ? a : a % b); }
17+
typedef pair<ll,ll> pi;
18+
typedef struct
19+
{
20+
int x;
21+
int y;
22+
}point;
23+
int crossprod(point points[],int a,int b,int c)//This function calculates the crossproduct of the given two vectors . one vector is ab and other is ac.
24+
{
25+
point current = points[a];
26+
point target = points[b];
27+
point temp = points[c];
28+
int y1 = target.y-current.y;
29+
int x1 = target.x-current.x;
30+
int x2 = temp.x-current.x;
31+
int y2 = temp.y-current.y;
32+
return y2*x1-x2*y1;
33+
}
34+
int closer(point points[],int a,int b,int c)//This function calculates the closer of the given two points from a and returns the index of the closer one.
35+
{
36+
point current = points[a];
37+
point target = points[b];
38+
point temp = points[c];
39+
double dist1 = sqrt((current.x-target.x)*(current.x-target.x)-(current.y-target.y)*(current.y-target.y));
40+
double dist2 = sqrt((current.x-temp.x)*(current.x-temp.x)-(current.y-temp.y)*(current.y-temp.y));
41+
if(dist1<dist2)
42+
return b;
43+
else
44+
return c;
45+
}
46+
int main()
47+
{
48+
int n;//no of points
49+
cin>>n;
50+
point points[n];
51+
for(int i=0;i<n;i++)
52+
{
53+
cin>>points[i].x>>points[i].y;
54+
}
55+
//now we look for the leftmost point
56+
int index=0,mini=100000;
57+
for(int i=0;i<n;i++)
58+
{
59+
if(points[i].x<mini)
60+
{
61+
mini=points[i].x;
62+
index=i;
63+
}
64+
}
65+
set<int> result;
66+
67+
result.insert(index);//we add the left most point in the result array
68+
int current = index;//now our index becomes the index to the current point
69+
vi colinear;
70+
while(true)
71+
{
72+
int target = 0 ;//consider the starting target to be point with index zero
73+
for(int i=1;i<n;i++)
74+
{
75+
76+
if(current==i)//if the current point and the point we are going to compare are the same then nothing below should be executed
77+
{
78+
continue;
79+
}
80+
else
81+
{
82+
int val = crossprod(points,current,target,i);
83+
if(val > 0)//If the cross prod value is greater than zero than this means that we the point i is in the left side of the line segement that passes through the target and current so now the target needs to be changed.
84+
{
85+
86+
target = i;
87+
colinear.clear();//we also need to clear the colinear array as the target itself is changed.
88+
}
89+
if(val == 0)
90+
{
91+
int close = closer(points,current,target,i);//This gives the index of the closer point.
92+
if(close == target)
93+
target = i;
94+
colinear.pb(close);
95+
}
96+
}
97+
}
98+
int len = colinear.size();
99+
for(int k = 0;k<len;k++)
100+
101+
colinear.clear();
102+
if(target == index)//we should end our search if our target becomes equal to the first index we added in the result
103+
{
104+
break;
105+
}
106+
else
107+
{
108+
109+
result.insert(target);
110+
current = target;
111+
}
112+
}
113+
int len = result.size();
114+
set<int> :: iterator i= result.begin();
115+
for(;i!=result.end();++i)
116+
{
117+
cout<<points[(*i)].x<<" "<<points[(*i)].y<<endl;
118+
}
119+
120+
121+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Convex Hull (Jarvis March)
2+
3+
Given a set of points in the plane. the convex hull of the set is the smallest convex polygon that contains all the points of it.
4+
5+
![alt text](http://www.geeksforgeeks.org/wp-content/uploads/convexHull1.png)
6+
7+
The idea of Jarvis’s Algorithm is simple, we start from the leftmost point (or point with minimum x coordinate value) and we keep wrapping points in counterclockwise direction.
8+
The big question is, given a point p as current point, how to find the next point in output? The idea is to use orientation() here.
9+
Next point is selected as the point that beats all other points at counterclockwise orientation, i.e., next point is q if for any other point r, we have "orientation(p, r, q) = counterclockwise". Following is the detailed algorithm.
10+
11+
1) Initialize p as leftmost point.
12+
2) Do following while we don’t come back to the first (or leftmost) point.
13+
.....a) The next point q is the point such that the triplet (p, q, r) is counterclockwise for any other point r.
14+
.....b) next[p] = q (Store q as next of p in the output convex hull).
15+
.....c) p = q (Set p as q for next iteration).
16+
17+
The below Gif may help:
18+
19+
![alt text](https://upload.wikimedia.org/wikipedia/commons/thumb/9/9c/Animation_depicting_the_gift_wrapping_algorithm.gif/330px-Animation_depicting_the_gift_wrapping_algorithm.gif)

‎Competitive Coding/Graphs/Graph_Search/DepthFIrstSearch/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,29 @@ This is the algorithm for Depth First Search in a given graph.
1010
* Now we pop an element from a stack and push all its vertexes in the stack , and we also mark down these vertexes as visited.
1111
* After executing the code, we will get the vertex visited in a depth first manner.
1212

13+
Some real life applications of DFS are as follows:
14+
1) For an unweighted graph, DFS traversal of the graph produces the minimum spanning tree and all pair shortest path tree.
15+
16+
2) Detecting cycle in a graph
17+
A graph has cycle if and only if we see a back edge during DFS. So we can run DFS for the graph and check for back edges. (See this for details)
18+
19+
3) Path Finding
20+
We can specialize the DFS algorithm to find a path between two given vertices u and z.
21+
i) Call DFS(G, u) with u as the start vertex.
22+
ii) Use a stack S to keep track of the path between the start vertex and the current vertex.
23+
iii) As soon as destination vertex z is encountered, return the path as the
24+
contents of the stack
25+
26+
See this for details.
27+
28+
4) Topological Sorting
29+
Topological Sorting is mainly used for scheduling jobs from the given dependencies among jobs. In computer science, applications of this type arise in instruction scheduling, ordering of formula cell evaluation when recomputing formula values in spreadsheets, logic synthesis, determining the order of compilation tasks to perform in makefiles, data serialization, and resolving symbol dependencies in linkers [2].
30+
31+
5) To test if a graph is bipartite
32+
We can augment either BFS or DFS when we first discover a new vertex, color it opposited its parents, and for each other edge, check it doesn’t link two vertices of the same color. The first vertex in any connected component can be red or black! See this for details.
33+
34+
6) Finding Strongly Connected Components of a graph A directed graph is called strongly connected if there is a path from each vertex in the graph to every other vertex. (See this for DFS based algo for finding Strongly Connected Components)
35+
36+
7) Solving puzzles with only one solution, such as mazes. (DFS can be adapted to find all solutions to a maze by only including nodes on the current path in the visited set.)
37+
1338
[more info](https://en.wikipedia.org/wiki/Depth-first_search)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /