Skip to content

Commit

Permalink
Merge branch 'Lakhankumawat:main' into dequeue
Browse files Browse the repository at this point in the history
  • Loading branch information
AyuBisht authored May 16, 2022
2 parents 9fdf0c4 + 8df75a9 commit 069f0e2
Show file tree
Hide file tree
Showing 13 changed files with 820 additions and 2 deletions.
27 changes: 27 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,33 @@
"contributions": [
"code"
]
},
{
"login": "LakshayN",
"name": "Lakshay Narula",
"avatar_url": "https://avatars.githubusercontent.com/u/67185564?v=4",
"profile": "https://github.com/LakshayN",
"contributions": [
"code"
]
},
{
"login": "SupriyaKumari08",
"name": "Supriya Kumari ",
"avatar_url": "https://avatars.githubusercontent.com/u/84588360?v=4",
"profile": "https://github.com/SupriyaKumari08",
"contributions": [
"code"
]
},
{
"login": "gp919934",
"name": "Gaurav Patel",
"avatar_url": "https://avatars.githubusercontent.com/u/86097554?v=4",
"profile": "https://github.com/gp919934",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
Expand Down
73 changes: 73 additions & 0 deletions G-Graphs/ConnectedComponentUndirectedGraph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//-------To find Connencted component in an undirected graph-------//

#include<iostream>
#include<list>

using namespace std ;

class Graph{//Graph class to create Undirected graph
int n; //number of vertex in the graph
list<int>* adj; //array pointer of adjacency list

void dfs(int n,bool checked[]); //applying dfs

public :
Graph(int n);
~Graph();
void addEdge(int v, int w); // to add an edge in graph
void conenctedComponents(); // print connected components of graph
};

void Graph::conenctedComponents(){ // print connected components of graph
bool* checked = new bool[n]; // initialized all vertices as unchecked
for (int v = 0; v < n; v++){
checked[v] =false;
}
for (int v = 0; v < n; v++){
if(checked[v]==false) { //if not checked than apply dfs
dfs(v,checked); // calling dfs function
cout<<endl;
}
}
delete[] checked;
}

void Graph::dfs(int v,bool checked[]){
checked[v] = true;
cout<<v<<"\t";
list<int>::iterator i; //for neighbouring vertex
for ( i = adj[v].begin() ; i !=adj[v].end() ; ++i){
if (!checked[*i]) // if not checked
dfs(*i,checked); // applying dfs to unchecked neighbouring vertex
}
}

Graph::Graph(int n){
this->n=n;
adj = new list<int>[n];
}

Graph::~Graph(){
delete[] adj;
}

void Graph::addEdge(int v,int w){
adj[v].push_back(w);
adj[w].push_back(v);
}

int main(){
int n,v,w;
cout<<"enter the number of vertex"<<endl;
cin>>n;
Graph g(n);
for (int i = 0; i < (n*(n-1)/2); i++)
{ cout<<"enter edge or -1 -1 to exit : ";
cin>>v>>w;
if(v==-1&&w==-1){break;}
g.addEdge(v,w);
}
cout<<"Connected Componentes are : "<<endl;
g.conenctedComponents();
return 0;
}
39 changes: 39 additions & 0 deletions G-Graphs/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
# Table of content
- [Connected components in undirected graph](#connected-components-in-undirected-graph)
- [Code](ConnectedComponentUndirectedGraph.cpp)
- [Algorithm](#algorithm)
- [Properties](#properties)
- [Advantages](#advantages)


## Connected components in undirected graph
- connected components in a undirected graph ia a subgraph in which each pair of node in the subgraph have path between them .

<!-- image to help better explain the concept -->

![Connected components in undirected graph](https://media.geeksforgeeks.org/wp-content/uploads/20190401132549/Diagram.jpg)

### Algorithm
```
ConnectedComponent(){
1. initialized all vertices as unchecked
2. for all vertices
- if not checked than apply dfs by calling dfs(v,checked[]) function .
}
dfs(v,checked[]){
1. mark v as checked and diaplay it
2. for neighbouring vertex
- if not checked apply dfs to unchecked neighbouring vertex
}
```

### Properties

- Time Complexity: O(V+E), where V is the number of vertices and E is total number edges in a graph.

- Space Complexity: O(n) , where n is total number of vertices in graph .

### Advantages
- use in the social networking sites, connected components are used to depict the group of people who are friends of each other or who have any common interest
- It can also be used to convert a graph into a Direct Acyclic graph of strongly connected components .
48 changes: 47 additions & 1 deletion R-Recursion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
- [Time Complexity](#time-complexity-7)
- [Space Complexity](#space-complexity-7)

- [Subsequence K Sum](#subsequence-k-sum)
- [Examples](#examples-7)
- [Algorithm](#algorithm-7)
- [Time Complexity](#time-complexity-8)
- [Space Complexity](#space-complexity-8)


## Inversion Count

Expand Down Expand Up @@ -411,9 +417,49 @@ Example 2 : Input: n = 27
- Another case may be if given no. is 0 we have to return false.
- Call the recursive function for n/3 times.

## Time Complexity and Space Complexity
## Time Complexity
- Time Complexity : O(N)
## Space Complexity
- Space Complexity : O(N) here N is the recursion stack space.


## Subsequence K sum

Given an array arr[] of length N and a number K, the task is to find all the subsequences of the array whose sum of elements is K using recursion.

### Examples

```
Input: arr[] = {1, 2, 3}, K = 3
Output:
1 2
3
Input: arr[] = {17, 18, 6, 11, 2, 4}, K = 6
Output:
2 4
6
```

### Algorithm

-The idea is to use the jagged array to store the subsequences of the array of different lengths.

-For every element in the array, there are mainly two choices for it that are either to include in the subsequence or not.

-Apply this for every element in the array by reducing the sum, if the element is included otherwise search for the subsequence without including it.

![image](https://user-images.githubusercontent.com/67185564/168480484-8fcc3edf-cea6-483b-a355-910da3dad3bb.png)


### Time Complexity
```
-O(n^2) as this is a recursive solution for the problem. We are using recursion to iterate over all the subsequences and then finding the desired one.
```

### Space Complexity
```
-O(n)
```
64 changes: 64 additions & 0 deletions R-Recursion/SubsequenceKSUM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@


//------------------------------------PROBLEM : Subsequence with K sum ----------------------------------

#include<iostream>
#include<vector>
using namespace std;

void generate(vector<int> arr, vector<int> output, int index, int sum, int tempSum)
{
if (index >= arr.size())
{
if (tempSum == sum)
{
//printing all subsequences
for (int i = 0; i < output.size(); i++)
{
cout << output[i] << " ";
}
cout << endl;
}
return;
}

output.push_back(arr[index]);
tempSum += arr[index];
generate(arr, output, index + 1, sum, tempSum);
output.pop_back();
tempSum -= arr[index];
generate(arr, output, index + 1, sum, tempSum);
}

int main()
{

vector<int> arr;
int n, sum;

cout << "Enter the total number of elements in the array/vector : " << endl;
cin >> n;

cout << "Enter the elements : " << endl;
for (int i = 0; i < n; i++)
{
int temp;
cin >> temp;
arr.push_back(temp);
}

cout << "Enter the subsequence sum : " << endl;
cin >> sum;
cout << "The subsequences are as follows : " << endl;

int tempSum = 0;
vector<int> output;
int index = 0;

//this prints all the subsequences
generate(arr, output, index, sum, tempSum);
//It is also possible to store all the subsequences in a 2D vector by passing it as a reference in the 'generate' function
return 0;

}

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-71-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-74-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->


Expand Down Expand Up @@ -643,6 +643,9 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
</tr>
<tr>
<td align="center"><a href="https://www.linkedin.com/in/mirette-amin-439b0281/"><img src="https://avatars.githubusercontent.com/u/29145628?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Mirette Amin</b></sub></a><br /><a href="https://github.com/Lakhankumawat/LearnCPP/commits?author=miretteamin" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/LakshayN"><img src="https://avatars.githubusercontent.com/u/67185564?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Lakshay Narula</b></sub></a><br /><a href="https://github.com/Lakhankumawat/LearnCPP/commits?author=LakshayN" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/SupriyaKumari08"><img src="https://avatars.githubusercontent.com/u/84588360?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Supriya Kumari </b></sub></a><br /><a href="https://github.com/Lakhankumawat/LearnCPP/commits?author=SupriyaKumari08" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/gp919934"><img src="https://avatars.githubusercontent.com/u/86097554?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Gaurav Patel</b></sub></a><br /><a href="https://github.com/Lakhankumawat/LearnCPP/commits?author=gp919934" title="Code">💻</a></td>
</tr>
</table>

Expand Down
68 changes: 68 additions & 0 deletions S-SortingAlgorithms/CycleSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <iostream>
using namespace std;

void cycleSort(int arr[], int n)
{
//All required variables initilized
int counter = 0,start,elem,pos,temp,i;

//Array elems are traversed to ensure that they are in correct positions
//elem represents the initial point
for (start = 0; start <= n - 2; start++) {
elem = arr[start];

//Smaller array elements to be shifted to the elem varaible's right
//to maintain the sorted order of the final array
pos = start;
for (i = start + 1; i < n; i++)
if (arr[i] < elem)
pos++;
if (pos == start)
continue;

//Ignore duplicate elements
while (elem == arr[pos])
pos += 1;
//Element swapped to keep it at right position
if (pos != start) {
temp = elem;
elem = arr[pos];
arr[pos] = temp;
counter++;
}
while (pos != start) {
pos = start;
for (i = start + 1; i < n; i++)
if (arr[i] < elem)
pos += 1;
while (elem == arr[pos])
pos += 1;
if (elem != arr[pos]) {
temp = elem;
elem = arr[pos];
arr[pos] = temp;
counter++;
}
}
}
}

//Driver method
int main()
{
//Taking input from the user
cout<<"Enter the no. of elements in array:"<<endl;
int n;
cin>>n;
int arr[n];
cout<<"Enter the elements in array:"<<endl;
for(int i=0;i<n;i++){
cin>>arr[i];
}
cycleSort(arr,n);
cout<<"Sorted array:"<<endl;
for(int i=0;i<n;i++){
cout << arr[i] << " ";
}
return 0;
}
Loading

0 comments on commit 069f0e2

Please sign in to comment.