COP3530.01, Spring 2001
April 20, 2001
S. Lang
Partial Solution Key to Quiz #2
1.
Suppose an undirected graph
G
= (
V
,
E
) is represented in its adjacency lists structure, see the
following figure for the graph and its representation.
(b) (8 pts.) Write in C++ or Java (
circle which one!)
to implement the breadthfirstsearch
(BFS) method, assuming a FIFO queue class Queue (of integers) is available which contains
the following methods:
Queue(): a constructor for an empty Queue object.
boolean isEmpty(): a method that tells if a Queue object is empty (true) or not (false).
void add(int x): add integer x to the end of the Queue object.
int remove(): remove and return the first integer at the front of the Queue object (throw
an exception if the Queue is empty).
Your void BFS(int x) method should print out the node numbers as they are being visited,
starting at node x.
You may assume int n gives the number of nodes, and array adjLists[1.
.n]
gives the adjacency lists structure.
Specify any additional data structures you may need.
(Consult the text and the notes for the BFS function.)
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Summer '09
 Computer Science, Graph Theory, S. Lang, Queue object, adjacency lists structure

Click to edit the document details