|
| 1 | +Exercise 1. (25 points) Ford-Fulkerson |
| 2 | +We will implement the Ford-Fulkerson algorithm to calculate the Maximum Flow of a directed weighted graph. |
| 3 | +Here, you will use the files WGraph.java and FordFulkerson.java, |
| 4 | +which are available on the course website. Your role will be to complete two methods in |
| 5 | +the template FordFulkerson.java. |
| 6 | +The file WGraph.java is similar to the file that you used in your previous assignment to |
| 7 | +build graphs. The only differences are the addition of setter and getter methods for the |
| 8 | +Edges and the addition of the parameters “source” and “destination”. There is also an |
| 9 | +additional constructor that will allow the creation of a graph cloning a WGraph object. |
| 10 | +Graphs are encoded using a similar format than the one used in the previous assignment. |
| 11 | +The only difference is that now the first line corresponds to two integers, separated by |
| 12 | +one space, that represent the “source” and the “destination” nodes. An example of such |
| 13 | +file can be found on the course website in the file ff2.txt. These files will be used |
| 14 | +as an input in the program FordFulkerson.java to initialize the graphs. This graph |
| 15 | +corresponds to the same graph depicted in [CLRS2009] page 727. |
| 16 | +Your task will be to complete the two static methods fordfulkerson(Integer source, |
| 17 | +Integer destination, WGraph graph, String filePath) and pathDFS(Integer source, |
| 18 | +Integer destination, WGraph graph). The second method pathDFS finds a path |
| 19 | +via Depth First Search (DFS) between the nodes “source” and “destination” in the |
| 20 | +“graph”. You must return an ArrayList of Integers with the list of unique nodes belonging to the path found by the DFS. |
| 21 | +The first element in the list must correspond to |
| 22 | +the “source” node, the second element in the list must be the second node in the path, |
| 23 | +and so on until the last element (i.e., the “destination” node) is stored. The method |
| 24 | +fordfulkerson must compute an integer corresponding to the max flow of the “graph”, |
| 25 | +as well as the graph encoding the assignment associated with this max flow. The method |
| 26 | +fordfulkerson has a variable called myMcGillID, which must be initialized with your |
| 27 | +McGill ID number. |
| 28 | +Once completed, compile all the java files and run the command line java FordFulkerson |
| 29 | +ff2.txt. Your program must use the function writeAnswer to save your output in a file. |
| 30 | +An example of the expected output file is available in the file ff226000000.txt. This |
| 31 | +output keeps the same format than the file used to build the graph; the only difference |
| 32 | +is that the first line now represents the maximum flow (instead of the “source” and “destination” nodes). |
| 33 | +The other lines represent the same graph with the weights updated |
| 34 | +to the values that allow the maximum flow. The file ff226000000.txt represents the |
| 35 | +answer to the example showed in [CLRS2009] Page 727. You are invited to run other |
| 36 | +examples of your own to verify that your program is correct. |
| 37 | + |
| 38 | +Exercise 2. (25 points) Bellman-Ford |
| 39 | +We want to implement the Bellman-Ford algorithm for finding the shortest path in |
| 40 | +a graph where edges can have negative weights. Once again, you will use the object |
| 41 | +WGraph. Your task is to complete the method BellmanFord(WGraph g, int source) |
| 42 | +and shortestPath(int destination) in the file BellmanFord.java. |
| 43 | +The method BellmanFord takes an object WGraph named g as an input and an integer |
| 44 | +that indicates the source of the paths. If the input graph g contains a negative cycle, |
| 45 | +then the method should throw an exception (see template). Otherwise, it will return |
| 46 | +an object BellmanFord that contains the shortest path estimates (the private array of |
| 47 | +integers distances), and for each node, its predecessor in the shortest path from the |
| 48 | +source (the private array of integers predecessors). |
| 49 | +The method shortestPath will return the list of nodes as an array of integers along |
| 50 | +the shortest path from the source to the destination. If this path does not exists, the |
| 51 | +method should throw an exception (see template). |
| 52 | +Input graphs are available on the course webpage to test your program. Nonetheless, we |
| 53 | +invite you to also make your own graphs to test your program. |
0 commit comments