Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

l import java.io.*; 2 import java.util.*; 4/ Epublic abstract class GraphUtils ( 6 static Graph empty Graph ) return new AdjSetGraph ); String dumpGraph (Graph

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

l import java.io.*; 2 import java.util.*; 4/ Epublic abstract class GraphUtils ( 6 static Graph empty Graph ) return new AdjSetGraph); String dumpGraph (Graph g) { 10 static "Vertex count _ " + g.vertexCount() + " "; String s for (Vv : g.ve rtices()) { 12 13 14 15 16 17 18 19 20 for (V w g.neighbors (v)) return s; 21 AdjSetGraph.java 1 Epublic class GraphTest ( 3 public static void main (String argv[]) throws Exception { Graph g = Graphutils.emptyGraph(); g.addVertex (10) g.addVertex (20) g.addVertex (30) g.addEdge (10,20); g.addEdge (10, 30) g. addEdge (20, 30) : system.out.println("Stage 1:"); system.out.print (GraphUtils. dumpGraph (g)); System.out.println ("Graph contains vertex 10: "g.hasVertex (1O)); System.out.println("Graph contains vertex 50: " + g.hasVertex (50)); System.out.println ("Degree of vertex 10 g.degree (10)) System.out.println("Degree of vertex 30 g.degree (30)) g.addVertex (30) ; g.addVertex(40) g.addEdge (10,40); g.addEdge (10,30) g.addVertex (50): System.out.printin ("SLage 2:") System.out.print (GraphUtils.dumpGraph (g)): System.out.println ("Graph contains vertex 10: " g.hasVertex (10)): System.out.println("Graph contains vertex 50: g.hasVertex (So)): System.out.println ("Degree of vertex 10g.degree (10)) 6 12 14 20 23 24 25 27 System.out.println ("Degree of vertex 30"+ g.degree (30)) Java source file 1 // Abstract Data Type for undirected graphs with vertices of type V // Self-loops are allowed, but parallel edges are not interface Graph { 61 Add a vertex. No-op if vertex already exists. void addVertex (V v) 9/ Return all the vertices Iterable vertices; 12I Return the number of vertices. 13 | int vertexCountDa 14 // Answer whether a particular vertex is in the graph boolean hasVertex (V v); 1E 18 1 Add an edge between two vertices. 19 19 Raises IllegalArgumentException if either vertex is not in graph 20 / No-op if edge already exists 21 void addEdge (V vl,V v2) 23 24 25 26 / Return the neighbors of a vertex // Raises IllegalArgumentException if vertex is not in graph Iterable neighbors (V v): // Return the degree (number of neighbors) of a vertex // Raises IllegalrgumentException if vertex is not in graph 28 29 30 31 32 int degree (V v) 34 l import java.io.*; 2 import java.util.*; 4/ Epublic abstract class GraphUtils ( 6 static Graph empty Graph ) return new AdjSetGraph); String dumpGraph (Graph g) { 10 static "Vertex count _ " + g.vertexCount() + " "; String s for (Vv : g.ve rtices()) { 12 13 14 15 16 17 18 19 20 for (V w g.neighbors (v)) return s; 21 AdjSetGraph.java 1 Epublic class GraphTest ( 3 public static void main (String argv[]) throws Exception { Graph g = Graphutils.emptyGraph(); g.addVertex (10) g.addVertex (20) g.addVertex (30) g.addEdge (10,20); g.addEdge (10, 30) g. addEdge (20, 30) : system.out.println("Stage 1:"); system.out.print (GraphUtils. dumpGraph (g)); System.out.println ("Graph contains vertex 10: "g.hasVertex (1O)); System.out.println("Graph contains vertex 50: " + g.hasVertex (50)); System.out.println ("Degree of vertex 10 g.degree (10)) System.out.println("Degree of vertex 30 g.degree (30)) g.addVertex (30) ; g.addVertex(40) g.addEdge (10,40); g.addEdge (10,30) g.addVertex (50): System.out.printin ("SLage 2:") System.out.print (GraphUtils.dumpGraph (g)): System.out.println ("Graph contains vertex 10: " g.hasVertex (10)): System.out.println("Graph contains vertex 50: g.hasVertex (So)): System.out.println ("Degree of vertex 10g.degree (10)) 6 12 14 20 23 24 25 27 System.out.println ("Degree of vertex 30"+ g.degree (30)) Java source file 1 // Abstract Data Type for undirected graphs with vertices of type V // Self-loops are allowed, but parallel edges are not interface Graph { 61 Add a vertex. No-op if vertex already exists. void addVertex (V v) 9/ Return all the vertices Iterable vertices; 12I Return the number of vertices. 13 | int vertexCountDa 14 // Answer whether a particular vertex is in the graph boolean hasVertex (V v); 1E 18 1 Add an edge between two vertices. 19 19 Raises IllegalArgumentException if either vertex is not in graph 20 / No-op if edge already exists 21 void addEdge (V vl,V v2) 23 24 25 26 / Return the neighbors of a vertex // Raises IllegalArgumentException if vertex is not in graph Iterable neighbors (V v): // Return the degree (number of neighbors) of a vertex // Raises IllegalrgumentException if vertex is not in graph 28 29 30 31 32 int degree (V v) 34

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

Database Management Systems Designing And Building Business Applications

Authors: Gerald V. Post

1st Edition

0072898933, 978-0072898934

More Books

Students also viewed these Databases questions

Question

3. Existing organizations and programs constrain behavior.

Answered: 1 week ago