Question
// File: Graph.java // Author: Rita Ester // Team: (your names here) // Date: Mar. 24, 2018. // Description: Graph class for CSCI 225 lab07
// File: Graph.java
// Author: Rita Ester
// Team: (your names here)
// Date: Mar. 24, 2018.
// Description: Graph class for CSCI 225 lab07
//
import java.util.LinkedList;
// can be used to create the Adjacency list for the Graph representation,
// and to create the queue and the stack needed.
public class Graph {
// This class represents a directed graph using
// an adjacency list representation
private int nbVertices; // No. of vertices
private LinkedList
// Constructor
public Graph(int v){
nbVertices = v;
adjList = new LinkedList[nbVertices];
for (int i=0; i adjList[i] = new LinkedList } // Method to add an edge to the graph public void addEdge(int v1, int v2){ adjList[v1].add(v2); } // prints the adjacency lists of the graph. public void printAdjacencyLists(){ for (int i=0; i System.out.print(" Neighbors at node "+i + ": "); for (int j=0; j System.out.print(adjList[i].get(j) +" - "); } System.out.println(); } // prints the vertices (separated by comma) of a BFS traversal starting at vertex // Author: public void BFS(int vertex){ // to be completed } // DFS prints the vertices (separated by comma) of a DFS traversal starting at vertex. // Author: public void DFS(int vertex){ // to be completed } } ---------------------------------------------------------------------------------------------------------------------------------- // File: Lab07Driver.java // Author: Rita Ester // Team: (your names here) // Date: Mar. 24, 2018 // Description: Driver file for CSCI 225 Lab 07 (Graphs) public class Lab07Driver { public static void main(String[] args) { Test1(); Test2(); Test3(); // add your own tests here. // TestA() by Author: // TestB() by Author: } public static void Test1(){ Graph g = new Graph(4); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(1, 2); g.addEdge(2, 0); g.addEdge(2, 3); g.addEdge(3, 3); System.out.print("***** Test 1 Graph"); g.printAdjacencyLists(); System.out.print(" BFS starting at vertex 2: "); g.BFS(2); System.out.print(" DFS starting at vertex 2: "); g.DFS(2); System.out.println(); } public static void Test2(){ Graph g = new Graph(8); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(0, 3); g.addEdge(1, 2); g.addEdge(2, 0); g.addEdge(2, 5); g.addEdge(2, 6); g.addEdge(3, 1); g.addEdge(3, 7); g.addEdge(4, 2); g.addEdge(5, 1); g.addEdge(5, 3); g.addEdge(6, 4); g.addEdge(6, 5); g.addEdge(7, 6); System.out.print(" ***** Test 2 Graph"); g.printAdjacencyLists(); System.out.print(" BFS starting at vertex 6: "); g.BFS(6); System.out.print(" DFS starting at vertex 6: "); g.DFS(6); System.out.println(); } public static void Test3(){ Graph g = new Graph(9); g.addEdge(0, 1); g.addEdge(0, 4); g.addEdge(0, 7); g.addEdge(1, 0); g.addEdge(1, 2); g.addEdge(1, 4); g.addEdge(2, 1); g.addEdge(2, 6); g.addEdge(4, 0); g.addEdge(4, 1); g.addEdge(4, 5); g.addEdge(5, 4); g.addEdge(5, 8); g.addEdge(6, 2); g.addEdge(7, 0); g.addEdge(8, 5); System.out.print("**** Test 3 Graph"); g.printAdjacencyLists(); System.out.print(" BFS starting at vertex 0: "); g.BFS(0); System.out.print(" DFS starting at vertex 0: "); g.DFS(0); System.out.println(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started