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

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

Octave Depth-First Search (DFS) on Adjacency List

Octave

Goal -- WPM

Ready
Exercise Algorithm Area
1function visitedOrder = dfsAdjacencyList(graph, startNode)
2% Performs Depth-First Search (DFS) on a graph represented by an adjacency list.
3% graph: A cell array where graph{i} contains a list of neighbors for node i.
4% startNode: The node from which to start the DFS traversal.
5% visitedOrder: A cell array containing the order in which nodes were visited.
6% This implementation uses recursion to manage the DFS stack implicitly.
7
8numNodes = length(graph);
9visited = false(1, numNodes);
10visitedOrder = {}; % Initialize as cell array to store node IDs
11
12% --- Input Validation ---
13if startNode < 1 || startNode > numNodes
14error('Start node is out of bounds.');
15end
16
17% --- DFS Helper Function ---
18% This recursive function explores the graph.
19function dfsRecursive(currentNode)
20% Mark the current node as visited.
21visited(currentNode) = true;
22% Add the current node to the visited order.
23visitedOrder{end + 1} = currentNode;
24
25% Get the neighbors of the current node.
26neighbors = graph{currentNode};
27
28% Iterate through each neighbor.
29for i = 1:length(neighbors)
30neighborNode = neighbors(i);
31% If the neighbor has not been visited, recursively call DFS on it.
32if ~visited(neighborNode)
33dfsRecursive(neighborNode);
34end
35end
36end
37
38% --- Start DFS ---
39% Initiate the DFS traversal from the startNode.
40dfsRecursive(startNode);
41
42% --- Handle Disconnected Components (Optional but good practice) ---
43% If the graph might be disconnected, we can iterate through all nodes
44% to ensure all reachable nodes are visited. This part is commented out
45% as the primary goal is traversal from a start node.
46% for node = 1:numNodes
47% if ~visited(node)
48% dfsRecursive(node);
49% end
50% end
51end
Algorithm description viewbox

Octave Depth-First Search (DFS) on Adjacency List

Algorithm description:

This Octave code performs a Depth-First Search (DFS) on a graph represented by an adjacency list. DFS explores as far as possible along each branch before backtracking. It's crucial for tasks like finding connected components, topological sorting, and pathfinding in unweighted graphs.

Algorithm explanation:

The `dfsAdjacencyList` function implements Depth-First Search starting from a specified node. It uses a boolean array `visited` to keep track of nodes that have already been explored and a cell array `visitedOrder` to record the sequence of visited nodes. The core of the algorithm is the recursive `dfsRecursive` function. When `dfsRecursive` is called for a `currentNode`, it marks the node as visited and adds it to `visitedOrder`. It then iterates through all the `neighbors` of `currentNode` from the adjacency list. If a `neighborNode` has not yet been visited, `dfsRecursive` is called on that `neighborNode`, effectively going deeper into the graph. This process continues until a node has no unvisited neighbors, at which point the recursion unwinds, and the search backtracks. The time complexity is O(V + E), where V is the number of vertices and E is the number of edges, because each vertex and each edge is visited at most once. The space complexity is O(V) in the worst case for the recursion stack and the `visited` array. An invariant is that `visited(node)` is true if and only if `node` has been fully explored or is currently on the recursion stack.

Pseudocode:

function dfsAdjacencyList(graph, startNode):
  numNodes = length(graph)
  visited = array of false(numNodes)
  visitedOrder = empty list
  dfsRecursive(startNode)
  return visitedOrder

function dfsRecursive(currentNode):
  mark currentNode as visited
  add currentNode to visitedOrder
  for each neighborNode in graph[currentNode]:
    if neighborNode is not visited:
      dfsRecursive(neighborNode)