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

ADDED GRAPH ALGORITHM #16

Open
wants to merge 1 commit 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
54 changes: 54 additions & 0 deletions Graph Algorithms/Strongly Connected Component.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//(Detection of Directed Cycle in a connected component): http://codeforces.com/contest/505/problem/D
vector<int> g[N], newg[N], rg[N], todo;
int comp[N], indeg[N];
bool vis[N];
vector<int> gr[N];

void dfs(int k)
{
vis[k]=1;
for(auto it:g[k])
{
if(!vis[it])
dfs(it);
}
todo.push_back(k);
}

void dfs2(int k, int val)
{
comp[k]=val;
for(auto it:rg[k])
{
if(comp[it]==-1)
dfs2(it, val);
}
}

void sccAddEdge(int from, int to)
{
g[from].push_back(to);
rg[to].push_back(from);
}

void scc()
{
for(int i=1;i<=n;i++)
comp[i]=-1;

for(int i=1;i<=n;i++)
{
if(!vis[i])
dfs(i);
}

reverse(todo.begin(), todo.end());

for(auto it:todo)
{
if(comp[it]==-1)
{
dfs2(it, ++grp);
}
}
}
54 changes: 54 additions & 0 deletions Recursion, Backtracking etc/K-Combinations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Petar 'PetarV' Velickovic
Algorithm: K-Combinations
*/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
using namespace std;
typedef long long lld;

int n, k;
int skup[100];
bool inSet[100];

//Program koji generise sve kombinacije od po K elemenata datog skupa
//Slozenost: O((n choose k))

void kCombinations(int pos, int amt)
{
if (n-pos<k-amt) return;
if (amt==k)
{
for (int i=0;i<n;i++) if (inSet[i]) printf("%d ",skup[i]);
printf("\n");
return;
}
inSet[pos] = true;
kCombinations(pos+1,amt+1);
inSet[pos] = false;
kCombinations(pos+1,amt);
}

int main()
{
n = 5, k = 2;
skup[0] = 1;
skup[1] = 2;
skup[2] = 3;
skup[3] = 4;
skup[4] = 5;
kCombinations(0, 0);
return 0;
}
57 changes: 57 additions & 0 deletions Recursion, Backtracking etc/Permutations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Petar 'PetarV' Velickovic
Algorithm: Permutations
*/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
using namespace std;
typedef long long lld;

int n;
int niz[100];
bool inPerm[100];
int currPerm[100];

//Algoritam koji generise sve permutacije datog niza
//Slozenost: O(n!)

void generatePermutations(int pos)
{
if (pos == n)
{
for (int i=0;i<n;i++) printf("%d ",currPerm[i]);
printf("\n");
}
for (int i=0;i<n;i++)
{
if (!inPerm[i])
{
currPerm[pos] = niz[i];
inPerm[i] = true;
generatePermutations(pos+1);
inPerm[i] = false;
}
}
}

int main()
{
n = 3;
niz[0] = 1;
niz[1] = 2;
niz[2] = 3;
generatePermutations(0);
return 0;
}
51 changes: 51 additions & 0 deletions Recursion, Backtracking etc/Power Set.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Petar 'PetarV' Velickovic
Algorithm: Power Set
*/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
using namespace std;
typedef long long lld;

//Metoda koja generise partitivni skup nekog skupa
//Slozenost: O(2^n)

int n;
int skup[100];
bool inSet[100];

void powerSet(int pos)
{
if (pos==n)
{
for (int i=0;i<n;i++) if (inSet[i]) printf("%d ",skup[i]);
printf("\n");
return;
}
inSet[pos] = false;
powerSet(pos+1);
inSet[pos] = true;
powerSet(pos+1);
}

int main()
{
n = 3;
skup[0] = 1;
skup[1] = 2;
skup[2] = 3;
powerSet(0);
return 0;
}