Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, guys. my source code is fine, but the output sounds nonsense. I wonder if you can help or give me some feedback about my

Hello, guys. my source code is fine, but the output sounds nonsense. I wonder if you can help or give me some feedback about my code. THANK YOU!!!!!!

Definitions and Specifications:

A task (e.g., A B C ...) can be completed during a level only if it has no dependencies or all its dependencies have been have been completed.

A dependency: (A, B) indicates that task A has to be completed before B.

A task list is a list of all tasks to be completed.

A dependency list is a list of all dependencies considered.

The task list and dependency list will be read from a text file. The task list and the dependency list considered can be modified during run time using user inputs.

The task list and dependency list is read into the application from a single text file (taskData.txt). The taskData.txt file should follow the format: (1) the first line in the txt file list all the task to be completed, separated by a space (2) the second line list all the dependencies (3) a dependency is given as two task inside a parenthesis separated by a comma (4) the first task given in the dependency must be completed before the second task in the dependency can be completed (5) the dependencies in the second line are separated by a space.

Given the data in the taskData.txt, provide the following:

1) a valid ordering of task if one exist; 2) if no valid ordering is possible, identify a cycle that prevents a valid ordering; 3) if a valid ordering exist, indicate whether or not the there is more than one; and 4) the minimum number of levels needed to complete all tasks. 5) the option to keep adding tasks and dependencies and getting the information provided in 1-4 using the amended task list and dependencies.

Example Interaction 1:

When the data in taskData.txt is ABCDEFG (A,B) (C,D) (A,C) (C,E) (E,G) (F,G) (B,E) (D,F)

Welcome to my Task Scheduling Project:

A valid ordering of tasks is as follows: ABCDEFG This is NOT the only valid ordering of task. The minimum number of levels is five.

Select: (a) add task (b) add dependencies (c) quit >> a

What tasks to you want to add? >> H I

What dependencies does H have? >> C

What tasks depend on H? >> I

What dependencies does I have? >D

What depends on I? >>

Do you want to add additional dependencies? (Y/N) >> Y

What dependencies do you want to add? >> (I, B) (F, I)

A valid ordering of tasks is as follows: ACHDEFIGB This is NOT the only valid ordering of task. The minimum number of levels is six.

Select: (a) add task (b) add dependencies (c) quit >> b

What dependencies do you want to add?

>> (F,C)

There is NO valid ordering of task. There is a cycle: C D F C

Example Interaction 2:

When the data in taskData.txt is

ABC (A,B) (B,C) (A,C)

Welcome to my Task Scheduling Project:

A valid ordering of tasks is as follows: ABC This is the ONLY valid ordering of task. The minimum number of levels is three.

Select: (a) add task (b) add dependencies (c) quit >> c

package TopologicalSort; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.LinkedList; import java.util.Scanner; import java.util.Stack; import java.util.*; import java.io.*;

class Graph { public int V; // No. of vertices public ArrayList> adj; // Adjacency List public int path; public int[] indegree; public int minLevels; //adj will become ArrayList> boolean cycle = false; ArrayList cyclePath; ArrayList lineOfCircle; // static HashMap nodeName; int[] charNode = new int[26]; //Constructor Graph(int v) { V = v; indegree = new int[V]; path = 1; minLevels = 1; lineOfCircle = new ArrayList(); adj = new ArrayList>(); for (int i=0; i()); charNode = new int[26]; cyclePath = new ArrayList(); }

// Function to add an edge into the graph void addEdge(int v,int w) { adj.get(v).add(w); indegree[w]++; }

void addNode(char name) { // nodeName.put(name, V); charNode[name - 'A'] = V; V++; adj.add(new ArrayList()); int[] nexIndegree = new int[V]; for(int i=0; i

// A recursive function used by topologicalSort void topologicalSortUtil(int v, boolean visited[], Stack stack, boolean currStack[], int depth) { // Mark the current node as visited. visited[v] = true; //Integer i;

// Recur for all the vertices adjacent to this // vertex //Iterator it = adj[v].iterator(); ArrayList neighbours = adj.get(v); int unvis = 0; for(int i : neighbours) { // i = it.next(); if(currStack[i]) { cycle = true; while(!stack.isEmpty()) { int out = (Integer)stack.pop(); lineOfCircle.add(out); if(out == i)break; } } if(cycle)return; if (!visited[i] ) { currStack[i] = true; topologicalSortUtil(i, visited, stack, currStack, depth + 1); currStack[i] = false; unvis++; } if(unvis > 0) path *= unvis; if(cycle)return; }

// Push current vertex to stack which stores result stack.push(new Integer(v)); minLevels = Math.max(minLevels , depth); }

// The function to do Topological Sort. It uses // recursive topologicalSortUtil() void topologicalSort() { Stack stack = new Stack();

// Mark all the vertices as not visited boolean visited[] = new boolean[V]; boolean currStack[] = new boolean[V];

for (int i = 0; i < V; i++) visited[i] = false; int depth =1 ; // Call the recursive helper function to store // Topological Sort starting from all vertices // one by one int unvis = 0; for (int i = 0; i < V; i++) if (visited[i] == false && indegree[i] ==0) { unvis++; currStack[i] = true; topologicalSortUtil(i, visited, stack, currStack,depth + 1); currStack[i] = false; if(cycle)break; } // System.out.println("main fn unvis" + unvis) ; path *= unvis; if(cycle) { System.out.println("no Valid Ordering System due to cycle"); // for(int x : lineOfCircle)System.out.print(x + " "); return; } else { System.out.println("A valid ordering of tasks is as follows: "); } // Print contents of stack while (stack.empty()==false) { int val = (Integer)stack.pop(); char nam = (char)(val + 'A'); System.out.print(nam + " "); } System.out.println(); } }

public class myFile { public static void AddTask(String in, Graph g, Scanner sc) { String[] taskPre = in.split(" "); char[] tasks = new char[taskPre.length]; int i = g.V;int j =0; for(String str : taskPre) { char ad = str.charAt(0); tasks[j] = ad; g.charNode[ad - 'A'] = i; g.addNode(ad); // System.out.println("current task : "+ ad); i++;j++; } for(j =0; j < taskPre.length; j++) { System.out.println("What dependencies does " + tasks[j] +" have?" ); String inp = sc.nextLine(); // System.out.println("inp is " + inp); String[] dcs = inp.split(" "); char[] dependencies = new char[dcs.length]; int iter = 0 ; for(String str : dcs) { dependencies[iter] = str.charAt(0); iter++; } for(char x : dependencies) { // System.out.println("x is " + x); g.addEdge(x - 'A', tasks[j] - 'A'); } System.out.println("What task depend on " + tasks[j] + " ?" ); inp = sc.nextLine();

dcs = inp.split(" "); // System.out.println("length of dcs is" + dcs.length + "contains" + dcs[0]); if(!dcs[0].equals("")) { char[] depends = new char[dcs.length]; iter = 0 ; for(String str : dcs) { depends[iter] = str.charAt(0); iter++; } for(char x : depends) { g.addEdge(tasks[j] - 'A', x - 'A'); } } } } public static void AddDep(Graph g, Scanner in) { System.out.println("What dependencies do you want to add?"); String DependLine = in.nextLine(); String[] depPre = DependLine.split(" "); for(String str : depPre) { char up = str.charAt(1); char down = str.charAt(3); int upnum = g.charNode[up - 'A']; int dnnum = g.charNode[down - 'A']; g.addEdge(upnum, dnnum); // System.out.println(up + "<" + down); } // System.out.println("Added dependency"); }

public static void main(String[] args) throws FileNotFoundException { // TODO Auto-generated method stub // String path = "taskData.txt"; // FileInputStream fis = new FileInputStream(path); // BufferedReader in = new BufferedReader(new FileReader("taskData.txt"));

File file = new File("/Volumes/BLACKSPIDER/TogologicalSortFile/src/TopologicalSort/taskData.txt"); if (!file.exists()) { System.out.println("does not exist."); return; }

Scanner scan = new Scanner(file); String taskLine = scan.nextLine(); String[] taskPre = taskLine.split(" "); char[] tasks = new char[taskPre.length];

String DependLine = scan.nextLine(); String[] depPre = DependLine.split(" ");

int nodeNumber =tasks.length;

Graph g = new Graph(nodeNumber); int i = 0; for(String str : taskPre) { char ad = str.charAt(0); tasks[i] = ad; g.charNode[ad - 'A'] = i; // System.out.println("current task : "+ ad); i++; } for(String str : depPre) { char up = str.charAt(1); char down = str.charAt(3); int upnum = g.charNode[up - 'A']; int dnnum = g.charNode[down - 'A']; g.addEdge(upnum, dnnum); // System.out.println(up + "<" + down); } g.topologicalSort(); if(g.path > 1 ) System.out.println("This is NOT the only valid ordering of task."); else System.out.println("This is the ONLY valid ordering of task."); // for(int z = 0 ; z < 3 ; z++) { // System.out.println("indg" + g.indegree[z]); // }

System.out.println("The minimum number of levels is " + g.minLevels);

Scanner input = new Scanner(System.in); System.out.println("Select: (a) add task (b) add dependency (c) quit (d) show the status "); char choice = input.nextLine().charAt(0); //input.nextLine(); while (choice != 'c') { if(choice == 'a') { System.out.println("(a)add task "); String intask = input.nextLine(); //System.out.println("entering addtask<" + intask + ">"); AddTask(intask , g, input);

System.out.println("Do you want to add additional dependencies? (Y/N) "); char yorn = input.nextLine().charAt(0); if(yorn == 'Y') AddDep(g,input); // break; } else if(choice == 'b') { System.out.println("(b)add dependencies "); AddDep(g,input); // break; } else if(choice == 'c') { System.out.println("(c)Quit! "); break; } else if(choice == 'd') { System.out.println("(d)Current status of tasks "); g.topologicalSort(); if(!g.cycle) { if(g.path > 1 ) System.out.println("This is NOT the only valid ordering of task."); else System.out.println("This is the ONLY valid ordering of task."); System.out.println("The minimum number of levels is " + g.minLevels); } } else System.out.println("Invalid. Please read carefully the choices: a, b,c,d "); System.out.println("Select: (a) add task (b) add dependency (c) quit (d) show the status "); choice = input.nextLine().charAt(0); input.nextLine(); } } }

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

Finance The Role Of Data Analytics In Manda Due Diligence

Authors: Ps Publishing

1st Edition

B0CR6SKTQG, 979-8873324675

More Books

Students also viewed these Databases questions

Question

2. What potential barriers would you encourage Samuel to avoid?

Answered: 1 week ago