Assignment 2

This second assignment focuses on a DFS search. You will accomplish a number of tasks in this assignment:

Let’s get started!

Updating your repository

Earlier in the term you made a copy of my GitLab repository over to your account, we called this a fork. You then cloned your GitLab repository to your local computer. Your computer now has a local repository, which is linked to a remote repository, the one in your GitLab account. The name origin is used to identify this remote repository. You can see this when you look in GitKraken. You should see a “Remotes” section on the left, and an “origin” repository in there.

Since then you have added many commits to your local repository and pushed them onto your remote repository (origin). At the same time, I have created the second assignment files and added them to my GitLab repository. What you will need to do is copy these updates from my repository over to yours. This is a 2/3-step process:

  1. We will link your local repository to my GitLab repository as a second remote. This is called upstream repository, the idea being that we want changes made to that branch to trickle downstream to your repository. You only need to do this once. In future labs you’ll be able to skip this step.
  2. After we have set up this upstream repository, we will fetch changes from that repository into your local repository, and merge those changes in to your repository.
  3. We will then push those changes to your remote (origin).

Then you’ll be able to start work on the assignment.

Let’s get to it!

Setting up an upstream

Before we start anything, make sure that you have NO uncommitted changes. You must either commit what you have, if it is ready to commit, or stash the changes, allowing you to retrieve them later.

To add a new upstream remote, we will use GitKraken’s interface. Hover over the “Remotes” section on the left, and a Plus button should appear to add a new remote. Click on it, and a little window will appear to let you choose the remote. Find the remote that is in the skiadas account and called algorithms-assignments. Its name will likely be skiadas/algorithms-assignments. Then as the name for this repo put upstream. Then click “Add Remote”. You should now be seeing a new remote on the left, called upstream, and you should be seeing some new branches showing up in the main screen, corresponding to that remote.

Merging the upstream changes

Once again the first key step is to make sure that you have NO uncommitted changes. You must either commit what you have, if it is ready to commit, or stash the changes, allowing you to retrieve them later.

We now want to merge in the changes that the upstream remote has into your remote.

  1. First make sure that your local master is the active branch. It should have a little checkbox next to it.
  2. We should make sure to bring in the newest version of the upstream remote. Right-click on the “upstream” remote in the “Remotes” section on the left, and choose “fetch upstream”.
  3. Now right-click on the upstream/master branch within the “Remotes” section to bring up its context menu. One of the options will say “Merge upstream/master onto master”. Click it.
  4. You should now be ready to go. Whenever you push your changes to your origin, these commits from the upstream will go along with them. If you like, you can go ahead and push now.

The assignment

Introductory stuff

In the assignment you work with a simple graph implementation and depth-first-search implementations on it. You should already be familiar with the depth-first-search material from the book.

As in the previous assignment, you will find numerous TODO comments throughout the file. They mark the locations where you need to add something to the implementations. Some times it will be one or two lines, some times it may be more. If you use Eclipse, you can see blue rectangles on the right side of the code indicating the location of all TODOs, and you can click on them to go there. You can keep track of your progress that way, so make sure to remove the TODO from the methods you have already implemented.

A standard printout of a run of the various methods can be obtained by running the main method of the DFS class (so running the DFS class as an application). To do that, you will need to first build using the corresponding gradle task, and then run the runDFS gradle task from the run group of tasks (you may need to refresh the gradle project and possibly close and reopen the gradle tasks to make it appear).

Alternatively, you can go to your terminal (or Eclipse’s console), go to the main project folder and run:

java -cp build/libs/algorithms-assignments.jar utils.DFS

If you like to write more tests, you can create test files under /src/test/java. Ask for help if you are not sure how to start those tests. You can use the :test Gradle task to run the tests.

You should make a new commit in GitKraken after completing each self-contained part, typically after each method you complete. Do not try to commit everything at once.

When you are done with the assignment, in order to “submit” it, simply push your commits via GitKraken. These will now be posted on your account in GitLab and I will be able to follow them from there.

Important: This assignment requires you to use lists at some point. You may use your DLList implementation if you like, or use ArrayList or LinkedList.

The structure of the files

There are two files of concern in this assignment. SimpleGraph.java, and DFS.java.

The actual tasks

Now on to the tasks you need to complete. Make sure you have read the previous section for an understanding of the structure of the files.

  1. We start with the SimpleDFS class.
  2. Next we have to implement the ComponentsDFS class. This is probably the easiest of the four classes to implement.
  3. The third inner class to implement is the CycleDFS class. It looks for a back-edge and uses it to construct a cycle.
  4. The last inner class we have to work on is the BipartiteDFS class. It uses the DFS search to determine if the graph is bipartite, and what the two parts of it are. It keeps “colors” for the vertices in the colors array, and also a boolean failed variable to indicate if the search has failed or not.