Emon Algorithm Report01
Emon Algorithm Report01
Lab Report NO # 01
Course Title: Algorithms
Lab
Course Code: CSE 206 Section: 223,D4
Name ID
1. Moinul Hasan 221902281
2. OBJECTIVES
Through this lab, the aim is to gain a practical understanding of BFS and DFS
traversal techniques, cycle detection mechanisms, shortest path finding, and the
importance of topological sorting in organizing dependencies in a directed graph.
This also helps in comparing the applicability of BFS and DFS for different graph-
based problems.
3. PROCEDURE
1. Define graph structure using adjacency lists.
2. Implement BFS for cycle detection.
3. Implement BFS for shortest path finding.
4. Implement DFS for topological sorting.
5. Test BFS for cycle detection and pathfinding.
6. Test DFS for topological sort.
7. Analyze and verify results.
4. IMPLEMENTATION
Source code:
1. BFS Cycle Detection and Path
Finding: import java.util.*;
class GraphBFS {
private int V;
private LinkedList<Integer>[] adjList;
GraphBFS(int v) {
V = v;
adjList = new
LinkedList[v]; for (int i = 0;
i < v; i++) {
adjList[i] = new LinkedList<>();
}
}
void addEdge(int v, int w)
{ adjList[v].add(w);
adjList[w].add(v);
}
boolean detectCycle() {
boolean[] visited = new
boolean[V]; int[] parent = new
int[V];
for (int i = 0; i < V; i++)
{ if (!visited[i]) {
if (bfsCycleCheck(i, visited, parent))
{ return true;
}
}
}
return false;
}
private boolean bfsCycleCheck(int src, boolean[] visited, int[] parent)
{ Queue<Integer> queue = new LinkedList<>();
queue.add(src);
visited[src] = true;
while (!queue.isEmpty()) {
int node = queue.poll();
for (int neighbor : adjList[node])
{ if (!visited[neighbor])
{
visited[neighbor] = true;
queue.add(neighbor);
parent[neighbor] = node;
} else if (parent[node] != neighbor)
{ return true;
}
}
}
return false;
}
while (!queue.isEmpty()) {
int node = queue.poll();
if (node == dest)
{ break; }
Collections.reverse(path);
if (path.get(0) == src) {
return path;
} else {
return Collections.emptyList();
}
}
import java.util.*;
class GraphDFS {
private int V;
private LinkedList<Integer>[] adjList; // Adjacency
list GraphDFS(int v) {
V = v;
adjList = new
LinkedList[v]; for (int i = 0;
i < v; i++) {
adjList[i] = new LinkedList<>();
}
}
void addEdge(int v, int w)
{ adjList[v].add(w); }
private void dfsTopologicalSort(int node, boolean[] visited, Stack<Integer> stack)
{ visited[node] = true;
stack.push(node);
}
graph.topologicalSort();
}
}
Output:
ANALYSIS AND DISCUSSION
In this lab, BFS and DFS were applied to solve different graph problems. BFS was used for cycle
detection and shortest path finding in an undirected graph. It efficiently detects cycles by
checking re-visited nodes and guarantees the shortest path in unweighted graphs, with time
complexity O(V
+ E). Meanwhile, DFS was utilized for topological sorting in a Directed Acyclic Graph (DAG), correctly
producing a valid ordering of nodes based on dependencies. While BFS is best for shortest paths, DFS excels
in organizing tasks with dependencies. Both algorithms are crucial in different graph-related applications,
demonstrating