ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Fortran Depth-First Search (DFS) on Adjacency List

Fortran

Goal -- WPM

Ready
Exercise Algorithm Area
1PROGRAM dfs_example
2IMPLICIT NONE
3
4INTEGER, PARAMETER :: MAX_NODES = 10
5INTEGER :: adj_list(MAX_NODES) ! Stores the number of neighbors for each node
6INTEGER, ALLOCATABLE :: graph(:,:)
7LOGICAL :: visited(MAX_NODES)
8INTEGER :: num_nodes, start_node
9INTEGER :: i, u, v
10
11! Example graph setup
12num_nodes = 5
13adj_list = [2, 3, 2, 1, 0] ! Number of neighbors for nodes 1 to 5
14ALLOCATE(graph(MAX_NODES, MAX_NODES))
15graph = 0 ! Initialize with no edges
16
17! Define edges (undirected graph)
18! Node 1 neighbors: 2, 3
19graph(1, 1) = 2; graph(1, 2) = 3
20! Node 2 neighbors: 1, 3, 4
21graph(2, 1) = 1; graph(2, 2) = 3; graph(2, 3) = 4
22! Node 3 neighbors: 1, 2
23graph(3, 1) = 1; graph(3, 2) = 2
24! Node 4 neighbors: 2
25graph(4, 1) = 2
26! Node 5 has no neighbors
27
28start_node = 1
29
30PRINT *, "Graph represented by adjacency list (node, neighbors):"
31DO i = 1, num_nodes
32PRINT *, "Node ", i, " has ", adj_list(i), " neighbors:"
33DO v = 1, adj_list(i)
34PRINT *, " ", graph(i, v)
35END DO
36END DO
37PRINT *
38
39CALL dfs(graph, adj_list, visited, num_nodes, start_node)
40
41PRINT *, "DFS traversal starting from node ", start_node, ":"
42DO i = 1, num_nodes
43IF (visited(i)) THEN
44PRINT *, "Visited node: ", i
45END IF
46END DO
47
48DEALLOCATE(graph)
49
50CONTAINS
51
52SUBROUTINE dfs(graph, adj_list, visited, num_nodes, current_node)
53IMPLICIT NONE
54INTEGER, INTENT(IN) :: graph(MAX_NODES, MAX_NODES), adj_list(MAX_NODES)
55LOGICAL, INTENT(INOUT) :: visited(MAX_NODES)
56INTEGER, INTENT(IN) :: num_nodes, current_node
57INTEGER :: v
58
59! Mark the current node as visited
60visited(current_node) = .TRUE.
61PRINT *, "Visiting node: ", current_node
62
63! Recur for all the vertices adjacent to this vertex
64DO v = 1, adj_list(current_node)
65INTEGER :: neighbor
66neighbor = graph(current_node, v)
67IF (.NOT. visited(neighbor)) THEN
68CALL dfs(graph, adj_list, visited, num_nodes, neighbor)
69END IF
70END DO
71END SUBROUTINE dfs
72
73END PROGRAM dfs_example
Algorithm description viewbox

Fortran Depth-First Search (DFS) on Adjacency List

Algorithm description:

This Fortran program implements Depth-First Search (DFS), a graph traversal algorithm that explores as far as possible along each branch before backtracking. It starts at a given node and explores each neighbor recursively. DFS is used for topological sorting, finding connected components, and solving puzzles like mazes. The implementation uses an adjacency list for the graph and recursion to manage the traversal path.

Algorithm explanation:

DFS has a time complexity of O(V + E), where V is the number of vertices and E is the number of edges, as each vertex and edge is visited at most once. The space complexity is O(V) in the worst case for the recursion stack (or an explicit stack if implemented iteratively). Edge cases include disconnected graphs (DFS will only explore the component containing the start node), graphs with cycles (handled by the `visited` array), and starting DFS from an isolated node. The correctness relies on the recursive structure, which naturally explores depth-first, and the `visited` array preventing infinite loops in cyclic graphs.

Pseudocode:

FUNCTION dfs(graph, adj_list, visited, num_nodes, current_node):
  Mark current_node as visited
  PRINT current_node

  FOR each neighbor v of current_node (from adj_list[current_node]):
    IF v is not visited:
      dfs(graph, adj_list, visited, num_nodes, v)