|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
This interactive simulation visualizes the fundamental Graph Search and Traversal Algorithms. Understanding how to systematically explore graphs is essential for solving problems in pathfinding, network analysis, puzzle solving, and artificial intelligence. What is Graph Traversal?Graph traversal (or graph search) is the process of visiting all vertices in a graph in a systematic way. The two fundamental approaches are:
Breadth-First Search (BFS)BFS explores the graph in "layers" or "levels." Starting from a source node, it visits all nodes at distance 1, then all nodes at distance 2, and so on.
BFS Algorithm:
Time Complexity: O(V + E) where V = vertices, E = edges Space Complexity: O(V) for the queue Depth-First Search (DFS)DFS explores as far as possible along each branch before backtracking. It uses a stack (either explicit or via recursion) to keep track of the path.
DFS Algorithm:
Discovery/Finish Times: DFS assigns timestamps to each vertex:
Connected ComponentsA connected component is a maximal subgraph where every vertex is reachable from every other vertex within that subgraph.
Finding Connected Components:
Bipartite Graphs (2-Coloring)A graph is bipartite if its vertices can be divided into two disjoint sets such that every edge connects a vertex from one set to a vertex in the other set. Equivalently, the graph can be 2-colored with no adjacent vertices sharing the same color.
Bipartite Check Algorithm:
Key Theorem: A graph is bipartite if and only if it contains no odd-length cycles. Cycle DetectionDetecting cycles is crucial for many applications: dependency resolution, deadlock detection, and validating DAGs (Directed Acyclic Graphs). This simulation finds ALL cycles in the graph, not just the first one encountered.
Back Edge: In directed graphs, a back edge is an edge from a vertex to one of its ancestors in the DFS tree. Back edges are the definitive indicator of cycles in directed graphs.
Edge Classification (DFS)DFS classifies edges into four types based on discovery/finish times:
Applications
Breadth-First SearchExplores all neighbors at current depth before moving to next level. Uses a Queue (FIFO). Colors nodes by distance layers. Queue📝 Algorithm Log
Nodes:
0
Edges:
0
Default
Visiting
Visited
In Queue
In Stack
Back Edge / Conflict
Click: Add node |
Drag: Move node |
Shift+Drag: Create edge |
Right-click: Delete node/edge
Usage Instructions
Preset Graphs
Algorithm Comparison
Key Observations to Make
Understanding the VisualizationNode Colors:
BFS Layer Colors: In BFS, visited nodes are colored by their distance from the start node:
DFS Timestamps: Numbers shown above nodes in format
Pseudocode Reference
BFS(G, start):
queue ← [start] visited[start] ← true distance[start] ← 0 while queue not empty: v ← dequeue(queue) for each neighbor u of v: if not visited[u]: visited[u] ← true distance[u] ← distance[v] + 1 enqueue(queue, u)
DFS(G, v):
visited[v] ← true discovery[v] ← time++ for each neighbor u of v: if not visited[u]: DFS(G, u) finish[v] ← time++
HasCycle(G):
visited ← ∅ recStack ← ∅ for each vertex v in G: if v not in visited: if CycleDFS(v): return true return false CycleDFS(v): visited.add(v) recStack.add(v) for each neighbor u of v: if u in recStack: return true // Back edge! if u not in visited and CycleDFS(u): return true recStack.remove(v) return false Complexity Analysis
Simulation Features
Force-Directed Layout PhysicsThe Auto-Layout feature uses a force-directed graph drawing algorithm inspired by Fruchterman-Reingold:
Physics Model:
Repulsion (Coulomb's Law): Frepel = repulsion / distance² Applied between ALL pairs of nodes Attraction (Hooke's Law - Springs): Fattract = springK × (distance - idealLength) Applied only to connected nodes (edges act as springs) Center Gravity: Weak pull toward canvas center to keep graph visible Damping: Velocity reduced each frame to reach equilibrium
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||