You are on page 1of 5

Algorithms Lab

Warning!
Adhere to the Code of Academic Integrity. You may discuss background
issues and general strategies with others and seek help from course instructor,
but the implementations that you submit must be your own. You are not
allowed to work out the detailed coding and solutions with others, copy code
from published/Internet sources or let others to do coding for you. If you
feel that you cannot complete the checkpoint on you own, seek help from the
course instructor.

Checkpoint 4: Graph Algorithm


Checkpoint 4.1: Depth-first search
The strategy followed by depth-first search is, as its name implies, to search
“deeper” in the graph whenever possible. In depth-first search, edges are ex-
plored out of the most recently discovered vertex v that still has unexplored
edges leaving it. When all of v’s edges have been explored, the search “back-
tracks” to explore edges leaving the vertex from which v was discovered. This
process continues until we have discovered all the vertices that are reachable
from the original source vertex. If any undiscovered vertices remain, then
one of them is selected as a new source and the search is repeated from that
source. This entire process is repeated until all vertices are discovered.

Algorithm 1 Depth-First Search

DFS(G)

1: for each vertex u ∈ V[G] do


2: color[u] ← WHITE

3: for each vertex u ∈ V[G] do


4: if color[u] = WHITE then
5: DFS-VISIT(u)

1
Algorithm 2 Depth-First Search Visit

DFS-VISIT(u)

1: color[u] ← GRAY
2: for each vertex v ∈ Adj[u] do
3: if color[v] = WHITE then
4: DFS-VISIT(v)
5: color[u] ← BLACK

Given the description and algorithms above, you are asked to implement DFS
algorithm and all tree arcs shall be coloured in green.

Checkpoint 4.2: An Example


An example of performing DFS on example6 file. Figure 1 shows input graph
while Figure 5 shows output graph.

B C 1 D
1
1 1
1
A G
1 1 1
1
1
1 1
E 1 F 1 H

Figure 1: Input Graph

2
B C 1 D
1
1 1
1
A G
1 1 1
1
1
1 1
E 1 F 1 H

Figure 2: Output Graph

1 Coding DFS Algorithm


You can download the sorting project from Blackboard which contains:

• Checkpoin4 class

• Graphs JAR that involves a collection of classes and methods to build


graph data structure

• Graphs API

• a set of graph examples files

• Checkpoint4Driver class which contains your main program

It is critically important that:

• Your DFS algorithms must be coded in Java

3
• Your Checkpoin4 class remains named as is.

Otherwise, the grading program will not function correctly, and


you will lose marks!

2 Grading Policy
Practical part of this course worth 20 percent of the total course grade. How-
ever, each Checkpoint will be marked out according to the following criteria:

• The correctness of the outputs

• The correctness of implementation

• Code style

3 Submitting Your Work


You are required to submit your work by TBA. There are penalties foe late
submission.

Late Submission Penalty


You have not submit your work on time! You still can submit your work and
some obtain marks. However, checkpoint tasks submitted up to five days
late will be capped at 5 percent per day.

4 Submission Instructions
• Upload Checkpoin4 class source file to the BlackBoard in the submis-
sion area corresponding to Checkpoint 4 as .java and another file .pdf.

• If you need to resubmit, fix all the problems prior due date and go back
to first point!

• If you face any problem, please contact me via email.

4
• In case of late submission (up to 2 days after due date), please contact
me via email. Otherwise you are done with this checkpoint. Well done!

You might also like