View Single Post
  #1 (permalink)  
Old 01-09-2008, 09:08 PM
hey hey is offline
Member
 
Join Date: Dec 2007
Posts: 21
hey is on a distinguished road
Graph DPS and BFS implementation
Hello everyone,

I'm preparing for an exemption exam. There are a couple of questions I need to solve.((

Given the following Java class definition for a graph
Code:
public class MyGraph<E> { public class Node { E myValue; boolean tag; ArrayList<Node> myNeighbors; } ArrayList<Node> myNodes; void visitNode(Node n) {/* Action to be performed when traversing node */} void deptFirstSearch(Node n) { /* Perform depth-first search of graph starting at n */ } }
i. Write code for the method depthFirstSearch( n ) that performs a depth first traversal
starting at node n.
ii. Write code for the method breadthFirstSearch(n) that performs a breadth first traversal
starting at node n.


I looked through available lectures and there is no code mentioned there, just description of those algorithms.

DFS Traversal Algorithm
for all nodes X
X.tag = False
put 1stnode in Stack
while (Stack not empty )
pop X off Stack
if (X.tag = False)
set X.tag = True
for each successor Y of X
if (Y.tag = False)
push Y onto Stack


BFS Traversal Algorithm
for all nodes X
X.tag = False
put 1stnode in Queue
while ( Queue not empty )
take node X out of Queue
if (X.tag = False)
set X.tag = True
for each successor Y of X
if (Y.tag = False)
put Y in Queue


I was wondering, if someone can helpme with writing a code.
Reply With Quote
Sponsored Links