Advanced Graph Algorithm Visualizer

← Back to Home

Graph Controls

Algorithms

Output / Result:

Breadth-First Search (BFS) - Graph

Steps:

  1. Start at the given source node and mark it as visited.
  2. Initialize a queue and enqueue the source node.
  3. While the queue is not empty:
    1. Dequeue a node from the front of the queue.
    2. Process the node (visit it).
    3. Enqueue all unvisited neighbors of the node and mark them as visited.
  4. Continue until the queue is empty and all reachable nodes are visited.
Time Complexity: O(V + E)
Space Complexity: O(V)

Depth-First Search (DFS) - Graph

Steps:

  1. Start at the given source node and mark it as visited.
  2. Process the current node (visit it).
  3. Recursively visit all unvisited neighbors of the current node.
  4. Backtrack when all neighbors of a node are visited and continue the recursion.
  5. Continue until all reachable nodes are visited.
Time Complexity: O(V + E)
Space Complexity: O(V)

Dijkstra's Algorithm (Shortest Path)

Steps:

  1. Initialize distances of all vertices as ∞ except the source vertex, which is 0.
  2. Create a priority queue (min-heap) and insert the source vertex with distance 0.
  3. While the priority queue is not empty:
    1. Extract the vertex u with the minimum distance from the queue.
    2. For each neighbor v of u, check if the distance to v can be minimized through u.
    3. If yes, update the distance of v and insert it into the priority queue.
  4. Repeat until all vertices are processed.
  5. At the end, the shortest distances from the source to all vertices are determined.
Time Complexity: O((V + E) log V)
Space Complexity: O(V + E)

Prim's Algorithm (Minimum Spanning Tree)

Steps:

  1. Initialize a set to track vertices included in the MST (start with any vertex).
  2. Assign key values to all vertices as ∞, except the starting vertex which is 0.
  3. While all vertices are not included in the MST:
    1. Select the vertex u with the minimum key value not yet included in MST.
    2. Include u in the MST set.
    3. For each neighbor v of u, if v is not in MST and edge weight(u,v) < key[v], update key[v] = edge weight(u,v).
  4. Repeat until all vertices are included in the MST.
  5. At the end, the MST edges and their weights are determined.
Time Complexity: O((V + E) log V)
Space Complexity: O(V + E)

Graph Visualization