Anonymous
timer Asked: Apr 18th, 2020

Question Description

In this assignment, you will build upon the Node and Edge classes from Homework Assignment 8 to create a class representing a graph. I have provided you with the following files: • NodeEdge.jar: Java archive containing compiled Node and Edge classes (see Homework 8 solution for implementation). • Graph.java: implementation of an abstract class representing general graphs. Since it is an abstract class, it cannot be used to create objects. • UndirectedGraph.java: partial implementation of a class representing undirected graphs. An undirected graph is a graph that contains only undirected edges. See two examples of undirected graphs shown in Figure 1 below. The UML diagram for the graph classes is shown in Figure 2.

Unformatted Attachment Preview

4/16/2020 https://blackboard.utdl.edu/bbcswebdav/pid-7773195-dt-content-rid-64930850_1/courses/XLST.6X.202010/Edge.java package edu.utoledo.eecs1510.graph; public class Edge { private Node source, target; private boolean directed; public Edge(Node source, Node target) { this(source, target, false); } public Edge(Node source, Node target, boolean directed) { this.source = source; this.target = target; this.directed = directed; } public Edge(Edge e) { this(e.source, e.target, e.directed); } public Node getSourceNode() { return source; } public Node getTargetNode() { return target; } public Node[] getNodes() { Node[] nodeArray = {source, target}; return nodeArray; } public boolean isDirected() { return directed; } public String toString() { String edgeStr = "(" + source.getId() + ", " + target.getId() + ")"; return edgeStr; } public boolean equals(Edge e) { // If one edge is directed and the other undirected, then they can't // be equivalent if (directed != e.isDirected()) return false; // Two undirected edges can be equivalent if they connect the same two // nodes but have source and target backwards if (directed == false) { if (source.equals(e.target) && target.equals(e.source)) return true; } } } if (source.equals(e.source) && target.equals(e.target)) return true; else return false; https://blackboard.utdl.edu/bbcswebdav/pid-7773195-dt-content-rid-64930850_1/courses/XLST.6X.202010/Edge.java 1/1 EECS 1510 Spring 2020 Sections 009/011/093 Introduction to Object-Oriented Programming Instructor: Dr. Kevin S. Xu Homework Assignment 9 Undirected Graphs Assigned: Friday, April 10, 2020 Due: Thursday, April 16, 2020 at 11:59pm In this assignment, you will build upon the Node and Edge classes from Homework Assignment 8 to create a class representing a graph. I have provided you with the following files: • • • NodeEdge.jar: Java archive containing compiled Node and Edge classes (see Homework 8 solution for implementation). Graph.java: implementation of an abstract class representing general graphs. Since it is an abstract class, it cannot be used to create objects. UndirectedGraph.java: partial implementation of a class representing undirected graphs. An undirected graph is a graph that contains only undirected edges. See two examples of undirected graphs shown in Figure 1 below. The UML diagram for the graph classes is shown in Figure 2. graph2 graph1 Bob Bob Alice Alice Charlie Charlie Zulu Figure 1. Two examples of undirected graphs. Figure 2. UML diagram for graph classes. EECS 1510 Spring 2020 Sections 009/011/093 Introduction to Object-Oriented Programming Instructor: Dr. Kevin S. Xu CLASS SPECIFICATION The specification of the Graph abstract class is as follows: List of nodes in the graph. Constructs an empty graph with no nodes. Constructs a graph with the given list of nodes. Returns the list of nodes in the graph. Returns the index in nodeArray for the node with the given ID. Adds the given node and returns true if successfully added. Removes the given node and returns true if successfully removed. Returns true if node n has the same ID as the current node. Returns the edge with the given source and target nodes. Adds the given edge and returns true if successfully added. Removes the given edge and returns true if successfully removed. Returns a string representation of the graph. Since Graph is an abstract class, no Graph objects can be created. Notice that some of the methods are implemented while others in italic are abstract methods that require the subclass to provide the implementation. The specification of the UndirectedGraph subclass is as follows: List of edges in the graph stored as an adjacency list. Constructs an empty undirected graph with no nodes. Constructs an undirected graph with the given list of nodes. Returns the list of edges (neighbors) connected to the given node. ADJACENCY LIST The adjacency list is a way of representing the edges in a graph using ragged arrays. It is actually a list of lists, where the outer list (rows) consists of the nodes in the graph, and the inner list (columns) for each node contains the edges connected to that node, which we call the neighboring edges. For example, graph2 in Figure 1 could be represented as shown in Figure 3 below. Inner lists Outer list Alice: [(Alice, Bob), (Alice, Zulu), (Alice, Charlie)] Bob: [(Alice, Bob), (Bob, Charlie)] Charlie: [(Bob, Charlie), (Charlie, Zulu), (Alice, Charlie)] Zulu: [(Alice, Zulu), (Charlie, Zulu)] Figure 3. Illustration of adjacency list for graph2 in Figure 1. Notice that the inner lists can contain different numbers of elements because each node may have a different number of edges connected to it. The ith element in the outer list of adjacencyList should correspond to the node in the ith element of nodeArray. In the example adjacency list shown in Figure 3, the elements of nodeArray should be [Alice, Bob, Charlie, Zulu]. EECS 1510 Spring 2020 Sections 009/011/093 Introduction to Object-Oriented Programming Instructor: Dr. Kevin S. Xu SUBMISSION INSTRUCTIONS A portion of your grade will be assigned based on adherence to OOP practices. Submit only your fully implemented UndirectedGraph.java source file! Do not include a main() method in your submission! Sample output for the following code block: // Create nodes ArrayList allNodes = new ArrayList<>(); allNodes.add(new Node("Alice")); allNodes.add(new Node("Bob")); allNodes.add(new Node("Charlie")); allNodes.add(new Node("Zulu")); // Create graphs and add nodes to them UndirectedGraph graph = new UndirectedGraph(); graph.addNode(allNodes.get(0)); graph.addNode(allNodes.get(1)); graph.addNode(allNodes.get(2)); UndirectedGraph graph2 = new UndirectedGraph(allNodes); // Create edges ArrayList allEdges = new ArrayList<>(); allEdges.add(new Edge(graph2.getNode("Alice"), graph2.getNode("Bob"))); allEdges.add(new Edge(graph2.getNode("Bob"), graph2.getNode("Charlie"))); allEdges.add(new Edge(graph2.getNode("Alice"), graph2.getNode("Zulu"))); allEdges.add(new Edge(graph2.getNode("Charlie"), graph2.getNode("Zulu"))); allEdges.add(new Edge(graph2.getNode("Alice"), graph2.getNode("Charlie"))); // Add edges to graphs graph.addEdge(allEdges.get(0)); graph.addEdge(allEdges.get(1)); for (int i = 0; i < allEdges.size(); i++) graph2.addEdge(allEdges.get(i)); // Print node lists and adjacency lists System.out.println(graph.getNodeArray().toString()); System.out.println(graph.toString()); System.out.println(graph2.getNodeArray().toString()); System.out.println(graph2.toString()); Output: [Alice, Bob, Charlie] Alice: [(Alice, Bob)] Bob: [(Alice, Bob), (Bob, Charlie)] Charlie: [(Bob, Charlie)] [Alice, Bob, Charlie, Zulu] Alice: [(Alice, Bob), (Alice, Zulu), (Alice, Charlie)] Bob: [(Alice, Bob), (Bob, Charlie)] Charlie: [(Bob, Charlie), (Charlie, Zulu), (Alice, Charlie)] Zulu: [(Alice, Zulu), (Charlie, Zulu)] Additional practice problems from the textbook: 11.2 (see attached solution to 10.14 for implementation of the MyDate class), 11.5, 11.9, 11.12, 11.13, 13.3. 4/16/2020 https://blackboard.utdl.edu/bbcswebdav/pid-7773195-dt-content-rid-64930851_1/courses/XLST.6X.202010/Node.java package edu.utoledo.eecs1510.graph; public class Node { public static int numNodes = 0; private String id; public Node() { this(Integer.toString(numNodes)); } public Node(String id) { this.id = id; numNodes++; } public Node(Node n) { this(n.getId()); } public String getId() { return id; } public String toString() { return id; } public static int getNumNodes() { return numNodes; } } public boolean equals(Node n) { if (id == n.getId()) return true; else return false; } https://blackboard.utdl.edu/bbcswebdav/pid-7773195-dt-content-rid-64930851_1/courses/XLST.6X.202010/Node.java 1/1 4/16/2020 https://blackboard.utdl.edu/bbcswebdav/pid-7770899-dt-content-rid-64849796_1/courses/XLST.6X.202010/UndirectedGraph.java package edu.utoledo.eecs1510.graph; import java.util.ArrayList; public class UndirectedGraph extends Graph { private ArrayList> adjacencyList; } @Override public String toString() { // TODO Auto-generated method stub String adjacencyListString = ""; for (int i = 0; i < nodeArray.size(); i++) { String iStr = nodeArray.get(i).toString(); String iNeighborStr = adjacencyList.get(i).toString(); adjacencyListString += iStr + ": " + iNeighborStr; if (i < nodeArray.size() - 1) adjacencyListString += "\n"; } return adjacencyListString; } https://blackboard.utdl.edu/bbcswebdav/pid-7770899-dt-content-rid-64849796_1/courses/XLST.6X.202010/UndirectedGraph.java 1/1 4/16/2020 https://blackboard.utdl.edu/bbcswebdav/pid-7770899-dt-content-rid-64849794_1/courses/XLST.6X.202010/Graph.java package edu.utoledo.eecs1510.graph; import java.util.ArrayList; public abstract class Graph { protected ArrayList nodeArray; /** Create an empty graph with no nodes */ protected Graph() { nodeArray = new ArrayList<>(); } /** Create a graph with given list of nodes but no edges */ protected Graph(ArrayList nodeArray) { this.nodeArray = nodeArray; } /** Get copy of entire list of nodes */ public ArrayList getNodeArray() { // Return a copy of nodeArray using ArrayList copy constructor rather // than the reference to nodeArray to prevent unintended modifications return new ArrayList<>(nodeArray); } /** Get index in nodeArray for node with given ID */ protected int getNodeIndex(String id) { // Check if already a node with same ID is present. If so, return its // index in nodeArray; otherwise, return -1 to indicate node not // present for (int i = 0; i < nodeArray.size(); i++) { if (id == nodeArray.get(i).getId()) return i; } return -1; } /** Get Node object matching given ID */ public Node getNode(String id) { int nodeIndex = getNodeIndex(id); if (nodeIndex == -1) return null; else return nodeArray.get(nodeIndex); } /** Add node n to graph if it isn't already present */ public boolean addNode(Node n) { if (getNodeIndex(n.getId()) == -1) { // Node with same ID not present, so add the node to the end of // nodeArray nodeArray.add(n); return true; } else // Node with same ID already there, so don't add it return false; } /** Remove node n from graph if it is present */ public abstract boolean removeNode(Node n); /** Get Edge object for edge between given Node objects */ public abstract Edge getEdge(Node source, Node target); /** Add edge e between two nodes if it isn't already present */ https://blackboard.utdl.edu/bbcswebdav/pid-7770899-dt-content-rid-64849794_1/courses/XLST.6X.202010/Graph.java 1/2 4/16/2020 https://blackboard.utdl.edu/bbcswebdav/pid-7770899-dt-content-rid-64849794_1/courses/XLST.6X.202010/Graph.java public abstract boolean addEdge(Edge e); /** Remove edge e between two nodes if it is present */ public abstract boolean removeEdge(Edge e); } /** Output a string representation of the graph adjacency structure */ @Override public abstract String toString(); https://blackboard.utdl.edu/bbcswebdav/pid-7770899-dt-content-rid-64849794_1/courses/XLST.6X.202010/Graph.java 2/2 ...
Student has agreed that all tutoring, explanations, and answers provided by the tutor will be used to help in the learning process and in accordance with Studypool's honor code & terms of service.

This question has not been answered.

Create a free account to get help with this and any other question!

Similar Questions
Related Tags

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors