Skip to content

Commit 404b0da

Browse files
Merge branch 'main' into main
2 parents 2f8dcf1 + 5bebaf9 commit 404b0da

File tree

503 files changed

+12284
-401
lines changed

Some content is hidden

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

503 files changed

+12284
-401
lines changed

Add-digit-dp-problem.java

Lines changed: 0 additions & 166 deletions
This file was deleted.

BFS_AND_DFS.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int v; //No. of Vertices
5+
6+
void Add_edge(vector<int> adj[], int a, int b)
7+
{
8+
adj[a].push_back(b);
9+
}
10+
11+
/************************************* ALGORITHM FOR BREADTH FIRST SEARCH ******************************************************/
12+
13+
// Take the empty queue and bool type array (visit) initialise with FALSE.
14+
// Push the starting node in the queue and set the value TRUE for this node in visited array.
15+
// Pop out the front node of the queue and print the node.
16+
// Push the adjacent node of pop node in queue which are not visited. Set the value TRUE in visited array of adding node.
17+
// Repeat step 3 and 4 until the queue becomes empty.
18+
19+
void bfs(int n, vector<int>adj[])
20+
{
21+
vector<bool> visit(v,false);
22+
queue<int> q;
23+
q.push(n);
24+
visit[n]=true;
25+
while(!q.empty())
26+
{
27+
n=q.front();
28+
cout<<n<<" ";
29+
q.pop();
30+
for(int i=0;i<adj[n].size();i++)
31+
{
32+
if(!visit[adj[n][i]])
33+
{
34+
q.push(adj[n][i]);
35+
visit[adj[n][i]]=true;
36+
}
37+
}
38+
}
39+
}
40+
41+
/************************************* ALGORITHM FOR DEPTH FIRST SEARCH ******************************************************/
42+
43+
// Take the empty stack and bool type array (visit) initialise with FALSE.
44+
// Push the starting node in the stack and set the value TRUE for this node in visited array.
45+
// Pop the top node from the stack and print that node.
46+
// Push the adjacent node of pop node in the stack which is not visited. Set the value TRUE in visited array of adding node.
47+
// Repeat step 3 and 4 until the stack becomes empty.
48+
49+
50+
void dfs(int n, vector<int> adj[])
51+
{
52+
vector<bool> visit(v,false);
53+
stack<int> s;
54+
s.push(n);
55+
visit[n]=true;
56+
while(!s.empty())
57+
{
58+
n=s.top();
59+
cout<<n<<" ";
60+
s.pop();
61+
for(int i=0;i<adj[n].size();i++)
62+
{
63+
if(!visit[adj[n][i]])
64+
{
65+
s.push(adj[n][i]);
66+
visit[adj[n][i]]=true;
67+
}
68+
}
69+
}
70+
71+
}
72+
73+
int main()
74+
{
75+
cout<<"Enter the no. of vertices : ";
76+
cin>>v;
77+
vector<int> adj[v];
78+
vector<bool> visit(v,false);
79+
Add_edge(adj,0,1);
80+
Add_edge(adj,0,2);
81+
Add_edge(adj,1,2);
82+
Add_edge(adj,2,0);
83+
Add_edge(adj,2,3);
84+
Add_edge(adj,3,3);
85+
86+
int temp;
87+
cout<<"Choose the vertex from you want to start traversing : ";
88+
cin>>temp;
89+
cout<<"\nBFS traversal is"<<" ";
90+
bfs(temp,adj);
91+
cout<<"\nDFS traversal is"<<" ";
92+
dfs(temp,adj);
93+
94+
return 0;
95+
}

CPP/Greedy_algorithm.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// All denominations of Indian Currency
5+
int deno[] = { 1, 2, 5, 10, 20, 50, 100, 500, 1000 };
6+
int n = sizeof(deno) / sizeof(deno[0]);
7+
8+
// Driver program
9+
void findMin(int V)
10+
{
11+
// Initialize result
12+
vector<int> ans;
13+
14+
// Traverse through all denomination
15+
for (int i = n - 1; i >= 0; i--) {
16+
// Find denominations
17+
while (V >= deno[i]) {
18+
V -= deno[i];
19+
ans.push_back(deno[i]);
20+
}
21+
}
22+
23+
// Print result
24+
for (int i = 0; i < ans.size(); i++)
25+
cout << ans[i] << " ";
26+
}
27+
28+
// Driver program
29+
int main()
30+
{
31+
int n = 93;
32+
cout << "Following is minimal number of change for " << n << " is ";
33+
findMin(n);
34+
return 0;
35+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
7+
8+
struct Node
9+
{
10+
int data;
11+
struct Node *next;
12+
};
13+
14+
struct Node *head;
15+
16+
void
17+
Insert (int data)
18+
{
19+
struct Node *temp = (struct Node *) malloc (sizeof (struct Node *));
20+
21+
22+
temp->data = data;
23+
temp->next = NULL;
24+
25+
if (head == NULL)
26+
{
27+
head = temp;
28+
return;
29+
}
30+
31+
struct Node *temp2 = head;
32+
while (temp2->next != NULL)
33+
{
34+
temp2 = temp2->next;
35+
}
36+
temp2->next = temp;
37+
}
38+
39+
void
40+
Print ()
41+
{
42+
struct Node *temp = head;
43+
while (temp != NULL)
44+
{
45+
printf (" %d", temp->data);
46+
temp = temp->next;
47+
}
48+
printf ("\n");
49+
50+
}
51+
52+
53+
int
54+
main ()
55+
{
56+
head = NULL;
57+
Insert (4);
58+
Insert (6);
59+
Insert (8);
60+
Insert (2);
61+
Print ();
62+
63+
return 0;
64+
}
65+

0 commit comments

Comments
 (0)