Bölüm 4.1 · Simülatör

Graph Traversal Simulator

← Bölüm 4.1
visitedin queue / stack

Ready.

 

 

Graph Traversal Simulator

Generate an undirected graph, drag the nodes, and watch Breadth-First Search or Depth-First Search visit the graph step by step.

How to use

  • Set the number of nodes (2–20) and press Generate Graph. Each pair of nodes is joined with probability 0.4, so the graph may be disconnected.
  • Choose the Start node, then run BFS or DFS. Cancel stops the animation; Speed changes its pace.
  • Drag nodes with the mouse (or a finger) to untangle the drawing, also while a traversal is running.
  • Visited nodes are colored and numbered in visit order; nodes waiting in the queue (BFS) or on the stack (DFS) are highlighted; tree edges show how each node was reached. Neighbors are examined in increasing order.

Breadth-First Search

BFS uses a queue. It visits all currently reachable neighbors before moving to the next depth level, so the BFS tree contains shortest paths (in number of edges) from the start node. Its time complexity is O(V + E).

Depth-First Search

DFS uses a stack-like strategy. It follows one branch as deeply as possible before backtracking. This demo uses an explicit stack: neighbors are pushed in reverse order so the smallest one is popped first, and a node is marked visited when it is popped. Its time complexity is also O(V + E).

Nodes that are not reachable from the start node are never visited; the status line reports them.