In the following graph, write a Java program using Topological Sort.
please write java code and show me the output.
Thank you :)
CODE:
import java.io.*;
import java.util.*;
public class Graph
{
private int V; // No. of vertices
private LinkedList<Integer> adj[]; // Adjacency List
//Constructor
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
// Function to add an edge into the graph
void addEdge(int v,int w) { adj[v].add(w); }
// A recursive function used by topologicalSort
void topologicalSortUtil(int v, boolean visited[],
Stack stack)
{
// Mark the current node as visited.
visited[v] = true;
Integer i;
Iterator<Integer> it = adj[v].iterator();
while (it.hasNext())
{
i = it.next();
if (!visited[i])
topologicalSortUtil(i, visited, stack);
}
// Push current vertex to stack which stores result
stack.push(new Integer(v));
}
void topologicalSort()
{
Stack stack = new Stack();
// Mark all the vertices as not visited
boolean visited[] = new boolean[V];
for (int i = 0; i < V; i++)
visited[i] = false;
// Call the recursive helper function to store
// Topological Sort starting from all vertices
// one by one
for (int i = 0; i < V; i++)
if (visited[i] == false)
topologicalSortUtil(i, visited, stack);
// Print contents of stack
while (stack.empty()==false)
System.out.print(stack.pop() + " ");
}
// Driver method
public static void main(String args[])
{
// Create a graph given in the above diagram
Graph g = new Graph(8);
g.addEdge(0, 1);
g.addEdge(0, 3);
g.addEdge(1, 2);
g.addEdge(3, 4);
g.addEdge(3, 5);
g.addEdge(4, 5);
g.addEdge(4, 6);
g.addEdge(5, 6);
g.addEdge(5, 7);
System.out.println("Following is a Topological " +
"sort of the given graph");
g.topologicalSort();
}
}
OUTPUT:
Following is a Topological sort of the given graph 0 3 4 5 7 6 1 2
NOTE: Consider
a as 0,
b as 1,
c as 2,
d as 3,
e as 4,
f as 5,
g as 6,
h as 7
In the following graph, write a Java program using Topological Sort. please write java code and...
Write a java program to do BFS topological sort . your program must be able to catch the loop. m у 2.
In Java code, for a topological sort assignment: How I load a .txt file and use its information to populate a hashtable that maps IDs to characters, and add the IDs as nodes in the graph.
Hello, I need the correct answer for this please , a quick sort program -java code- that have 2 arrays first one will have different values , the second one the values will be from smallest to largest number then the program have a quick sort to do it for both of them and will count the time - how long the quick sort is take for each one - which array is faster. Thank you ❤️
write a program for topological ordering of a graph in c/c++ programming. The input contains a list of adjacent list of a directed acyclic graph. The output is a topological ordering of verticies.
JAVA question: Write a program to sort the following US States in their natural order : Missouri, Illinois, California, Washington, Maine. HINT : Create a List of the States and user the sort method of the Collections interface. Please copy and paste your code below.
Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...
---Using Java to solve--- Write the code for three sort algorithms. Sort an array of Integers, strings and an array of Cars using each technique. At least seven items in each case.
Shortest Path (DAG) Students, In this coding assignment, you will be asked to write Java code to read a text file (input.txt) of a graph represented as an adjacency list. Example: A, 7, B, 9, C B, 1, C C, 8, E, 2, F D, 1, E, 3, G E, 3, G F, 3, D, -3, H H, 6, G K, 3, H, -1, F The graph represented by the adjacency list above looks like this: The graph is a...
3. Apply Topological sort algorithm on the following graph. Then, draw the sorted graph. 11 marvel