Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 28, Problem 28.17PE
Program Plan Intro
Program Plan:
- Include the required import statement.
- Create a package “main”.
- Add a java class named “Edge” to the package which is used to get the edges from the graph.
- Add a java class named “Graph” to the package which is used to add and remove vertices, edges.
- Add a java class named “UnweightedGraph” to the package which is used to store vertices and neighbors.
- Add a java class named “WeightedGraph” to the package which is used to get the weighted edges and print the edges.
- Add a java class named “WeightedEdge” to the package which is used to compare edges.
- Add a java class named “NineTailModel” to the package which is used to compare edges.
- Define the main method using public static main.
- Allocate memory to the class “Test”.
- Define the “Test” class.
- Declare and set the values for the variables.
- Create an object1 for the “Graph” class.
- Display the edge.
- Allocate memory for the “edge” variable.
- Create an object2 for the “Graph” class.
- Display the edge.
- Display the results.
- Define the “Graph” class.
- A graph interface gets created.
- Method to return the size gets defined.
- Method to return the vertices gets defined.
- Method to return the index gets created.
- Method to get the neighbor node gets created.
- Method to get the degree gets created.
- Method to print the edges.
- Method to clear the node gets created.
- Method to add the edges, add vertex gets created.
- Method to remove the vertices gets defined.
- Method for the depth first search gets defined.
- Method for the breadth first search gets defined.
- Create a class “UnweightedGraph”:
- New list for the vertices gets created.
- New list for the neighbor node gets created.
- Create an empty constructor.
- Method to create new graph gets created and adjacency list gets created.
- Method to create an adjacency list gets created.
- Method to return the size of the vertices.
- Method to return the index of the vertices gets defined.
- Method to gets the neighbor node gets defined.
- Method to return the degree of the vertices gets created.
- Method to print the Edges gets created.
- New to clear the graph gets created.
- Method to add vertex gets created.
- Method to add edge gets created.
- Method to perform the depth first search gets defined.
- Method to perform breadth first search gets defined.
- Search tree gets returned.
- Create a class “SearchTree”
- Define the method to return the root.
- Method to return the parent of the vertices
- Method to return the search order gets defined.
- Method to return the number of vertices found gets defined.
- Method to get the path of the vertices gets defined.
- Loop to validate the path gets defined.
- Path gets returned.
- Method to print the path gets defined.
- Method to print the tree gets defined.
- Display the edge.
- Display the root.
- Condition to validate the parent node to display the vertices gets created.
- Define the “getHamiltonianPath” method.
- If the size of graph is equal to “i” value, return a Hamiltonian path from the specified vertex object.
- Otherwise, return null if the graph does not have a Hamiltonian path.
- Define the “getHamiltonianPath” method.
- Return the value using array list.
- Define the “getHamiltonianPath” method.
- Declare the required variables.
- If the length of the graph, indicate no subpath from “i” is found.
- The vertices in the Hamiltonian path are stored in result.
- Reorder the adjacency list for each vertex.
- Create a list for path
- Add vertex to the result list.
- Get the next vertex in the path.
- Return null if no Hamiltonian path is found.
- Define the “reorderNeigborsBasedOnDegree” method.
- Check the condition.
- Find the maximum in the list.
- Check the condition and swap the list with another list.
- Check the condition.
- Define the “allVisited” method.
- Declare the variables.
- Check if all the elements in array is visited and return true.
- Otherwise return the result.
- Define the “getHamiltonianPath” method.
- Declare the variables
- If all the vertices are found then include all the vertices.
- Then search for a Hamiltonian path from “v”.
- Define “getHamiltonianCycle” method.
- Return a Hamiltonian cycle.
- Define “getHamiltonianCycle” method.
- Declare the variable.
- If the length of the graph, indicate no subpath from “i” is found.
- The vertices in the Hamiltonian path are stored in result.
- Reorder the adjacency list for each vertex.
- Create a list for path
- Add vertex to the result list.
- Get the next vertex in the path.
- Return null if no Hamiltonian path is found.
- Define “getHamiltonianCycle” method.
- Declare the variable.
- If all the vertex is visited the return true.
- Otherwise backtrack “v” is marked unvisited.
- Return false.
- Define “isCycle” method.
- Return the value.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
(0)URGENT!!!!
Please use this graph class and SOLVE FOR ISOLATEDTRIPLETS QUESTION AND AVERAGE BALANCE RATIO
Here is the graph class:
public class Graph {private final int V;private int E;private final Bag<Integer>[] adj;
/*** Create an empty graph with V vertices.*/@SuppressWarnings("unchecked")public Graph(int V) {if (V < 0) throw new Error("Number of vertices must be nonnegative");this.V = V;this.E = 0;this.adj = new Bag[V];for (int v = 0; v < V; v++) {adj[v] = new Bag<>();}}
/*** Return the number of vertices in the graph.*/public int V() { return V; }
/*** Return the number of edges in the graph.*/public int E() { return E; }
/*** Add the undirected edge v-w to graph.* @throws java.lang.IndexOutOfBoundsException unless both 0 <= v < V and 0 <= w < V*/public void addEdge(int v, int w) {if (v < 0 || v >= V) throw new IndexOutOfBoundsException();if (w < 0 || w >= V) throw new IndexOutOfBoundsException();E++;adj[v].add(w);adj[w].add(v);}
/***…
1. Using the above class map, write function void map::mapToGraph(graph &g){...} to create a graph g that represents the
legal moves in the map m. Each vertex should represent a cell, and each edge should represent a legal move between
adjacent cells.
2. Write a recursive function findPathRecursive(graph &g, stack &moves) that looks for a path from the start island
to the destination island. If a path from the start to the destination exists, your function should call the map::printPath()
function that should print a sequence of correct moves (Go left, Go right, Go down, Go up, etc.). If no path from the
start to the destination exists, the program should print, "No path exists". If a solution exists the solver should also
simulate the solution to each map by calling the map::print() function. The map::print() function prints out a map
visualization, with the goal and current position of the car in the map at each move, marked to show the progress. Hint:
consider recursive-DFS.
3.…
In this project, you will develop algorithms that find road routes through the bridges to travel between islands.
The input is a text file containing data about the given map. Each file begins with the number of rows and columns in
the map considered as maximum latitudes and maximum longitudes respectively on the map. The character "X" in the
file represents the water that means if a cell contains "X" then the traveler is not allowed to occupy that cell as this car is
not drivable on water. The character "0" in the file represents the road connected island. That means if a cell contains
"0" then the traveler is allowed to occupy that cell as this car can drive on roads.
The traveler starts at the island located at latitude = 0 and longitude = 0 (i.e., (0,0)) in the upper left comer, and the goal
is to drive to the island located at (MaxLattitude-1, MaxLongitudes-1) in the lower right corner. A legal move from an
island is to move left, right, up, or down to an immediately adjacent cell…
Chapter 28 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Ch. 28.2 - What is the famous Seven Bridges of Knigsberg...Ch. 28.2 - Prob. 28.2.2CPCh. 28.2 - Prob. 28.2.3CPCh. 28.2 - Prob. 28.2.4CPCh. 28.3 - Prob. 28.3.1CPCh. 28.3 - Prob. 28.3.2CPCh. 28.4 - Prob. 28.4.1CPCh. 28.4 - Prob. 28.4.2CPCh. 28.4 - Show the output of the following code: public...Ch. 28.4 - Prob. 28.4.4CP
Ch. 28.5 - Prob. 28.5.2CPCh. 28.6 - Prob. 28.6.1CPCh. 28.6 - Prob. 28.6.2CPCh. 28.7 - Prob. 28.7.1CPCh. 28.7 - Prob. 28.7.2CPCh. 28.7 - Prob. 28.7.3CPCh. 28.7 - Prob. 28.7.4CPCh. 28.7 - Prob. 28.7.5CPCh. 28.8 - Prob. 28.8.1CPCh. 28.8 - When you click the mouse inside a circle, does the...Ch. 28.8 - Prob. 28.8.3CPCh. 28.9 - Prob. 28.9.1CPCh. 28.9 - Prob. 28.9.2CPCh. 28.9 - Prob. 28.9.3CPCh. 28.9 - Prob. 28.9.4CPCh. 28.10 - Prob. 28.10.1CPCh. 28.10 - Prob. 28.10.2CPCh. 28.10 - Prob. 28.10.3CPCh. 28.10 - If lines 26 and 27 are swapped in Listing 28.13,...Ch. 28 - Prob. 28.1PECh. 28 - (Create a file for a graph) Modify Listing 28.2,...Ch. 28 - Prob. 28.3PECh. 28 - Prob. 28.4PECh. 28 - (Detect cycles) Define a new class named...Ch. 28 - Prob. 28.7PECh. 28 - Prob. 28.8PECh. 28 - Prob. 28.9PECh. 28 - Prob. 28.10PECh. 28 - (Revise Listing 28.14, NineTail.java) The program...Ch. 28 - (Variation of the nine tails problem) In the nine...Ch. 28 - (4 4 16 tails problem) Listing 28.14,...Ch. 28 - (4 4 16 tails analysis) The nine tails problem in...Ch. 28 - (4 4 16 tails GUI) Rewrite Programming Exercise...Ch. 28 - Prob. 28.16PECh. 28 - Prob. 28.17PECh. 28 - Prob. 28.19PECh. 28 - (Display a graph) Write a program that reads a...Ch. 28 - Prob. 28.21PECh. 28 - Prob. 28.22PECh. 28 - (Connected rectangles) Listing 28.10,...Ch. 28 - Prob. 28.24PECh. 28 - (Implement remove(V v)) Modify Listing 28.4,...Ch. 28 - (Implement remove(int u, int v)) Modify Listing...
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- can you give me the code to the following question?arrow_forward"""1.In a directed graph, a strongly connected component is a set of vertices suchthat for any pairs of vertices u and v there exists a path (u-...-v) thatconnects them. A graph is strongly connected if it is a single stronglyconnected component.""" from collections import defaultdict class Graph: """ A directed graph where edges are one-way (a two-way edge can be represented by using two edges). """ def __init__(self,vertex_count): """ Create a new graph with vertex_count vertices. """ self.vertex_count = vertex_count self.graph = defaultdict(list) def add_edge(self,source,target): """ Add an edge going from source to target """ self.graph[source].append(target) def dfs(self): """ Determine if all nodes are reachable from node 0 """ visited = [False] * self.vertex_count self.dfs_util(0,visited) if visited == [True]*self.vertex_count: return True…arrow_forwardPart 2) Adjacency-matrix graph representation: Java implementation Adjacency-list graph representation: Java implementation Create two classes Graph, one for each representation. Add variable members, constructors, addEdge, searchEdge (given two nodes return true if adjacent or false otherwise), and Iterable (iterates through adjacent nodes).arrow_forward
- Can you help me with a C++ programming task I am trying to complete for myself please: Write a program (in main.cpp) that: Prompts the user for a filename containing node data. Outputs the minimal spanning tree for a given graph. You will need to implement the createSpanningGraph method in minimalSpanTreeType.h to create the graph and the weight matrix. There are a few tabs: main.cpp, graphType.h, linkedList.h, linkedQueue.h, queueADT.h, minimalSpanTreeType.h, and then two data files labeled: CH20_Ex21Data.txt, CH20Ex4Data.txtarrow_forwardLiskov Substitution Principle (LSP)a. Propose a way to fix the problem in there.arrow_forwardComputer Science java programarrow_forward
- Using the graph in the question: Write Java code to create an Adjacency Matrix M to represent the graph. Write Java code to create an Adjacency List L to represent the graph.arrow_forwardCourse:Data Structures and Algorithms Given the graph class using the adjacency matrix representation,Implement the DFS algorithm ongraph using the adjacency matrix representation. You have to use thegraph code as a starting point, and add the DFS method in the graph class similar to the BFS method already present in the Graph class. Note: Code in C++arrow_forwardIn C++ language. Sample run should be exact as the one on image. Thanksarrow_forward
- Exercise # 2: Modify getShortestPath (graph, from, to) function so that if there is more than one minimum path from source node to destination node, it gives you the one with the minimum number of edges. Example 1: Input Graph: Adjacency list of the following graph. 6. 4 From: A To: For B [(A, F)] That is, the distance from A to Fis 10 both through the following path A>D>F, AD>c>G>F Output the correct answer is AD>F ((A, B) That is, the distance from A to B is 7 both through the following paths A>B, AD>c>B the correct answer is ABarrow_forwardPlease help with this Java program, and include explanations and commentsarrow_forwardPlease provide python 3 code. Also, add comments Don't use classes!arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education