Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java: Breadth first search and graph generation, adjusting code. The code completes the assigned task correctly, but it utilizes the BreadthFirstPaths class to get to

Java: Breadth first search and graph generation, adjusting code.

The code completes the assigned task correctly, but it utilizes the BreadthFirstPaths class to get to the answer, and for the final code to be accepted, it may copy code from other Java classes but not directly use/call them. How can I adjust the code to utilize the BreadthFirstPaths class but not directly call on it?

Current code

// Exercise 4.1.16 packagealgs41; importstdlib.*; // You need only change the constructor. // You can also change the main method. // But you should not change eccentricity(), diameter(), radius(), center() or isCenter() // You can (and should!) add more private methods (such as bfs or dfs) //TODO: complete the following, using only Graph and GraphGenerator. // Definitions: // - The "distance" from vertex v to vertex w is the length of the shortest path from v to w. // - The "eccentricity" of a vertex v is distance to the farthest vertex from v. // - The "diameter" of a graph is the maximum eccentricity of any vertex. // - The "radius" of a graph is the smallest eccentricity of any vertex. // - A "center" is a vertex whose eccentricity is the radius. The center is not necessarily unique. publicclassMyGraphProperties { int[] eccentricity; intdiameter; intradius; intcenter; // Constructor can be linear in G.V() * G.E() publicMyGraphProperties(Graph G) { this.eccentricity =newint[G.V()]; intdiameter = Integer.MIN_VALUE; intradius = Integer.MAX_VALUE; intcenter = -1; // If G.V()==0, then these are the correct values. // If G is not connected, you should throw a new IllegalArgumentException() BreadthFirstPaths bfs =newBreadthFirstPaths(G,0); for(inti=0;iif(!bfs.marked[i]){ thrownewIllegalArgumentException(); } } //filling eccentricity array with corresponding values for(inti=0;i// compute the eccentricity, diameter, radius and center // The center of the graph is not unique, set center to SOME center --- it does not matter which one this.diameter = diameter; this.radius = radius; this.center = center; } privateintcomputeEccentricity(Graph G,intv){ BreadthFirstPaths bfs =newBreadthFirstPaths(G,v); intec = Integer.MIN_VALUE; for(inti=0;ireturnec; } // Do not change the following constant time methods publicinteccentricity(intv) {returneccentricity[v]; } publicintdiameter() {returndiameter; } publicintradius() {returnradius; } publicintcenter() {returncenter; } publicbooleanisCenter(intv) {returneccentricity[v] == radius; } publicstaticvoidmain(String[] args) { //Graph G = GraphGenerator.fromIn (new In("data/tinyG.txt")); // this is non-connected -- should throw an exception //Graph G = GraphGenerator.connected (10, 20, 2); // Random non-connected graph -- should throw an exception Graph G = GraphGenerator.fromIn(newIn("data/tinyCG.txt")); // diameter=2, radius=2, every node is a center //Graph G = GraphGenerator.binaryTree (10); // A complete binary tree //Graph G = GraphGenerator.path (10); // A path -- diameter=V-1 //Graph G = GraphGenerator.connected (20, 400); // Random connected graph StdOut.println(G); G.toGraphviz ("g.png"); MyGraphProperties gp =newMyGraphProperties(G); for(intv = 0; v < G.V(); v++) StdOut.format("eccentricity of %d: %d ", v, gp.eccentricity (v)); StdOut.format("diameter=%d, radius=%d, center=%d ", gp.diameter(), gp.radius (), gp.center ()); } } 

====== BreadthFirstPaths code (can be copied but not directly called) ========

publicclassBreadthFirstPaths { privatestaticfinalintINFINITY= Integer.MAX_VALUE; publicfinalboolean[] marked; // marked[v] = is there an s-v path privatefinalint[] edgeTo; // edgeTo[v] = previous edge on shortest s-v path publicfinalint[] distTo; // distTo[v] = number of edges shortest s-v path // single source publicBreadthFirstPaths(Graph G,ints) { marked =newboolean[G.V()]; distTo =newint[G.V()]; edgeTo =newint[G.V()]; bfs(G, s); assertcheck(G, s); } // multiple sources publicBreadthFirstPaths(Graph G, Iterable sources) { marked =newboolean[G.V()]; distTo =newint[G.V()]; edgeTo =newint[G.V()]; for(intv = 0; v < G.V(); v++) distTo[v] =INFINITY; bfs(G, sources); } // BFS from single soruce privatevoidbfs(Graph G,ints) { Queue q =newQueue<>(); for(intv = 0; v < G.V(); v++) distTo[v] =INFINITY; distTo[s] = 0; marked[s] =true; q.enqueue(s); while(!q.isEmpty()) { intv = q.dequeue(); for(intw : G.adj(v)) { if(!marked[w]) { edgeTo[w] = v; distTo[w] = distTo[v] + 1; marked[w] =true; q.enqueue(w); } } } } // BFS from multiple sources privatevoidbfs(Graph G, Iterable sources) { Queue q =newQueue<>(); for(ints : sources) { marked[s] =true; distTo[s] = 0; q.enqueue(s); } while(!q.isEmpty()) { intv = q.dequeue(); for(intw : G.adj(v)) { if(!marked[w]) { edgeTo[w] = v; distTo[w] = distTo[v] + 1; marked[w] =true; q.enqueue(w); } } } } 

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

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

Explain Promotion Mix.

Answered: 1 week ago

Question

Explain the promotional mix elements.

Answered: 1 week ago

Question

1. There are many social organisations around us?

Answered: 1 week ago

Question

Why advertising is important in promotion of a product ?

Answered: 1 week ago