0% found this document useful (0 votes)
14 views

221902285-Algorithm Lab Report 4

This lab report discusses an algorithm lab experiment to find the topological order of nodes in a graph using depth-first search (DFS). The student implemented a Java program that takes a directed graph as input and uses DFS with cycle detection to output the topological order. The program was tested on sample input from a text file and produced the correct output, demonstrating it can find topological order for acyclic graphs as intended.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

221902285-Algorithm Lab Report 4

This lab report discusses an algorithm lab experiment to find the topological order of nodes in a graph using depth-first search (DFS). The student implemented a Java program that takes a directed graph as input and uses DFS with cycle detection to output the topological order. The program was tested on sample input from a text file and produced the correct output, demonstrating it can find topological order for acyclic graphs as intended.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Green University of Bangladesh

Department of Computer Science and Engineering (CSE)


Faculty of Sciences and Engineering
Semester: (Fall, Year:2023), B.Sc. in CSE (Day)

Lab Report No. #04


Course Title: Algorithm Lab
Course Code: CSE-206 Section: 221-D6

Student Details

Name ID

1. Md. Tasnimur Rahman Shakir 221902285

Submission Date : 08.12.2023


Course Teacher’s Name : Jargis Ahmed

Lab Report Status


Marks: ………………………………… Signature:.....................
Comments:.............................................. Date:..............................
1. TITLE OF THE LAB REPORT EXPERIMENT
Find the topological order of nodes of a graph using DFS.

2. OBJECTIVES/AIM
The objective of this lab is to develop a Java program that utilizes the Depth-First Search
(DFS) algorithm to find the topological order of nodes in a graph. The program aims to
handle acyclic directed graphs and identify cycles to ensure a valid topological order.

3. IMPLEMENTATION
Question 1:
Write a program to find the topological order of nodes of a graph using DFS.

Code:
package GraphTheory;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;

public class TopologicalOrder {


static void topoOrder(int source, ArrayList<ArrayList<Integer>> list,
boolean visited[], boolean cycleCheck[], Stack<Integer> result) {
visited[source] = true;
cycleCheck[source] = true;

for (Integer x : list.get(source)) {


if (!visited[x]) {
topoOrder(x, list, visited, cycleCheck, result);
} else if (cycleCheck[x]) {
System.out.println("Graph has a cycle. Topological sort not
possible.");
System.exit(0);
}
}

cycleCheck[source] = false;
result.push(source);
}

public static void main(String[] args) {


File file = new File("Topological Order.txt");
int nodeNo = 0, edgeNo = 0;
ArrayList<ArrayList<Integer>> list = new ArrayList<>();

try {
Scanner sc = new Scanner(file);
nodeNo = sc.nextInt();
edgeNo = sc.nextInt();
for (int i = 0; i < nodeNo; i++) {
list.add(new ArrayList<>());
}

while (sc.hasNextLine()) {
int s = sc.nextInt();
int d = sc.nextInt();
list.get(s).add(d);
}
} catch (FileNotFoundException e) {
System.out.println("File Not Found.");
}

boolean visited[] = new boolean[nodeNo];


boolean cycleCheck[] = new boolean[nodeNo];
Stack<Integer> result = new Stack<>();

for (int i = 0; i < nodeNo; i++) {


if (!visited[i]) {
topoOrder(i, list, visited, cycleCheck, result);
}
}

System.out.println("Topological Order:");
while (!result.empty()) {
System.out.print(result.pop() + " ");
}
}
}
5. TEST RESULT / OUTPUT
The Graph:

Input in “Topological Order.txt”


5
6
0 1
0 2
1 3
2 1
2 3
2 4
Output:

6. Discussion
The implemented program successfully achieves the objective of finding the topological
order of nodes in a directed acyclic graph. The DFS algorithm ensures a systematic
exploration of the graph, and the cycle detection mechanism prevents the algorithm from
producing an incorrect topological order for cyclic graphs.

You might also like