The objective of this project is to use the Depth First Search (DFS) algorithm t
ID: 3706311 • Letter: T
Question
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 proiect are as follows (Submit as one word or PDF document): (1 - 60 pts) Extend the DFS alorithm to determine/set the 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 the directions assigned for the edges so that the resulting directed graph is a DAG. Include the entire code with all the extensions in your project report. (2- 15 pts) Justify why the above suggested strategy of assigning edges will indeed work. Give a clear explanation (3 -15 pts) Draw an undirected graph of 10 or more vertices and use the above strategy to assign directions of the edges. 4 10 pts) Prepare the edge text file based on the undirected graph of (3), pass it to your code of (1) and capture a screenshot of the output. Compare the directions of the edges assigned in (3) and (4): they are expected to be the same A sample output for an undirected graph of ertices is sbown belom Enter the file nane for the edges of the grapht graphEdges 2.txt Enter nunber of nodes: sh and Pop Order of a Push and Pop Order of 1 Push and Pop Order of 2 3 sh and Pop Order of 35 3 Push and Pop Order of 4 Push and Pop Order of 5 6 2 sh and Pop Order of 6 7 sh and Pop Order of ? 8 2 Directions assigned to the edges 0-21 1-22 3 3-25 4->3 4-26 5-26 All questions .ust be answeredExplanation / Answer
JAVA CODING:
import java.util.*;
public class DFS{
private boolean [ ] visited;
public DFS { graph G, int s)
{
visited= new boolean [ GY()];
validdatevertex(s);
Iterator<integer> adj=(Iterator<integer>[])new iterator[GV()];
for(int v=0; v<GV();v++)
adj[v]=c+.adj(v).iterator();
visited[s]=true;
st.push(s);
while(!st.is empty()){
int v=st.peek();
if(adj[v].has next()){
int w=adj[v].next();
if(!visited[w]){
visited[w]=true;
st.push(w);
stdout.printf("dfs(%d) ",w);
}
else
{
stack.pop();
}
}
}
public boolean visited (int v){
validate-vertex(v);
return visited[v];
}
private void validate-vertex(int v){
int v=visited.length;
if(v<0 // v>=v)
throw new illegalarguement exception
("vertex"+v+" is not between 0&"+(v-1))
}
public static voidmain(string[]args)
{
IN i=new in(args(0));
graph g=new graph(i);
ints=integer.parse int(args[1]);
DFS dfs=new DFS(G,S);
for(int v=0;v<GV();v++)
{
if(dfs.visited(v))
stdout.print(v+"");
stdout.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.