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

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

Fortran Breadth-First Search (BFS) on Adjacency List

Fortran

Goal -- WPM

Ready
Exercise Algorithm Area
1PROGRAM bfs_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 :: queue(MAX_NODES)
9INTEGER :: front, rear
10INTEGER :: num_nodes, start_node
11INTEGER :: i, u, v
12
13! Example graph setup
14num_nodes = 5
15adj_list = [2, 3, 2, 1, 0] ! Number of neighbors for nodes 1 to 5
16ALLOCATE(graph(MAX_NODES, MAX_NODES))
17graph = 0 ! Initialize with no edges
18
19! Define edges (undirected graph)
20! Node 1 neighbors: 2, 3
21graph(1, 1) = 2; graph(1, 2) = 3
22! Node 2 neighbors: 1, 3, 4
23graph(2, 1) = 1; graph(2, 2) = 3; graph(2, 3) = 4
24! Node 3 neighbors: 1, 2
25graph(3, 1) = 1; graph(3, 2) = 2
26! Node 4 neighbors: 2
27graph(4, 1) = 2
28! Node 5 has no neighbors
29
30start_node = 1
31
32PRINT *, "Graph represented by adjacency list (node, neighbors):"
33DO i = 1, num_nodes
34PRINT *, "Node ", i, " has ", adj_list(i), " neighbors:"
35DO v = 1, adj_list(i)
36PRINT *, " ", graph(i, v)
37END DO
38END DO
39PRINT *
40
41CALL bfs(graph, adj_list, visited, queue, front, rear, num_nodes, start_node)
42
43PRINT *, "BFS traversal starting from node ", start_node, ":"
44DO i = 1, num_nodes
45IF (visited(i)) THEN
46PRINT *, "Visited node: ", i
47END IF
48END DO
49
50DEALLOCATE(graph)
51
52CONTAINS
53
54SUBROUTINE bfs(graph, adj_list, visited, queue, front, rear, num_nodes, start_node)
55IMPLICIT NONE
56INTEGER, INTENT(IN) :: graph(MAX_NODES, MAX_NODES), adj_list(MAX_NODES)
57LOGICAL, INTENT(OUT) :: visited(MAX_NODES)
58INTEGER, INTENT(OUT) :: queue(MAX_NODES)
59INTEGER, INTENT(INOUT) :: front, rear
60INTEGER, INTENT(IN) :: num_nodes, start_node
61INTEGER :: u, v
62
63! Initialize visited array and queue
64visited = .FALSE.
65front = 1
66rear = 0
67
68! Mark the source node as visited and enqueue it
69visited(start_node) = .TRUE.
70rear = rear + 1
71queue(rear) = start_node
72
73DO WHILE (front <= rear) ! While queue is not empty
74! Dequeue a vertex from queue
75u = queue(front)
76front = front + 1
77
78! Get all adjacent vertices of the dequeued vertex u
79! If an adjacent has not been visited, then mark it visited and enqueue it
80DO v = 1, adj_list(u)
81INTEGER :: neighbor
82neighbor = graph(u, v)
83IF (.NOT. visited(neighbor)) THEN
84visited(neighbor) = .TRUE.
85rear = rear + 1
86queue(rear) = neighbor
87END IF
88END DO
89END DO
90END SUBROUTINE bfs
91
92END PROGRAM bfs_example
Algorithm description viewbox

Fortran Breadth-First Search (BFS) on Adjacency List

Algorithm description:

This Fortran program implements Breadth-First Search (BFS), a graph traversal algorithm. It starts at a given node and explores the graph layer by layer, visiting all neighbors of the current node before moving to the next level. BFS is used for finding the shortest path in unweighted graphs, network broadcasting, and web crawlers. The implementation uses an adjacency list to represent the graph and an array to simulate a queue.

Algorithm explanation:

BFS has a time complexity of O(V + E), where V is the number of vertices and E is the number of edges, because each vertex and edge is visited at most once. The space complexity is O(V) in the worst case for storing the queue and the visited set. Edge cases include disconnected graphs (BFS will only explore the component containing the start node), graphs with cycles (handled by the `visited` array), and starting BFS from an isolated node. The correctness relies on the FIFO property of the queue, ensuring that nodes are visited in order of their distance from the source.

Pseudocode:

FUNCTION bfs(graph, adj_list, num_nodes, start_node):
  Initialize visited array of size num_nodes to false
  Initialize queue (array) and set front=1, rear=0

  Mark start_node as visited
  Enqueue start_node

  WHILE queue is not empty:
    u = Dequeue from queue

    FOR each neighbor v of u (from adj_list[u]):
      IF v is not visited:
        Mark v as visited
        Enqueue v