Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Do both of the following methods using an adjacency List and an adjacency matrix. public void breadthFirstTraversal(int start) { } public void depthFirstTraversal(int start) {

Do both of the following methods using an adjacency List and an adjacency matrix.

public void breadthFirstTraversal(int start) {

}

public void depthFirstTraversal(int start) {

}

public class AdjListIntVertices {

private int N;

private HashSet[] adjList;

@SuppressWarnings("unchecked")

public AdjListIntVertices(String fname) {

try {

Scanner input = new Scanner(new File(fname));

N = input.nextInt();

adjList = (HashSet[]) Array

.newInstance(new HashSet().getClass(), N);

for (int r = 0; r < N; r++) {

adjList[r] = new HashSet();

for (int c = 0; c < N; c++) {

if (input.nextInt() == 1) {

adjList[r].add(c);

}

}

}

input.close();

} catch (Exception e) {

System.err.println("Problem during file input");

System.exit(0);

}

}

public void breadthFirstTraversal(int start) {

}

public void depthFirstTraversal(int start) {

}

public Set canReach(int startVertex) {

HashSet reachable = new HashSet<>();

HashSet recentAdditions = new HashSet<>();

reachable.add(startVertex);

recentAdditions.add(startVertex);

do {

HashSet copyRecentAdditions = new HashSet(

recentAdditions);

recentAdditions.clear();

for (int v : copyRecentAdditions) {

for (int i = 0; i < N; i++) {

if (adjList[v].contains(i) && !reachable.contains(i)) {

recentAdditions.add(i);

}

}

}

reachable.addAll(recentAdditions);

} while (recentAdditions.size() != 0);

return reachable;

}

}

public class AdjMatrixIntVertices {

private int N;

private boolean[][] adjMatrix;

public AdjMatrixIntVertices(String fname) {

try {

Scanner input = new Scanner(new File(fname));

N = input.nextInt();

adjMatrix = new boolean[N][];

for (int r = 0; r < N; r++) {

adjMatrix[r] = new boolean[N];

for (int c = 0; c < N; c++) {

adjMatrix[r][c] = input.nextInt() == 1;

}

}

input.close();

} catch (Exception e) {

System.err.println("Problem during file input");

System.exit(0);

}

}

public void breadthFirstTraversal(int start) {

}

public void depthFirstTraversal(int start) {

}

public Set canReach(int startVertex) {

HashSet reachable = new HashSet<>();

HashSet recentAdditions = new HashSet<>();

reachable.add(startVertex);

recentAdditions.add(startVertex);

do {

HashSet copyRecentAdditions = new HashSet(

recentAdditions);

recentAdditions.clear();

for (int v : copyRecentAdditions) {

for (int i = 0; i < N; i++) {

if (adjMatrix[v][i] && !reachable.contains(i)) {

recentAdditions.add(i);

}

}

}

reachable.addAll(recentAdditions);

} while (recentAdditions.size() != 0);

return reachable;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional IPhone And IPad Database Application Programming

Authors: Patrick Alessi

1st Edition

0470636173, 978-0470636176

More Books

Students also viewed these Databases questions