Vous êtes sur la page 1sur 4

import import import import import import

java.io.File; java.io.FileNotFoundException; java.util.Collections; java.util.LinkedList; java.util.Scanner; java.util.Vector;

public class Lab10 { /* Fisiere de intrare */ final static String TASK_1_INPUT_FILE = "task1.in"; final static String TASK_2_INPUT_FILE = "task2.in"; /* Sursa si destinatia pentru flux */ final static int flowSource = 0; final static int flowDest = 5; public static Vector<Integer> BFS_function(Graph g, int s, int a) { int[] parinti; LinkedList<Integer> lista = new LinkedList<Integer>(); Vector<Integer> rez = new Vector<Integer>(); parinti = new int[g.flow.length]; for (int i = 0; i < g.getSize(); i++) { parinti[i] = -1; } lista.addLast(s); while(lista.size() > 0 && parinti[a] == -1) { int nod; nod = lista.peekFirst(); lista.pop(); for(int i = 0; i < g.getSize(); i++) { if (g.capacityMatrix[nod][i] - g.flow[nod][i] > 0 && parinti[ i] == -1 && g.adjMatrix[nod][i] == true) { parinti[i] = nod; lista.addLast(i); } } } if (parinti[a] == -1) { return new Vector<Integer>(); } for (int i = a; true; i = parinti[i]) { rez.add(i); if (i == s) { break; } } Collections.reverse(rez); return rez; } public static int saturare_drum(Graph g, Vector<Integer> drum) { if(drum.size() < 2) { return 0; }

int flux = g.capacityMatrix[drum.get(0)][drum.get(1)] - g.flow[drum. get(0)][drum.get(1)]; for (int i = 0; i < drum.size() - 1; ++i) { int u = drum.get(i); int v = drum.get(i + 1); flux = (flux < g.capacityMatrix[u][v] - g.flow[u][v] ? flux : g. capacityMatrix[u][v] - g.flow[u][v]); } for (int i = 0; i < drum.size() - 1; ++i) { int u = drum.get(i); int v = drum.get(i + 1); g.flow[u][v] += flux; g.flow[v][u] -= flux; } return flux; } public static int maxFlow(Graph g, int u, int v) { int maxFlow = 0; /* * TODO * * calculeaza fluxul maxim care poate fi transportat prin retea de la * nodul u la nodul v * */ while(true) { Vector<Integer> path; path = BFS_function(g, flowSource, flowDest); System.out.println(path.toString()); if(path.size() == 0) { break; } System.out.println(maxFlow); maxFlow += saturare_drum(g, path); } System.out.println("Flux maxim de la " + u + " la " + v + ": " + maxFlow); return maxFlow; } public static void minCut(Graph g, int s) { Vector<Integer> A = new Vector<Integer>(); Vector<Integer> B = new Vector<Integer>(); LinkedList<Integer> lista = new LinkedList<Integer>(); boolean[] queue = new boolean[g.getSize()]; lista.addLast(s);

for (int i=0; i<g.getSize();i++) queue[i]=false; queue[s] = true; while ( lista.size() > 0 ) { int nod = lista.peekFirst(); lista.pop(); for ( int i = 0; i < g.getSize(); ++i) { if (queue[i] == false && g.adjMatrix[nod][i] && g.capacity Matrix[nod][i] - g.flow[nod][i] > 0) { queue[i] = true; lista.addLast(i); } } } for ( int i = 0; i < g.getSize(); ++i) { if (queue[i]) { A.add(i); } else { B.add(i); } } /* afisam cele doua multimi */ System.out.println("Taietura minima:"); System.out.println("A: " + A); System.out.println("B: " + B); System.out.println(); } public static void disjointPaths(Graph g, int u, int v) throws FileNotFo undException { Graph g1 = new Graph(TASK_1_INPUT_FILE, Graph.Type.DIRECTED); } public static void task2() throws FileNotFoundException { Scanner scanner = new Scanner(new File(TASK_2_INPUT_FILE)); /* numar de linii */ int n = scanner.nextInt(); /* numar de coloane */ int m = scanner.nextInt(); /* matricea de numere reale */ float numbers[][] = new float[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) numbers[i][j] = scanner.nextFloat(); scanner.close(); /* * TODO

* * Task 2. * * Folositi n, m, numbers (vezi mai sus). Pentru a crea un graf nou, * gol, folositi constructorul Graph(size, type) si adaugati muc hii * folosind Graph.addEdge(x, y, c). */ } public static void main(String[] args) throws FileNotFoundException { Graph g = new Graph(TASK_1_INPUT_FILE, Graph.Type.DIRECTED); /* TASK 1 - a */ maxFlow(g, flowSource, flowDest); /* TASK 1 - b */ minCut(g, flowSource); /* TASK 1 - c */ disjointPaths(g, flowSource, flowDest); /* TASK 2 */ task2(); } }

Vous aimerez peut-être aussi