Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me with the Textbook solutions for Data Structures Abstreaction and

ID: 656365 • Letter: P

Question

Please help me with the Textbook solutions for Data Structures Abstreaction and Design Using Java 2nd Edition Chapter 10 Problem 8PP

We can use Matrix and List to present a graph. A third possible representation of a graph is to use the TreeSet class to contain the edges. By defining a comparator that compares first on the source vertex and then the destination vertex, we can use the subSet method (of the TreeSet class) to create a view that contains only edges originating at a specified vertex and then use the iterator of that view to iterate through edges. Design and implement a class that meets the requirements of the Graph interface and uses a TreeSet to hold the edges. In addition, you must add the depthFirstSearch method to your class.

Hints:When creating your Comparator class, please be aware that since a TreeSet ISA set, all elements of the set must be unique. It decides on the uniqueness of elements by the compareTo and/or compare methods (i.e., if two elements are equal, then they are not unique) . You will most likely use the first version of the subSet method: NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)

Explanation / Answer

import java.util.Scanner;

   

    public class Graph

    {

        private final int MAX_VERTICES;

        private int adj_matrix[][];

   

        public GraphAdjMatrix(int number_of_vertices)

        {

            MAX_NO_OF_VERTICES = number_of_vertices;

            adj_matrix = new int[MAX_NO_OF_VERTICES + 1][MAX_NO_OF_VERTICES + 1];

        }

   

        public void setEdge(int fm_vertex, int to_vertex, int edge)

        {

            try

            {

                adj_matrix[from_vertex][to_vertex] = edge;

            } catch (ArrayIndexOutOfBoundsException indexBounce)

            {

                System.out.println(" vertex is not present");

            }

        }

   

        public int getEdg(int from_vertex, int to_vertex)

        {

            try

            {

                return adj_matrix[from_vertex][to_vertex];

            } catch (ArrayIndexOutOfBoundsException indexBounce)

            {

                System.out.println(" vertex not present")

            }

            return -1;

        }

   

        public static void main(String argument)

        {

            int number_of_vertices, count = 1;

            int source = 0, destination = 0;

            Scanner scan = new Scanner(System.in);

            GraphAdjMatrix adjMatrix;

   

            try

            {

                System.out.println(" Number for Vertices");

                number_of_vertices = scan.nextInt();

                System.out.println(" Number for Edges");

                int number_of_edges = scan.nextInt();

                adjMatrix = new GraphAdjMatrix(number_of_vertices);

   

                System.out.println("Enter Graph Egdes :<source> <destination>");

                while (count <= no_of_edges)

                {

                    source = scan.nextInt();

                    destination = scan.nextInt();

                    adjMatrix.setEdge(source, destination, 1);

                    count++;

                }

                System.out.println(" adj matrix for graph is");

                for (int i = 1; i <= no_of_vertices; i++)

                    System.out.print(i);

   

                System.out.println();

                for (int i = 1; i <= no_of_vertices; i++)

                {

                    System.out.print(i);

                    for (int j = 1; j <= no_of_vertices; j++)

                    {

                        System.out.print(adj.getEdg(i, j));

                    }

                    System.out.println();

                }

            } catch (InputMismatchException inputMisMatch)

            {

                System.out.println("Error ");

            }

            scan.close();

        }

    }