-
Notifications
You must be signed in to change notification settings - Fork 59
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
Showing
19 changed files
with
1,319 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,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="lib" path="lib/algs4.jar"/> | ||
<classpathentry kind="lib" path="lib/stdlib.jar"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
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 @@ | ||
/bin |
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>Coursera-Algorithms</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
Binary file not shown.
Binary file not shown.
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,228 @@ | ||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Scanner; | ||
|
||
public class BaseballElimination { | ||
|
||
private int[] w; | ||
|
||
private int[] l; | ||
|
||
private int[] r; | ||
|
||
private int[][] g; | ||
|
||
private Map<String, Integer> teamNames; | ||
|
||
private String[] nameList; | ||
|
||
private int numTeams; | ||
|
||
public BaseballElimination(String filename) throws Exception { | ||
// create a baseball division from given filename in format specified | ||
// below | ||
Scanner input = new Scanner(new File(filename)); | ||
numTeams = input.nextInt(); | ||
this.w = new int[numTeams]; | ||
this.l = new int[numTeams]; | ||
this.r = new int[numTeams]; | ||
this.g = new int[numTeams][numTeams]; | ||
this.nameList = new String[numTeams]; | ||
this.teamNames = new HashMap<String, Integer>(); | ||
for (int n = 0; n < numTeams; n++) { | ||
String name = input.next(); | ||
teamNames.put(name, n); | ||
w[n] = input.nextInt(); | ||
l[n] = input.nextInt(); | ||
r[n] = input.nextInt(); | ||
for (int m = 0; m < numTeams; m++) | ||
g[n][m] = input.nextInt(); | ||
nameList[n] = name; | ||
} | ||
input.close(); | ||
} | ||
|
||
public int numberOfTeams() { | ||
return this.numTeams; | ||
} | ||
|
||
public Iterable<String> teams() { | ||
// all teams | ||
return teamNames.keySet(); | ||
} | ||
|
||
public int wins(String team) { | ||
// number of wins for given team | ||
if (!teamNames.containsKey(team)) | ||
throw new java.lang.IllegalArgumentException(); | ||
return w[teamNames.get(team)]; | ||
} | ||
|
||
public int losses(String team) { | ||
// number of losses for given team | ||
if (!teamNames.containsKey(team)) | ||
throw new java.lang.IllegalArgumentException(); | ||
return l[teamNames.get(team)]; | ||
} | ||
|
||
public int remaining(String team) { | ||
// number of remaining games for given team | ||
if (!teamNames.containsKey(team)) | ||
throw new java.lang.IllegalArgumentException(); | ||
return r[teamNames.get(team)]; | ||
} | ||
|
||
public int against(String team1, String team2) { | ||
// number of remaining games between team1 and team2 | ||
if (!teamNames.containsKey(team1) || !teamNames.containsKey(team2)) | ||
throw new java.lang.IllegalArgumentException(); | ||
int a = teamNames.get(team1); | ||
int b = teamNames.get(team2); | ||
return g[a][b]; | ||
} | ||
|
||
public boolean isEliminated(String team) { | ||
// is given team eliminated? | ||
if (!teamNames.containsKey(team)) | ||
throw new java.lang.IllegalArgumentException(); | ||
int x = teamNames.get(team); | ||
|
||
for (int i = 0; i < this.numTeams; i++) { | ||
if (w[x] + r[x] - w[i] < 0) | ||
return true; | ||
} | ||
|
||
int numMatches = this.numTeams * (this.numTeams - 1) / 2; | ||
int nodeID = 0; | ||
|
||
FlowNetwork fn = new FlowNetwork(numMatches + numTeams + 2); | ||
int s = numMatches + numTeams; | ||
int t = s + 1; | ||
|
||
for (int i = 0; i < numTeams; i++) { | ||
for (int j = i + 1; j < numTeams; j++) { | ||
if (i == j) | ||
continue; | ||
fn.addEdge(new FlowEdge(s, nodeID, g[i][j])); // source to match | ||
// nodes | ||
fn.addEdge(new FlowEdge(nodeID, numMatches + i, | ||
Integer.MAX_VALUE)); // match to team nodes | ||
fn.addEdge(new FlowEdge(nodeID, numMatches + j, | ||
Integer.MAX_VALUE)); // match to team nodes | ||
nodeID += 1; | ||
} | ||
fn.addEdge(new FlowEdge(numMatches + i, t, Math.max(0, w[x] + r[x] | ||
- w[i]))); // game nodes to target | ||
} | ||
|
||
new FordFulkerson(fn, s, t); | ||
|
||
for (FlowEdge e : fn.adj(s)) { | ||
if (e.flow() != e.capacity()) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public Iterable<String> certificateOfElimination(String team) { | ||
// subset R of teams that eliminates given team; null if not eliminated | ||
if (!teamNames.containsKey(team)) | ||
throw new java.lang.IllegalArgumentException(); | ||
int x = teamNames.get(team); | ||
int numMatches = this.numTeams * (this.numTeams - 1) / 2; | ||
int nodeID = 0; | ||
|
||
List<String> nList = new ArrayList<String>(); | ||
for (int i = 0; i < this.numTeams; i++) { | ||
if (w[x] + r[x] - w[i] < 0) | ||
nList.add(nameList[i]); | ||
} | ||
if (nList.size() > 0) | ||
return nList; | ||
|
||
FlowNetwork fn = new FlowNetwork(numMatches + numTeams + 2); | ||
int s = numMatches + numTeams; | ||
int t = s + 1; | ||
|
||
for (int i = 0; i < numTeams; i++) { | ||
for (int j = i + 1; j < numTeams; j++) { | ||
if (i == j) | ||
continue; | ||
fn.addEdge(new FlowEdge(s, nodeID, g[i][j])); // source to match | ||
// nodes | ||
fn.addEdge(new FlowEdge(nodeID, numMatches + i, | ||
Integer.MAX_VALUE)); // match to team nodes | ||
fn.addEdge(new FlowEdge(nodeID, numMatches + j, | ||
Integer.MAX_VALUE)); // match to team nodes | ||
nodeID += 1; | ||
} | ||
fn.addEdge(new FlowEdge(numMatches + i, t, Math.max(0, w[x] + r[x] | ||
- w[i]))); // game nodes to target | ||
} | ||
|
||
FordFulkerson FF = new FordFulkerson(fn, s, t); | ||
|
||
boolean flag = false; | ||
for (FlowEdge e : fn.adj(s)) { | ||
if (e.flow() != e.capacity()) { | ||
flag = true; | ||
break; | ||
} | ||
} | ||
if (!flag) | ||
return null; | ||
else { | ||
List<Integer> nodeList = this.BFSRes(fn, s); | ||
List<String> nl = new ArrayList<String>(); | ||
for (Integer v : nodeList) { | ||
if (FF.inCut(v) && v >= numMatches) { | ||
nl.add(this.nameList[v - numMatches]); | ||
} | ||
} | ||
return nl; | ||
} | ||
} | ||
|
||
private List<Integer> BFSRes(FlowNetwork graph, int node) { | ||
Queue<Integer> Q = new Queue<Integer>(); | ||
boolean[] visited = new boolean[graph.V()]; | ||
Q.enqueue(node); | ||
visited[node] = true; | ||
List<Integer> nodeList = new ArrayList<Integer>(); | ||
while (!Q.isEmpty()) { | ||
int cn = Q.dequeue(); | ||
for (FlowEdge e : graph.adj(cn)) { | ||
int t = -1; | ||
if (e.from() == cn) | ||
t = e.to(); | ||
else | ||
t = e.from(); | ||
if (e.residualCapacityTo(t) > 0) { | ||
if (!visited[t]) { | ||
Q.enqueue(t); | ||
visited[t] = true; | ||
nodeList.add(t); | ||
} | ||
} | ||
} | ||
} | ||
return nodeList; | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
BaseballElimination division = new BaseballElimination(args[0]); | ||
for (String team : division.teams()) { | ||
if (division.isEliminated(team)) { | ||
StdOut.print(team + " is eliminated by the subset R = { "); | ||
for (String t : division.certificateOfElimination(team)) | ||
StdOut.print(t + " "); | ||
StdOut.println("}"); | ||
} else { | ||
StdOut.println(team + " is not eliminated"); | ||
} | ||
} | ||
} | ||
} |
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,94 @@ | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
|
||
public class BurrowsWheeler { | ||
|
||
private static int[] argsort(final char[] a, final boolean ascending) { | ||
Integer[] indexes = new Integer[a.length]; | ||
for (int i = 0; i < indexes.length; i++) { | ||
indexes[i] = i; | ||
} | ||
Arrays.sort(indexes, new Comparator<Integer>() { | ||
@Override | ||
public int compare(final Integer i1, final Integer i2) { | ||
return (ascending ? 1 : -1) * Float.compare(a[i1], a[i2]); | ||
} | ||
}); | ||
return asArray(indexes); | ||
} | ||
|
||
private static <T extends Number> int[] asArray(final T... a) { | ||
int[] b = new int[a.length]; | ||
for (int i = 0; i < b.length; i++) { | ||
b[i] = a[i].intValue(); | ||
} | ||
return b; | ||
} | ||
|
||
// apply Burrows-Wheeler encoding, reading from standard input and writing | ||
// to standard output | ||
public static void encode() { | ||
|
||
StringBuilder str = new StringBuilder(); | ||
while (!BinaryStdIn.isEmpty()) | ||
str = str.append(BinaryStdIn.readChar()); | ||
CircularSuffixArray csa = new CircularSuffixArray(str.toString()); | ||
for (int i = 0; i < csa.length(); i++) { | ||
if (csa.index(i) == 0) { | ||
BinaryStdOut.write(i, 32); | ||
// System.out.printf("%c%c%c%c",0,0,0,i); | ||
break; | ||
} | ||
} | ||
for (int i = 0; i < csa.length(); i++) { | ||
// System.out.print(str.charAt((csa.index(i) - 1 + csa.length()) % | ||
// csa.length())); | ||
BinaryStdOut.write(str.charAt((csa.index(i) - 1 + csa.length()) | ||
% csa.length())); | ||
} | ||
BinaryStdIn.close(); | ||
BinaryStdOut.close(); | ||
} | ||
|
||
// apply Burrows-Wheeler decoding, reading from standard input and writing | ||
// to standard output | ||
public static void decode() { | ||
StringBuilder str = new StringBuilder(); | ||
int s = BinaryStdIn.readInt(32); | ||
while (!BinaryStdIn.isEmpty()) | ||
str = str.append(BinaryStdIn.readChar()); | ||
int next[] = new int[str.length()]; | ||
// System.out.println(s+" "+str.length()); | ||
// build next array | ||
int[] index = argsort(str.toString().toCharArray(), true); | ||
for (int i = 0; i < str.length(); i++) { | ||
next[i] = index[i]; | ||
// System.out.println(next[i]); | ||
} | ||
|
||
// output | ||
int idx = 0; | ||
int pt = next[s]; | ||
while (idx < str.length() - 1) { | ||
BinaryStdOut.write(str.charAt(pt)); | ||
// System.out.print(str.charAt(pt)); | ||
pt = next[pt]; | ||
idx++; | ||
} | ||
BinaryStdOut.write(str.charAt(s)); | ||
// System.out.print(str.charAt(s)); | ||
BinaryStdIn.close(); | ||
BinaryStdOut.close(); | ||
} | ||
|
||
// if args[0] is '-', apply Burrows-Wheeler encoding | ||
// if args[0] is '+', apply Burrows-Wheeler decoding | ||
public static void main(String[] args) { | ||
if (args[0].equals("-")) | ||
encode(); | ||
else if (args[0].equals("+")) | ||
decode(); | ||
else | ||
return; | ||
} | ||
} |
Oops, something went wrong.