Espadero, Graphs
Espadero, Graphs
Espadero, Graphs
BSIT-NET2A
Assessment
Compare and contrast BFS vs DFS through 3 examples of traversals
1. For determining the shortest path, BFS (Breadth First Search) employs a Queue data structure. The
Stack data structure is used by DFS (Depth First Search).
2. BFS is better at finding vertices that are close to the specified source. When there are solutions that are
not available at the source, DFS is a better option.
3. When a dead end occurs in any iteration, the Breadth First Search (BFS) method traverses a graph in a
breadth ward motion and uses a queue to remember to retrieve the next vertex to start a search. When a
dead end occurs in any iteration, the Breadth First Search (BFS) method traverses a graph in a breadth
ward motion and uses a queue to remember to retrieve the next vertex to start a search.
Assignment
Implement a Graph Data Structure Traversal using Java Program
import java.util.*;
class Edge {
int src, dest, weight;
Edge(int src, int dest, int weight) {
this.src = src;
this.dest = dest;
this.weight = weight;
}
}
class Graph {
System.out.println();
src_vertex++;
}
}
}
class Main{
public static void main (String[] args) {
Graph.printGraph(graph);
}
}
Output:
The contents of the graph:
Vertex:0 ==> 1 (2) Vertex:0 ==> 2 (4)
Vertex:1 ==> 2 (4)
Vertex:2 ==> 0 (5) Vertex:2 ==> 1 (4)
Vertex:3 ==> 2 (3)
Vertex:4 ==> 5 (1)
Vertex:5 ==> 4 (3)