
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Instructions for a homework assignment where students are required to implement the breadth-first and depth-first search algorithms for a given graph represented as a linked list. The use of a queue (linkedlist) and a stack (java's stack) for the respective algorithms, and provides some context on the differences between the two interfaces and data structures. The document also mentions some complications in implementing the depth-first search algorithm and suggests a method to improve its efficiency.
Typology: Assignments
1 / 1
This page cannot be seen from the preview
Don't miss anything!

1
In Lab Assignment 11 you read in a graph given as a matrix and converted the representation to that of a linked list. For this homework, you are to implement the breadth-first and the depth- first search algorithms from the handout. As before, a main program is provided for you on the website, as is an interface IMyGraph.java that differs slightly from the interface of the same name that was provided for Lab 11. There are three sample data files linked off the website, together with my sample output (complete with tracing and debugging information that might help you determine where your bugs were, should you have any). With both these graph traversal algorithms, you will need a queue data structure. Although there is such a thing in Java, you are probably going to be better off using the Java Collections Framework LinkedList. If you read the documentation, you will notice that the add method appends a new element to the end of the list and there are methods to examine and to remove an element from the head of the list, so the functionality of a queue is provided with this structure. For the depth-first search, you will need a stack, and you can use the Stack structure provided in Java. Note that you can print either a LinkedList or a Stack simply by asking it to be printed as a String, and Java will convert it to a printable string for you. The LinkedList prints as you would expect it, but the Stack appears (I have not done this before, so it’s new to me) to print in what I would consider to be backwards order. That is, the top of the stack is printed at the right end of the output list, not at the left end. So be it. You should build your search not using the matrix representation but in- stead using the linked list representation of the graph, since this is more effi- cient. You should use an iterator to go over the neighbor nodes. Note that there is one complication you will have to code around. The breadth-first algorithm runs a while loop over the neighbors of the frontVertex, and does something if there is an unvisited neighbor. That is easy to do with an iterator. The depth-first algorithm, however, looks for the first unvisited neighbor. If you code this as you did the breadth-first search, it will be somewhat cumber- some. The algorithm as written asks first if there is an unvisited neighbor, and then asks to retrieve that unvisited neighbor. That takes two searches, which is also cumbersome and inefficient. I wrote a method to return the first unvisited node as an Integer, so I could have that method return a null if no unvisited neighbors were left. Then I could test for the null and have the actual node value and yet have to do only one search.