Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pratham kumar125 patch 9 #6774

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <stdio.h>

//Represent a node of the singly linked list
struct node{
int data;
struct node *next;
};

//Represent the head and tail of the singly linked list
struct node *head, *tail = NULL;

//addNode() will add a new node to the list
void addNode(int data) {
//Create a new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;

//Checks if the list is empty
if(head == NULL) {
//If list is empty, both head and tail will point to new node
head = newNode;
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail->next = newNode;
//newNode will become new tail of the list
tail = newNode;
}
}

//reverse() will the reverse the order of the list
void reverse(struct node *current) {
//Checks if list is empty
if(head == NULL) {
printf("List is empty\n");
return;
}
else{
//Checks if the next node is null, if yes then prints it.
if(current->next == NULL) {
printf("%d ", current->data);
return;
}
//Recursively calls the reverse function
reverse(current->next);
printf("%d ", current->data);
}
}

//display() will display all the nodes present in the list
void display() {
//Node current will point to head
struct node *current = head;

if(head == NULL) {
printf("List is empty\n");
return;
}
while(current != NULL) {
//Prints each node by incrementing pointer
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

int main()
{
//Add nodes to the list
addNode(1);
addNode(2);
addNode(3);
addNode(4);

printf("Original List: \n");
display();

printf("Reversed List: \n");
//Print reversed list
reverse(head);

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// The longest common subsequence in Java

class LCS_ALGO {
static void lcs(String S1, String S2, int m, int n) {
int[][] LCS_table = new int[m + 1][n + 1];

// Building the mtrix in bottom-up way
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0)
LCS_table[i][j] = 0;
else if (S1.charAt(i - 1) == S2.charAt(j - 1))
LCS_table[i][j] = LCS_table[i - 1][j - 1] + 1;
else
LCS_table[i][j] = Math.max(LCS_table[i - 1][j], LCS_table[i][j - 1]);
}
}

int index = LCS_table[m][n];
int temp = index;

char[] lcs = new char[index + 1];
lcs[index] = '\0';

int i = m, j = n;
while (i > 0 && j > 0) {
if (S1.charAt(i - 1) == S2.charAt(j - 1)) {
lcs[index - 1] = S1.charAt(i - 1);

i--;
j--;
index--;
}

else if (LCS_table[i - 1][j] > LCS_table[i][j - 1])
i--;
else
j--;
}

// Printing the sub sequences
System.out.print("S1 : " + S1 + "\nS2 : " + S2 + "\nLCS: ");
for (int k = 0; k <= temp; k++)
System.out.print(lcs[k]);
System.out.println("");
}

public static void main(String[] args) {
String S1 = "ACADB";
String S2 = "CBDA";
int m = S1.length();
int n = S2.length();
lcs(S1, S2, m, n);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# The longest common subsequence in Python


# Function to find lcs_algo
def lcs_algo(S1, S2, m, n):
L = [[0 for x in range(n+1)] for x in range(m+1)]

# Building the mtrix in bottom-up way
for i in range(m+1):
for j in range(n+1):
if i == 0 or j == 0:
L[i][j] = 0
elif S1[i-1] == S2[j-1]:
L[i][j] = L[i-1][j-1] + 1
else:
L[i][j] = max(L[i-1][j], L[i][j-1])

index = L[m][n]

lcs_algo = [""] * (index+1)
lcs_algo[index] = ""

i = m
j = n
while i > 0 and j > 0:

if S1[i-1] == S2[j-1]:
lcs_algo[index-1] = S1[i-1]
i -= 1
j -= 1
index -= 1

elif L[i-1][j] > L[i][j-1]:
i -= 1
else:
j -= 1

# Printing the sub sequences
print("S1 : " + S1 + "\nS2 : " + S2)
print("LCS: " + "".join(lcs_algo))


S1 = "ACADB"
S2 = "CBDA"
m = len(S1)
n = len(S2)
lcs_algo(S1, S2, m, n)
Original file line number Diff line number Diff line change
@@ -1,61 +1,93 @@
// Kruskal's algorithm in C++

#include <algorithm>
#include <iostream>
#include <vector>
#include <algorithm>
// Part of Cosmos by OpenGenus Foundation
using namespace std;

#define edge pair<int, int>

class Graph {
private:
vector<pair<int, edge> > G; // graph
vector<pair<int, edge> > T; // mst
int *parent;
int V; // number of vertices/nodes in graph
public:
Graph(int V);
void AddWeightedEdge(int u, int v, int w);
int find_set(int i);
void union_set(int u, int v);
void kruskal();
void print();
};
Graph::Graph(int V) {
parent = new int[V];

//i 0 1 2 3 4 5
//parent[i] 0 1 2 3 4 5
for (int i = 0; i < V; i++)
parent[i] = i;

int n, dj[100], rank[100]; //disjoint set
int findset(int a)
{
if (dj[a] != a)
return dj[a] = findset(dj[a]);
else
return a;
G.clear();
T.clear();
}
bool sameset(int a, int b)
{
return findset(a) == findset(b);
void Graph::AddWeightedEdge(int u, int v, int w) {
G.push_back(make_pair(w, edge(u, v)));
}
void unionset(int a, int b)
{
int x = findset(a), y = findset(b);
if (rank[x] > rank[y])
dj[y] = x;
else
{
dj[x] = y;
if (rank[x] == rank[y])
rank[y]++;
}
int Graph::find_set(int i) {
// If i is the parent of itself
if (i == parent[i])
return i;
else
// Else if i is not the parent of itself
// Then i is not the representative of his set,
// so we recursively call Find on its parent
return find_set(parent[i]);
}

int main()
{
using namespace std;
int e, u, v, w;
vector< pair<int, pair<int, int>>> edge; //(weight, two vertices that the edge connects)
for (int i = 0; i < n; i++)
{
dj[i] = i;
::rank[i] = 0;
}
cout << "Input Number of Edges" << endl;
cin >> e;
cout << "Input Edges (weight and then two vertices that the edge connects)" << endl;
for (int i = 0; i < e; i++)
{
cin >> u >> v >> w; //u,v,w are just temporary variables
edge.push_back({u, {v, w}});
}
sort(edge.begin(), edge.end()); //sort by edge weight
int mst = 0;
for (int i = 0; i < e; i++)
{
int x = edge[i].second.first, y = edge[i].second.second;
if (!sameset(x, y))
{
mst += edge[i].first;
unionset(x, y);
}
void Graph::union_set(int u, int v) {
parent[u] = parent[v];
}
void Graph::kruskal() {
int i, uRep, vRep;
sort(G.begin(), G.end()); // increasing weight
for (i = 0; i < G.size(); i++) {
uRep = find_set(G[i].second.first);
vRep = find_set(G[i].second.second);
if (uRep != vRep) {
T.push_back(G[i]); // add to tree
union_set(uRep, vRep);
}
cout << mst << endl;
}
}
void Graph::print() {
cout << "Edge :"
<< " Weight" << endl;
for (int i = 0; i < T.size(); i++) {
cout << T[i].second.first << " - " << T[i].second.second << " : "
<< T[i].first;
cout << endl;
}
}
int main() {
Graph g(6);
g.AddWeightedEdge(0, 1, 4);
g.AddWeightedEdge(0, 2, 4);
g.AddWeightedEdge(1, 2, 2);
g.AddWeightedEdge(1, 0, 4);
g.AddWeightedEdge(2, 0, 4);
g.AddWeightedEdge(2, 1, 2);
g.AddWeightedEdge(2, 3, 3);
g.AddWeightedEdge(2, 5, 2);
g.AddWeightedEdge(2, 4, 4);
g.AddWeightedEdge(3, 2, 3);
g.AddWeightedEdge(3, 4, 3);
g.AddWeightedEdge(4, 2, 4);
g.AddWeightedEdge(4, 3, 3);
g.AddWeightedEdge(5, 2, 2);
g.AddWeightedEdge(5, 4, 3);
g.kruskal();
g.print();
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdio.h>

// Function to calculate the GCD of two numbers
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}

// Function to check if two numbers are coprime
int areCoprime(int num1, int num2) {
return (gcd(num1, num2) == 1);
}

int main() {
int num1, num2;

printf("Enter the first number: ");
scanf("%d", &num1);

printf("Enter the second number: ");
scanf("%d", &num2);

if (areCoprime(num1, num2)) {
printf("%d and %d are coprime.\n", num1, num2);
} else {
printf("%d and %d are not coprime.\n", num1, num2);
}

return 0;
}
Loading