-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b71e9c6
Showing
19 changed files
with
4,969 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package rzr; | ||
|
||
class Node{ | ||
int key; | ||
Node parent; | ||
int rank; | ||
} | ||
|
||
class DisjointSets{ | ||
static Node makeSet(int k) | ||
{ | ||
Node x=new Node(); | ||
x.key=k; | ||
x.parent=x; | ||
x.rank=0; | ||
return x; | ||
} | ||
static Node findSet(Node x) | ||
{ | ||
if(x!=x.parent) | ||
{ | ||
x.parent= findSet(x.parent); | ||
} | ||
return x.parent; | ||
} | ||
static void union(Node x, Node y){ | ||
if(y.rank > x.rank) | ||
{ | ||
x.parent = y; | ||
} | ||
else | ||
{ | ||
y.parent = x; | ||
if(x.rank==y.rank) | ||
{ | ||
x.rank++; | ||
} | ||
} | ||
} | ||
|
||
static void printPath(Node x,String s) | ||
{ | ||
if(x.parent==x) | ||
{ | ||
System.out.println(s+ x.key+"("+x.rank+ ")"); | ||
return; | ||
} | ||
printPath(x.parent,s+x.key+"->"); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Node z1=makeSet(1); | ||
Node z2=makeSet(2); | ||
Node z3=makeSet(3); | ||
Node z4=makeSet(4); | ||
Node z5=makeSet(5); | ||
Node z6=makeSet(6); | ||
Node z7=makeSet(7); | ||
Node z8=makeSet(8); | ||
Node z9=makeSet(9); | ||
|
||
union(findSet(z1),findSet(z2)); | ||
union(findSet(z3),findSet(z4)); | ||
union(findSet(z5),findSet(z4)); | ||
union(findSet(z1),findSet(z5)); | ||
union(findSet(z6),findSet(z7)); | ||
union(findSet(z8),findSet(z9)); | ||
union(findSet(z6),findSet(z8)); | ||
union(findSet(z7),findSet(z4)); | ||
|
||
printPath(z1,""); | ||
printPath(z2,""); | ||
printPath(z3,""); | ||
printPath(z4,""); | ||
printPath(z5,""); | ||
printPath(z6,""); | ||
printPath(z7,""); | ||
printPath(z8,""); | ||
printPath(z9,""); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
class Node{ | ||
int key; | ||
int rank; | ||
Node parent; | ||
} | ||
class DisjointSets{ | ||
public static Node findSet(Node x){ | ||
if (x != x.parent){ | ||
x.parent = findSet(x.parent); | ||
} | ||
return x.parent; | ||
} | ||
static void union(Node x, Node y){ | ||
if(y.rank > x.rank) | ||
{ | ||
x.parent = y; | ||
} | ||
else | ||
{ | ||
y.parent = x; | ||
if(x.rank==y.rank) | ||
{ | ||
x.rank++; | ||
} | ||
} | ||
} | ||
static Node makeSet(int k) | ||
{ | ||
Node x=new Node(); | ||
x.key=k; | ||
x.parent=x; | ||
x.rank=0; | ||
return x; | ||
} | ||
} | ||
class Graph{ | ||
List<Integer> vertices = new ArrayList<>(); | ||
List<Edge> edges = new ArrayList<>(); | ||
|
||
void addVertex(int value){ | ||
vertices.add(value); | ||
} | ||
void addEdge(int u, int v, int weight){ | ||
edges.add(new Edge(u, v, weight)); | ||
} | ||
void sortEdges(){ | ||
Collections.sort(edges, new Comparator<Edge>(){ | ||
public int compare(Edge e1, Edge e2){ | ||
return e1.weight-e2.weight; | ||
} | ||
}); | ||
} | ||
} | ||
class Edge{ | ||
int u; | ||
int v; | ||
int weight; | ||
|
||
public Edge(int u, int v, int weight){ | ||
this.u = u; | ||
this.v = v; | ||
this.weight = weight; | ||
} | ||
|
||
void printEdge(){ | ||
System.out.println(this.u + "---"+ this.v+ " waga: " + this.weight); | ||
} | ||
} | ||
class Kruskal{ | ||
static void kruskal(Graph graph){ | ||
Node[] vertices = new Node[graph.vertices.size()+1]; | ||
for (int i = 0; i < graph.vertices.size(); i++){ | ||
vertices[graph.vertices.get(i)] = | ||
DisjointSets.makeSet(graph.vertices.get(i)); | ||
} | ||
graph.sortEdges(); | ||
for (Edge edge : graph.edges){ | ||
Node ru=DisjointSets.findSet(vertices[edge.u]); | ||
Node rv=DisjointSets.findSet(vertices[edge.v]); | ||
if (ru != rv){ | ||
edge.printEdge(); | ||
DisjointSets.union(ru,rv); | ||
} | ||
} | ||
} | ||
public static void main(String[] args){ | ||
Graph graph = new Graph(); | ||
|
||
graph.addVertex(1); | ||
graph.addVertex(2); | ||
graph.addVertex(3); | ||
graph.addVertex(4); | ||
graph.addVertex(5); | ||
graph.addVertex(6); | ||
graph.addVertex(7); | ||
|
||
graph.addEdge(1, 2, 1); | ||
graph.addEdge(1, 7, 6); | ||
graph.addEdge(1, 6, 6); | ||
graph.addEdge(2, 3, 2); | ||
graph.addEdge(2, 7, 8); | ||
graph.addEdge(3, 7, 8); | ||
graph.addEdge(3, 4, 3); | ||
graph.addEdge(4, 7, 10); | ||
graph.addEdge(4, 5, 4); | ||
graph.addEdge(4, 7, 10); | ||
graph.addEdge(5, 6, 5); | ||
graph.addEdge(6, 7, 10); | ||
|
||
Kruskal.kruskal(graph); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
|
||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
#define N 20 | ||
int c[N][N]; | ||
char b[N][N]; | ||
void LCSLength(string X, string Y){ | ||
int m = X.length(); | ||
int n = Y.length(); | ||
int i, j; | ||
|
||
for(i = 0; i <= m; i++) c[i][0] = 0; | ||
for(j = 0; j <= n; j++) c[0][j] = 0; | ||
|
||
for(i = 1; i <= m; i++) | ||
for(j = 1; j <= n; j++) | ||
if(X[i-1] == Y[j-1]) | ||
{ | ||
c[i][j] = c[i-1][j-1] + 1; | ||
b[i][j] = '\\'; | ||
} | ||
else | ||
if(c[i-1][j] >= c[i][j-1]) | ||
{ | ||
c[i][j] = c[i-1][j]; | ||
b[i][j] = '|'; | ||
} | ||
else | ||
{ | ||
c[i][j] = c[i][j-1]; | ||
b[i][j] = '-'; | ||
} | ||
} | ||
void PrintLCS(string x, string y, int i, int j){ | ||
if(i == 0 || j == 0) | ||
return; | ||
if(b[i][j] == '\\') | ||
{ | ||
PrintLCS(x, y, i-1, j-1); | ||
cout << x[i-1]; | ||
} | ||
else | ||
if(b[i][j] == '|') | ||
PrintLCS(x, y, i-1, j); | ||
else | ||
PrintLCS(x, y, i, j-1); | ||
} | ||
set<string> LCSall(string X, string Y, int m, int n) | ||
{ | ||
set<string> s; | ||
|
||
if (m == 0 || n == 0) | ||
{ | ||
s.insert(""); | ||
return s; | ||
} | ||
if (X[m - 1] == Y[n - 1]) | ||
{ | ||
set<string> tmp = LCSall(X, Y, m - 1, n - 1); | ||
for (string str : tmp) | ||
s.insert(str + X[m - 1]); | ||
} | ||
else | ||
{ | ||
if (c[m - 1][n] >= c[m][n - 1]) | ||
s = LCSall(X, Y, m - 1, n); | ||
if (c[m][n - 1] >= c[m - 1][n]) | ||
{ | ||
set<string> tmp = LCSall(X, Y, m, n - 1); | ||
s.insert(tmp.begin(), tmp.end()); | ||
} | ||
} | ||
return s; | ||
} | ||
void printAll(set<string> s) | ||
{ cout<< endl << "---Wszystkie NWP---" << endl; | ||
for (string str : s) | ||
cout << str << endl; | ||
} | ||
int main() | ||
{ | ||
//string X = "ABCBDAB"; | ||
//string Y = "BDCABA"; | ||
|
||
string X = "BAB"; | ||
string Y = "ABA"; | ||
int m = X.length(); | ||
int n = Y.length(); | ||
LCSLength(X,Y); | ||
cout << "---NWP---" << endl; | ||
PrintLCS(X,Y,m,n); | ||
set<string> s = LCSall(X, Y, m, n); | ||
printAll(s); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.