Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please post working code. Here is start up code given by the instructor. Start up code: import java.io.*; import java.util.*; class Node{ private int data;

image text in transcribed

image text in transcribed

please post working code. Here is start up code given by the instructor. Start up code:

import java.io.*; import java.util.*; class Node{ private int data; private Node nextNodePtr; public Node(){} public void setData(int d){ data = d; } public int getData(){ return data; } public void setNextNodePtr(Node nodePtr){ nextNodePtr = nodePtr; } public Node getNextNodePtr(){ return nextNodePtr; } } class List{ private Node headPtr; public List(){ headPtr = new Node(); headPtr.setNextNodePtr(null); } public Node getHeadPtr(){ return headPtr; } public boolean isEmpty(){ if (headPtr.getNextNodePtr() == null) return true; return false; } public void insert(int data){ Node currentNodePtr = headPtr.getNextNodePtr(); Node prevNodePtr = headPtr; while (currentNodePtr != null){ prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr.getNextNodePtr(); } Node newNodePtr = new Node(); newNodePtr.setData(data); newNodePtr.setNextNodePtr(null); prevNodePtr.setNextNodePtr(newNodePtr); } public void insertAtIndex(int insertIndex, int data){ Node currentNodePtr = headPtr.getNextNodePtr(); Node prevNodePtr = headPtr; int index = 0; while (currentNodePtr != null){ if (index == insertIndex) break; prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr.getNextNodePtr(); index++; } Node newNodePtr = new Node(); newNodePtr.setData(data); newNodePtr.setNextNodePtr(currentNodePtr); prevNodePtr.setNextNodePtr(newNodePtr); } public int read(int readIndex){ Node currentNodePtr = headPtr.getNextNodePtr(); Node prevNodePtr = headPtr; int index = 0; while (currentNodePtr != null){ if (index == readIndex) return currentNodePtr.getData(); prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr.getNextNodePtr(); index++; } return -1; // an invalid value indicating // index is out of range } public void modifyElement(int modifyIndex, int data){ Node currentNodePtr = headPtr.getNextNodePtr(); Node prevNodePtr = headPtr; int index = 0; while (currentNodePtr != null){ if (index == modifyIndex){ currentNodePtr.setData(data); return; } prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr.getNextNodePtr(); index++; } } public void deleteElementAtIndex(int deleteIndex){ Node currentNodePtr = headPtr.getNextNodePtr(); Node prevNodePtr = headPtr; Node nextNodePtr = headPtr; int index = 0; while (currentNodePtr != null){ if (index == deleteIndex){ nextNodePtr = currentNodePtr.getNextNodePtr(); break; } prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr.getNextNodePtr(); index++; } prevNodePtr.setNextNodePtr(nextNodePtr); } public void deleteElement(int deleteData){ Node currentNodePtr = headPtr.getNextNodePtr(); Node prevNodePtr = headPtr; Node nextNodePtr = headPtr; while (currentNodePtr != null){ if (currentNodePtr.getData() == deleteData){ nextNodePtr = currentNodePtr.getNextNodePtr(); break; } prevNodePtr = currentNodePtr; currentNodePtr = currentNodePtr.getNextNodePtr(); } prevNodePtr.setNextNodePtr(nextNodePtr); } public void IterativePrint(){ Node currentNodePtr = headPtr.getNextNodePtr(); while (currentNodePtr != null){ System.out.print(currentNodePtr.getData()+" "); currentNodePtr = currentNodePtr.getNextNodePtr(); } System.out.println(); } public int countList(){ Node currentNodePtr = headPtr.getNextNodePtr(); int countElements = 0; while (currentNodePtr != null){ //System.out.print(currentNodePtr.getData()+" "); countElements++; currentNodePtr = currentNodePtr.getNextNodePtr(); } return countElements; } } class Graph{ private int numNodes; private List[] adjacencyList; private int[] pushOrder; private int[] popOrder; private int lastPushNumber = 0; private int lastPopNumber = 0; Graph(int n){ numNodes = n; adjacencyList = new List[numNodes]; pushOrder = new int[numNodes]; popOrder = new int[numNodes]; for (int id = 0; id ject 7: Use the Results of Depth First Search to Assign Directlons to an Undirected Graph and Obtain a Directed Acyclic Graph (DAG) Due: April 12, 2018: by 1 PM (in Canvas) The objective of this project is to use the Depth First Search (DFS) algorithm to assign directions to the edges of an undirected graph so that the resulting directed graph is a directed acyclic graph (DAG). You are given the code to run the DFS on an undirected graph as well as all the auxiliary classes needed. The Graph class has member variables to assign the push order and pop order for the vertices as well as to keep track of the number of vertices that have been pushed and popped until then. Strategy: Run DFS on the given undirected graph and determine the pop order of the vertices. For any undirected edge u-v in the graph, assign the direction u-> v if the pop order of u is greater than the pop order of v, or assign the direction v-> u if the pop order of v is greater than the pop order of u. You are given the code for running the DFS algorithm (in a recursive fashion) on an undirected graph (whose edge information is input to the user). Your tasks in this project are as follows (Submit as one word or PDF document) (1-60 pts) Extend the DFS algorithm to determine/set the the directions assigned for the edges so that the resulting directed graph is a DAG. Include the e push and pop order of the vertices and extend the code in the main function to use the pop order of the vertices in the graph and assign the directions to the edges. Your code in the main function should output the push and pop order of the vertices as well as code with all the extensions in your project report 2 15 pts) Justify why the above suggested strategy of assigning edges wilindeed work Give a explanation (3- 15 pts) Draw an undirected graph of 1O or more vertices and use the ahove strategy to assign directions of the edges. (4- 10 pls) Prepare the edge text file based on the undirected graph of (3, pass it to your code of (I capture a screenshot of the output. Compare the directions of the edges assigned in (3) and (4):t expected to be the same

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

Expert Oracle9i Database Administration

Authors: Sam R. Alapati

1st Edition

1590590228, 978-1590590225

More Books

Students also viewed these Databases questions