Skip to content

one new Dynamic Programming Algorithm and a modification adding recommanding input method #663

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

Merged
merged 2 commits into from
Dec 19, 2018
Merged
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
71 changes: 71 additions & 0 deletions Dynamic Programming/Ford_Fulkerson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Vector;

public class Ford_Fulkerson {
Scanner scan = new Scanner(System.in);
final static int INF = 987654321;
static int V; // edges
static int[][] capacity, flow;

public static void main(String[] args) {
System.out.println("V : 6");
V = 6;
capacity = new int[V][V];

capacity[0][1] = 12;
capacity[0][3] = 13;
capacity[1][2] = 10;
capacity[2][3] = 13;
capacity[2][4] = 3;
capacity[2][5] = 15;
capacity[3][2] = 7;
capacity[3][4] = 15;
capacity[4][5] = 17;

System.out.println("Max capacity in networkFlow : " + networkFlow(0, 5));
}

private static int networkFlow(int source, int sink) {
flow = new int[V][V];
int totalFlow = 0;
while (true) {
Vector<Integer> parent = new Vector<>(V);
for (int i = 0; i < V; i++)
parent.add(-1);
Queue<Integer> q = new LinkedList<>();
parent.set(source, source);
q.add(source);
while (!q.isEmpty() && parent.get(sink) == -1) {
int here = q.peek();
q.poll();
for (int there = 0; there < V; ++there)
if (capacity[here][there] - flow[here][there] > 0 && parent.get(there) == -1) {
q.add(there);
parent.set(there, here);
}
}
if (parent.get(sink) == -1)
break;

int amount = INF;
String printer = "path : ";
StringBuilder sb = new StringBuilder();
for (int p = sink; p != source; p = parent.get(p)) {
amount = Math.min(capacity[parent.get(p)][p] - flow[parent.get(p)][p], amount);
sb.append(p + "-");
}
sb.append(source);
for (int p = sink; p != source; p = parent.get(p)) {
flow[parent.get(p)][p] += amount;
flow[p][parent.get(p)] -= amount;
}
totalFlow += amount;
printer += sb.reverse() + " / max flow : " + totalFlow;
System.out.println(printer);
}

return totalFlow;
}
}
11 changes: 7 additions & 4 deletions Others/Armstrong.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
*
*/
public class Armstrong {
static Scanner scan;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("please enter the number");
int n = scan.nextInt();
scan = new Scanner(System.in);
int n = inputInt("please enter the number");
boolean isArmstrong = checkIfANumberIsAmstrongOrNot(n);
if (isArmstrong) {
System.out.println("the number is armstrong");
Expand Down Expand Up @@ -42,6 +42,9 @@ public static boolean checkIfANumberIsAmstrongOrNot(int number) {
} else {
return false;
}

}
private static int inputInt(String string) {
System.out.print(string);
return Integer.parseInt(scan.nextLine());
}
}